← Components

Perspective Card Stack

A pile of photos that swings open into a 3D fan.

Cards · FreeInstall ↓
Open preview ↗
Loading the demoNew tab ↗

Customize

500 px
48 deg
1.00
40 px
0.40 s
Your code
<PerspectiveCardStack cards={cards} />

Changes show live and stay in the address, so a link keeps them.

Code

"use client";

import { useEffect, useRef, useState, useSyncExternalStore, type PointerEvent } from "react";

// A small deck of photos in perspective. At rest the cards sit in a tidy pile, each one peeking
// out above the card in front. Hover the pile (or tap it, or focus it with the keyboard) and the
// cards swing out sideways like pages of a book, turned toward you, and the card under the cursor
// lifts. It is plain CSS transitions on a transform, so there is no animation library to load.

export type PerspectiveCard = {
  /** The photo's address. */
  image: string;
  /** Describes the photo for screen readers. */
  alt?: string;
};

export type PerspectiveCardStackProps = {
  /** The photos, front card first. Up to five are used; three look best. */
  cards: PerspectiveCard[];
  /** Width of each card, in pixels. On a narrow screen the cards shrink to fit. */
  cardWidth?: number;
  /** Height of each card, in pixels. */
  cardHeight?: number;
  /** How strong the 3D is, in pixels: the distance of the "camera". Smaller is more dramatic. */
  perspective?: number;
  /** How far the front card turns when the stack fans out, in degrees. The cards behind it turn less. */
  tilt?: number;
  /** How far the cards spread sideways when fanned. 1 is the normal spread. */
  spread?: number;
  /** How far the card under the cursor rises, in pixels. */
  lift?: number;
  /** How long the fan takes, in seconds. */
  duration?: number;
  /** Which side the cards fan out toward. */
  direction?: "left" | "right";
  /** What opens the stack: the cursor resting on it, or a click or tap. Touch screens always use taps. */
  trigger?: "hover" | "click";
  /** Open and close the stack by itself while nobody is using it. */
  autoplay?: boolean;
  /** Extra classes for the outer box. Give it a size (it fills it). */
  className?: string;
  /** Extra classes for each card, e.g. a radius or a shadow. */
  cardClassName?: string;
};

function subscribeToMotion(onChange: () => void) {
  const query = window.matchMedia("(prefers-reduced-motion: reduce)");
  query.addEventListener("change", onChange);
  return () => query.removeEventListener("change", onChange);
}
const prefersReducedMotion = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches;

const MAX_CARDS = 5;

// Where card number `index` sits. `t` runs from 0 (front card) to 1 (last card), so the whole
// pile always covers the same area, however many cards there are. For three cards these are
// the numbers of the original design. Percentages are shares of the card's own size.
function cardTransform(index: number, count: number, open: boolean, tilt: number, spread: number, lift: number, direction: 1 | -1) {
  const t = count > 1 ? index / (count - 1) : 0;
  if (!open) {
    // A pile: each card a step higher and a step smaller than the one in front
    return `translate(-50%, ${-35 - 40 * t}%) scale(${1 - 0.2 * t})`;
  }
  // A fan: the front card swings out furthest and is the biggest, the rest follow, smaller and
  // turned less, so the row seems to run away from you
  // (measured from the middle of the box; the mirror image for a fan to the right)
  const x = -50 + (-22 + 40 * t) * spread * direction;
  const turn = -(tilt - tilt * 0.5 * t) * direction;
  return `translate(${x}%, -50%) translateY(${-lift}px) scale(${0.8 - 0.2 * t}) rotateY(${turn}deg)`;
}

