---
url: /reference/items/Carousel/js-api.md
---
# JS API
The `Carousel` class extends the [`Indexable` primitive](/reference/items/Indexable/). It inherits its index-navigation methods (`goTo()`, `goNext()`, `goPrev()`), its `boundary` and `reverse` options and its `index` event, so make sure to have a look at [its API reference](/reference/items/Indexable/js-api) too.
A carousel is composed of several components working together:
* `Carousel` — the root component, extending `Indexable`
* `CarouselWrapper` — the scrollable container holding the items
* `CarouselItem` — a single slide
* `CarouselBtn` — a previous, next or go-to control
* `CarouselDrag` — optional pointer-drag behavior, mounted only on fine-pointer devices
* `CarouselPlay` — optional rotation control, which rotates the carousel on a timer
* `CarouselDots` — optional pagination dots, one button per slide
* `CarouselThumbnails` — optional thumbnail picker, one image button per slide
* `CarouselCount` — optional "3 / 5" readout
* `CarouselProgress` — optional progress bar, following the scroll offset
Every control resolves the carousel through the shared `CarouselContext`, so none of them names a selector, imports the `Carousel` class or has to be ordered against the others. A control mounted before its carousel waits for it; a control mounted outside a carousel does nothing at all.
## Options
### `axis`
* Type: `'x' | 'y'`
* Default: `'x'`
Defines the scroll direction of the carousel: `'x'` for horizontal, `'y'` for vertical.
```html {2}
...
```
### `slide-label`
* Type: `string`
* Default: `'{index} of {total}'`
The template for the accessible name a slide falls back to when it has no `aria-label` or `aria-labelledby` of its own. `{index}` is the slide's one-based position, `{total}` the live slide count — appending a slide rewrites every name.
```html {3}
...
```
### `boundary`
* Type: `'clamp' | 'loop' | 'bounce'`
* Default: `'clamp'`
Inherited from the [`Indexable`](/reference/items/Indexable/js-api#boundary) primitive, it controls what happens at the ends of the track: `clamp` stops at the first/last item, `loop` wraps around, and `bounce` reverses direction. The `CarouselBtn` controls follow it — `prev`/`next` disable at the ends in `clamp` mode but stay active in `loop` and `bounce`. See the [boundaries example](./examples#boundaries).
```html {3}
...
```
The [`reverse`](/reference/items/Indexable/js-api#reverse) option is also inherited from `Indexable`. The `total` option has no effect here: the carousel derives its length from the number of `CarouselItem` children.
## Getters
### `isHorizontal`
* Type: `boolean`
Whether the carousel scrolls horizontally (the `axis` option is `'x'`).
### `isVertical`
* Type: `boolean`
Whether the carousel scrolls vertically (the `axis` option is `'y'`).
### `items`
* Type: `CarouselItem[]`
The carousel's item components.
### `length`
* Type: `number`
The number of items, used as the `Indexable` length.
### `wrapper`
* Type: `CarouselWrapper`
The scrollable wrapper component.
### `progress`
* Type: `number`
The current scroll progress, between `0` and `1`.
## Methods
### `goTo(indexOrInstruction)`
* Arguments: `number | 'next' | 'previous' | 'first' | 'last' | 'random'`
* Returns: `Promise`
Scroll to the given item. Accepts an index or one of the [`Indexable` instructions](/reference/items/Indexable/js-api). The `goNext()` and `goPrev()` shortcuts are inherited from the primitive.
### `slideLabel(index, total)`
* Arguments: `number`, `number`
* Returns: `string`
The accessible name of the slide at a zero-based `index`, built from the [`slide-label`](#slide-label) option. `CarouselItem` calls it; it is on the API so a control never has to reach back for the option.
### `syncPresence()`
* Returns: `void`
Rebuild the observer that decides which slides are presented, and re-apply `inert` to the ones that do not intersect the track. Called on mount and whenever a slide or the track arrives or leaves. Call it yourself only if you resize the track without a `ResizeObserver` noticing.
## Diagnostics
Every one is a development-only warning on the [toolkit diagnostic channel](https://js-toolkit-v4.studiometa.dev/).
| Code | Meaning |
| ----------------------------------- | ---------------------------------------------------------------------- |
| `carousel.unnamed` | The root has neither an `aria-label` nor an `aria-labelledby`. |
| `carousel.unnamed-btn` | A `CarouselBtn` has no text, no `aria-label` and no `aria-labelledby`. |
| `carousel-count.no-refs` | A `CarouselCount` has neither a `current` nor a `total` ref. |
| `carousel-progress.no-ref` | A `CarouselProgress` has no `progress` ref. |
| `carousel-play.not-first-focusable` | A `CarouselPlay` is not the first focusable element in the carousel. |
## Events
### `progress`
Emitted while the carousel scrolls, with the current progress between `0` and `1`. The same value is reflected on the root element through the [`--carousel-progress`](#carousel-progress) custom property.
```js
onCarouselProgress(progress) {
// progress is a number between 0 and 1
}
```
The `index` event is inherited from the [`Indexable` primitive](/reference/items/Indexable/js-api) and emitted whenever the current index changes.
## CSS custom properties
### `--carousel-progress`
Set on the root `Carousel` element, reflects the scroll progress between `0` and `1`.
### `--carousel-item-active`
Set on each `CarouselItem`, equals `1` when the item is the current one and `0` otherwise. Use it to style the active slide.
## CarouselBtn
A control button, delegating to the parent carousel on click.
### `action`
* Type: `'next' | 'prev' | string`
Use `next` or `prev` to step through the items, or a numeric string (e.g. `"2"`) to jump to a specific index. The button marks itself unavailable when its action would not move the index — `prev` on the first item, `next` on the last one, a picker for the slide already showing.
**How it does so depends on which button it is.** A `prev`/`next` button names an action, so at the end of the track it gets the native `disabled` property. A numeric button names a slide, which makes it a picker: it gets `aria-current="true"`, the same marker [`CarouselDots`](#carouseldots) and [`CarouselThumbnails`](#carouselthumbnails) use, so one selector — `[aria-current="true"]` — styles the current control whichever kind it is. It is never disabled in either form: the picker for the slide on screen is the one a screen reader user looks for. Clicking it moves nothing.
Pickers stay plain buttons — no `role="tab"`, no `role="tablist"`, no `aria-selected`. The APG prescribes tab semantics for them and every piece of user testing published since contradicts it; the objection has been open on the APG repository, unanswered, for eight years.
```html {3}
```
## CarouselDots
Pagination dots: one `