mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-22 20:25:44 +00:00
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { Card, CardContent } from "@/examples/base/ui/card"
|
|
import {
|
|
Carousel,
|
|
CarouselContent,
|
|
CarouselItem,
|
|
CarouselNext,
|
|
CarouselPrevious,
|
|
type CarouselApi,
|
|
} from "@/examples/base/ui/carousel"
|
|
|
|
export default function CarouselDApiDemo() {
|
|
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 (
|
|
<div className="mx-auto max-w-[10rem] sm:max-w-xs">
|
|
<Carousel setApi={setApi} className="w-full max-w-xs">
|
|
<CarouselContent>
|
|
{Array.from({ length: 5 }).map((_, index) => (
|
|
<CarouselItem key={index}>
|
|
<Card className="m-px">
|
|
<CardContent className="flex aspect-square items-center justify-center p-6">
|
|
<span className="text-4xl font-semibold">{index + 1}</span>
|
|
</CardContent>
|
|
</Card>
|
|
</CarouselItem>
|
|
))}
|
|
</CarouselContent>
|
|
<CarouselPrevious />
|
|
<CarouselNext />
|
|
</Carousel>
|
|
<div className="py-2 text-center text-sm text-muted-foreground">
|
|
Slide {current} of {count}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|