Components67 / 118
Pill Carousel
A card carousel whose page dots grow into name pills. Every dot follows the carousel's live position, so the old pill melts back into a dot while the next swells into a pill and its name sharpens out of a blur, even mid-drag. One spring runs it all: cards overshoot, squash and stretch with their speed and smear with a motion blur; dots swell as your cursor nears.
CarouselCarouselPaginationPaginationDragDragSpringsSpringsSquash & stretchSquash & stretchGSAPGSAP
Drag or flick the cardsClick a dot or a side card
Tweak
LIVELIVECards
300px
0.80
7
Look
Page
Usage
Follows your tweaks.
usage.tsx
import { PillCarousel } from "@/components/inreact/pill-carousel";
const team = [
{ src: "/team/ines.jpg", name: "Ines" },
{ src: "/team/ayo.jpg", name: "Ayo" },
{ src: "/team/nia.jpg", name: "Nia" },
];
export function Team() {
return <PillCarousel items={team} />;
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/pill-carousel.jsonOr copy the source
Install the dependencies, then save this as components/inreact/pill-carousel.tsx.
terminal
npm install gsapcomponents/inreact/pill-carousel.tsx
"use client";
import { gsap } from "gsap";
import { useEffect, useRef } from "react";
/**
* Pill Carousel: a card carousel whose page dots grow into name pills.
*
* The active dot is a pill carrying the card's name. Move to another card and
* the pill doesn't jump: every dot is tied to the carousel's live position, so
* the old pill melts back into a dot while the next one swells into a pill,
* the names sharpening out of a blur, even mid-drag. The whole thing runs on
* one spring, so it overshoots and settles; the cards squash and stretch with
* their speed and smear sideways like a long exposure, and the pills squash as they
* travel. Dots swell as your cursor comes near. Drag, flick, click a dot or
* a neighbouring card, swipe sideways on a trackpad, or use the arrow keys.
*
* <PillCarousel items={[{ src: "/team/ines.jpg", name: "Ines" }, ...]} />
*/
export interface PillCarouselItem {
src: string;
name: string;
/** object-position for the photo. */
focus?: string;
alt?: string;
}
export interface PillCarouselProps {
items: PillCarouselItem[];
/** Starting card. */
index?: number;
onChange?: (index: number) => void;
/** Card width, px. */
cardWidth?: number;
/** Card width / height. */
aspect?: number;
/** Space between the pager and the cards, px. */
gap?: number;
/** Pager colours for a light or a dark page. */
tone?: "light" | "dark";
className?: string;
}
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const clamp = (a: number, b: number, v: number) => Math.max(a, Math.min(b, v));
const smooth = (t: number) => t * t * (3 - 2 * t);
const DOT = 10;
const PILL_H = 28;
export function PillCarousel({ items, index = 0, onChange, cardWidth = 300, aspect = 0.8, gap = 22, tone = "light", className }: PillCarouselProps) {
const rootRef = useRef<HTMLDivElement>(null);
const viewRef = useRef<HTMLDivElement>(null);
const pagerRef = useRef<HTMLDivElement>(null);
const cards = useRef<(HTMLDivElement | null)[]>([]);
const dots = useRef<(HTMLSpanElement | null)[]>([]);
const names = useRef<(HTMLSpanElement | null)[]>([]);
const smears = useRef<(HTMLCanvasElement | null)[]>([]);
const go = useRef<(k: number) => void>(() => {});
const onChangeRef = useRef(onChange);
onChangeRef.current = onChange;
const n = items.length;
const cardH = Math.round(cardWidth / aspect);
useEffect(() => {
const root = rootRef.current, view = viewRef.current, pager = pagerRef.current;
if (!root || !view || !pager || !n) return;
const still = reduced();
const step = cardWidth * 0.94; // centre to centre; neighbours are scaled down
const s = { p: clamp(0, n - 1, index), v: 0, target: clamp(0, n - 1, index), drag: false };
let current = Math.round(s.target);
// the pill each name needs
let pillW: number[] = items.map(() => 64);
const measure = () => {
pillW = names.current.map((el) => (el ? el.offsetWidth + 26 : 64));
};
measure();
// each photo gets a motion-blurred twin, painted once: a horizontal long
// exposure (an equal-weight average of shifted copies). It fades in with
// speed, so a fast card smears like the camera panned, at no per-frame cost
const paintSmear = (k: number, img: HTMLImageElement) => {
const cv = smears.current[k];
const ctx = cv?.getContext("2d");
if (!cv || !ctx || !img.naturalWidth) return;
const dpr = Math.min(1.5, window.devicePixelRatio || 1);
const W = Math.round(cardWidth * dpr), Hh = Math.round(cardH * dpr);
cv.width = W;
cv.height = Hh;
// the same crop as object-fit: cover at the photo's focus point
const [fx, fy] = (items[k].focus ?? "50% 30%").split(" ").map((v) => parseFloat(v) / 100);
const iw = img.naturalWidth, ih = img.naturalHeight;
const z = Math.max(W / iw, Hh / ih);
const sw = W / z, sh = Hh / z, sx = (iw - sw) * fx, sy = (ih - sh) * fy;
ctx.drawImage(img, sx, sy, sw, sh, 0, 0, W, Hh);
const R = W * 0.075, N = 14;
for (let i = 0; i < N; i++) {
ctx.globalAlpha = 1 / (i + 2); // running average: every copy weighs the same
ctx.drawImage(img, sx, sy, sw, sh, -R + (2 * R * i) / (N - 1), 0, W, Hh);
}
ctx.globalAlpha = 1;
};
cards.current.forEach((el, k) => {
const img = el?.querySelector("img");
if (!img) return;
img.decode().then(() => paintSmear(k, img)).catch(() => {});
});
document.fonts?.ready.then(measure).catch(() => {});
// dots swell toward the cursor (their own wobbly springs)
const swell = items.map(() => ({ x: 0, v: 0, t: 0 }));
const setTarget = (k: number) => {
s.target = clamp(0, n - 1, k);
const r = Math.round(s.target);
if (r !== current) {
current = r;
onChangeRef.current?.(r);
}
};
go.current = setTarget;
const last = { card: items.map(() => ""), cardF: items.map(() => ""), dot: items.map(() => ""), name: items.map(() => "") };
// pre-warm: for the first few frames every photo (and each name blur) is
// drawn, invisibly, in view, so the GPU uploads and compiles before anyone moves
let warm = still ? 0 : 8;
const tick = (_t: number, ms: number) => {
const dt = Math.min(ms / 1000, 1 / 30);
if (warm > 0) {
warm--;
for (let k = 0; k < n; k++) {
const el = cards.current[k];
if (!el || k === Math.round(s.p)) continue;
el.style.transform = warm ? `translate3d(${((k - s.p) * cardWidth * 0.08).toFixed(1)}px,0,0) scale(0.9)` : "";
el.style.opacity = warm ? "0.02" : ""; // drawn (not culled), hidden behind the front card
}
names.current.forEach((el, k) => {
if (el) el.style.filter = warm ? `blur(${(k % 4) + 1}px)` : "none";
});
if (warm) return;
last.name.fill("");
last.card.fill("");
last.cardF.fill("");
}
if (!s.drag) {
if (still) {
s.p = s.target;
s.v = 0;
} else {
// one spring for everything: a little overshoot, then it settles
const k = 190, c = 2 * 0.58 * Math.sqrt(k), h = dt / 2;
for (let i = 0; i < 2; i++) {
s.v += (k * (s.target - s.p) - c * s.v) * h;
s.p += s.v * h;
}
}
}
const speed = Math.abs(s.v); // cards per second
// cards: scaled by distance from centre, stretched and smeared by speed
const st = still ? 0 : Math.min(0.12, speed * 0.035);
// motion blur: the pre-painted smear fades in with speed
const smear = still ? 0 : Math.min(1, Math.max(0, speed - 0.6) / 2);
const sop = smooth(smear);
for (let k = 0; k < n; k++) {
const el = cards.current[k];
if (!el) continue;
const d = k - s.p, ad = Math.abs(d);
const sc = 1 - 0.16 * Math.min(ad, 1.4);
const x = d * step + Math.sign(d) * Math.max(0, ad - 1) * -cardWidth * 0.04;
const tr = `translate3d(${x.toFixed(2)}px,0,0) scale(${(sc * (1 + st)).toFixed(4)},${(sc / (1 + st)).toFixed(4)})`;
if (tr !== last.card[k]) {
el.style.transform = tr;
el.style.zIndex = String(100 - Math.round(ad * 10));
last.card[k] = tr;
}
// far cards fade, but never to 0: they stay rastered, so they don't hitch when they arrive
const fade = ad > 2.2 ? Math.max(0.001, 1 - (ad - 2.2) / 0.8) : 1;
const f = `${sop.toFixed(3)}|${fade.toFixed(2)}`;
if (f !== last.cardF[k]) {
el.style.opacity = fade.toFixed(2);
// never quite 0, so the smear stays on the GPU and doesn't hitch on the first move
const sm = smears.current[k];
if (sm) sm.style.opacity = Math.max(0.004, sop).toFixed(3);
last.cardF[k] = f;
}
}
// pager: each dot is as much of a pill as the carousel is on its card
const squash = still ? 0 : Math.min(0.16, speed * 0.05);
for (let k = 0; k < n; k++) {
const dot = dots.current[k], name = names.current[k];
if (!dot || !name) continue;
const sw = swell[k];
if (still) sw.x = 0;
else {
const kk = 260, cc = 2 * 0.32 * Math.sqrt(kk);
sw.v += (kk * (sw.t - sw.x) - cc * sw.v) * dt;
sw.x += sw.v * dt;
}
const a = smooth(clamp(0, 1, 1 - Math.abs(k - s.p)));
const w = DOT + (pillW[k] - DOT) * a;
const hgt = (DOT + (PILL_H - DOT) * a) * (1 - squash * a);
const grow = 1 + 0.45 * sw.x * (1 - a);
const css = `${w.toFixed(2)}|${hgt.toFixed(2)}|${grow.toFixed(3)}`;
if (css !== last.dot[k]) {
dot.style.width = `${w.toFixed(2)}px`;
dot.style.height = `${hgt.toFixed(2)}px`;
dot.style.transform = `scale(${grow.toFixed(3)})`;
last.dot[k] = css;
}
const op = smooth(clamp(0, 1, (a - 0.3) / 0.7));
const nb = Math.round((1 - op) * 4); // whole pixels, as above
const ncss = `${op.toFixed(3)}|${nb}|${a.toFixed(3)}`;
if (ncss !== last.name[k]) {
name.style.opacity = op.toFixed(3);
name.style.filter = nb ? `blur(${nb}px)` : "none";
name.style.transform = `translate(-50%,-50%) scale(${(0.82 + 0.18 * a).toFixed(3)})`;
last.name[k] = ncss;
}
}
};
gsap.ticker.add(tick);
// drag and flick
let sx = 0, sp = 0, lx = 0, lt = 0, moved = 0, id = -1;
const onDown = (e: PointerEvent) => {
if (e.button !== 0) return;
id = e.pointerId;
s.drag = true;
sx = lx = e.clientX;
sp = s.p;
lt = performance.now();
moved = 0;
s.v = 0;
};
const onMove = (e: PointerEvent) => {
if (e.pointerId !== id || !s.drag) return;
const dx = e.clientX - sx;
moved = Math.max(moved, Math.abs(dx));
if (moved > 4 && !view.hasPointerCapture(e.pointerId)) view.setPointerCapture(e.pointerId);
let p = sp - dx / step;
// rubber band past the ends
if (p < 0) p = -Math.pow(-p, 0.7) * 0.4;
if (p > n - 1) p = n - 1 + Math.pow(p - (n - 1), 0.7) * 0.4;
const now = performance.now(), ddt = Math.max(1, now - lt) / 1000;
const vNow = (-(e.clientX - lx) / step) / ddt;
s.v = s.v * 0.6 + vNow * 0.4;
lx = e.clientX;
lt = now;
s.p = p;
};
const onUp = (e: PointerEvent) => {
if (e.pointerId !== id) return;
id = -1;
if (!s.drag) return;
s.drag = false;
if (moved < 5) {
// a click: a neighbouring card comes to the front
const card = (e.target as HTMLElement).closest<HTMLElement>("[data-card]");
if (card) setTarget(Number(card.dataset.card));
return;
}
// a flick carries on: project where the spring would take it
setTarget(Math.round(s.p + clamp(-2.5, 2.5, s.v * 0.22)));
};
view.addEventListener("pointerdown", onDown);
view.addEventListener("pointermove", onMove);
view.addEventListener("pointerup", onUp);
view.addEventListener("pointercancel", onUp);
// sideways trackpad swipes
let wheelAcc = 0, wheelLock = 0;
const onWheel = (e: WheelEvent) => {
const dx = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.shiftKey ? e.deltaY : 0;
if (!dx) return;
e.preventDefault();
const now = performance.now();
if (now < wheelLock) return;
wheelAcc += dx;
if (Math.abs(wheelAcc) > 40) {
setTarget(Math.round(s.target) + Math.sign(wheelAcc));
wheelAcc = 0;
wheelLock = now + 380;
}
};
view.addEventListener("wheel", onWheel, { passive: false });
// dots near the cursor swell
const onPagerMove = (e: PointerEvent) => {
for (let k = 0; k < n; k++) {
const d = dots.current[k];
if (!d) continue;
const r = d.getBoundingClientRect();
const dist = Math.hypot(e.clientX - (r.left + r.width / 2), e.clientY - (r.top + r.height / 2));
swell[k].t = clamp(0, 1, 1 - dist / 46);
}
};
const onPagerLeave = () => swell.forEach((w) => (w.t = 0));
pager.addEventListener("pointermove", onPagerMove);
pager.addEventListener("pointerleave", onPagerLeave);
const onKey = (e: KeyboardEvent) => {
if (e.key === "ArrowRight") setTarget(Math.round(s.target) + 1);
else if (e.key === "ArrowLeft") setTarget(Math.round(s.target) - 1);
else return;
e.preventDefault();
};
root.addEventListener("keydown", onKey);
return () => {
gsap.ticker.remove(tick);
view.removeEventListener("pointerdown", onDown);
view.removeEventListener("pointermove", onMove);
view.removeEventListener("pointerup", onUp);
view.removeEventListener("pointercancel", onUp);
view.removeEventListener("wheel", onWheel);
pager.removeEventListener("pointermove", onPagerMove);
pager.removeEventListener("pointerleave", onPagerLeave);
root.removeEventListener("keydown", onKey);
};
// index is only the starting card
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [n, cardWidth, items]);
const light = tone === "light";
return (
<div
ref={rootRef}
tabIndex={0}
role="region"
aria-roledescription="carousel"
aria-label="Carousel"
className={["relative w-full select-none outline-none focus-visible:ring-2 focus-visible:ring-black/20 rounded-[28px]", className].filter(Boolean).join(" ")}
>
<div ref={viewRef} className="relative w-full cursor-grab touch-pan-y active:cursor-grabbing" style={{ height: cardH }}>
{items.map((it, k) => (
<div
key={k}
ref={(el) => void (cards.current[k] = el)}
data-card={k}
role="group"
aria-roledescription="slide"
aria-label={`${k + 1} of ${n}: ${it.name}`}
className="absolute top-0 left-1/2 overflow-hidden will-change-transform"
style={{
width: cardWidth,
height: cardH,
marginLeft: -cardWidth / 2,
borderRadius: Math.round(cardWidth * 0.075),
background: light ? "#ecebee" : "#1a1a1d",
boxShadow: light
? "0 1px 2px rgba(20,20,30,.05), 0 6px 16px rgba(20,20,30,.06), 0 18px 40px rgba(20,20,30,.07)"
: "0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35)",
}}
>
<img src={it.src} alt={it.alt ?? it.name} draggable={false} decoding="async" className="pointer-events-none h-full w-full object-cover" style={{ objectPosition: it.focus ?? "50% 30%" }} />
{/* the motion blur, painted once */}
<canvas ref={(el) => void (smears.current[k] = el)} aria-hidden="true" className="pointer-events-none absolute inset-0 h-full w-full opacity-[0.004] will-change-[opacity]" />
</div>
))}
</div>
<div ref={pagerRef} className="relative flex items-center justify-center" style={{ marginTop: gap, height: PILL_H + 8 }}>
{items.map((it, k) => (
<button
key={k}
type="button"
aria-label={`Show ${it.name}`}
onClick={() => go.current(k)}
className="grid cursor-pointer place-items-center p-[3px] outline-none [-webkit-tap-highlight-color:transparent] focus-visible:[&>span]:ring-2 focus-visible:[&>span]:ring-current/30"
style={{ height: PILL_H + 8 }}
>
<span
ref={(el) => void (dots.current[k] = el)}
className="relative block overflow-hidden rounded-full"
style={{
width: DOT,
height: DOT,
background: light ? "rgba(20,18,26,.085)" : "rgba(255,255,255,.14)",
boxShadow: light ? "inset 0 1px 0 rgba(255,255,255,.7)" : "inset 0 1px 0 rgba(255,255,255,.08)",
}}
>
<span
ref={(el) => void (names.current[k] = el)}
className="absolute top-1/2 left-1/2 whitespace-nowrap font-medium"
style={{ fontSize: 13.5, letterSpacing: "0.005em", color: light ? "#6e6b72" : "rgba(255,255,255,.82)", opacity: 0, transform: "translate(-50%,-50%)" }}
>
{it.name}
</span>
</span>
</button>
))}
</div>
</div>
);
}
export default PillCarousel;
One prompt, any AI agent
Paste into Claude Code, Codex, Cursor or Windsurf. It carries your tweaks and the full source, so the agent installs it exactly.
Props
8 propsitems{ src: string; name: string; focus?: string; alt?: string }[]The cards (keep the array stable).indexnumberStarting card.default 0default 0onChange(index: number) => voidCalled when the card changes.cardWidthnumberCard width, px.default 300default 300aspectnumberCard width / height.default 0.8default 0.8gapnumberSpace between cards and pager, px.default 22default 22tone"light" | "dark"Pager colours for the page.default "light"default "light"classNamestringExtra classes.