Skip to content

@gertalot/sliders

React custom hooks for sliders, knobs, and dials.

What is this?

@gertalot/sliders is a small React library with a number of custom hooks that make it easy to build sliders, dials, and other components that respond to dragging and sliding actions from the user. The hooks take care of all user interaction, such as dragging, rotating, using the mouse wheel, or two-finger scrolling.

Demo

Slider

Drag the slider, or use the mouse wheel. This component is created with a simple SVG, and uses the useSlider custom hook. The code for this component is on the useSlider documentation.

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

40%

Dial

Turn the dial to go up to eleven! Or use the mousewheel, or drag vertically. This component is an SVG and uses the useDial custom hook to handle all user interaction and returns an angle and a value to the component. The code for this component is on the useDial documentation.

601234567891011

Why does it exist?

For a project involving a climate-controlled environment, I needed something to control temperature and humidity, preferrably from my phone. This was an excellent excuse to put together this library. Moving the code that handles the user’s dragging, rotating, and scrolling actions out of my components and into reusable hooks seemed sensible, and I am sharing this in the hope it might be useful or educational for others.

Example

Here’s a minimal example of one of the custom hooks, useWheelToAdjust. These hooks take care of all user interaction and return properties you can use in your components. In this example, the hook returns a wheelDelta value representing the speed at which the user is rotating the mouse wheel.

Use the mouse wheel to change!

Scroll faster for coarse adjustments, and slower for fine adjustments

0.0

To learn more about the hooks and how to use them, check out the docs!