Components
Carousel
One slide at a time, in a scroll-snapped strip the page's own scroll drives, with the slides rendered on the server.
Usage
Three slides
A slide is a figure the width of the track.
Its caption sits under it in the caption ink.
The counter and the buttons say where you are.
1 / 3
Uses.aka-carousel-track.aka-carousel-slide
import { Carousel } from '@/components/ui/carousel'
import { CarouselSlide } from '@/components/ui/carousel-slide'
<Carousel label="Three sample slides">
{lines.map((line, i) => (
<CarouselSlide key={line} index={i + 1} total={lines.length} caption={`Slide ${i + 1}`}>
<div className="aka-card-well aka-card-media aspect-[16/9] rounded-xl">{line}</div>
</CarouselSlide>
))}
</Carousel>The slides are rendered on the server and handed to the one client piece as finished markup, so adding a slide does not grow the bundle by a byte. Where you are is read from the track's scroll position and written to the DOM, never to React state: law 08 applied to a strip instead of a deck.
Props
Carousel
Client component
| Prop | Description |
|---|---|
labelstring | What the strip is a strip of, for the region's name and the counter. |
childrenReactNode | CarouselSlides, rendered on the server. |
className?string | On the outer region. |
CarouselSlide
Server component
| Prop | Description |
|---|---|
indexnumber | This slide's position, from 1, announced as "3 of 10". |
totalnumber | How many slides the carousel holds. |
caption?ReactNode | One or two lines under the figure, in the caption ink. |
childrenReactNode | The figure: an image, a recording's poster, a card. |
Best practices
When to use
- A sequence of figures read one at a time: the screens of a flow, the states of a tool.
- A grid instead when the reader should see all of them, or compare two side by side.
Behaviour
- An ordinary horizontal scroll with CSS snap: touch, trackpad and the keyboard move it, and nothing calls preventDefault.
- The two buttons move the track by exactly one slide, for a mouse.
- Smooth scrolling only where motion is wanted; under reduced motion the track and the buttons jump.
Accessibility
- The carousel is a named region, and each slide a group announced as "n of total".
- The counter is a polite live region, and both buttons are named after the strip.
- The track is focusable and shows the select outline, so the arrow keys can move it.
CSS
The rules, as globals.css has them, with the reasoning the stylesheet keeps beside them.
@layer components {
/*
* The carousel. One slide the width of the track at a time, snapped, in a
* container the page's own scroll drives: touch, trackpad and the keyboard
* already move it, and the two buttons in components/ui/carousel.tsx move it
* by one slide for a mouse. The scrollbar is hidden because the counter and
* the buttons say where you are; the container still scrolls.
*
* Smooth scrolling only where motion is wanted. Under reduced motion the
* track jumps, and the buttons ask for the same.
*/
.aka-carousel-track {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
overscroll-behavior-x: contain;
scrollbar-width: none;
border-radius: var(--radius-xl);
}
.aka-carousel-track::-webkit-scrollbar {
display: none;
}
.aka-carousel-track:focus-visible {
outline: 1px solid var(--select);
outline-offset: 4px;
}
.aka-carousel-slide {
flex: 0 0 100%;
min-width: 0;
margin: 0;
scroll-snap-align: start;
scroll-snap-stop: always;
}
@media (prefers-reduced-motion: no-preference) {
.aka-carousel-track {
scroll-behavior: smooth;
}
}
}