Stacking Cards
A component used in websites to display layered cards that stack on top of one another, often with interactive animations.
by Khoa Phan
Installation
1pnpm dlx shadcn add @fancy/stacking-cards
Usage
Wrap StackingCards around the content you want to animate and StackingCardItem around each card you want to animate.
The structure looks like this:
Stacking cards usage example
1<StackingCards>2 <StackingCardItem>3 {/* Your card goes here */}4 </StackingCardItem>5</StackingCards>
Understanding the component
The component utilizes scroll progress to determine the scale of each element. The first element has the highest scale multiplier, making it the smallest when it reaches the bottom of the container's scroll area, while the last element follows the opposite pattern, creating a layered effect.
To achieve this, I use each element's index to calculate its scale multiplier. Just simple as that! π
Notes
-
By default, this component uses the
windowto track scroll progress. However, in some cases, you may want to wrap it inside another scrollable container. To achieve this, simply define the container foruseScrollfrommotion. In theDemoabove, I defined thecontainerRefand passed it to thescrollOptionsprop of theStackingCardscomponent. -
To ensure
StackingCardItemworks correctly, you need to define its height. This allows the wrapper to have a larger height than the card itself, ensuring that thetopPositionfunctions properly.
Props
StackingCards
| Prop | Type | Default | Description |
|---|---|---|---|
totalCards* | number | 0 | Total number of cards to be animated (this is for calculating the scale intensity) |
| scaleMultiplier | number | 0.03 | The intensity of the card to scale |
| scrollOptons | UseScrollOptions | {offset: ["start start", "end end"]} | Scroll options for useScroll hook from motion. |
| className | string | - | className for the container |
| Other Props | - | - | All attributes for HTMLDivElement |
StackingCardItem
| Prop | Type | Default | Description |
|---|---|---|---|
index* | number | - |
|
| topPosition | string | 5 + index * 3 | The top position of the card |
| className | string | - | className for the StackingCardItem element |
| Other Props | - | - | All attributes for HTMLDivElement |