Parallax carousels
Five parallax carousels: soft, deep, layered, tilt and zoom. The frame slides a full step while the inner layer shifts less, creating depth. 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-parallaxDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/carousel-parallax.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"
/* Parallax carousel family: 5 self-contained sliders (NO embla, NO radix). On a
slide change the frame moves a full slide, but the inner layer of the slide
moves less (parallax depth). 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 parallax 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-20 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[]
}
/* Parallax settings. depth: how much less the inner layer moves relative to the frame (a percentage of its own width; smaller = deeper lag), rotate: rotateY per step (degrees), zoom: a slight shrink on the inactive slide, overlay: an extra token layer. */
interface ParallaxConfig {
depth: number
rotate: number
zoom: number
overlay: boolean
}
function Parallax({
className,
size = "md",
slides,
config,
}: CarouselProps & { config: ParallaxConfig }) {
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])}
style={config.rotate && !reduce ? { perspective: "1200px" } : undefined}
>
<motion.div
className="flex h-full w-full [transform-style:preserve-3d]"
animate={{ x: `-${index * 100}%` }}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 300, damping: 34 }}
>
{items.map((slide, i) => {
const offset = i - index
const active = offset === 0
const inner = reduce
? { x: "0%", rotateY: 0, scale: 1 }
: {
x: `${offset * config.depth}%`,
rotateY: config.rotate ? -offset * config.rotate : 0,
scale: active ? 1.1 : 1.1 - config.zoom,
}
return (
<div
key={i}
role="group"
aria-roledescription="slide"
aria-label={`${i + 1} of ${items.length}`}
aria-hidden={!active}
className="relative h-full w-full shrink-0 basis-full overflow-hidden [transform-style:preserve-3d]"
>
<motion.div
className="h-full w-full"
initial={false}
animate={inner}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 300, damping: 34 }}
>
{slide}
</motion.div>
{config.overlay && !reduce ? (
<motion.span
aria-hidden="true"
className="pointer-events-none absolute inset-0 bg-gradient-to-tr from-[color-mix(in_oklab,var(--color-foreground)_16%,transparent)] to-transparent"
initial={false}
animate={{ x: `${offset * config.depth * 2}%` }}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 300, damping: 34 }}
/>
) : null}
</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>
)
}
/* Soft: hafif parallax, ic katman az kayar. */
export function SoftCarousel(props: CarouselProps) {
return <Parallax {...props} config={{ depth: 8, rotate: 0, zoom: 0, overlay: false }} />
}
/* Deep: pronounced parallax; the inner layer lags far behind. */
export function DeepCarousel(props: CarouselProps) {
return <Parallax {...props} config={{ depth: 22, rotate: 0, zoom: 0, overlay: false }} />
}
/* Layered: parallax + a token overlay layer moving at a different speed. */
export function LayeredCarousel(props: CarouselProps) {
return <Parallax {...props} config={{ depth: 12, rotate: 0, zoom: 0, overlay: true }} />
}
/* Tilt: parallax + hafif 3D rotateY egimi. */
export function TiltCarousel(props: CarouselProps) {
return <Parallax {...props} config={{ depth: 12, rotate: 10, zoom: 0, overlay: false }} />
}
/* Zoom: parallax + aktif slide yakinlasir, komsular uzaklasir. */
export function ZoomCarousel(props: CarouselProps) {
return <Parallax {...props} config={{ depth: 10, rotate: 0, zoom: 0.14, overlay: false }} />
}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.
Soft
A gentle parallax where the inner layer lags slightly behind the frame.
import { SoftCarousel } from "@/components/ui/carousel-parallax"
<SoftCarousel />Deep
A pronounced parallax where the inner layer lags far behind.
import { DeepCarousel } from "@/components/ui/carousel-parallax"
<DeepCarousel />Layered
Parallax plus a token overlay layer that shifts at a different rate.
import { LayeredCarousel } from "@/components/ui/carousel-parallax"
<LayeredCarousel />Tilt
Parallax with a slight 3D rotateY tilt on the inner layer.
import { TiltCarousel } from "@/components/ui/carousel-parallax"
<TiltCarousel />Zoom
Parallax where the active slide zooms in and neighbors zoom out.
import { ZoomCarousel } from "@/components/ui/carousel-parallax"
<ZoomCarousel />ai2 Parallax carousels: 5 styled variations on the token system
The ai2 Parallax carousels are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around parallax carousels where the inner layer shifts less than the slide for depth. 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 frame and the slower inner layer 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 parallax offset is skipped and slides change instantly.
What is in the ai2 Parallax carousels?
5 exports in one file: Soft, Deep, Layered, Tilt and Zoom. 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 frame and the slower inner layer on change.
- Reduced-motion aware: Under prefers-reduced-motion, the parallax offset is skipped and slides change 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 Parallax 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.