Skip to content

useSlider

A custom React hook that registers horizontal or vertical dragging actions from the user that start on the element referenced by dragAreaRef. This hook responds to touch and mouse events, and will return a value between minValue and maxValue corresponding to the distance of the dragging action relative to the drag area.

This hook responds to the user dragging the target, clicking or touching along the drag area, or using the mouse wheel.

This hook can be configured to return a value between minValue and maxValue inclusive (defaults to 0-1), and the sensitivity of the mouse wheel can be configured as well. Finally, the hook can be configured to respond to horizontal or vertical dragging motions.

Example: A Volume Slider

The example below shows a volume slider that uses the useSlider hook.

Drag the slider to adjust the volume, or use the mouse wheel

40%

Usage

const { value, isAdjusting, isOnTarget, isOnDragArea } = useSlider({
targetRef,
dragAreaRef,
minValue = 0,
maxValue = 1,
initialValue = 0,
sensitivity = 100,
isVertical = true,
});

Props

The following props can be passed to the custom hook:

dragAreaRef

(required) A RefObject referencing a DOM element that functions as the “active” area. Any dragging or sliding action that happens when the pointer is over this element is registered as dragging the target.

targetRef

(required) A RefObject referencing a DOM element in your component. This is the “target” element of your slider, i.e. the thing you slide.

minValue

a number representing the minimum possible value. If the user keeps dragging beyond the minimum value, the hook’s value will stay at minValue. Default is 0.

maxValue

a number representing the maximum possible value. If the user keeps dragging beyond the maximum value, the hook’s value will stay at maxValue. Default is 1.

initialValue

a number that is used as the initial value when rendering the component. Defaults to 0.

sensitivity

a number that is a measure of how much rotating the mouse wheel will change the slider. Larger numbers mean slower changes. This uses useWheelToAdjust under the hood.

isVertical

true if the custom hook should register vertical dragging motion, false for horizontal dragging motion. Defaults to true.

Returns

The useSlider hook returns an object with the following properties:

value

A value between minValue and maxValue that represents the current value of the slider object. Use this to set the position of the target element or otherwise indicate the value.

isAdjusting

true if the user is actively dragging or using the wheel. This can be used to conditionally render the component in a particular way.

isOnTarget

true if the pointer is currently over the target element referenced by targetAreaRef.

isOnDragArea

true if the pointer is currently over the element referenced by dragAreaRef. Note that this actually checks if the pointer coordinates are within the bounding rect of the dragArea elements; it doesn’t use the pointer event target or composed path, in case the element is obscured by another element.