embla-carousel
Provides a carousel API for scrolling, focus handling, and responsive layout updates.
- Downloads / mo
- 119.0M
- Stars
- 8.5K
- Last release
- 8.6.0
- License
- MIT
Strengths
- Scrolls focused slides into view. Focus events trigger scrolling to the focused element.
- Updates when slide sizes change. Size changes automatically trigger carousel reinitialization.
Limitations
- Short content disables looping. Embla turns loop off when slide content cannot form a loop without visible glitches.
- RTL needs matching content direction. Set the HTML dir attribute or CSS direction alongside the carousel option.
Quickstart
<style> .embla { overflow: hidden; } .embla__container { display: flex; } .embla__slide { flex: 0 0 100%; min-width: 0; } </style> <div class="embla"><div class="embla__container"> <div class="embla__slide">One</div><div class="embla__slide">Two</div> </div></div> <script src="./node_modules/embla-carousel/embla-carousel.umd.js"></script> <script>EmblaCarousel(document.querySelector(".embla"));</script>
embla-carousel examples
function Carousel({ orientation = "horizontal", opts, setApi, plugins, className, children, ...props }: React.ComponentProps<"div"> & CarouselProps) { const [carouselRef, api] = useEmblaCarousel( { ...opts, axis: orientation === "horizontal" ? "x" : "y", }, plugins ) const [canScrollPrev, setCanScrollPrev] = React.useState(false) const [canScrollNext, setCanScrollNext] = React.useState(false) const onSelect = React.useCallback((api: CarouselApi) => { if (!api) return setCanScrollPrev(api.canScrollPrev()) setCanScrollNext(api.canScrollNext()) }, []) const scrollPrev = React.useCallback(() => { api?.scrollPrev() }, [api]) const scrollNext = React.useCallback(() => { api?.scrollNext() }, [api]) const handleKeyDown = React.useCallback( (event: React.KeyboardEvent<HTMLDivElement>) => { if (event.key === "ArrowLeft") { event.preventDefault() scrollPrev() } else if (event.key === "ArrowRight") { event.preventDefault() scrollNext() } }, [scrollPrev, scrollNext] ) React.useEffect(() => { if (!api || !setApi) return setApi(api) }, [api, setApi]) React.useEffect(() => { if (!api) return onSelect(api) api.on("reInit", onSelect) api.on("select", onSelect) return () => { api?.off("select", onSelect) } }, [api, onSelect]) return ( <CarouselContext.Provider value={{ carouselRef, api: api, opts, orientation: orientation || (opts?.axis === "y" ? "vertical" : "horizontal"), scrollPrev, scrollNext, canScrollPrev, canScrollNext, }} > <div onKeyDownCapture={handleKeyDown} className={cn("relative", className)} role="region" aria-roledescription="carousel" data-slot="carousel" {...props} > {children} </div> </CarouselContext.Provider> ) }