---
title: Swipeable Cards
description: A deck of cards you swipe through.
category: Cards
tier: free
url: https://ui.dimaac.com/components/swipeable-cards
registry: https://ui.dimaac.com/r/swipeable-cards.json
---

# Swipeable Cards

A deck of photo cards built on Swiper's cards effect. Drag the top card away and the next one settles into place with the rest fanned out behind; click a card behind to bring it forward. It has optional previous and next buttons, dot navigation, autoplay that pauses under the pointer, and arrow keys when the deck is focused. The buttons talk to their own deck, so two decks on one page never steer each other. Card size, corner radius, tilt and fan-out are props, and visitors who prefer reduced motion get instant changes and no autoplay. Ported from the first DiMaac UI, with its global button selectors, fixed card size and hard-coded autoplay speed fixed.

## Install

With the shadcn CLI:

```bash
npx shadcn@latest add https://ui.dimaac.com/r/swipeable-cards.json
pnpm dlx shadcn@latest add https://ui.dimaac.com/r/swipeable-cards.json
bunx --bun shadcn@latest add https://ui.dimaac.com/r/swipeable-cards.json
```

With the `@dimaac` namespace in components.json: `npx shadcn@latest add @dimaac/swipeable-cards`

Dependencies:

```bash
pnpm add swiper lucide-react
npm install swiper lucide-react
```

By hand:

1. Install the packages it uses: swiper, lucide-react.
2. Copy the component file below into components/dimaac/swipeable-cards.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 { SwipeableCards } from "@/components/dimaac/swipeable-cards". Give its parent a size: it fills it.

## Usage

```tsx
import { SwipeableCards } from "@/components/dimaac/swipeable-cards";

<SwipeableCards images={images} />
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `images` (required) | `SwipeableCard[]` | - | The cards, top one first. Eight or more looks best. |
| `cardWidth` | `number` | `300` | Width of a card, in pixels. On a narrower screen it shrinks to fit. |
| `cardHeight` | `number` | `440` | Height of a card, in pixels. |
| `radius` | `number` | `24` | Corner radius of a card, in pixels. |
| `loop` | `boolean` | `true` | Start again at the first card after the last one. |
| `autoplay` | `boolean` | `false` | Flick to the next card on a timer. It pauses under the pointer and never runs for visitors who prefer reduced motion. |
| `autoplayDelay` | `number` | `2500` | Milliseconds between cards while autoplay is on. |
| `speed` | `number` | `300` | Milliseconds a card takes to move. Visitors who prefer reduced motion get an instant change. |
| `perSlideRotate` | `number` | `2` | Tilt of each card behind the top one, in degrees. |
| `perSlideOffset` | `number` | `8` | How far each card behind the top one peeks out to the side, in pixels (roughly). |
| `rotate` | `boolean` | `true` | Tilt the cards as they fan out. Off keeps the deck flat and straight. |
| `slideShadows` | `boolean` | `true` | Darken the cards behind the top one, so the deck has depth. |
| `showNavigation` | `boolean` | `false` | Show previous and next buttons under the deck. |
| `showPagination` | `boolean` | `false` | Show one dot per card under the deck. They are buttons too. |
| `grabCursor` | `boolean` | `true` | Show a grab hand over the deck. |
| `label` | `string` | `"Swipeable cards"` | The name screen readers announce for the deck. |
| `onIndexChange` | `(index: number) => void` | - | Called with the number of the card on top (starting at 0) each time it changes. |
| `className` | `string` | `""` | Extra classes for the outer box. |
| `cardClassName` | `string` | `"shadow-[0_26px_40px_-22px_rgba(0,0,0,0.5)]"` | Extra classes for each card, e.g. a different shadow. |

## Credits

- Portrait with Neon Sunglasses: Shaun Duval, Lummi (https://www.lummi.ai/photo/portrait-with-neon-sunglasses-gindg), Lummi License
- Vibrant Woman Portrait: SHIHO, Lummi (https://www.lummi.ai/photo/vibrant-woman-portrait-htofy), Lummi License
- Vivid Portrait on Pink: Amino, Lummi (https://www.lummi.ai/photo/vivid-portrait-on-pink-oxbow), Lummi License
- Expressive Minimal Portrait: Steph Meade, Lummi (https://www.lummi.ai/photo/expressive-minimal-portrait-jrfyc), Lummi License
- Poised Portrait: Lloyd Creates, Lummi (https://www.lummi.ai/photo/poised-portrait-ooi9b), Lummi License
- Stylish Portrait: Amino, Lummi (https://www.lummi.ai/photo/stylish-portrait-8inth), Lummi License
- Monochrome Portrait of a Young Woman: West Kast, Lummi (https://www.lummi.ai/photo/monochrome-portrait-of-a-young-woman-shb7s), Lummi License
- Mystery Portrait: SHIHO, Lummi (https://www.lummi.ai/photo/mystery-portrait-cffyo), Lummi License
- Abstract Eye Close-Up: Kuba, Lummi (https://www.lummi.ai/photo/abstract-eye-close-up-uamae), Lummi License
- Striking Portrait: studiomiklus, Lummi (https://www.lummi.ai/photo/striking-portrait-jodiu), Lummi License
- Fashion Portrait with Orange Visor: West Kast, Lummi (https://www.lummi.ai/photo/fashion-portrait-with-orange-visor-damql), Lummi License
- Stylish Studio Portrait: Amino, Lummi (https://www.lummi.ai/photo/stylish-studio-portrait-d9xnl), Lummi License

## Source

### swipeable-cards.tsx → `components/dimaac/swipeable-cards.tsx`

```tsx
"use client";

