Docs
β†’
Blocks
β†’
Stacking Cards

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

Loading...

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 window to track scroll progress. However, in some cases, you may want to wrap it inside another scrollable container. To achieve this, simply define the container for useScroll from motion. In the Demo above, I defined the containerRef and passed it to the scrollOptions prop of the StackingCards component.

  • To ensure StackingCardItem works correctly, you need to define its height. This allows the wrapper to have a larger height than the card itself, ensuring that the topPosition functions properly.

Props


StackingCards

PropTypeDefaultDescription

totalCards*

number0

Total number of cards to be animated (this is for calculating the scale intensity)

scaleMultipliernumber0.03The intensity of the card to scale
scrollOptonsUseScrollOptions{offset: ["start start", "end end"]}Scroll options for useScroll hook from motion.
classNamestring-className for the container
Other Props--All attributes for HTMLDivElement

StackingCardItem

PropTypeDefaultDescription

index*

number-

index value of your card (to calculate scale intensity)

topPositionstring5 + index * 3The top position of the card
classNamestring-className for the StackingCardItem element
Other Props--All attributes for HTMLDivElement