Skip to main content

Overview

The useDragScroll hook provides mouse drag-to-scroll behavior for horizontal or vertical scrollable containers. It’s completely headless and unstyled, giving you full control over the UI while handling all the complex drag, scroll, and navigation logic.

Use Cases

  • Horizontal scrolling galleries and carousels
  • Product listing rows
  • Tab navigation with many items
  • Timeline components
  • Mobile-style swipeable lists on desktop
  • Image galleries and media browsers

Installation

The DragScroll hook is included with the UI package:

Basic Usage

API

useDragScroll Options

Options:
The scroll direction.
  • "horizontal" (default) - Scroll left/right
  • "vertical" - Scroll up/down
  • "both" - Scroll in both directions
Distance to scroll when using navigation buttons.
  • "item" (default) - Scroll by first child’s width/height
  • number - Scroll by fixed pixel amount
Device constraints for drag behavior.
  • "allScreens" (default) - Works on all devices
  • "desktopOnly" - Drag only on desktop (768px and above)
  • "mobileAndTabletOnly" - Drag only on mobile/tablet (below 768px)
Custom CSS classes.
  • base - Applied to root container
  • item - Applied to each item
Disable automatic data-attribute updates (default: false). Set to true if you want to subscribe to state manually.

Return Value

Prop Getters

getRootProps

Props for the scrollable container.
Returned Props:
  • ref - Container ref callback
  • className - Merged classes including drag cursor styles
  • data-scope="drag-scroll"
  • data-part="root"
  • data-dragging - Present when actively dragging (if state subscription enabled)

getItemProps

Props for each scrollable item.
Returned Props:
  • className - Includes snap-center styles
  • data-scope="drag-scroll"
  • data-part="item"

getBackButtonProps

Props for the previous/back navigation button.
Returned Props:
  • type="button"
  • onClick - Scrolls to previous item/section
  • disabled - Auto-disabled when can’t scroll back
  • aria-label="Scroll back"
  • data-disabled - Reflects disabled state
  • data-scope="drag-scroll"
  • data-part="back-button"

getNextButtonProps

Props for the next/forward navigation button.
Returned Props:
  • type="button"
  • onClick - Scrolls to next item/section
  • disabled - Auto-disabled when can’t scroll forward
  • aria-label="Scroll forward"
  • data-disabled - Reflects disabled state
  • data-scope="drag-scroll"
  • data-part="next-button"

Examples

Vertical Timeline

Desktop-Only Drag

Accessing State

Subscribe to internal state using the returned hook:

Styling with Data Attributes

When disableInternalStateSubscription is false (default), the root element receives a data-dragging attribute:
Style based on dragging state:
The hook automatically manages scroll position tracking and updates button disabled states based on scroll position.
The scrollAmount: "item" option uses the first child’s dimensions. Ensure all items have consistent sizing for predictable scrolling.

Accessibility

  • Navigation buttons include proper aria-label attributes
  • Buttons are automatically disabled when scrolling is not possible in that direction
  • The hook maintains focus management during interactions
  • Keyboard users can still use native scroll with arrow keys and tab navigation

API Reference

For detailed type definitions, see the DragScroll API Reference.