import { useEffect, useRef, useState, type KeyboardEvent } from "react";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import { A11y, Autoplay, EffectCards } from "swiper/modules";
import { Swiper, SwiperSlide } from "swiper/react";
import type { Swiper as SwiperInstance } from "swiper/types";
import "swiper/css";
import "swiper/css/effect-cards";
import "swiper/css/a11y";

// A deck of photos you flick through. The top card flies off to the side when you drag it and the
// next one settles into its place, with the rest fanned out behind. Swiper's "cards" effect does
// the 3D, we add the buttons and dots around it. Those talk to THIS deck through its instance,
// not through global class names, so two decks on one page never steer each other.

export type SwipeableCard = {
  /** The image address. */
  src: string;
  /** What the image shows, for screen readers. */
  alt: string;
};

export type SwipeableCardsProps = {
  /** The cards, top one first. Eight or more looks best. */
  images: SwipeableCard[];
  /** Width of a card, in pixels. On a narrower screen it shrinks to fit. */
  cardWidth?: number;
  /** Height of a card, in pixels. */
  cardHeight?: number;
  /** Corner radius of a card, in pixels. */
  radius?: number;
  /** Start again at the first card after the last one. */
  loop?: boolean;
  /** Flick to the next card on a timer. It pauses under the pointer and never runs for visitors who prefer reduced motion. */
  autoplay?: boolean;
  /** Milliseconds between cards while autoplay is on. */
  autoplayDelay?: number;
  /** Milliseconds a card takes to move. Visitors who prefer reduced motion get an instant change. */
  speed?: number;
  /** Tilt of each card behind the top one, in degrees. */
  perSlideRotate?: number;
  /** How far each card behind the top one peeks out to the side, in pixels (roughly). */
  perSlideOffset?: number;
  /** Tilt the cards as they fan out. Off keeps the deck flat and straight. */
  rotate?: boolean;
  /** Darken the cards behind the top one, so the deck has depth. */
  slideShadows?: boolean;
  /** Show previous and next buttons under the deck. */
  showNavigation?: boolean;
  /** Show one dot per card under the deck. They are buttons too. */
  showPagination?: boolean;
  /** Show a grab hand over the deck. */
  grabCursor?: boolean;
  /** The name screen readers announce for the deck. */
  label?: string;
  /** Called with the number of the card on top (starting at 0) each time it changes. */
  onIndexChange?: (index: number) => void;
  /** Extra classes for the outer box. */
  className?: string;
  /** Extra classes for each card, e.g. a different shadow. */
  cardClassName?: string;
};

