Carousel
When to use
- A gallery, a selection or a set of cards that should fill one row of the screen instead of growing downward: previews, recommendations, onboarding steps.
- You need to show several slides at once and change their number with the screen
width (
slidesPerView={{ base: 1, md: 2, xl: 4 }}). - You need to tie the strip to the rest of the screen: a caption, thumbnails, a
counter — through
indexandonIndexChange. - A component "for the future" (ADR-0048): there was no requesting screen at release, and
the API was modeled on comparable kits — shadcn/ui
Carousel, MantineCarousel, ChakraCarousel, AntCarousel, Fluent 2Carousel. Your screen is the first to test this API: if it needs a different API, that is not a violation of the contract.
When not to use
- Automatically changing slides, an infinite strip, dot indicators — they do not exist and never will: the kit takes on neither a pause button nor the accessibility traps of autoplay (motion that cannot be stopped, text that cannot be read in time).
- A ready-made lightbox component: there is none and never will be, but a
viewer that pages through slides in a dialog is assembled from
DialogandCarousel— see "Viewing with a carousel in aDialog". - For tabs with a single visible panel, use
Tabs: they have different semantics (tablist), not a strip. - Paged browsing of records with arrow buttons and keys is a separate task (
DetailPagerin the candidate registry), not a carousel. - If a set can reasonably be shown in full as a grid, use
Grid: hiding half of the content behind scrolling is a price paid only when the grid does not fit. - For a long list of rows, use a
Tableor a list; a horizontal strip is the wrong way to present it.
Minimal example
import { Carousel } from "@olddevs/ui";
<Carousel aria-label="Recommendations" slidesPerView={{ base: 1, md: 3 }}>
<ProductCard id="a" />
<ProductCard id="b" />
<ProductCard id="c" />
<ProductCard id="d" />
</Carousel>;Public API
| Prop | Default | Meaning |
|---|---|---|
aria-label | — | The region name as a string. Exactly one of aria-label and aria-labelledby is required; both at once is a type error. |
aria-labelledby | — | The region name taken from a visible heading above the strip — the heading's id. |
slidesPerView | 1 | 1, 2, 3 or 4 slides in view; an object { base, sm, md, lg, xl } — a separate number starting at each width. |
index | — | The current slide, controlled mode: the leftmost visible one, zero-based. |
defaultIndex | 0 | The initial slide in uncontrolled mode. It is shown without animation. |
onIndexChange | — | (index: number) => void — the strip has stopped on a different slide. |
children | — | The slides: each direct child is one slide. |
section attributes | — | className, id and data-* are passed to the region; ref goes to the <section> (forwardRef). |
A slide is a direct child. A fragment is one child, so slides inside it do not
become separate slides; null, false and empty strings do not become slides.
There is no gap between slides. A slide's width is a fraction of the row, and
a gap would make it depend on the number of slides in view. The space between
cards is the inner padding of the slide's content (p-2 on the card root).
The index is the leftmost visible slide
With slidesPerView={3} and five slides, indexes 0–2 are reachable: the strip does
not scroll past its end, and the last view holds the slides with indexes 2–4. An index that cannot
be reached (index={4} in this example) is replaced by the nearest
reachable one.
onIndexChange fires after scrolling stops
The callback is called when scrolling has stopped (100 ms after the last scroll
event), not on every frame. So in controlled mode, changing index externally
does not create an echo: the strip travels to the assigned slide, passes the
intermediate ones silently, and the callback is not called on arrival — the slide
is already the right one.
This is controlled mode in the usual sense: index assigns the slide, and the
callback asks the parent to change it. A parent that keeps the old index gets the
strip back on it — smoothly, after scrolling stops. A slide you assigned yourself
(index, defaultIndex) does not call the callback.
Buttons and edges
The buttons are the kit's Button, icon only; their names come from the message
catalog ("Previous slide", "Next slide"). They are disabled at the edges; if the
whole strip fits in view, both are disabled. A pressed button that becomes
disabled passes focus to its neighbor — a keyboard user does not end up at the
start of the page. If you press several times in a row, each press counts from the slide the strip is
already scrolling to.
Smoothness and prefers-reduced-motion
The buttons scroll with behavior: "smooth", but instantly under
prefers-reduced-motion: reduce. The preference is read at the moment of scrolling. Settling
on a slide after a gesture is the browser's built-in snapping, and the browser
itself drives its animation.
Viewing with a carousel in a Dialog
A lightbox is not a component but a composition: Dialog supplies the modal surface,
focus and focus return, Carousel supplies the paging. It needs no new
exports. Opening a particular slide is handled by the calling code: it remembers the index
and the list of slides at the moment of opening, Dialog shows them, and
Carousel starts at the right place through defaultIndex.
import { Carousel, Dialog, Image } from "@olddevs/ui";
const [view, setView] = useState<{ start: number; files: MediaFile[] } | null>(null);
const [index, setIndex] = useState(0);
const open = (start: number) => {
setIndex(start);
// The list is frozen on opening: an upload that lands later must not shift
// the slides under the user's finger.
setView({ start, files });
};
<Dialog
open={view !== null}
onClose={() => setView(null)}
title={view?.files[index]?.name ?? "Preview"}
subtitle={view ? `${index + 1} of ${view.files.length}` : undefined}
className="w-full max-w-[720px]"
>
{view && (
<Carousel aria-label="Files" defaultIndex={view.start} onIndexChange={setIndex}>
{view.files.map((file) => (
<Image key={file.id} src={file.url} alt={file.name} ratio={16 / 9} fit="contain" />
))}
</Carousel>
)}
</Dialog>;What matters here:
defaultIndex, notindex. The dialog mounts its content on opening, so the strip starts atview.starteach time, without animation. A controlledindexis needed only if the slide can also be changed from elsewhere (thumbnails, buttons in the header).- The title and the counter follow
onIndexChange. The callback fires after the strip stops, not on every frame — the title changes once the slide has settled. The dialog's name stays thetitleofDialog, while the slide name "N of M" is announced by the strip itself. {view && …}inside the dialog. Whileviewis empty there is no strip at all: the next opening creates it anew and does not inherit the previous scroll position.- A slide whose content has its own label. The image's
altis the file name: the slide name "N of M" does not replace it. - Focus: opening the dialog moves focus into it;
Escapecloses the dialog and returns focus to where it was opened from. The arrow keys page through the slides when the strip is focused; the strip's buttons are separate tab stops.
It is convenient to open the dialog from the onAction of a
VirtualGrid: a click or Enter on a cell calls open
without selecting it.
Accessibility
This component follows the WAI-ARIA APG Carousel pattern, without autoplay and so without a pause button.
- The region is a
<section>witharia-roledescription"carousel" (in the Russian locale "карусель") and a name fromaria-labeloraria-labelledby. Without a name the screen reader announces an unnamed region, which is why the type requires the name. The carousel has nolabelprop: in the kitlabelis only a visible label (conventions.md). - A slide is
role="group"witharia-roledescription"slide" and the name "N of M" from the catalog: the screen reader reads "slide, 2 of 5". Thearia-roledescriptionwords are text that screen readers speak, so they live in the message catalog and are not an ARIA token. - Slides outside the view are not hidden from the screen reader: without autoplay the strip is read in full, and when focus lands on a slide that is out of view, the browser scrolls the strip to that slide on its own.
- Keyboard: Tab reaches the "previous" button, the strip and the "next" button; disabled buttons at the edges are skipped. On a focused strip, the arrow keys scroll it natively, with snapping. If the slides contain links and buttons, Tab through them scrolls the strip to that slide.
- The strip gets
tabIndex={0}: a scrollable block with no place in the tab order is the axe violationscrollable-region-focusable. - Give slides meaningful captions ("Photo 2: sunset") through the content — the slide name "N of M" does not replace them.
- In RTL, the scrolling and the arrows on the buttons are mirrored.
Anti-patterns
- Putting interactive forms in slides when they cannot be completed without paging: what is hidden past the edge is not visible and cannot be found by eye.
- Setting
slidesPerViewto a number outside the set 1–4 (slidesPerView={5}) or assembling the width class yourself — the set is closed, and a class for each value is written out in the source. - Building a lightbox not from
Dialogbut onModalOverlayor your own scrim: the dialog name, the focus trap and the focus return are lost. - Changing
indexfrom a timer to "liven up" the strip: auto-advance is what the component deliberately does not do, and the user does not have time to read. - Deriving the number of slides from
indexwhenslidesPerView > 1: the index is the leftmost visible slide, not a "page".
Related sources and Storybook
- Source:
src/components/content/Carousel.tsx; the "value per breakpoint → classes" map is the internalsrc/lib/responsive.ts(not public API). - Tests:
tests/carousel.test.tsx(names, buttons, edges,onIndexChange, modes; scrolling is stubbed because jsdom does not compute layout); snapping to a slide, the gesture and the slide width at breakpoints are checked by hand in the showcase in a browser; every width class in the built CSS —tests/class-coverage.test.ts. - Story:
src/components/content/Carousel.stories.tsx. The tier is core (ADR-0014): its own accessibility model and its own scrolling behavior.
TypeScript types
CarouselProps
Prop
Type
Plus DOM attributes, such as onChange, onClick, className — 275 in total; the table does not repeat them.