Number 25: Reviews that take turns
Specification
- Catalogue
- No. 25 of 110
- Section
- Cards and accordions
- Interaction
- Layout Animation
- Element
- Card
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
A grid of reviews where one card at a time rolls over to show another.
More reviews than fit on screen, without a carousel to operate. One change at a time, never while you are reading one, and it stops after a round.
Roll over the X axis, 0.55s, cubic-bezier(.65, 0, .35, 1). One random card every 3s. Paused on hover and focus; one round, then rests.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useEffect, useRef, useState } from "react";
import { Stars } from "@/components/pieces/shared";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "./review-roll.css";
const REVIEWS = [
"Booked on Monday, fixed by Wednesday. The price was clear before anything started.",
"Friendly, on time and tidy. I have already recommended them to my neighbour.",
"They explained every option without pushing the expensive one.",
"Quick replies and an easy booking. Exactly what I hoped for.",
"Second time using them and just as good as the first.",
"Turned up when they said they would, which says a lot these days.",
];
const SLOTS = 3;
type Slot = { now: number; before: number | null };
function ReviewRollDemo() {
const reduced = useSafeReducedMotion();
const root = useRef<HTMLDivElement>(null);
const [slots, setSlots] = useState<Slot[]>(() => Array.from({ length: SLOTS }, (_, i) => ({ now: i, before: null })));
const current = useRef(slots);
useEffect(() => {
current.current = slots;
}, [slots]);
useEffect(() => {
if (reduced) return;
let swaps = 0;
let lastSlot = -1;
const id = window.setInterval(() => {
const el = root.current;
if (!el || document.hidden || el.matches(":hover") || el.querySelector(":focus-visible")) return;
if (swaps >= REVIEWS.length) {
window.clearInterval(id);
return;
}
swaps += 1;
const visible = new Set(current.current.map((s) => s.now));
const free = REVIEWS.map((_, i) => i).filter((i) => !visible.has(i));
let slot = Math.floor(Math.random() * SLOTS);
if (slot === lastSlot) slot = (slot + 1) % SLOTS;
lastSlot = slot;
const next = free[Math.floor(Math.random() * free.length)];
if (next === undefined) return;
setSlots(current.current.map((s, k) => (k === slot ? { now: next, before: s.now } : { now: s.now, before: null })));
}, 3000);
return () => window.clearInterval(id);
}, [reduced]);
const card = (i: number) => (
<figure className="dm-review">
<Stars />
<blockquote>{REVIEWS[i]}</blockquote>
<cite>Sample review</cite>
</figure>
);
return (
<div className="dm" ref={root}>
<div className="dm-reviews">
{slots.map((s, k) => (
<div key={k} className="dm-review-cell">
{s.before !== null && (
<div key={`out-${s.before}`} className="roll-out" aria-hidden="true">
{card(s.before)}
</div>
)}
<div key={`in-${s.now}`} className={s.before !== null ? "roll-in" : undefined}>
{card(s.now)}
</div>
</div>
))}
</div>
<p className="dm-hint">One card changes every 3 seconds, never while you are on it</p>
</div>
);
}
export default function ReviewRollEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Reviews that take turns" sample>
<ReviewRollDemo />
</PieceStage>
);
}