Embla Carousel

repository·master·Indexed 11 days ago

https://github.com/davidjerleke/embla-carousel

A lightweight, dependency-free, and framework-agnostic carousel library focused on smooth motion and precision swiping. Version 9.0.0-rc02 provides a core engine with official wrappers for React, Vue, Svelte, and Solid, as well as community support for Angular. It includes a variety of plugins such as Autoplay, Auto Scroll, Auto Height, Fade, Accessibility, and Class Names, and offers specialized utilities for Server-Side Rendering (SSR) and reactive frameworks.

Tokens
42.1K
Snippets
180
Records
234
Agent score
92%

What's inside Embla Carousel

  1. Overview of Embla Carousel SSR

    master
    Embla Carousel SSR is a specialized module for the Embla Carousel library designed to support Server-Side Rendering (SSR) environments. While the core Embla Carousel is a lightweight, dependency-free, and framework-agnostic library focused on smooth motion and precision swiping, the SSR package provides the necessary utilities to ensure carousel functionality and state are correctly handled during server-side rendering processes.
  2. Overview of Embla Carousel

    master
    Embla Carousel is a lightweight, dependency-free carousel library designed for smooth, fluid motion and precision swiping. It is framework-agnostic, meaning it can be used with vanilla JavaScript/TypeScript or integrated into various modern UI frameworks. It is built to provide developers with complete control over carousel behavior without unnecessary bloat.
  3. Overview of Embla Carousel Reactive Utils

    master
    Embla Carousel Reactive Utils is a package designed to work with the Embla Carousel library. Embla Carousel itself is a lightweight, dependency-free, framework-agnostic carousel library focused on smooth motion and precision swiping. The reactive utils package provides additional functionality to help integrate Embla Carousel more seamlessly with reactive frameworks.
  4. Overview of Embla Carousel React

    master
    Embla Carousel React is a React-specific wrapper for Embla Carousel, a lightweight, dependency-free carousel library. It is designed for smooth, fluid motion and precision swiping, providing developers with complete control over carousel behavior without unnecessary bloat. It is built to be framework-agnostic at its core but offers optimized hooks and components for React integration.
  5. Manage button disabled state for non-looping carousels

    master

    In non-looping carousels, it is good UX to disable navigation buttons when the carousel reaches the first or last slide. To implement this, listen to the following Embla events to update your button state:

    • select: Fires when the selected scroll snap changes.
    • reInit: Fires when the carousel resets.

    Note on Looping Carousels: In looping carousels with enough slides to wrap around, the scrollPrev and scrollNext methods always trigger scrolling, so buttons are never disabled.

  6. Understand Embla Carousel's DOM manipulation model

    master

    Embla Carousel is designed for high performance by using minimal DOM manipulation. It does not encapsulate or wrap your elements, meaning you retain full control over your markup and structure.

    Embla only modifies the DOM in two specific ways:

    1. Carousel container: Applies translate3d to create the scrolling effect.
    2. Carousel slides: Applies translate3d to adjust slide positions (primarily for looping functionality).

    Because Embla uses inline styles for these properties, it is compatible with all frameworks and allows for highly custom layouts. Note that while the core library and official framework wrappers follow this minimal approach, certain plugins may modify the DOM further.

  7. Extend carousel functionality with plugins

    master

    Plugins allow you to add advanced features to your Embla Carousel instance. Common plugin-based features include:

    • Autoplay: Automatically advances the carousel slides.
    • Auto Scroll: Creates a continuous, smooth scrolling effect.
    • Auto Height: Automatically adjusts the carousel container height based on the current slide.
    • Fade: Changes the transition effect from sliding to a fade transition.
    • Class Names: Provides CSS class names for different carousel states (e.g., selected, inactive) to facilitate styling.
  8. How Embla Carousel handles slide sizes

    master

    Embla Carousel does not assign slide sizes directly. Instead, it measures the computed dimensions of your slides as determined by your CSS. This allows you to use any layout method (Flexbox, Grid, etc.) and any CSS units.

    Key Requirements:

    • Slides must stack along the carousel's scroll axis (horizontally for horizontal carousels, vertically for vertical carousels).
    • You have full control over widths, heights, and layout methods via CSS.
  9. How to use Embla Carousel methods

    master

    Embla Carousel provides methods to programmatically extend and control the carousel.

    Key Requirements:

    • Methods require an initialized carousel instance.
    • Methods are accessible only while the instance is active. They become invalid after calling destroy.

    To use methods, you typically access the instance returned by the Embla initialization function (or via framework-specific hooks like useEmblaCarousel in React) and call the desired method on that instance.

    // Example: Accessing methods in Vanilla JS
    const emblaApi = emblaApi;
    if (emblaApi) {
      console.log(emblaApi.slideNodes());
    }
  10. Cancel events in Embla Carousel

    master

    Some events are cancellable, allowing you to prevent Embla Carousel's internal logic from executing. To cancel an event, your event handler must return false. Returning any other value (like true or undefined) allows the event to proceed normally.

    // Example: Preventing dragging if allowPointerDownEvent is false
    emblaApi.on('pointerdown', () => {
      if (!allowPointerDownEvent) return false
    })