Carousel Primitive
Use Carousel to browse a row of images or cards. The previous and next buttons move through the slides and update as you reach either end.
Anatomy
Use the Astro primitive adapter to render Carousel anatomy with the Runtime wiring included.
---import { Carousel } from "@starwind-ui/astro/carousel";---
<Carousel.Root> <Carousel.Viewport> <Carousel.Container> <Carousel.Item>Slide 1</Carousel.Item> </Carousel.Container> </Carousel.Viewport> <Carousel.Previous>Previous</Carousel.Previous> <Carousel.Next>Next</Carousel.Next></Carousel.Root>Use the React primitive adapter when Carousel state participates in React rendering.
import { Carousel } from "@starwind-ui/react/carousel";
export function Example() { return ( <Carousel.Root> <Carousel.Viewport> <Carousel.Container> <Carousel.Item>Slide 1</Carousel.Item> </Carousel.Container> </Carousel.Viewport> <Carousel.Previous>Previous</Carousel.Previous> <Carousel.Next>Next</Carousel.Next> </Carousel.Root> );}Use the Vue components to build Carousel.
<script setup lang="ts">import Carousel from "@starwind-ui/vue/carousel";</script>
<template> <Carousel.Root> <Carousel.Viewport> <Carousel.Container> <Carousel.Item>Slide 1</Carousel.Item> </Carousel.Container> </Carousel.Viewport> <Carousel.Previous>Previous</Carousel.Previous> <Carousel.Next>Next</Carousel.Next> </Carousel.Root></template>Use the Svelte components to build Carousel.
<script lang="ts"> import { Carousel } from "@starwind-ui/svelte/carousel";</script>
<Carousel.Root> <Carousel.Viewport> <Carousel.Container> <Carousel.Item>Slide 1</Carousel.Item> </Carousel.Container> </Carousel.Viewport> <Carousel.Previous>Previous</Carousel.Previous> <Carousel.Next>Next</Carousel.Next></Carousel.Root>Render the Carousel data-sw-* contract yourself, then initialize createCarousel.
<div data-sw-carousel role="region" aria-roledescription="carousel"> <div data-sw-carousel-viewport> <div data-sw-carousel-container> <div data-sw-carousel-item role="group" aria-roledescription="slide">Slide 1</div> </div> </div> <button data-sw-carousel-previous type="button">Previous</button> <button data-sw-carousel-next type="button">Next</button></div>
<script type="module"> import { createCarousel } from "@starwind-ui/runtime/carousel";
const root = document.querySelector("[data-sw-carousel]"); if (root) { createCarousel(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed package’s types for the available props and events. Plain HTML uses DOM events and Runtime methods.
API Reference
Root
The main element that owns the Carousel Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-carousel
- Role
- region
Props
orientation "horizontal" | "vertical" "horizontal"
- Description
- Sets the Carousel orientation.
- Kind
- option
- Targets
- root
- Full type
- "horizontal" | "vertical"
opts CarouselOptions["opts"] {}
- Description
- Passes options to the underlying carousel engine.
- Kind
- option
- Targets
- root
- Full type
- CarouselOptions["opts"]
plugins React, Vue, Svelte only CarouselOptions["plugins"] -
- Description
- Passes plugins to the underlying carousel engine.
- Kind
- option
- Targets
- root
- Frameworks
- React, Vue, Svelte
- Full type
- CarouselOptions["plugins"]
setApi React, Vue, Svelte only (api: CarouselInstance["api"]) => void -
- Description
- Receives the carousel API instance when it is ready.
- Kind
- callback
- Targets
- root
- Frameworks
- React, Vue, Svelte
- Full type
- (api: CarouselInstance["api"]) => void
autoInit Astro only boolean true
- Description
- Initializes the carousel automatically when the page loads.
- Kind
- option
- Targets
- root
- Frameworks
- Astro
- Full type
- boolean
Data Attributes
Runtime hooks
Metadata
Viewport
The visible viewport for Carousel content.
- Default element
- div
- Discovery hook
- data-sw-carousel-viewport
- Role
- -
Data Attributes
Runtime hooks
Container
Groups the scrolling or moving content for Carousel.
- Default element
- div
- Discovery hook
- data-sw-carousel-container
- Role
- -
Data Attributes
Runtime hooks
Item
An interactive item inside the Carousel collection.
- Default element
- div
- Discovery hook
- data-sw-carousel-item
- Role
- group
Data Attributes
Runtime hooks
Previous
Moves Carousel to the previous item or slide.
- Default element
- button
- Discovery hook
- data-sw-carousel-previous
- Role
- -
Data Attributes
Runtime hooks
State
Next
Moves Carousel to the next item or slide.
- Default element
- button
- Discovery hook
- data-sw-carousel-next
- Role
- -
Data Attributes
Runtime hooks
State
Runtime API
- Factory
createCarousel- Import
@starwind-ui/runtime/carousel- Root hook
- root
data-sw-carousel - Option props
- orientation, opts, plugins, setApi
Option Lifecycles
| Option | Lifecycle |
|---|---|
| orientation | refresh-required |
| opts | refresh-required |
| plugins | refresh-required |
| setApi | constructor-only |
Related Styled Components
Changelog
View version history v1.1.0 6 releases
v1.1.0
- Carousel horizontal keys and drag match the slide flow in RTL. When your app changes direction and sends `starwind:direction-change`, it keeps the selected slide.
- In Vue, this component now needs Vue 3.5.21 or newer.
v1.0.2
- In Astro, an attribute that you set on a Carousel part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
v1.0.1
- Internal maintenance. No change to how the component behaves.
- Reduced repeated JavaScript when Astro Carousel is used with other Starwind components.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
v0.1.0
- First release. Carousel handles navigation, orientation, looping, and the state of each slide. It also sets up and cleans up plugins.