mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-22 20:25:44 +00:00
* feat: rtl * feat * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: add sidebar * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * chore: changeset * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix
323 lines
7.2 KiB
Plaintext
323 lines
7.2 KiB
Plaintext
---
|
|
title: Carousel
|
|
description: A carousel with motion and swipe built using Embla.
|
|
base: base
|
|
component: true
|
|
links:
|
|
doc: https://www.embla-carousel.com/get-started/react
|
|
api: https://www.embla-carousel.com/api
|
|
---
|
|
|
|
<ComponentPreview
|
|
styleName="base-nova"
|
|
name="carousel-demo"
|
|
previewClassName="h-80 sm:h-[32rem]"
|
|
/>
|
|
|
|
## About
|
|
|
|
The carousel component is built using the [Embla Carousel](https://www.embla-carousel.com/) library.
|
|
|
|
## Installation
|
|
|
|
<CodeTabs>
|
|
|
|
<TabsList>
|
|
<TabsTrigger value="cli">Command</TabsTrigger>
|
|
<TabsTrigger value="manual">Manual</TabsTrigger>
|
|
</TabsList>
|
|
|
|
<TabsContent value="cli">
|
|
|
|
```bash
|
|
npx shadcn@latest add carousel
|
|
```
|
|
|
|
</TabsContent>
|
|
|
|
<TabsContent value="manual">
|
|
|
|
<Steps className="mb-0 pt-2">
|
|
|
|
<Step>Install the following dependencies:</Step>
|
|
|
|
```bash
|
|
npm install embla-carousel-react
|
|
```
|
|
|
|
<Step>Copy and paste the following code into your project.</Step>
|
|
|
|
<ComponentSource
|
|
name="carousel"
|
|
title="components/ui/carousel.tsx"
|
|
styleName="base-nova"
|
|
/>
|
|
|
|
<Step>Update the import paths to match your project setup.</Step>
|
|
|
|
</Steps>
|
|
|
|
</TabsContent>
|
|
|
|
</CodeTabs>
|
|
|
|
## Usage
|
|
|
|
```tsx showLineNumbers
|
|
import {
|
|
Carousel,
|
|
CarouselContent,
|
|
CarouselItem,
|
|
CarouselNext,
|
|
CarouselPrevious,
|
|
} from "@/components/ui/carousel"
|
|
```
|
|
|
|
```tsx showLineNumbers
|
|
<Carousel>
|
|
<CarouselContent>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
</CarouselContent>
|
|
<CarouselPrevious />
|
|
<CarouselNext />
|
|
</Carousel>
|
|
```
|
|
|
|
## Examples
|
|
|
|
### Sizes
|
|
|
|
To set the size of the items, you can use the `basis` utility class on the `<CarouselItem />`.
|
|
|
|
<ComponentPreview styleName="base-nova" name="carousel-size" />
|
|
|
|
```tsx showLineNumbers {4-6}
|
|
// 33% of the carousel width.
|
|
<Carousel>
|
|
<CarouselContent>
|
|
<CarouselItem className="basis-1/3">...</CarouselItem>
|
|
<CarouselItem className="basis-1/3">...</CarouselItem>
|
|
<CarouselItem className="basis-1/3">...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
```
|
|
|
|
```tsx showLineNumbers {4-6}
|
|
// 50% on small screens and 33% on larger screens.
|
|
<Carousel>
|
|
<CarouselContent>
|
|
<CarouselItem className="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
|
|
<CarouselItem className="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
|
|
<CarouselItem className="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
```
|
|
|
|
### Spacing
|
|
|
|
To set the spacing between the items, we use a `pl-[VALUE]` utility on the `<CarouselItem />` and a negative `-ml-[VALUE]` on the `<CarouselContent />`.
|
|
|
|
<ComponentPreview styleName="base-nova" name="carousel-spacing" />
|
|
|
|
```tsx showLineNumbers /-ml-4/ /pl-4/
|
|
<Carousel>
|
|
<CarouselContent className="-ml-4">
|
|
<CarouselItem className="pl-4">...</CarouselItem>
|
|
<CarouselItem className="pl-4">...</CarouselItem>
|
|
<CarouselItem className="pl-4">...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
```
|
|
|
|
```tsx showLineNumbers /-ml-2/ /pl-2/ /md:-ml-4/ /md:pl-4/
|
|
<Carousel>
|
|
<CarouselContent className="-ml-2 md:-ml-4">
|
|
<CarouselItem className="pl-2 md:pl-4">...</CarouselItem>
|
|
<CarouselItem className="pl-2 md:pl-4">...</CarouselItem>
|
|
<CarouselItem className="pl-2 md:pl-4">...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
```
|
|
|
|
### Orientation
|
|
|
|
Use the `orientation` prop to set the orientation of the carousel.
|
|
|
|
<ComponentPreview
|
|
styleName="base-nova"
|
|
name="carousel-orientation"
|
|
previewClassName="h-[32rem]"
|
|
/>
|
|
|
|
```tsx showLineNumbers /vertical | horizontal/
|
|
<Carousel orientation="vertical | horizontal">
|
|
<CarouselContent>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
```
|
|
|
|
## Options
|
|
|
|
You can pass options to the carousel using the `opts` prop. See the [Embla Carousel docs](https://www.embla-carousel.com/api/options/) for more information.
|
|
|
|
```tsx showLineNumbers {2-5}
|
|
<Carousel
|
|
opts={{
|
|
align: "start",
|
|
loop: true,
|
|
}}
|
|
>
|
|
<CarouselContent>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
```
|
|
|
|
## API
|
|
|
|
Use a state and the `setApi` props to get an instance of the carousel API.
|
|
|
|
<ComponentPreview
|
|
styleName="base-nova"
|
|
name="carousel-api"
|
|
previewClassName="sm:h-[32rem]"
|
|
/>
|
|
|
|
```tsx showLineNumbers {1,4,22}
|
|
import { type CarouselApi } from "@/components/ui/carousel"
|
|
|
|
export function Example() {
|
|
const [api, setApi] = React.useState<CarouselApi>()
|
|
const [current, setCurrent] = React.useState(0)
|
|
const [count, setCount] = React.useState(0)
|
|
|
|
React.useEffect(() => {
|
|
if (!api) {
|
|
return
|
|
}
|
|
|
|
setCount(api.scrollSnapList().length)
|
|
setCurrent(api.selectedScrollSnap() + 1)
|
|
|
|
api.on("select", () => {
|
|
setCurrent(api.selectedScrollSnap() + 1)
|
|
})
|
|
}, [api])
|
|
|
|
return (
|
|
<Carousel setApi={setApi}>
|
|
<CarouselContent>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
)
|
|
}
|
|
```
|
|
|
|
## Events
|
|
|
|
You can listen to events using the api instance from `setApi`.
|
|
|
|
```tsx showLineNumbers {1,4-14,16}
|
|
import { type CarouselApi } from "@/components/ui/carousel"
|
|
|
|
export function Example() {
|
|
const [api, setApi] = React.useState<CarouselApi>()
|
|
|
|
React.useEffect(() => {
|
|
if (!api) {
|
|
return
|
|
}
|
|
|
|
api.on("select", () => {
|
|
// Do something on select.
|
|
})
|
|
}, [api])
|
|
|
|
return (
|
|
<Carousel setApi={setApi}>
|
|
<CarouselContent>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
</CarouselContent>
|
|
</Carousel>
|
|
)
|
|
}
|
|
```
|
|
|
|
See the [Embla Carousel docs](https://www.embla-carousel.com/api/events/) for more information on using events.
|
|
|
|
## Plugins
|
|
|
|
You can use the `plugins` prop to add plugins to the carousel.
|
|
|
|
```ts showLineNumbers {1,6-10}
|
|
import Autoplay from "embla-carousel-autoplay"
|
|
|
|
export function Example() {
|
|
return (
|
|
<Carousel
|
|
plugins={[
|
|
Autoplay({
|
|
delay: 2000,
|
|
}),
|
|
]}
|
|
>
|
|
// ...
|
|
</Carousel>
|
|
)
|
|
}
|
|
```
|
|
|
|
<ComponentPreview
|
|
styleName="base-nova"
|
|
name="carousel-plugin"
|
|
previewClassName="sm:h-[32rem]"
|
|
/>
|
|
|
|
## RTL
|
|
|
|
To enable RTL support in shadcn/ui, see the [RTL configuration guide](/docs/rtl).
|
|
|
|
<ComponentPreview
|
|
styleName="base-nova"
|
|
name="carousel-rtl"
|
|
direction="rtl"
|
|
previewClassName="h-80 sm:h-[32rem]"
|
|
/>
|
|
|
|
When localizing the carousel for RTL languages, you need to set the `direction` option in the `opts` prop to match the text direction. This ensures the carousel scrolls in the correct direction.
|
|
|
|
```tsx showLineNumbers {2-5}
|
|
<Carousel
|
|
dir={dir}
|
|
opts={{
|
|
direction: dir,
|
|
}}
|
|
>
|
|
<CarouselContent>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
<CarouselItem>...</CarouselItem>
|
|
</CarouselContent>
|
|
<CarouselPrevious className="rtl:rotate-180" />
|
|
<CarouselNext className="rtl:rotate-180" />
|
|
</Carousel>
|
|
```
|
|
|
|
The `direction` option accepts `"ltr"` or `"rtl"` and should match the `dir` prop value. You may also want to rotate the navigation buttons using the `rtl:rotate-180` class to ensure they point in the correct direction.
|
|
|
|
## API Reference
|
|
|
|
See the [Embla Carousel docs](https://www.embla-carousel.com/api/plugins/) for more information on props and plugins.
|