Coverflow carousels
Five 3D coverflow carousels: flow, deep, tilt, fan and stack. Each keeps the active slide flat and front while neighbors rotate on the Y axis, scale down and recede in perspective. Self-contained (no embla), token-driven and sized.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/carousel-coverflowDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/carousel-coverflow.tsx"use client"
import * as React from "react"
import { ChevronLeft, ChevronRight } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Coverflow carousel family: 5 self-contained 3D perspective sliders (NO embla,
NO radix). The active slide sits in front and stays flat; the neighbouring
slides are pushed back with rotateY + scale + translateZ. Each export is a
complete carousel: it holds the slide index internally, and the arrows + dots
change it. Deterministic (index-based, no Date.now or Math.random). The root
carries data-slot="styled-carousel" and role="region". Color comes ONLY from
semantic tokens; the active dot is primary, via alpha color-mix. Gated on
framer's useReducedMotion: under reduced motion there is no 3D transform and
the slide changes instantly. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const trackHeight: Record<StyledSize, string> = {
sm: "h-40",
md: "h-56",
lg: "h-72",
xl: "h-96",
}
const focusRing =
"outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50"
const arrowBtn =
"absolute top-1/2 z-30 inline-flex size-9 -translate-y-1/2 items-center justify-center rounded-full border border-border bg-[color-mix(in_oklab,var(--color-background)_72%,transparent)] text-foreground shadow-sm transition-colors supports-[backdrop-filter]:backdrop-blur hover:bg-background [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
/* Token-surfaced, numbered placeholder panels - for prop-less rendering. */
const panelTones = ["bg-surface-2", "bg-surface-3", "bg-muted", "bg-accent"]
function defaultSlides(count = 4): React.ReactNode[] {
return Array.from({ length: count }, (_, i) => (
<div
key={i}
className={cn(
"flex h-full w-full items-center justify-center",
panelTones[i % panelTones.length]
)}
>
<span className="text-4xl font-semibold tabular-nums text-foreground">{i + 1}</span>
</div>
))
}
function resolveSlides(slides?: React.ReactNode[]): React.ReactNode[] {
return slides && slides.length > 0 ? slides : defaultSlides()
}
/* Ortak index state: sonsuz sarma (wrap) ile prev/next ve dogrudan jump. */
function useCarousel(count: number) {
const [index, setIndex] = React.useState(0)
const total = Math.max(count, 1)
const wrap = React.useCallback((i: number) => ((i % total) + total) % total, [total])
const go = React.useCallback((i: number) => setIndex(wrap(i)), [wrap])
const next = React.useCallback(() => setIndex((p) => wrap(p + 1)), [wrap])
const prev = React.useCallback(() => setIndex((p) => wrap(p - 1)), [wrap])
return { index: Math.min(index, total - 1), setIndex: go, next, prev }
}
/* Shared dot group: the active one widens and turns primary. */
function CarouselDots({
count,
index,
onSelect,
className,
}: {
count: number
index: number
onSelect: (i: number) => void
className?: string
}) {
const reduce = useReducedMotion()
return (
<div
role="tablist"
aria-label="Choose slide to display"
className={cn("flex items-center justify-center", className)}
>
{Array.from({ length: count }, (_, i) => {
const active = i === index
return (
<button
key={i}
type="button"
role="tab"
aria-selected={active}
aria-label={`Go to slide ${i + 1}`}
onClick={() => onSelect(i)}
className={cn(
"relative inline-flex h-6 min-w-6 shrink-0 items-center justify-center rounded-full",
focusRing
)}
>
<motion.span
aria-hidden="true"
layout={!reduce}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 500, damping: 34 }}
className={cn(
"block h-2 rounded-full transition-colors",
active
? "w-6 bg-primary"
: "w-2 bg-[color-mix(in_oklab,var(--color-foreground)_25%,transparent)] hover:bg-[color-mix(in_oklab,var(--color-foreground)_45%,transparent)]"
)}
/>
</button>
)
})}
</div>
)
}
interface CarouselProps {
className?: string
size?: StyledSize
slides?: React.ReactNode[]
}
/* The setting set that decides the 3D intensity. gap: the neighbouring slide's horizontal shift (a percentage of its own width), angle: the rotateY degrees, scaleStep: the shrink per step, minScale: the lower bound, z: the translateZ pull-back (px), perspective: the scene depth, visible: how many neighbours are visible, width: the slide width. */
interface CoverflowConfig {
gap: number
angle: number
scaleStep: number
minScale: number
z: number
perspective: string
visible: number
width: string
}
function Coverflow({
className,
size = "md",
slides,
config,
}: CarouselProps & { config: CoverflowConfig }) {
const reduce = useReducedMotion()
const items = resolveSlides(slides)
const { index, setIndex, next, prev } = useCarousel(items.length)
return (
<div
data-slot="styled-carousel"
role="region"
aria-roledescription="carousel"
aria-label="Gallery"
className={cn("relative w-full", className)}
>
<div
className={cn("relative w-full overflow-hidden rounded-xl", trackHeight[size])}
style={reduce ? undefined : { perspective: config.perspective }}
>
<div className="relative h-full w-full [transform-style:preserve-3d]">
{items.map((slide, i) => {
const offset = i - index
const abs = Math.abs(offset)
const active = offset === 0
const target = reduce
? { x: "-50%", rotateY: 0, scale: 1, z: 0, opacity: active ? 1 : 0 }
: {
x: `calc(-50% + ${offset * config.gap}%)`,
rotateY: -offset * config.angle,
scale: active ? 1 : Math.max(config.minScale, 1 - abs * config.scaleStep),
z: active ? 0 : -abs * config.z,
opacity: abs > config.visible ? 0 : 1,
}
return (
<motion.div
key={i}
role="group"
aria-roledescription="slide"
aria-label={`${i + 1} of ${items.length}`}
aria-hidden={!active}
className="absolute left-1/2 top-1/2 h-[86%] -translate-y-1/2 overflow-hidden rounded-xl border border-border shadow-sm [transform-style:preserve-3d] [backface-visibility:hidden]"
style={{ width: config.width, zIndex: 100 - abs }}
initial={false}
animate={target}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 260, damping: 30 }}
>
<button
type="button"
tabIndex={active ? -1 : 0}
aria-label={`Go to slide ${i + 1}`}
onClick={() => setIndex(i)}
className={cn("block h-full w-full", focusRing)}
>
{slide}
</button>
</motion.div>
)
})}
</div>
<button type="button" aria-label="Previous slide" onClick={prev} className={cn(arrowBtn, focusRing, "left-3")}>
<ChevronLeft />
</button>
<button type="button" aria-label="Next slide" onClick={next} className={cn(arrowBtn, focusRing, "right-3")}>
<ChevronRight />
</button>
</div>
<CarouselDots count={items.length} index={index} onSelect={setIndex} className="mt-3" />
</div>
)
}
/* Flow: dengeli coverflow, orta yogunlukta rotateY + geri cekilme. */
export function FlowCarousel(props: CarouselProps) {
return (
<Coverflow
{...props}
config={{ gap: 54, angle: 35, scaleStep: 0.12, minScale: 0.7, z: 120, perspective: "1000px", visible: 2, width: "60%" }}
/>
)
}
/* Deep: guclu perspektif, komsular derine cekilir ve daha cok kuculur. */
export function DeepCarousel(props: CarouselProps) {
return (
<Coverflow
{...props}
config={{ gap: 46, angle: 46, scaleStep: 0.16, minScale: 0.64, z: 230, perspective: "900px", visible: 2, width: "60%" }}
/>
)
}
/* Tilt: hafif, ince egim; genis aralik, dusuk aci. */
export function TiltCarousel(props: CarouselProps) {
return (
<Coverflow
{...props}
config={{ gap: 62, angle: 20, scaleStep: 0.08, minScale: 0.78, z: 80, perspective: "1200px", visible: 2, width: "58%" }}
/>
)
}
/* Fan: genis yelpaze, buyuk aci ile uc komsu birden gorunur. */
export function FanCarousel(props: CarouselProps) {
return (
<Coverflow
{...props}
config={{ gap: 40, angle: 54, scaleStep: 0.13, minScale: 0.66, z: 170, perspective: "1100px", visible: 3, width: "56%" }}
/>
)
}
/* Stack: komsular merkezin arkasina istiflenir; az yatay, cok derinlik. */
export function StackCarousel(props: CarouselProps) {
return (
<Coverflow
{...props}
config={{ gap: 16, angle: 12, scaleStep: 0.09, minScale: 0.72, z: 210, perspective: "1000px", visible: 3, width: "64%" }}
/>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Flow
Balanced coverflow: neighbors rotate and recede at a medium intensity.
import { FlowCarousel } from "@/components/ui/carousel-coverflow"
<FlowCarousel />Deep
Strong perspective: neighbors pull far back and shrink more.
import { DeepCarousel } from "@/components/ui/carousel-coverflow"
<DeepCarousel />Tilt
Subtle tilt: wide spacing and a low rotation angle.
import { TiltCarousel } from "@/components/ui/carousel-coverflow"
<TiltCarousel />Fan
Wide fan: a large angle spreads three neighbors at once.
import { FanCarousel } from "@/components/ui/carousel-coverflow"
<FanCarousel />Stack
Neighbors stack behind the center with little offset and deep recession.
import { StackCarousel } from "@/components/ui/carousel-coverflow"
<StackCarousel />ai2 Coverflow carousels: 5 styled variations on the token system
The ai2 Coverflow carousels are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around 3D coverflow carousels where neighbor slides rotate and recede in perspective. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion springs the rotateY, scale and translateZ of each slide on change. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the 3D transforms are skipped and the active slide shows instantly.
What is in the ai2 Coverflow carousels?
5 exports in one file: Flow, Deep, Tilt, Fan and Stack. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion springs the rotateY, scale and translateZ of each slide on change.
- Reduced-motion aware: Under prefers-reduced-motion, the 3D transforms are skipped and the active slide shows instantly.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Coverflow carousels sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.