export function SwipeableCards({
  images,
  cardWidth = 300,
  cardHeight = 440,
  radius = 24,
  loop = true,
  autoplay = false,
  autoplayDelay = 2500,
  speed = 300,
  perSlideRotate = 2,
  perSlideOffset = 8,
  rotate = true,
  slideShadows = true,
  showNavigation = false,
  showPagination = false,
  grabCursor = true,
  label = "Swipeable cards",
  onIndexChange,
  className = "",
  cardClassName = "shadow-[0_26px_40px_-22px_rgba(0,0,0,0.5)]",
}: SwipeableCardsProps) {
  // The Swiper instance of this deck. The buttons and the dots use it.
  const swiperRef = useRef<SwiperInstance | null>(null);
  const [index, setIndex] = useState(0);
  // Read after mount so the server and the first client render match
  const [reducedMotion, setReducedMotion] = useState(false);

  useEffect(() => {
    const query = window.matchMedia("(prefers-reduced-motion: reduce)");
    const update = () => setReducedMotion(query.matches);
    update();
    query.addEventListener("change", update);
    return () => query.removeEventListener("change", update);
  }, []);

  // Swiper doesn't pick up a change of loop, autoplay or the card effect settings on its own, so
  // those rebuild the deck (it stays on the same card). Everything else, like speed, updates in place.
  const running = autoplay && !reducedMotion;
  const deckKey = `${loop}-${rotate}-${slideShadows}-${perSlideRotate}-${perSlideOffset}-${running}-${autoplayDelay}-${images.length}`;

  // The listener lives on the box that holds the deck, so only the deck the visitor focused reacts
  const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
    if (event.key === "ArrowRight") {
      event.preventDefault();
      swiperRef.current?.slideNext();
    } else if (event.key === "ArrowLeft") {
      event.preventDefault();
      swiperRef.current?.slidePrev();
    }
  };

  const buttonClass =
    "grid h-11 w-11 place-items-center rounded-full border border-current/25 bg-transparent text-inherit transition-colors duration-200 hover:bg-current/10 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-current";

  return (
    <div
      className={`group/deck relative flex w-full flex-col items-center gap-7 outline-none ${className}`}
      role="group"
      aria-roledescription="carousel"
      aria-label={label}
      tabIndex={0}
      onKeyDown={onKeyDown}
    >
      <div
        className="relative group-focus-visible/deck:rounded-[32px] group-focus-visible/deck:outline-2 group-focus-visible/deck:outline-offset-[10px] group-focus-visible/deck:outline-current/50"
        style={{ width: cardWidth, maxWidth: "calc(100% - 3rem)", height: cardHeight }}
      >
        <Swiper
          key={deckKey}
          className="!h-full !w-full"
          modules={[EffectCards, Autoplay, A11y]}
          effect="cards"
          cardsEffect={{ rotate, slideShadows, perSlideRotate, perSlideOffset }}
          grabCursor={grabCursor}
          loop={loop}
          speed={reducedMotion ? 0 : speed}
          slideToClickedSlide
          autoplay={running ? { delay: autoplayDelay, disableOnInteraction: false, pauseOnMouseEnter: true } : false}
          a11y={{ slideLabelMessage: "Card {{index}} of {{slidesLength}}" }}
          initialSlide={index}
          onSwiper={(swiper) => {
            swiperRef.current = swiper;
          }}
          onRealIndexChange={(swiper) => {
            setIndex(swiper.realIndex);
            onIndexChange?.(swiper.realIndex);
          }}
        >
          {images.map((image, i) => (
            <SwiperSlide key={`${i}-${image.src}`} className={cardClassName} style={{ borderRadius: radius }}>
              <img src={image.src} alt={image.alt} draggable={false} decoding="async" className="block h-full w-full object-cover select-none" />
            </SwiperSlide>
          ))}
        </Swiper>
      </div>

      {(showNavigation || showPagination) && (
        <div className="flex items-center gap-4">
          {showNavigation && (
            <button type="button" className={buttonClass} aria-label="Previous card" onClick={() => swiperRef.current?.slidePrev()}>
              <ChevronLeftIcon className="h-5 w-5" aria-hidden="true" />
            </button>
          )}
          {showPagination && (
            <div className="flex items-center" role="group" aria-label="Choose a card">
              {images.map((image, i) => (
                <button
                  key={`${i}-${image.src}`}
                  type="button"
                  className="group grid h-6 place-items-center px-[3px] text-inherit focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-current"
                  aria-label={`Show card ${i + 1} of ${images.length}`}
                  aria-current={i === index ? "true" : undefined}
                  onClick={() => (loop ? swiperRef.current?.slideToLoop(i) : swiperRef.current?.slideTo(i))}
                >
                  <span
                    className={`block h-[6px] rounded-full bg-current transition-[width,opacity] duration-300 ${i === index ? "w-5 opacity-100" : "w-[6px] opacity-30 group-hover:opacity-60"}`}
                  />
                </button>
              ))}
            </div>
          )}
          {showNavigation && (
            <button type="button" className={buttonClass} aria-label="Next card" onClick={() => swiperRef.current?.slideNext()}>
              <ChevronRightIcon className="h-5 w-5" aria-hidden="true" />
            </button>
          )}
        </div>
      )}
    </div>
  );
}
```

### demo.tsx (the demo on the page, not installed)

```tsx
"use client";

