A lightweight (~2kB) library to create range sliders that can capture a value or a range of values with one or two drag handles

Overview

range-slider-input

circleci npm minzipped size known vulnerabilities javascript style guide license

A lightweight (~2kB) library to create range sliders that can capture a value or a range of values with one or two drag handles.

Examples / CodePen

Demo

Features

  • High CSS customizability
  • Touch and keyboard accessible
  • Supports negative values
  • Vertical orientation
  • Small and fast
  • Zero dependencies
  • Supported by all major browsers
  • Has a React component wrapper

⚠️ It is recommended that you upgrade from v1.x to v2.x! What's new and what's changed in v2.x?



Installation

npm

npm install range-slider-input

Import the rangeSlider constructor and the core CSS:

import rangeSlider from 'range-slider-input';
import 'range-slider-input/dist/style.css';

CDN

<script src="https://cdn.jsdelivr.net/npm/range-slider-input@2/dist/rangeslider.umd.min.js"></script>

or

<script src="https://unpkg.com/range-slider-input@2"></script>

The core CSS comes bundled with the jsDelivr and unpkg imports.

Usage

import rangeSlider from 'range-slider-input';
import 'range-slider-input/dist/style.css';

const rangeSliderElement = rangeSlider(element);

API

rangeSlider(element, options = {})

Returns an object of functions that can be called to read or write the properties initially set by options.

Parameters

element

HTMLElement

options

Object that specifies the characteristics of the range slider element with the following available properties:

Property Type Default value Description
min number 0 Number that specifies the lowest value in the range of permitted values.
Its value must be less than that of max.
max number 100 Number that specifies the greatest value in the range of permitted values.
Its value must be greater than that of min.
step number / string 1 Number that specifies the amount by which the slider value(s) will change upon user interaction.
Other than numbers, the value of step can be a string value of any.

From MDN,
A string value of any means that no stepping is implied, and any value is allowed (barring other constraints, such as min and max).
value number[] [25, 75] Array of two numbers that specify the values of the lower and upper offsets of the range slider element respectively.
onInput function NOOP Function to be called when there is a change in the value(s) of range sliders upon user interaction or upon calling min(), max(), step() or value().
onThumbDragStart function NOOP Function to be called when the pointerdown event is triggered for any of the thumbs.
onThumbDragEnd function NOOP Function to be called when the pointerup event is triggered for any of the thumbs.
onRangeDragStart function NOOP Function to be called when the pointerdown event is triggered for the range.
onRangeDragEnd function NOOP Function to be called when the pointerup event is triggered for the range.
disabled boolean false Boolean that specifies if the range slider element is disabled or not.
rangeSlideDisabled boolean false Boolean that specifies if the range is slidable or not.
thumbsDisabled boolean[] [false, false] Array of two Booleans which specify if the lower and upper thumbs are disabled or not, respectively. If only one Boolean value is passed instead of an array, the value will apply to both thumbs.
orientation string horizontal String that specifies the axis along which the user interaction is to be registered. By default, the range slider element registers the user interaction along the X-axis. It takes two different values: horizontal and vertical.

Return value

Object of functions that can be called to read or write the properties initially set by the options parameter. Available functions:

min(), max(), step(), value() and orientation()

These are simple getter and setter functions. So, while calling these functions, if a parameter is supplied, the corresponding values will be set, and if a parameter is not supplied, the corresponding values will be returned. E.g. Calling step() will return the step value, and calling value([0, 0.5]) will set the lower and upper offsets to 0 and 0.5 respectively.

disabled(), rangeSlideDisabled()

The default parameter is set to true. So, if they are called without a parameter, they will set the corresponding values to true. Thus, calling disabled() or disabled(true) will set options.disabled = true and calling disabled(false) will set options.disabled = false.

thumbsDisabled()

The default parameter is set to [true, true]. So, if it is called without a parameter, it will disable both thumbs. Example uses:

//                          thumbs -> lower     upper
//                                    -----     -----
thumbsDisabled()                // disabled  disabled
thumbsDisabled(true)            // disabled  disabled
thumbsDisabled(false)           //  enabled   enabled
thumbsDisabled([])              //  enabled   enabled
thumbsDisabled([false])         //  enabled   enabled
thumbsDisabled([true])          // disabled   enabled
thumbsDisabled([true, false])   // disabled   enabled
thumbsDisabled([false, true])   //  enabled  disabled
thumbsDisabled([false, false])  //  enabled   enabled
thumbsDisabled([true, true])    // disabled  disabled

