import useEmblaCarousel, {
type UseEmblaCarouselType,
} from 'embla-carousel-react'
import { ArrowLeft, ArrowRight } from 'lucide-react'
import * as React from 'react'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
type CarouselApi = UseEmblaCarouselType[1]
type UseCarouselParameters = Parameters<typeof useEmblaCarousel>
type CarouselOptions = UseCarouselParameters[0]
type CarouselPlugin = UseCarouselParameters[1]
orientation?: 'horizontal' | 'vertical'
setApi?: (api: CarouselApi) => void
type CarouselContextProps = {
carouselRef: ReturnType<typeof useEmblaCarousel>[0]
api: ReturnType<typeof useEmblaCarousel>[1]
const CarouselContext = React.createContext<CarouselContextProps | null>(null)
const context = React.useContext(CarouselContext)
throw new Error('useCarousel must be used within a <Carousel />')
const Carousel = React.forwardRef<
React.HTMLAttributes<HTMLDivElement> & CarouselProps
orientation = 'horizontal',
const [carouselRef, api] = useEmblaCarousel(
axis: orientation === 'horizontal' ? 'x' : 'y',
const [canScrollPrev, setCanScrollPrev] = React.useState(false)
const [canScrollNext, setCanScrollNext] = React.useState(false)
const onSelect = React.useCallback((api: CarouselApi) => {
setCanScrollPrev(api.canScrollPrev())
setCanScrollNext(api.canScrollNext())
const scrollPrev = React.useCallback(() => {
const scrollNext = React.useCallback(() => {
const handleKeyDown = React.useCallback(
(event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'ArrowLeft') {
} else if (event.key === 'ArrowRight') {
[scrollPrev, scrollNext],
api.on('reInit', onSelect)
api.on('select', onSelect)
api?.off('select', onSelect)
<CarouselContext.Provider
orientation || (opts?.axis === 'y' ? 'vertical' : 'horizontal'),
onKeyDownCapture={handleKeyDown}
className={cn('relative', className)}
aria-roledescription="carousel"
</CarouselContext.Provider>
Carousel.displayName = 'Carousel'
const CarouselContent = React.forwardRef<
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
const { carouselRef, orientation } = useCarousel()
<div ref={carouselRef} className="overflow-hidden">
orientation === 'horizontal' ? '-ml-4' : '-mt-4 flex-col',
CarouselContent.displayName = 'CarouselContent'
const CarouselItem = React.forwardRef<
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
const { orientation } = useCarousel()
aria-roledescription="slide"
'min-w-0 shrink-0 grow-0 basis-full',
orientation === 'horizontal' ? 'pl-4' : 'pt-4',
CarouselItem.displayName = 'CarouselItem'
const CarouselPrevious = React.forwardRef<
React.ComponentProps<typeof Button>
>(({ className, variant = 'outline', size = 'icon', ...props }, ref) => {
const { orientation, scrollPrev, canScrollPrev } = useCarousel()
'absolute h-8 w-8 rounded-base',
orientation === 'horizontal'
? '-left-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
disabled={!canScrollPrev}
<ArrowLeft className="h-4 w-4" />
<span className="sr-only">Previous slide</span>
CarouselPrevious.displayName = 'CarouselPrevious'
const CarouselNext = React.forwardRef<
React.ComponentProps<typeof Button>
>(({ className, variant = 'outline', size = 'icon', ...props }, ref) => {
const { orientation, scrollNext, canScrollNext } = useCarousel()
'absolute h-8 w-8 rounded-base',
orientation === 'horizontal'
? '-right-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
disabled={!canScrollNext}
<ArrowRight className="h-4 w-4" />
<span className="sr-only">Next slide</span>
CarouselNext.displayName = 'CarouselNext'