Headless Carousel for React with Momentum Scrolling

Description:

React Headless Carousel is a React component library that creates scrollable and swipeable carousels with momentum scrolling, rubber banding, and CSS snap point alignment.

The carousel component implements custom momentum physics for desktop environments where native scroll snap behavior is inconsistent. You can flick through carousel items with velocity-based scrolling that decelerates naturally.

The component follows a headless architecture where you build your own markup and styling while the library manages scroll position, pointer events, and accessibility concerns.

Features

  • Momentum scrolling with velocity-based deceleration for natural flick gestures.
  • Rubber banding effects that mimic native touch device overscroll behavior.
  • CSS scroll snap alignment for precise item positioning.
  • Keyboard navigation and tabbing with automatic item visibility enforcement.
  • Content fade masks that adjust dynamically based on remaining scroll distance.
  • Manages pagination that respects boundary offsets and fade masks.
  • Coordinates pointer events for drag-to-scroll functionality across all device types.
  • Maintains scroll position during window resize and orientation changes.

Basic Usage

1. Install the package via your preferred package manager.

npm install @daformat/react-headless-carousel
yarn add @daformat/react-headless-carousel
pnpm add @daformat/react-headless-carousel

2. Import the components and build your carousel structure. You must provide your own CSS classes to handle the visual layout.

import * as Carousel from '@daformat/react-headless-carousel';
import './my-custom-carousel.css';
export default function FeaturedProducts() {
  return (
    // The root component provides the context state
    <Carousel.Root className="product-carousel-root">
      // The viewport handles the swipe gestures and scroll events
      <Carousel.Viewport className="product-carousel-viewport" scrollSnapType="x mandatory">
        // The content wrapper forces items into a single row
        <Carousel.Content className="product-carousel-content">
          // Individual items require custom styling for dimensions
          <Carousel.Item className="product-carousel-item">
            <h3>Wireless Headphones</h3>
          </Carousel.Item>
          <Carousel.Item className="product-carousel-item">
            <h3>Mechanical Keyboard</h3>
          </Carousel.Item>
          <Carousel.Item className="product-carousel-item">
            <h3>Gaming Mouse</h3>
          </Carousel.Item>
        </Carousel.Content>
      </Carousel.Viewport>
      <div className="carousel-controls">
        // Built-in pagination buttons automatically handle disabled states
        <Carousel.PrevPage className="btn-prev">Previous Product</Carousel.PrevPage>
        <Carousel.NextPage className="btn-next">Next Product</Carousel.NextPage>
      </div>
    </Carousel.Root>
  );
}

Add Comment