currentValueIndex()

Returns the index (0 for the lower value and 1 for the upper value) of the value which is currently being modified. Returns -1 when the slider is idle.

Elements

<div class="range-slider"><!-- range slider element -->
    <input type="range" /><!-- hidden -->
    <input type="range" /><!-- hidden -->
    <div class="range-slider__thumb" data-lower></div>
    <div class="range-slider__thumb" data-upper></div>
    <div class="range-slider__range"></div>
</div>

<div class="range-slider"></div> is the wrapper element that was used to instantiate the range slider initially and is added with a CSS class named range-slider.

<input type="range" /> elements are used to set values and are hidden.

<div class="range-slider__thumb"></div> elements are the slidable thumbs replacing the original thumbs from the <input type="range" /> elements.

<div class="range-slider__range"></div> element fills up the space between the thumbs.

Styling

View styled examples

element-selector {
    /* CSS for the wrapper element */
}
element-selector[data-disabled] {
    /* CSS for disabled range slider element */
}
element-selector .range-slider__range {
    /* CSS for range */
}
element-selector .range-slider__range[data-active] {
    /* CSS for active (actively being dragged) range */
}
element-selector .range-slider__thumb {
    /* CSS for thumbs */
}
element-selector .range-slider__thumb[data-lower] {
    /* CSS for lower thumb */
}
element-selector .range-slider__thumb[data-upper] {
    /* CSS for upper thumb */
}
element-selector .range-slider__thumb[data-active] {
    /* CSS for active (actively being dragged) thumbs */
}
element-selector .range-slider__thumb[data-disabled] {
    /* CSS for disabled thumbs */
}

Refer to the style.css file to know more about styling the range slider element and its children.

Component Wrappers

React: react-range-slider-input

License

MIT © Utkarsh Verma