import { useState } from "react";
import { SwipeableCards, type SwipeableCardsProps } from "@/components/dimaac/swipeable-cards";

// A deck of twelve photos from the Lummi set (Lummi License). The title under the deck follows the
// card on top.

const portraits = [
  { file: "portraits/neon-profile-4x5.jpg", title: "Neon shades", alt: "A woman in neon pink sunglasses in profile against a bright blue background" },
  { file: "portraits/tinted-glasses-4x5.jpg", title: "Purple shades", alt: "A woman in purple-tinted sunglasses and braids against a glowing orange background" },
  { file: "portraits/lime-blazer-4x5.jpg", title: "Lime blazer", alt: "A person with curly red hair in a lime green blazer against bright pink" },
  { file: "portraits/glossy-lowkey-4x5.jpg", title: "Low key", alt: "A woman with glossy lips and sparkling earrings in low, dramatic light" },
  { file: "portraits/red-coat-4x5.jpg", title: "Red coat", alt: "A person in a red coat against a deep blue background" },
  { file: "portraits/burgundy-blazer-4x5.jpg", title: "Burgundy blazer", alt: "A person in a burgundy blazer and white turtleneck against a neutral background" },
  { file: "portraits/mono-profile-4x5.jpg", title: "Mono profile", alt: "A black and white profile portrait of a woman in dramatic light" },
  { file: "portraits/red-brushstroke-4x5.jpg", title: "Red stroke", alt: "A portrait with a red brushstroke across the eyes against muted green" },
  { file: "portraits/light-streaks-4x5.jpg", title: "Light streaks", alt: "A close-up of a face with blue eyes and coloured light streaks" },
  { file: "portraits/platinum-blonde-4x5.jpg", title: "Yellow jacket", alt: "A young man with platinum blonde hair in a yellow jacket against orange" },
  { file: "fashion/orange-visor-4x5.jpg", title: "Orange visor", alt: "A woman in an orange visor and navy dress against a teal background" },
  { file: "fashion/varsity-jacket-4x5.jpg", title: "Varsity jacket", alt: "A person in a varsity jacket and orange sunglasses with a yellow ribbon in their hair" },
];

const images = portraits.map((portrait) => ({ src: media(portrait.file), alt: portrait.alt }));

export default function Demo(props: Partial<SwipeableCardsProps>) {
  const [index, setIndex] = useState(0);
  const number = String(index + 1).padStart(2, "0");
  const total = String(portraits.length).padStart(2, "0");

  return (
    <div className="relative flex h-full w-full flex-col items-center justify-center overflow-hidden bg-[#f3f0eb] text-[#1c1917] dark:bg-[#0f0b0b] dark:text-[#e8ddd4]">
      {/* A huge word behind the deck, so the cards sit on something */}
      <div
        aria-hidden="true"
        className="pointer-events-none absolute inset-x-0 top-1/2 -translate-y-1/2 text-center text-[clamp(90px,24vw,340px)] leading-none font-medium tracking-[-0.06em] opacity-[0.06] select-none dark:opacity-[0.09]"
      >
        Faces
      </div>

      <SwipeableCards
        images={images}
        label="Photo deck"
        {...props}
        onIndexChange={(next) => {
          setIndex(next);
          props.onIndexChange?.(next);
        }}
        className={`relative z-10 ${props.className ?? ""}`}
      />

      <div className="pointer-events-none relative z-10 mt-8 flex flex-col items-center gap-1 text-center">
        <p className="font-mono text-[13px] tracking-wide tabular-nums">
          {number} <span className="opacity-40">/ {total}</span>
        </p>
        <p className="text-[15px] opacity-70">{portraits[index]?.title}</p>
      </div>

      <p className="pointer-events-none absolute inset-x-6 bottom-6 z-10 text-center font-mono text-[11px] tracking-wide uppercase opacity-60">
        Drag a card<span className="hidden sm:inline"> or press ← →</span>
      </p>
    </div>
  );
}
```

## Agent prompt

```text
Add the Swipeable Cards component (<SwipeableCards />) from DiMaac UI to this project.

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

Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/swipeable-cards.json

Then use it like this:
import { SwipeableCards } from "@/components/dimaac/swipeable-cards";
<SwipeableCards images={images} />

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