export function PerspectiveCardStack({
  cards,
  cardWidth = 360,
  cardHeight = 280,
  perspective = 500,
  tilt = 48,
  spread = 1,
  lift = 40,
  duration = 0.4,
  direction = "left",
  trigger = "hover",
  autoplay = false,
  className = "",
  cardClassName = "rounded-[10px] shadow-[0_26px_50px_-20px_rgba(0,0,0,0.5)]",
}: PerspectiveCardStackProps) {
  const reducedMotion = useSyncExternalStore(subscribeToMotion, prefersReducedMotion, () => false);
  const [hovering, setHovering] = useState(false);
  const [focused, setFocused] = useState(false);
  const [pinned, setPinned] = useState(false);
  const [auto, setAuto] = useState(false);
  const [liftedCard, setLiftedCard] = useState<number | null>(null);
  // "mouse", "touch" or "pen": a mouse can hover, a finger can only tap
  const pointerType = useRef("mouse");

  const shown = cards.slice(0, MAX_CARDS);
  const inUse = hovering || focused || pinned;

  // Opened by the cursor or the keyboard in hover mode, by a click in click mode, by the
  // autoplay loop when nobody is around. With reduced motion there is nothing to open: the fan
  // is simply how it looks.
  const open = reducedMotion || (trigger === "hover" ? inUse : pinned) || (autoplay && !inUse && auto);

  // Autoplay is just a timer flipping a flag, and it gives way as soon as someone uses the stack
  useEffect(() => {
    if (!autoplay || reducedMotion) return;
    const timer = window.setInterval(() => setAuto((on) => !on), 2400);
    return () => window.clearInterval(timer);
  }, [autoplay, reducedMotion]);

  const sign = direction === "left" ? 1 : -1;
  const seconds = reducedMotion ? 0 : duration;

  const onHoverChange = (event: PointerEvent, on: boolean) => {
    pointerType.current = event.pointerType;
    // Touch fires enter and leave around every tap; only a real mouse should open on hover
    if (event.pointerType !== "mouse" || trigger !== "hover") return;
    setHovering(on);
    if (!on) setLiftedCard(null);
  };

  const onCardClick = () => {
    // A mouse in hover mode already sees the fan; everything else toggles it
    if (trigger === "click" || pointerType.current !== "mouse") setPinned((on) => !on);
  };

  return (
    <div
      className={`relative w-full [container-type:inline-size] ${className}`}
      style={{ perspective, minHeight: cardHeight * 1.4 }}
      onPointerEnter={(event) => onHoverChange(event, true)}
      onPointerLeave={(event) => onHoverChange(event, false)}
      onKeyDown={(event) => {
        if (event.key === "Escape") setPinned(false);
      }}
    >
      {shown.map((card, index) => (
        <button
          key={index}
          type="button"
          // One stop for the keyboard is enough: the front card
          tabIndex={index === 0 ? 0 : -1}
          aria-label={card.alt ?? `Card ${index + 1}`}
          aria-expanded={open}
          onClick={onCardClick}
          onFocus={(event) => {
            if (event.currentTarget.matches(":focus-visible")) setFocused(true);
          }}
          onBlur={() => setFocused(false)}
          onPointerEnter={(event) => {
            if (event.pointerType === "mouse") setLiftedCard(index);
          }}
          onPointerLeave={() => setLiftedCard((current) => (current === index ? null : current))}
          className="group absolute top-1/2 left-1/2 m-0 block cursor-pointer border-0 bg-transparent p-0 [backface-visibility:hidden] will-change-transform focus:outline-none"
          style={{
            width: `min(${cardWidth}px, 74cqw)`,
            aspectRatio: `${cardWidth} / ${cardHeight}`,
            zIndex: shown.length - index,
            transform: cardTransform(index, shown.length, open, tilt, spread, liftedCard === index && !reducedMotion ? lift : 0, sign),
            transition: `transform ${seconds}s cubic-bezier(0.22, 1, 0.36, 1), filter ${seconds}s ease`,
            // The cards at the back sit in a little shade, so the pile has depth
            filter: `brightness(${1 - (shown.length > 1 ? index / (shown.length - 1) : 0) * (open ? 0.1 : 0.18)})`,
          }}
        >
          {/* The look of the card (radius, shadow) lives on this inner box, so the button itself stays a plain rectangle */}
          <span className={`pointer-events-none block h-full w-full overflow-hidden group-focus-visible:outline-2 group-focus-visible:outline-offset-4 group-focus-visible:outline-current ${cardClassName}`}>
            {/* The button already carries the name, so the picture inside stays quiet */}
            <img src={card.image} alt="" draggable={false} className="block h-full w-full object-cover select-none" />
          </span>
          {/* When the card rises it leaves a gap under the cursor. This invisible strip fills it, so the card doesn't flicker up and down at its edge */}
          <span aria-hidden className="absolute inset-x-0 top-full" style={{ height: lift }} />
        </button>
      ))}
    </div>
  );
}

