Card Stack
A deck of cards that fans on hover and moves through one card at a time.
Overview
Card Stack shows one card on top of a short deck, with the next cards peeking out behind it. Moving forward lifts the top card off to the side and tucks it in at the back, and moving back brings the last card forward the same way. Hovering the deck fans the cards apart a little, which hints that there is more to see.
Use it for a handful of testimonials, feature highlights, or steps that people read one at a time. For a long list, or content people need to compare side by side, use a list or a grid instead.
People move through the cards by swiping or dragging the top card sideways, with the previous and next buttons, or with the left and right arrow keys while the deck has focus.
Installation
Usage
import { CardStack } from "@/components/animated/card-stack"
<CardStack
label="Customer stories"
items={stories}
className="max-w-sm"
renderItem={(story) => (
<figure>
<blockquote>{story.quote}</blockquote>
<figcaption>{story.name}</figcaption>
</figure>
)}
/>Children work as well, one per card:
<CardStack label="Release highlights" loop={false}>
<div>Faster search</div>
<div>Saved views</div>
<div>Audit log</div>
</CardStack>Control the top card with index and onIndexChange, for example to keep it in sync with a URL. Set autoplay to a number of milliseconds to move on its own.
Accessibility
The deck is a region with the role description "carousel" and the name from label. Each card is a group named by its position, such as "2 of 4". Only the top card is exposed to assistive technology and can receive focus; the cards behind it are inert.
The deck itself takes focus and explains that the arrow keys move through the cards. The previous and next buttons have names and are disabled at the ends when loop is off. A polite live region announces the new position after a move someone makes; automatic moves stay silent.
Autoplay pauses while the pointer is over the stack, while anything in it has focus, while the tab is hidden, and when the user prefers reduced motion. If the cards carry important content, leave autoplay off.
With reduced motion, cards crossfade in place: there is no fan, tilt, slide, or drag.