carousel
onChange called 0 time(s)
import { carousel, lucide } from '@markless/ui';export default function CarouselBasic() @{ <carousel.root class="carousel" value="paris"> <carousel.title class="title">Featured destinations</carousel.title> <div class="frame"> <carousel.backtrigger class="step"> <lucide.chevronleft class="step-icon" aria-hidden="true" /> </carousel.backtrigger> <carousel.scrollarea class="window"> <carousel.item class="slide" value="paris"> <span class="slide-name">Paris</span> <span class="slide-note">Cafés and long walks</span> </carousel.item> <carousel.item class="slide" value="oslo"> <span class="slide-name">Oslo</span> <span class="slide-note">Fjords and wooden houses</span> </carousel.item> <carousel.item class="slide" value="lima"> <span class="slide-name">Lima</span> <span class="slide-note">Cliffs above the Pacific</span> </carousel.item> </carousel.scrollarea> <carousel.forwardtrigger class="step"> <lucide.chevronright class="step-icon" aria-hidden="true" /> </carousel.forwardtrigger> </div> <carousel.navlist class="dots" aria-label="Choose a destination"> <carousel.navtrigger class="dot" value="paris" aria-label="Paris" /> <carousel.navtrigger class="dot" value="oslo" aria-label="Oslo" /> <carousel.navtrigger class="dot" value="lima" aria-label="Lima" /> </carousel.navlist> </carousel.root>;}
.carousel { display: grid; gap: 0.8em; justify-items: center; width: min(28rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.title { font-family: 'Joy Elia', system-ui, sans-serif; font-size: var(--step-0);}.frame { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 0.75em; align-items: center; width: 100%;}/* The track is the family's own element: a max-content column sizes it to the whole slide row. */.window { display: grid; grid-auto-columns: max-content; container-type: inline-size; overflow: hidden; border: 1px solid var(--ink); border-radius: 6px; font-size: 0; white-space: nowrap; touch-action: pan-y;}.window[ui-draggable] { cursor: grab;}.slide { display: inline-grid; place-content: center; box-sizing: border-box; width: 100cqi; font-size: var(--step--1); white-space: normal; vertical-align: top; min-height: 8.5rem; padding: 1em; background: color-mix(in srgb, var(--purple) 26%, var(--cream)); text-align: center; user-select: none; html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.slide-name { font-family: 'Joy Elia', system-ui, sans-serif; font-size: var(--step-2); line-height: 1.1;}.slide-note { font-family: 'Shantell Sans', system-ui, sans-serif;}.step { display: grid; place-items: center; box-sizing: border-box; width: 2.4em; height: 2.4em; padding: 0; border: 1px solid var(--ink); border-radius: 999px; background: var(--paper); color: var(--ink); cursor: pointer; transition: background 140ms ease;}.step:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.step:active { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.step-icon { width: 1.1em; height: 1.1em;}.dots { display: flex; gap: 0.6em;}.dot { box-sizing: border-box; width: 0.9em; height: 0.9em; padding: 0; border: 1px solid var(--ink); border-radius: 999px; background: var(--paper); cursor: pointer; transition: background 140ms ease;}.dot:hover { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.dot[ui-active] { background: var(--purple);}.step:focus-visible,.dot:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px;}@media (prefers-reduced-motion: reduce) { .step, .dot { transition: none; }}
A carousel shows one slide at a time, with buttons to step through the rest. Use it for a row of featured items that share one spot on the page.
Each slide has a name, its value. The root's value names the slide that is showing. Nothing
counts positions, so you always name a slide, never number it.
Anatomy
The carousel itself; the scroll area, slides, nav list and triggers go inside it.
The back and forward triggers step one slide. Set move on the root to step more, or set
move="view" to step a whole window of slides.
Add a carousel.navlist with one carousel.navtrigger per slide to get a row of slide pickers.
Then the pickers are tabs and the slides are tab panels. Without the nav list, each slide is a
group that screen readers call a "slide".
Keyboard
The back, forward and play triggers are plain buttons, so Tab, Enter and Space work as usual. The pickers in a nav list share one Tab stop.
| Key | What it does |
|---|---|
| Left or Right arrow | Show the previous or next slide and focus its picker. |
| Up or Down arrow | The same, in place of Left and Right, when orientation="vertical". |
| Home or End | Show the first or the last slide. |
Focusing a picker shows its slide. At either end the arrows stop. Set loop or rewind to make
the ends come round.
Autoplay
carousel.playtrigger starts and stops the slides moving on their own. Its screen reader label
says what the next press will do.
The rotation stops for good when focus moves inside the carousel. It pauses while the pointer is
over the carousel and starts again when the pointer leaves. While it runs, the root sets
aria-live="off", so a screen reader does not read every slide.
If the reader's system asks for reduced motion, the play trigger refuses to start. It gets
aria-disabled="true" and ui-reduced-motion, so you can show why.
Styling
The family ships no CSS, so an unstyled carousel is a stack of slides. The scroll area renders
two elements. The outer one gets your props, and it is the window. Give it a size and
overflow: hidden. The inner one carries ui-track, and the family moves it with a transform.
Give it display: flex.
A vertical carousel needs a fixed height on the window. Without one, the window grows to fit every slide, and there is nothing left to step to.
| Attribute | On | When |
|---|---|---|
ui-active |
item, navtrigger |
This slide is the one showing. |
ui-value |
item, navtrigger |
Always. It holds the slide's value. |
ui-vertical |
root, scrollarea, the track |
orientation="vertical". |
ui-loop |
root |
loop is set. rewind does not set it. |
ui-draggable |
scrollarea |
Dragging is on. It is on unless you set draggable={false}. |
ui-align |
scrollarea |
Always. It holds start, center or end. |
ui-track |
The track inside scrollarea |
Always. |
ui-reduced-motion |
root, playtrigger |
A press on play was refused for reduced motion. |
mousewheel lets the wheel step the slides. The family does not cancel the wheel event, so the
page can scroll at the same time.
Props on carousel.root
| Prop | Type | What it does |
|---|---|---|
value |
string |
The slide showing now, named by its value. |
orientation |
'horizontal' | 'vertical' |
The axis the slides run along. The default is horizontal. |
align |
'start' | 'center' | 'end' |
Where a slide rests in the window. The default is start. |
move |
number | 'view' |
How far one back or forward press goes. The default is 1. |
loop |
boolean |
Stepping past either end comes round to the other end. |
rewind |
boolean |
Stepping past either end comes round to the other end. It does not set ui-loop. |
draggable |
boolean |
A drag moves the slides. The default is true. |
mousewheel |
boolean |
The wheel steps the slides. The default is false. |
autoplay |
boolean |
The rotation state the root renders with. It starts no timer. |
autoplayInterval |
number |
Milliseconds per slide while the rotation runs. The default is 3000. |
sensitivity |
{ mouse?: number; touch?: number } |
How far a drag moves the slides. The defaults are 1.5 and 1.25. |
onChange |
(value: string) => void |
Called with the new value when the showing slide changes. |
Every other prop goes on the root <div>. carousel.item and carousel.navtrigger each need a
value.