Comments
  • Callback examples ?

    Callback examples ?

    Many thanks for this work, in general it works nicely.

    I am a bit confused how I can read the values when they are changed by the user or how to set them programmatically. Are there any callback functions ? The documentation describes something with onInput, but I don't really understand how it is used.

    Could you provide additional examples ?

    opened by bakman2 5
  • Single thumb workaround ?

    Single thumb workaround ?

    I want a slider with a single thumb as below (inspired by daisy ui). I styled the first thumb with the fore/background them same as the track.

    image

    with thumbsDisabled: [true, false],

    I cannot hide the first thumb because the track will not render correctly image

    The problem with a disabled thumb is that one cannot click directly on the beginning (=0) of the track. Is there a potential workaround ?

    opened by bakman2 2
  • Event listeners are never removed

    Event listeners are never removed

    As the titles says, (global) event listeners are never removed. In case you want to dynamically add or remove the range slider, the listeners need to be removed.

    I suggest to provide a method (like destroy() or unregister()), which unregisters the event listeners. That method should be called if somebody wants dynamically remove the slider.

    opened by dvdmunckhof 2
  • Build instructions missing

    Build instructions missing

    Build steps and build dependencies seem to be missing from the package.json file. Could you provide build instructions?

    I tried: (copied from https://github.com/n3r4zzurr0/react-range-slider-input/blob/master/package.json)

    NODE_ENV=production babel src --out-dir dist --copy-files
    

    However, the results did not match the contents of the npm package.

    The reason I ask, is I wrote TypeScript type declarations that I'd like to contribute back to the project. By the way, awesome library! :)

    opened by dvdmunckhof 2
  • Add Typescript type declarations

    Add Typescript type declarations

    I added Typescript type declarations for easier use in Typescript projects. I wrote these for my own use and like to contribute them back to this awesome project!

    opened by dvdmunckhof 0
Owner
Utkarsh Verma
Utkarsh Verma
Functions for testing the types of JavaScript values, cross-realm. Has testers for all standard built-in objects/values.

@suchipi/is Functions for testing the types of JavaScript values, cross-realm. Has testers for all standard built-in objects/values. Usage import { is

Lily Skye 5 Sep 8, 2022
🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

?? TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

CodelyTV 82 Dec 7, 2022
Drag and drop library for two-dimensional, resizable and responsive lists

DEPRECATED This project is no longer maintained, please consider using react-grid-layout instead. GridList Drag and drop library for a two-dimensional

Hootsuite 3.6k Dec 14, 2022
Make drag-and-drop easier using DropPoint. Drag content without having to open side-by-side windows

Make drag-and-drop easier using DropPoint! DropPoint helps you drag content without having to open side-by-side windows Works on Windows, Linux and Ma

Sudev Suresh Sreedevi 391 Dec 29, 2022
Zero Two Bot,A fully Modular Whatsapp Bot to do everything possible in WhatsApp by Team Zero Two

?? ???????? ?????? ???? ?? A Moduler WhatsApp Bot designed for both PM and Groups - To take your boring WhatsApp usage into a whole different level. T

Sam Pandey 69 Dec 25, 2022
Generate release notes from git commit history either commit range or tag range.

Would you like to support me? Release Notes Generate release notes from git commit history either commit range or tag range. App Store Template Change

Numan 6 Oct 8, 2022
A (very) minimalist creative coding playground. Make animations using only 64 HTML sliders!

Sliderland A (very) minimalist creative coding playground. Make animations using only 64 HTML sliders! Credits The recording feature uses ffmpeg.wasm

null 181 Dec 30, 2022
Avoid use of dangerouslySetInnerHTML with this lightweight (2KB) function.

Avoid use of dangerouslySetInnerHTML with this lightweight (2KB) function. Can parse HTML strings into VDom trees, ready to render in your Preact components

James Hill 4 May 14, 2022
Lightweight vanilla js modal component (just 2kb) , pure javascript Modal

Lightweight vanilla js modal component (just 2kb) pure javascript Modal , This is just 2kb Lightweight vanilla js modal component with zero dependenci

Salah Eddine Lalami 12 Dec 12, 2022
🦉The jQuery plugin "Stick to me" allows you to create exit-popups on your web page, so you can capture visitors just as they are about to leave.

?? The jQuery plugin "Stick to me" allows you to create exit-popups on your web page, so you can capture visitors just as they are about to leave. How

Guilherme Assemany 18 Feb 10, 2022
A ~2kb hotkeys library for solid that adds only 2 event listeners.

Solid Hotkeys - Cmd+S+H Solid Hotkeys is the easiest way to add hotkeys to your application. Built for and with Solid. Motivation You gotta have hotke

Alek Angelov 12 Aug 1, 2022
DoMe is a ToDo App. you can add, delete and reorder elements of the todo list using drag and drop. You can also toggle between dark&light mode

DO ME Todo App Live Preview : DO ME Built With : - ReactJS - TailwindCSS Make sure you have: - Git - Nodejs version 14 or higher (we recommend using

Medjahdi Islem 5 Nov 18, 2022
You can easily create the horizontal timeline with two types by using this jQuery plugin.

jQuery.Timeline V2 You are able to easily create two types of horizontal timeline with this jQuery plugin. Report bug · Request feature · Blog Table o

ka2 222 Dec 9, 2022
An interactive app that allows adding, editing and removing tasks of a to-do list. Drag-and-drop featured added. Webpack was used to bundle all the Js modules in to one main Js file.

To-do List A to-do list app This app let you to set your own to-do list. Built With HTML CSS JavaScript WebPack Jest Live Page Page Link Getting Start

Kenny Salazar 7 May 5, 2022
A Cli that handles the creation of a basic express App that supports Husky configuration & static analysis tools

@phazero/create-express-app · Create express app is a CLI that can generate boiler plate code for setting up an express app. Installation & Usage npx

PhazeRo 13 Oct 29, 2022
Download all Moodle files with one click. This is a Chrome extension built to save time and effort from downloading files manually one by one!

Moodle Downloader Extension Moodle downloader extension for Chrome. The extension is tested with both the TUM moodle and the official moodle demo. Not

Zhongpin Wang 8 Nov 15, 2022
Create beautiful quotes that capture your attention.

Features Easy text highlights via normal rich text formatting Yellow = Bold, Blue = Italic Export to png, jpeg, svg, or copy png to clipboard Supports

Travis Fischer 40 Dec 15, 2022
📷 Detects your face and adds filters from your webcam. You can capture and download images.

Snapchat Filters on WebCam ?? Detects your face and adds filters from your webcam. You can capture and download images. ?? Visit site ?? Screenshots ?

Orhan Emre Dikicigil 2 Apr 27, 2022