Snap carousel with progress
A native scroll-snap carousel of article cards with arrow buttons, a scrubbable progress bar and drag-to-scroll for mouse users.
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./snap-carousel.css";
type Story = {
kicker: string;
title: string;
read: string;
image: string;
tone: string;
};
const STORIES: Story[] = [
{
kicker: "Studio",
title: "How we design a type system in a week",
read: "6 min",
image: "/images/components-snap-carousel-story-01.jpg",
tone: "#ff5a1f",
},
{
kicker: "Process",
title: "The case for fewer, louder colors",
read: "4 min",
image: "/images/components-snap-carousel-story-02.jpg",
tone: "#2b35ff",
},
{
kicker: "Interview",
title: "A ceramicist on making the same cup 1,000 times",
read: "9 min",
image: "/images/components-snap-carousel-story-03.jpg",
tone: "#a8896b",
},
{
kicker: "Field notes",
title: "What Tokyo signage taught us about hierarchy",
read: "7 min",
image: "/images/components-snap-carousel-story-04.jpg",
tone: "#3b3f4a",
},
{
kicker: "Craft",
title: "Motion that explains instead of decorates",
read: "5 min",
image: "/images/components-snap-carousel-story-05.jpg",
tone: "#c4612f",
},
{
kicker: "Tools",
title: "Our desk setup, honestly",
read: "3 min",
image: "/images/components-snap-carousel-story-06.jpg",
tone: "#6b7d5c",
},
];
function Photo({ src, tone }: { src: string; tone: string }) {
const img = useRef<HTMLImageElement>(null);
const [missing, setMissing] = useState(false);
useEffect(() => {
const el = img.current;
if (el && el.complete && el.naturalWidth === 0) setMissing(true);
}, []);
return (
<span
className="gu-snap__photo"
style={{ "--tone": tone } as CSSProperties}
>
{!missing && (
<img
ref={img}
src={src}
alt=""
draggable={false}
onError={() => setMissing(true)}
/>
)}
</span>
);
}
/**
* SnapCarousel
* - The track is a normal horizontal scroller with scroll-snap, so touch, trackpad and keyboard work natively.
* - Mouse users can drag it (pointer events, only for pointerType "mouse").
* - Progress = scrollLeft / max, drawn as a bar you can click to jump.
*/
export function SnapCarousel({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const track = useRef<HTMLUListElement>(null);
const [progress, setProgress] = useState(0);
const [edges, setEdges] = useState({ start: true, end: false });
const drag = useRef<{ x: number; left: number; moved: boolean } | null>(null);
const [dragging, setDragging] = useState(false);
const suppressClick = useRef(false); // a mouse drag should not also open the card it ended on
const read = () => {
const el = track.current;
if (!el) return;
const max = el.scrollWidth - el.clientWidth;
setProgress(max ? el.scrollLeft / max : 0);
setEdges({ start: el.scrollLeft < 4, end: el.scrollLeft > max - 4 });
};
useEffect(() => {
const el = track.current;
if (!el) return;
const ro = new ResizeObserver(read);
ro.observe(el);
return () => ro.disconnect();
}, []);
const behavior = (): ScrollBehavior =>
matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth";
const page = (dir: 1 | -1) => {
const el = track.current;
if (!el) return;
const card = el.querySelector("li")?.getBoundingClientRect().width ?? 300;
el.scrollBy({ left: dir * (card + 16), behavior: behavior() });
};
const onDown = (e: ReactPointerEvent<HTMLUListElement>) => {
if (e.pointerType !== "mouse" || !track.current) return;
drag.current = {
x: e.clientX,
left: track.current.scrollLeft,
moved: false,
};
};
const onMove = (e: ReactPointerEvent<HTMLUListElement>) => {
const d = drag.current;
if (!d || !track.current) return;
const dx = e.clientX - d.x;
if (!d.moved && Math.abs(dx) > 5) {
d.moved = true;
setDragging(true);
track.current.setPointerCapture(e.pointerId);
}
if (d.moved) track.current.scrollLeft = d.left - dx;
};
const onUp = () => {
if (!drag.current) return;
const moved = drag.current.moved;
suppressClick.current = moved;
drag.current = null;
setDragging(false);
// Let snapping settle on the closest card after a mouse drag
if (moved)
requestAnimationFrame(() =>
track.current?.scrollBy({ left: 1, behavior: behavior() }),
);
};
return (
<section
className="gu-snap"
data-theme={theme}
aria-labelledby="gu-snap-title"
>
<header>
<h2 id="gu-snap-title">From the journal</h2>
<div className="gu-snap__arrows">
<button
type="button"
onClick={() => page(-1)}
disabled={edges.start}
aria-label="Previous stories"
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="M12.5 4 6.5 10l6 6" />
</svg>
</button>
<button
type="button"
onClick={() => page(1)}
disabled={edges.end}
aria-label="Next stories"
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="m7.5 4 6 6-6 6" />
</svg>
</button>
</div>
</header>
<ul
ref={track}
className="gu-snap__track"
data-dragging={dragging ? "" : undefined}
onScroll={read}
onPointerDown={onDown}
onPointerMove={onMove}
onPointerUp={onUp}
onPointerCancel={onUp}
onClickCapture={(e) => {
if (suppressClick.current) {
e.preventDefault();
suppressClick.current = false;
}
}}
data-lenis-prevent
>
{STORIES.map((s, i) => (
<li key={s.title}>
<a
href={`#story-${i + 1}`}
className="gu-snap__card"
draggable={false}
>
<Photo src={s.image} tone={s.tone} />
<span className="gu-snap__meta">
<small>
{s.kicker} · {s.read} read
</small>
<b>{s.title}</b>
</span>
</a>
</li>
))}
</ul>
<div
className="gu-snap__bar"
style={{ "--p": progress } as CSSProperties}
onClick={(e) => {
const el = track.current;
if (!el) return;
const r = e.currentTarget.getBoundingClientRect();
el.scrollTo({
left:
((e.clientX - r.left) / r.width) *
(el.scrollWidth - el.clientWidth),
behavior: behavior(),
});
}}
aria-hidden="true"
>
<i />
</div>
</section>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Snap carousel with progress
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A journal/article carousel using native horizontal scrolling with scroll-snap. Arrow buttons page through the cards, a slim progress bar shows (and sets) the position, and mouse users can drag the track.
## Layout and look
- Header: "From the journal" (clamp 29–48px, weight 900) and two 48px round outline arrow buttons (Ink fill on hover, 30% opacity when disabled at an edge).
- Cards: clamp(220px, 34cqi, 320px) wide, 16px gaps; a 4:5 photo with radius 20px, then "Studio · 6 min read" (muted, bold) and a title (18px, weight 800). Hover: photo zooms 5% and the title gets an orange underline.
- Progress bar: 3px gray line with a 22%-wide Ink thumb that slides with scroll.
## Behavior and motion
1. Track: display grid, grid-auto-flow column, overflow-x auto, scroll-snap-type x mandatory, hidden scrollbar.
2. Arrows: scrollBy one card width + gap (smooth). Disabled at the start/end.
3. Mouse drag: after 5px, capture the pointer, move scrollLeft, disable snapping during the drag, suppress the click, then nudge so snapping settles.
4. Clicking the bar jumps to that position.
## Images
Six editorial photos (4:5). Until they exist, each card shows a striped tone block.
## Touch
Native swipe with snap.
## Accessibility
- Real links in a list; arrows labelled "Previous/Next stories"; the progress bar is decorative.
- prefers-reduced-motion: arrow and bar jumps are instant.
## Props
theme. Edit STORIES.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### SnapCarousel.tsx
```tsx
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./snap-carousel.css";
type Story = {
kicker: string;
title: string;
read: string;
image: string;
tone: string;
};
const STORIES: Story[] = [
{
kicker: "Studio",
title: "How we design a type system in a week",
read: "6 min",
image: "/images/components-snap-carousel-story-01.jpg",
tone: "#ff5a1f",
},
{
kicker: "Process",
title: "The case for fewer, louder colors",
read: "4 min",
image: "/images/components-snap-carousel-story-02.jpg",
tone: "#2b35ff",
},
{
kicker: "Interview",
title: "A ceramicist on making the same cup 1,000 times",
read: "9 min",
image: "/images/components-snap-carousel-story-03.jpg",
tone: "#a8896b",
},
{
kicker: "Field notes",
title: "What Tokyo signage taught us about hierarchy",
read: "7 min",
image: "/images/components-snap-carousel-story-04.jpg",
tone: "#3b3f4a",
},
{
kicker: "Craft",
title: "Motion that explains instead of decorates",
read: "5 min",
image: "/images/components-snap-carousel-story-05.jpg",
tone: "#c4612f",
},
{
kicker: "Tools",
title: "Our desk setup, honestly",
read: "3 min",
image: "/images/components-snap-carousel-story-06.jpg",
tone: "#6b7d5c",
},
];
function Photo({ src, tone }: { src: string; tone: string }) {
const img = useRef<HTMLImageElement>(null);
const [missing, setMissing] = useState(false);
useEffect(() => {
const el = img.current;
if (el && el.complete && el.naturalWidth === 0) setMissing(true);
}, []);
return (
<span
className="gu-snap__photo"
style={{ "--tone": tone } as CSSProperties}
>
{!missing && (
<img
ref={img}
src={src}
alt=""
draggable={false}
onError={() => setMissing(true)}
/>
)}
</span>
);
}
/**
* SnapCarousel
* - The track is a normal horizontal scroller with scroll-snap, so touch, trackpad and keyboard work natively.
* - Mouse users can drag it (pointer events, only for pointerType "mouse").
* - Progress = scrollLeft / max, drawn as a bar you can click to jump.
*/
export function SnapCarousel({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const track = useRef<HTMLUListElement>(null);
const [progress, setProgress] = useState(0);
const [edges, setEdges] = useState({ start: true, end: false });
const drag = useRef<{ x: number; left: number; moved: boolean } | null>(null);
const [dragging, setDragging] = useState(false);
const suppressClick = useRef(false); // a mouse drag should not also open the card it ended on
const read = () => {
const el = track.current;
if (!el) return;
const max = el.scrollWidth - el.clientWidth;
setProgress(max ? el.scrollLeft / max : 0);
setEdges({ start: el.scrollLeft < 4, end: el.scrollLeft > max - 4 });
};
useEffect(() => {
const el = track.current;
if (!el) return;
const ro = new ResizeObserver(read);
ro.observe(el);
return () => ro.disconnect();
}, []);
const behavior = (): ScrollBehavior =>
matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth";
const page = (dir: 1 | -1) => {
const el = track.current;
if (!el) return;
const card = el.querySelector("li")?.getBoundingClientRect().width ?? 300;
el.scrollBy({ left: dir * (card + 16), behavior: behavior() });
};
const onDown = (e: ReactPointerEvent<HTMLUListElement>) => {
if (e.pointerType !== "mouse" || !track.current) return;
drag.current = {
x: e.clientX,
left: track.current.scrollLeft,
moved: false,
};
};
const onMove = (e: ReactPointerEvent<HTMLUListElement>) => {
const d = drag.current;
if (!d || !track.current) return;
const dx = e.clientX - d.x;
if (!d.moved && Math.abs(dx) > 5) {
d.moved = true;
setDragging(true);
track.current.setPointerCapture(e.pointerId);
}
if (d.moved) track.current.scrollLeft = d.left - dx;
};
const onUp = () => {
if (!drag.current) return;
const moved = drag.current.moved;
suppressClick.current = moved;
drag.current = null;
setDragging(false);
// Let snapping settle on the closest card after a mouse drag
if (moved)
requestAnimationFrame(() =>
track.current?.scrollBy({ left: 1, behavior: behavior() }),
);
};
return (
<section
className="gu-snap"
data-theme={theme}
aria-labelledby="gu-snap-title"
>
<header>
<h2 id="gu-snap-title">From the journal</h2>
<div className="gu-snap__arrows">
<button
type="button"
onClick={() => page(-1)}
disabled={edges.start}
aria-label="Previous stories"
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="M12.5 4 6.5 10l6 6" />
</svg>
</button>
<button
type="button"
onClick={() => page(1)}
disabled={edges.end}
aria-label="Next stories"
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="m7.5 4 6 6-6 6" />
</svg>
</button>
</div>
</header>
<ul
ref={track}
className="gu-snap__track"
data-dragging={dragging ? "" : undefined}
onScroll={read}
onPointerDown={onDown}
onPointerMove={onMove}
onPointerUp={onUp}
onPointerCancel={onUp}
onClickCapture={(e) => {
if (suppressClick.current) {
e.preventDefault();
suppressClick.current = false;
}
}}
data-lenis-prevent
>
{STORIES.map((s, i) => (
<li key={s.title}>
<a
href={`#story-${i + 1}`}
className="gu-snap__card"
draggable={false}
>
<Photo src={s.image} tone={s.tone} />
<span className="gu-snap__meta">
<small>
{s.kicker} · {s.read} read
</small>
<b>{s.title}</b>
</span>
</a>
</li>
))}
</ul>
<div
className="gu-snap__bar"
style={{ "--p": progress } as CSSProperties}
onClick={(e) => {
const el = track.current;
if (!el) return;
const r = e.currentTarget.getBoundingClientRect();
el.scrollTo({
left:
((e.clientX - r.left) / r.width) *
(el.scrollWidth - el.clientWidth),
behavior: behavior(),
});
}}
aria-hidden="true"
>
<i />
</div>
</section>
);
}
```
### snap-carousel.css
```css
/* SnapCarousel: change these colors to match your brand. */
.gu-snap {
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.14);
--accent: #ff5a1f;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
container-type: inline-size;
display: grid;
gap: 20px;
width: min(100%, 980px);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-snap[data-theme="dark"] {
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.14);
--focus: #d7ff3a;
}
.gu-snap header {
display: flex;
align-items: end;
justify-content: space-between;
gap: 16px;
}
.gu-snap h2 {
margin: 0;
font-size: clamp(1.8rem, 5cqi, 3rem);
font-weight: 900;
letter-spacing: -0.045em;
line-height: 1;
}
.gu-snap__arrows {
display: flex;
gap: 8px;
}
.gu-snap__arrows button {
display: grid;
place-items: center;
width: 48px;
height: 48px;
border: 1.5px solid var(--line);
border-radius: 50%;
background: none;
color: inherit;
cursor: pointer;
transition:
background-color 0.2s,
color 0.2s,
opacity 0.2s;
}
.gu-snap__arrows button:not(:disabled):hover {
border-color: var(--text);
background: var(--text);
color: var(--bg, #f3eee3);
}
.gu-snap[data-theme="dark"] .gu-snap__arrows button:not(:disabled):hover {
color: #0e0e10;
}
.gu-snap__arrows button:disabled {
opacity: 0.3;
cursor: default;
}
.gu-snap__arrows svg {
width: 18px;
fill: none;
stroke: currentColor;
stroke-width: 2.2;
stroke-linecap: round;
stroke-linejoin: round;
}
.gu-snap :focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}
.gu-snap__track {
display: grid;
grid-auto-flow: column;
grid-auto-columns: clamp(220px, 34cqi, 320px);
gap: 16px;
margin: 0;
padding: 0 0 4px;
overflow-x: auto;
list-style: none;
scroll-snap-type: x mandatory;
scroll-padding-inline: 0;
scrollbar-width: none;
overscroll-behavior-x: contain;
cursor: grab;
}
.gu-snap__track::-webkit-scrollbar {
display: none;
}
.gu-snap__track[data-dragging] {
cursor: grabbing;
scroll-snap-type: none; /* free movement while dragging with a mouse */
user-select: none;
}
.gu-snap__track li {
scroll-snap-align: start;
}
.gu-snap__card {
display: grid;
gap: 12px;
color: inherit;
text-decoration: none;
}
.gu-snap__photo {
position: relative;
display: block;
aspect-ratio: 4 / 5;
overflow: hidden;
border-radius: 20px;
background: var(--tone)
repeating-linear-gradient(
135deg,
rgb(255 255 255 / 0.14) 0 2px,
transparent 2px 18px
);
}
.gu-snap__photo img {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.8s var(--ease);
}
.gu-snap__card:hover img {
transform: scale(1.05);
}
.gu-snap__meta {
display: grid;
gap: 4px;
}
.gu-snap__meta small {
color: var(--muted);
font-size: 0.8rem;
font-weight: 700;
}
.gu-snap__meta b {
font-size: 1.1rem;
font-weight: 800;
letter-spacing: -0.02em;
line-height: 1.2;
}
.gu-snap__card:hover b {
text-decoration: underline 2px var(--accent);
text-underline-offset: 4px;
}
.gu-snap__bar {
position: relative;
height: 20px;
cursor: pointer;
}
.gu-snap__bar::before,
.gu-snap__bar i {
content: "";
position: absolute;
top: 9px;
left: 0;
right: 0;
height: 3px;
border-radius: 3px;
background: var(--line);
}
.gu-snap__bar i {
right: auto;
width: 22%;
background: var(--text);
/* moves across the remaining 78% of the bar */
left: calc(var(--p) * 78%);
}
@media (prefers-reduced-motion: reduce) {
.gu-snap__photo img,
.gu-snap__arrows button {
transition: none;
}
}
```
You might also want
Next piece: Split action buttonSwipe card stack
Components, advanced
Before and after slider
Components, beginner
Draggable bottom sheet
Components, advanced





