Styled carousel
Five carousels: slide, fade, cards, dots and auto. Each is self-contained (no embla), keeps an internal slide index, exposes an accessible region, and is sized.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/carousel-styledDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/carousel-styled.tsx"use client"
import * as React from "react"
import { ChevronLeft, ChevronRight } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Carousel family: 5 self-contained (NO embla, NO radix) sliding components. Each
holds the active slide index internally; prev/next buttons and/or dots change
it, and the slides transition with framer. Deterministic (index-based, no
Date.now or Math.random). The root carries role="region"
aria-roledescription="carousel". Color comes ONLY from semantic tokens; the
active dot is primary, via alpha color-mix (never var(--x)/0.4). Gated on
framer's useReducedMotion (under reduced motion the slide change is instant). */
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-10 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", 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[]
}
/* SlideCarousel: slide'lar yatay kayar (translateX), oklar + noktalar. */
export function SlideCarousel({ className, size = "md", slides }: CarouselProps) {
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 border border-border", trackHeight[size])}>
<motion.div
className="flex h-full w-full"
animate={{ x: `-${index * 100}%` }}
transition={reduce ? { duration: 0 } : { type: "spring", stiffness: 320, damping: 34 }}
>
{items.map((slide, i) => (
<div
key={i}
role="group"
aria-roledescription="slide"
aria-label={`${i + 1} of ${items.length}`}
aria-hidden={i !== index}
className="h-full w-full shrink-0 basis-full overflow-hidden"
>
{slide}
</div>
))}
</motion.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>
)
}
/* FadeCarousel: the slides cross-fade, with dots. */
export function FadeCarousel({ className, size = "md", slides }: CarouselProps) {
const reduce = useReducedMotion()
const items = resolveSlides(slides)
const { index, setIndex } = 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 border border-border", trackHeight[size])}>
<AnimatePresence initial={false} mode="sync">
<motion.div
key={index}
role="group"
aria-roledescription="slide"
aria-label={`${index + 1} of ${items.length}`}
className="absolute inset-0 h-full w-full"
initial={reduce ? false : { opacity: 0 }}
animate={{ opacity: 1 }}
exit={reduce ? { opacity: 0 } : { opacity: 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.4, ease: "easeInOut" }}
>
{items[index]}
</motion.div>
</AnimatePresence>
</div>
<CarouselDots count={items.length} index={index} onSelect={setIndex} className="mt-3" />
</div>
)
}
/* CardsCarousel: a sliver of the previous/next card shows at the sides and the
middle card comes forward (larger + fully opaque). Arrows + dots. */
export function CardsCarousel({ className, size = "md", slides }: CarouselProps) {
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])}>
<motion.div
className="flex h-full w-full items-center"
animate={{ x: `calc(${-index * 70}% + 15%)` }}
transition={reduce ? { duration: 0 } : { type: "spring", stiffness: 300, damping: 32 }}
>
{items.map((slide, i) => {
const active = i === index
return (
<motion.div
key={i}
role="group"
aria-roledescription="slide"
aria-label={`${i + 1} of ${items.length}`}
aria-hidden={!active}
className="h-full shrink-0 basis-[70%] px-2"
animate={{ scale: active ? 1 : 0.86, opacity: active ? 1 : 0.5 }}
transition={reduce ? { duration: 0 } : { type: "spring", stiffness: 300, damping: 32 }}
>
<button
type="button"
tabIndex={active ? -1 : 0}
aria-label={`Go to slide ${i + 1}`}
onClick={() => setIndex(i)}
className={cn("block h-full w-full overflow-hidden rounded-xl border border-border", focusRing)}
>
{slide}
</button>
</motion.div>
)
})}
</motion.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>
)
}
/* DotsCarousel: minimal, dots only (no arrows); tapping a dot jumps to that slide.
The dots overlap the bottom edge of the image. */
export function DotsCarousel({ className, size = "md", slides }: CarouselProps) {
const reduce = useReducedMotion()
const items = resolveSlides(slides)
const { index, setIndex } = useCarousel(items.length)
return (
<div
data-slot="styled-carousel"
role="region"
aria-roledescription="carousel"
aria-label="Gallery"
className={cn("relative w-full overflow-hidden rounded-xl border border-border", trackHeight[size], className)}
>
<AnimatePresence initial={false} mode="sync">
<motion.div
key={index}
role="group"
aria-roledescription="slide"
aria-label={`${index + 1} of ${items.length}`}
className="absolute inset-0 h-full w-full"
initial={reduce ? false : { opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.35, ease: "easeInOut" }}
>
{items[index]}
</motion.div>
</AnimatePresence>
<div className="absolute inset-x-0 bottom-3 z-10">
<CarouselDots count={items.length} index={index} onSelect={setIndex} />
</div>
</div>
)
}
/* AutoCarousel: advances automatically on an interval with a token-coloured progress bar; it pauses on hover. setInterval plus cleanup (not Date.now, which is allowed). */
export function AutoCarousel({ className, size = "md", slides }: CarouselProps) {
const reduce = useReducedMotion()
const items = resolveSlides(slides)
const { index, setIndex, next } = useCarousel(items.length)
const [paused, setPaused] = React.useState(false)
const intervalMs = 4000
React.useEffect(() => {
if (paused || items.length <= 1) return
const id = window.setInterval(() => next(), intervalMs)
return () => window.clearInterval(id)
}, [paused, next, items.length])
return (
<div
data-slot="styled-carousel"
role="region"
aria-roledescription="carousel"
aria-label="Gallery"
className={cn("relative w-full", className)}
onMouseEnter={() => setPaused(true)}
onMouseLeave={() => setPaused(false)}
onFocusCapture={() => setPaused(true)}
onBlurCapture={() => setPaused(false)}
>
<div className={cn("relative w-full overflow-hidden rounded-xl border border-border", trackHeight[size])}>
<AnimatePresence initial={false} mode="sync">
<motion.div
key={index}
role="group"
aria-roledescription="slide"
aria-label={`${index + 1} of ${items.length}`}
className="absolute inset-0 h-full w-full"
initial={reduce ? false : { opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.4, ease: "easeInOut" }}
>
{items[index]}
</motion.div>
</AnimatePresence>
<div className="absolute inset-x-0 bottom-0 z-10 h-1 bg-[color-mix(in_oklab,var(--color-foreground)_14%,transparent)]">
{reduce ? (
<div className="h-full bg-primary" style={{ width: "100%" }} />
) : paused ? (
<div className="h-full bg-primary" style={{ width: "0%" }} />
) : (
<motion.div
key={index}
className="h-full bg-primary"
initial={{ width: "0%" }}
animate={{ width: "100%" }}
transition={{ duration: intervalMs / 1000, ease: "linear" }}
/>
)}
</div>
</div>
<CarouselDots count={items.length} index={index} onSelect={setIndex} className="mt-3" />
</div>
)
}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.
Slide
Slides move horizontally with arrows and dots.
import { SlideCarousel } from "@/components/ui/carousel-styled"
<SlideCarousel />Fade
Slides cross-fade between each other.
import { FadeCarousel } from "@/components/ui/carousel-styled"
<FadeCarousel />Cards
The center card scales up while neighbors peek at the edges.
import { CardsCarousel } from "@/components/ui/carousel-styled"
<CardsCarousel />Dots
Minimal, dots only, overlaid on the image.
import { DotsCarousel } from "@/components/ui/carousel-styled"
<DotsCarousel />Auto
Advances on an interval with a token progress bar; pauses on hover.
import { AutoCarousel } from "@/components/ui/carousel-styled"
<AutoCarousel />ai2 Styled carousel: 5 styled variations on the token system
The ai2 Styled carousel are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around slideshow carousels. 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 slides, fades and scales between slides. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, slides change instantly with no sliding or fading.
What is in the ai2 Styled carousel?
5 exports in one file: Slide, Fade, Cards, Dots and Auto. 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 slides, fades and scales between slides.
- Reduced-motion aware: Under prefers-reduced-motion, slides change instantly with no sliding or fading.
- 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 Styled carousel 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.