Swipeable Cards
A deck of cards you swipe through.
Customize
<SwipeableCards images={images} />Changes show live and stay in the address, so a link keeps them.
Code
"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>
);
}
The component. Installs to components/dimaac/swipeable-cards.tsx.
Install
With the shadcn CLI
npx shadcn@latest add https://ui.dimaac.com/r/swipeable-cards.jsonIt adds components/dimaac/swipeable-cards.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/swipeable-cards.
By hand
npm install swiper lucide-react- Install the packages it uses: swiper, lucide-react.
- Copy the component file below into components/dimaac/swipeable-cards.tsx.
- It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
- Import it with import { SwipeableCards } from "@/components/dimaac/swipeable-cards". Give its parent a size: it fills it.
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. |
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.
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.txtFiles agents read
- llms.txt
- Every component and how to install it
- swipeable-cards.md
- This page as Markdown: install, props and the full source
- swipeable-cards.json
- The shadcn registry item
About
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.
- Installs to
components/dimaac/swipeable-cards.tsx- Uses
- swiper, lucide-react
- Tags
- swiper, cards, swipe, deck, images
Portrait with Neon Sunglasses by Shaun Duval on Lummi (Lummi License). Vibrant Woman Portrait by SHIHO on Lummi (Lummi License). Vivid Portrait on Pink by Amino on Lummi (Lummi License). Expressive Minimal Portrait by Steph Meade on Lummi (Lummi License). Poised Portrait by Lloyd Creates on Lummi (Lummi License). Stylish Portrait by Amino on Lummi (Lummi License). Monochrome Portrait of a Young Woman by West Kast on Lummi (Lummi License). Mystery Portrait by SHIHO on Lummi (Lummi License). Abstract Eye Close-Up by Kuba on Lummi (Lummi License). Striking Portrait by studiomiklus on Lummi (Lummi License). Fashion Portrait with Orange Visor by West Kast on Lummi (Lummi License). Stylish Studio Portrait by Amino on Lummi (Lummi License).