The component. Installs to components/dimaac/perspective-card-stack.tsx.

Install

With the shadcn CLI

npx shadcn@latest add https://ui.dimaac.com/r/perspective-card-stack.json

It adds components/dimaac/perspective-card-stack.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/perspective-card-stack.

By hand

# Nothing to install: it only needs React
  1. It needs nothing but React 19.
  2. Copy the component file below into components/dimaac/perspective-card-stack.tsx.
  3. It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
  4. Import it with import { PerspectiveCardStack } from "@/components/dimaac/perspective-card-stack" and give its parent a size: it fills it.

Props

PropTypeDefaultDescription
cards (required)PerspectiveCard[]-The photos, front card first. Up to five are used; three look best.
cardWidthnumber360Width of each card, in pixels. On a narrow screen the cards shrink to fit.
cardHeightnumber280Height of each card, in pixels.
perspectivenumber500How strong the 3D is, in pixels: the distance of the "camera". Smaller is more dramatic.
tiltnumber48How far the front card turns when the stack fans out, in degrees. The cards behind it turn less.
spreadnumber1How far the cards spread sideways when fanned. 1 is the normal spread.
liftnumber40How far the card under the cursor rises, in pixels.
durationnumber0.4How long the fan takes, in seconds.
direction"left" | "right""left"Which side the cards fan out toward.
trigger"hover" | "click""hover"What opens the stack: the cursor resting on it, or a click or tap. Touch screens always use taps.
autoplaybooleanfalseOpen and close the stack by itself while nobody is using it.
classNamestring""Extra classes for the outer box. Give it a size (it fills it).
cardClassNamestring"rounded-[10px] shadow-[0_26px_50px_-20px_rgba(0,0,0,0.5)]"Extra classes for each card, e.g. a radius or a shadow.

Use with AI

Agent prompt

Paste it into Claude Code, Cursor or any coding agent, or open it straight in a chat. It carries your Customize settings.

Prompt
Add the Perspective Card Stack component (<PerspectiveCardStack />) from DiMaac UI to this project.

Docs: https://ui.dimaac.com/components/perspective-card-stack
Source and dependencies (shadcn registry JSON): https://ui.dimaac.com/r/perspective-card-stack.json

Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/perspective-card-stack.json

Then use it like this:
import { PerspectiveCardStack } from "@/components/dimaac/perspective-card-stack";
<PerspectiveCardStack cards={cards} />

If the shadcn CLI isn't set up here, fetch the registry JSON above, write files[0].content to components/dimaac/perspective-card-stack.tsx.
It uses Tailwind v4 classes, so make sure Tailwind scans that folder. Give its parent a size, keep light and dark mode working and keep its prefers-reduced-motion fallback.
Full index for agents: https://ui.dimaac.com/llms.txt

Files agents read

llms.txt
Every component and how to install it
perspective-card-stack.md
This page as Markdown: install, props and the full source
perspective-card-stack.json
The shadcn registry item

About

A small deck of photos in perspective. At rest the cards sit in a tidy pile, each peeking out above the one in front. Hover it, tap it or focus it with the keyboard and the cards swing out sideways, turned toward you like the pages of a book, and the card under the cursor rises. It is plain CSS transitions on one transform per card, so there is no animation library, and the 3D is tuned by a handful of numbers: the camera distance, the turn, the spread and the lift. It works with two to five photos, fans to either side, can open on click instead of hover, can open and close by itself, shrinks to fit narrow screens, and shows the fan open and still when the visitor prefers reduced motion.

Installs to
components/dimaac/perspective-card-stack.tsx
Uses
React only
Tags
cards, 3d, perspective, hover, css, stack

Photo by Binks AI, Siren Media and Ricardo Matos on Lummi (Lummi License).