Tool Cool Range Slider

Overview

Tool Cool Range Slider

GitHub license GitHub package.json version npm NPM Featured on Openbase Twitter

Responsive range slider library written in typescript and using web component technologies. Pure JavaScript without additional dependencies. It has a rich set of settings, including a vertical slider, touch, mousewheel and keyboard support, local and session storage, and RTL support.

Tool Cool Range Slider

  • Responsive, touch, mousewheel, and keyboard support πŸ“±
  • Accessible via ARIA-attributes πŸ›‘οΈ
  • ES6 JavaScript + TypeScript.
  • No dependencies πŸ”“
  • Predefined themes (optional) 🎨
  • Customizable with a large set of style attributes πŸ”§
  • Horizontal and vertical sliders support.
  • Based on web component technologies.
  • Allows programmatic attribute changes πŸ’»
  • Bottom to top support.
  • Simple dynamic rendering after ajax requests or delays.
  • Disabled / enabled range slider (including API).
  • Local storage and session storage support πŸ’Ύ
  • Supports a list of individual values (discrete values).
  • Right to left (RTL) support.
  • Text data support ✍️
  • Non-linear range slider πŸ“‰
  • Optional animation on panel click.
  • Works well with Bootstrap and other CSS frameworks πŸ‘
  • No CSS conflicts due to web components.

Table of contents

Basic Usage

Download the latest toolcool-range-slider.min.js script:

Add the following html to the page:

">
<toolcool-range-slider>toolcool-range-slider>

<script type="text/javascript" src="toolcool-range-slider.min.js">script>

You can control the range slider by referencing the toolcool-range-slider HTML tag.

">
<toolcool-range-slider id="slider-1">toolcool-range-slider>

<script type="text/javascript" src="toolcool-range-slider.min.js">script>

<script>
    // get the reference
    const $slider = document.getElementById('slider-1');
    
    // change value
    $slider.value = 50;
    
    // get value
    console.log($slider.value);

    // listen to the change event
    $slider.addEventListener('change', (evt) => {
      const value = Math.round(evt.detail.value);
      console.log(value);
    });
script>

The value label can also be automatically bound using the value-label attribute:

">
<toolcool-range-slider value-label=".value-1">toolcool-range-slider>

<div class="value-1">div>

<script type="text/javascript" src="toolcool-range-slider.min.js">script>

πŸ“Œ More examples with automatic label binding can be found here.

πŸ“Œ Examples with js binding can be found here.

CDN

The ToolCool Range Slider is also available in the jsDelivr CND:

">
<toolcool-range-slider>toolcool-range-slider>

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toolcool-range-slider/dist/toolcool-range-slider.min.js">script>

Node.js usage

Tool Cool Range Slider

Range slider may also be included as a node module using npm:

npm i toolcool-range-slider

or with Yarn:

yarn add toolcool-range-slider

And then you can include it in your application like this:

import 'toolcool-range-slider';

NPM package can fe found here.

Main Properties

Range slider has the following main properties: min, max, value, and step. All properties are optional, including step. Usage examples:

">
<toolcool-range-slider>toolcool-range-slider>

<toolcool-range-slider min="10" max="50">toolcool-range-slider>

<toolcool-range-slider min="-100" max="100" value="50">toolcool-range-slider>

<toolcool-range-slider min="0" max="100" value="50">toolcool-range-slider>

<toolcool-range-slider min="100" max="200" value="150" step="10">toolcool-range-slider>

The properties have the following default values:

Property Default Value
min 0
max 100
value 0
step undefined

If no value is specified, it will be equal to the minimum value.

List of individual values

It is also possible (but not required) to provide a list of numeric or text data instead of min and max properties.

ABC example:

">
<toolcool-range-slider
  data="a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z">toolcool-range-slider>

Geometric progression:

">
<toolcool-range-slider
data="2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192">toolcool-range-slider>

In the case where numeric and textual data is mixed, we assume that all data is textual.

πŸ“Œ Example page can be found here.

Width, Height, and Border Radius

By default, the slider width is 100% and the height is 0.25 rem. These values can be changed using the slider-width and slider-height properties:

">
<toolcool-range-slider
        slider-width="300px"
        slider-height="15px"
        slider-radius="0.5rem">toolcool-range-slider>

<toolcool-range-slider
       slider-width="100%"
       slider-height="1rem">toolcool-range-slider>

Pointer width, height, and border-radius can be change using pointer-width, pointer-height, and pointer-radius properties:

">
<toolcool-range-slider
        pointer-width="35px"
        pointer-height="35px"
        pointer-radius="5px">toolcool-range-slider>

Default values table:

Property Default Value
slider-width 300px
slider-height 0.25rem
slider-radius 1rem
pointer-width 1rem
pointer-height 1rem
pointer-radius 100%

Predefined Styles (Themes)

Range Slider

The slider has several optional predefined themes. Each theme defines a unique look-and-feel and can be used "as is" without defining each style parameter separately.

All themes are fully optional and can be partially or completely replaced by custom styles, as described later in this documentation.

The slider has the following themes:

Theme Code Name Example
target Example 1, Example 2
glass Example 1, Example 2
rect Example 1, Example 2
circle Example 1, Example 2
gradient Example
ruler Example

Usage examples:

">
<toolcool-range-slider
  slider-width="400px"
  slider-height="0.5rem"
  theme="rect">toolcool-range-slider>

It's possible to combine themes together with custom properties like this:

">
 <toolcool-range-slider
  slider-width="400px"
  slider-height="0.5rem"
  theme="rect"
  slider-bg="red">toolcool-range-slider>

Colors

Color and other styles can be customized with the following attributes:

">
<toolcool-range-slider
        slider-width="400px"
        slider-height="0.5rem"
        pointer-width="1.5rem"
        pointer-height="1.5rem"
        slider-bg="#6AD3BA"
        slider-bg-hover="#3F8A8A"
        pointer-border-hover="1px solid #79D6C0"
        pointer-border-focus="1px solid #79D6C0">toolcool-range-slider>


<toolcool-range-slider
          slider-width="400px"
          slider-height="0.5rem"
          pointer-width="1.5rem"
          pointer-height="1.5rem"
          pointer-bg="#6AD3BA"
          pointer-bg-hover="#50BDA3"
          pointer-shadow="none"
          pointer-shadow-hover="none"
          pointer-border="0"
          pointer-border-hover="1px solid #3F8A8A"
          pointer-border-focus="1px solid #3F8A8A">toolcool-range-slider>

The list of attributes and default values:

Property Default Value
slider-bg #4d69ad
slider-bg-hover #5f79b7
slider-bg-fill #000
pointer-bg #fff
pointer-bg-hover #dcdcdc
pointer-bg-focus #dcdcdc
pointer-shadow 0 0 2px rgba(0, 0, 0, 0.6)
pointer-shadow-hover 0 0 2px rgb(0, 0, 0)
pointer-shadow-focus 0 0 2px rgb(0, 0, 0)
pointer-border 1px solid hsla(0, 0%, 88%, 0.5)
pointer-border-hover 1px solid hsla(0, 0%, 88%, 0.5)
pointer-border-focus 1px solid hsl(201, 72%, 59%)

πŸ“Œ An example of a customized slider on a dark background:

Dark Mode - Range Slider

Vertical Slider

Tool Cool Vertical Slider

Vertical slider can be achieved using the type attribute as following:

">
<toolcool-range-slider type="vertical">toolcool-range-slider>

It accepts all the same attributes as the horizontal slider. The default height of a vertical slider is 300px unless the height attribute is provided.

It is also possible to reverse the direction and slide from bottom to top using the btt attribute:

">
<toolcool-range-slider type="vertical" btt="true">toolcool-range-slider>

Pointer Shapes

Tool Cool Pointer Shapes

There are also several predefined pointer shapes that can be defined using the pointer-shape attribute. For example, triangle pointer shape:

">
<toolcool-range-slider
  min="0"
  max="100"
  value="10"
  pointer-shape="triangle"
  pointer-width="2rem"
  pointer-height="2rem"
  pointer-bg="#d7067d"
  pointer-bg-hover="#0b94c7"
  pointer-bg-focus="#0b94c7"
  slider-width="400px"
  slider-bg="#6787cd">toolcool-range-slider>

There are the following pointer shapes:

Property
triangle
star
rhombus
trapezoid
parallelogram
right-arrow

Touch & Keyboard Support

The library supports touch screens and also handles the following keys:

Key Function
left arrow goes one step to the left in a horizontal slider or up in a vertical slider
right arrow goes one step to the right in a horizontal slider or down in a vertical slider
left up jumps to the min value
right down jumps to the max value

Events

The range slider has the following events:

Event Description
change it is sent every time the value of the range slider changes
onMouseDown the native browser mousedown event
onMouseUp the native browser mouseup event
onPointerClicked it is dispatched when the user clicks the range slide pointer (handler)
onKeyDown the native browser keydown event (arrow left, arrow right, arrow up, arrow down)

Usage examples:

const $slider1 = document.getElementById('slider-1');

// change event ------------
$slider1.addEventListener('change', (evt) => {
    const value = Math.round(evt.detail.value);
    $value1.textContent = value.toString();
    console.log(`Change event: ${ value }`)
});

// onMouseDown event ------------
$slider1.addEventListener('onMouseDown', (evt) => {
    const nativeEvent = evt.detail.nativeEvent;
    console.log('Native mousedown event:', nativeEvent)
});

// onMouseUp event ------------
$slider1.addEventListener('onMouseUp', (evt) => {
    const nativeEvent = evt.detail.nativeEvent;
    console.log('Native mouseup event:', nativeEvent);
});

// onPointerClicked event ------------
$slider1.addEventListener('onPointerClicked', (evt) => {
    const $pointer = evt.detail.$pointer;
    console.log('Pointer clicked event:', $pointer);
});

// onKeyDownEvent event ------------
$slider1.addEventListener('onKeyDown', (evt) => {
    const nativeEvent = evt.detail.nativeEvent;
    console.log('Native onKeyDown event:', nativeEvent);
});

πŸ“Œ The page with these examples can be found here.

Disabled

It's possible to disable the range slider using disabled attribute:

">
<toolcool-range-slider disabled="true">toolcool-range-slider>

This property can be easily toggled via APIs as follows:

const $slider1 = document.getElementById('slider-1');
const $toggleButton = document.getElementById('toggle-btn');

$toggleButton.addEventListener('click', () => {
  $slider1.disabled = !$slider1.disabled;
});

The default opacity of the range slider when disabled is 0.4. This value can be change using css variable --tc-range-slider-opacity:

">
<toolcool-range-slider
  disabled="true"
  style="--tc-range-slider-opacity: 0.1">toolcool-range-slider>

πŸ“Œ The page with this example can be found here.

Storage

The range slider library also supports local and session storage. It's used to save the selected slider value between different pages and page reloads.

">
<toolcool-range-slider storage="session-storage" storage-key="tc-range-slider-1">toolcool-range-slider>
<toolcool-range-slider storage="local-storage" storage-key="tc-range-slider-2">toolcool-range-slider>

The difference between local and session storage is that session storage keeps the value only during the current session, while the local storage keeps it until the user clears the browser cache.

πŸ“Œ The page with examples can be found here.

RTL support

The range slider also supports right to left (RTL) using rtl attribute as follows:

">
<toolcool-range-slider rtl="true">toolcool-range-slider>
">
<div class="row" style="direction: rtl">
  <label>0label>
  <toolcool-range-slider
    slider-width="100%"
    value-label=".value-1"
    rtl="true">toolcool-range-slider>
  <label>100label>
  <div class="value value-1">div>
div>

πŸ“Œ The page with examples can be found here.

Non-linear step

The range slider supports the non-linear step function. For example, the slider below has a step of 5 if the value is less than 50, otherwise the step is 10:

">
<toolcool-range-slider id="slider-1">toolcool-range-slider>

<script src="toolcool-range-slider.min.js">script>
<script>
    const $slider = document.getElementById('slider-1');
    $slider.step = (value) => {
        return value < 50 ? 5 : 10;
    };
script>

Step function has the following type:

(value: number | string) => number

It gets the value of the slider and returns the corresponding step value.

πŸ“Œ The page with examples can be found here.

Animation on panel click

It's possible to enable animation on panel click with the animate-onclick property:

">
<toolcool-range-slider animate-onclick="0.3s">toolcool-range-slider>

The value of the animate-onclick property is specified in seconds, ms, etc. and is the same as the css transition-duration property.

πŸ“Œ The page with example can be found here.

TypeScript Usage

import 'toolcool-range-slider';
import RangeSlider from 'toolcool-range-slider';

// ...

const $slider = document.getElementById('slider-1') as RangeSlider;

$slider.addEventListener('change', (evt: Event) => {
    const customEvent = evt as CustomEvent;
    const value = Math.round(evt.detail.value);
    console.log(value);
});

$slider.value = 10;

Usage with React and TypeScript

import 'toolcool-range-slider';

declare global {
    namespace JSX {
        interface IntrinsicElements {
            'toolcool-range-slider': any;
        }
    }
}

const RangeSliderExample = () => {

    const rangeSliderRef = useRef<HTMLElement>();

    useEffect(() => {

        const slider = rangeSliderRef.current;

        const onChange = (evt: Event) => {
            const customEvent = evt as CustomEvent;
            const value = Math.round(evt.detail.value);
            console.log(value);
        };

        slider?.addEventListener('change', onChange);

        return () => {
          slider?.removeEventListener('change', onChange);
        };
    }, []);

    return (
        <toolcool-range-slider ref={ rangeSliderRef } />;
    )
};

export default RangeSliderExample;

License

MIT license

It can be used for free and without attribution in any personal or commercial project 🎁

You might also like...

A JavaScript component that is a date & time range picker, no need to build, no dependencies except Moment.js, that is based on Dan Grossman's bootstrap-daterangepicker.

A JavaScript component that is a date & time range picker, no need to build, no dependencies except Moment.js, that is based on Dan Grossman's bootstrap-daterangepicker.

vanilla-datetimerange-picker Overview. A JavaScript component that is a date & time range picker, no need to build, no dependencies except Moment.js,

Dec 6, 2022

Beautiful UI-Range input component, highly customisable, based on CSS variables.

Beautiful UI-Range input component, highly customisable, based on CSS variables.

Beautiful UI-Range input component, highly customisable, based on CSS variables. including a floating output value, min & max values on the sides & ticks according to the steps

Dec 27, 2022

Obsidian plugin allowing for linking to a heading range, in the [[Page#HeaderA#HeaderB]] extended wikilink format.

Obsidian Link Heading Range Plugin This is a plugin for Obsidian (https://obsidian.md). It allows linking to a heading range, in the [[Page#HeaderA#He

Nov 14, 2022

Scrape tweets from Twitter search results based on keywords and date range using Playwright. Save scraped tweets in a CSV file for easy analysis

Tweet Harvest (Twitter Crawler) Tweet Harvest is a command-line tool that uses Playwright to scrape tweets from Twitter search results based on specif

Aug 9, 2023

MenuSlider-Javascript - How to create a menu slider with vanilla javascript

MenuSlider-Javascript How to create a menu slider with vanilla javascript Instal

Feb 8, 2022

A Responsive JQuery Slider

Turntable.js :: A Responsive JQuery Slider A jQuery plugin that will flip through a list of images as your mouse sweeps across a container Usage All i

Nov 1, 2022

A dependency-free JavaScript ES6 slider and carousel. It’s lightweight, flexible and fast. Designed to slide. No less, no more

Glide.js is a dependency-free JavaScript ES6 slider and carousel. It’s lightweight, flexible and fast. Designed to slide. No less, no more What can co

Jan 3, 2023

Smooth mobile touch slider for Mobile WebApp, HTML5 App, Hybrid App

Smooth mobile touch slider for Mobile WebApp, HTML5 App, Hybrid App

iSlider iSlider is a lightweight, high-performant, no library dependencies cross-platform slide controller. It can help handling most sliding effects,

Nov 25, 2022

Swipe is the most accurate touch slider.

Usage Swipe only needs to follow a simple pattern. Here is an example: div id='slider' class='swipe' div class='swipe-wrap' div/div

Dec 16, 2022
Comments
  • Setting value2 with Javascript not working

    Setting value2 with Javascript not working

    Not sure, why such a simple example is not working. What do I miss? I'm creating this React app:

    import { useRef, useEffect } from 'react';
    import './lib/tcrs-generated-labels.min.js';
    import './lib/toolcool-range-slider.min.js'; 
    
    export default function RangeSliderPro({ component, eventHandlers, appData, pageData, parentDataModel }) {
      
      const sliderRef = useRef(null);
      
      useEffect( ()=>{
        const slider = sliderRef.current;
        slider.value1 = 30;
        slider.value2 = 75;
        slider.generateLabels = true;
    
        return () => { 
          slider?.removeEventListener('change', onChange);
        }
        
      }, [] )
      
      return ( 
        <tc-range-slider ref={sliderRef}></tc-range-slider> 
      )
    }
    

    The value I'm setting for slider.value2 is ignored. value1 has effect. So, what I'm seeing is this:

    image

    Versions:

    Tool Cool Range Slider Version: 4.0.17

    Tool Cool Range Slider - Generated Labels Plugin Version: 1.0.4

    Any help is appreciated ...

    bug 
    opened by kla-ko 9
  • Setting generateLabelsUnits with Javascript not working

    Setting generateLabelsUnits with Javascript not working

    I have the following setup:

    import { useRef, useEffect } from 'react';
    import './lib/tcrs-generated-labels.min.js';
    import './lib/toolcool-range-slider.min.js'; 
    
    export default function RangeSliderPro({ component, eventHandlers, appData, pageData, parentDataModel }) {
      
      const sliderRef = useRef(null);
      
      useEffect( ()=>{
        const slider = sliderRef.current;
        slider.value1 = 30;
        slider.generateLabels = true;
        slider.generateLabelsUnits = '%';
    
        return () => { 
          slider?.removeEventListener('change', onChange);
        }
        
      }, [] )
      
      return ( 
        <tc-range-slider ref={sliderRef}></tc-range-slider> 
      )
    }
    

    Issue: the statement slider.generateLabelsUnits = '%' shows no effect.

    bug 
    opened by kla-ko 7
  • OnChange event fail

    OnChange event fail

    The event OnChange is trigger everything, for example:

    1. $('#tool_range').val(2);
    2. document.ready
    3. when created toolcool-range inserting html whit input toolcool
    opened by DaveARG 2
  • require is not defined when bundling code with node

    require is not defined when bundling code with node

    it works fine with when used with react only. when i build with yarn build for nodejs server (because i have apis) . server shows a blank page .console says require is not defined.

    opened by Prince-Hamza 1
Owner
Tool Cool
The ToolCool project is a collection of useful online web tools. Each tool has a rich set of features and can speed up the creation and development of websites
Tool Cool
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
Pure JavaScript HTML5 Canvas Range Slider. No stylesheet needed. Simple, elegant and powerful. A quirky alternative to other sliders.

CanvasSlider CanvasSlider is a lightweight JavaScript range slider. Most of the range slider controls use Javascript and a stylesheet. This slider use

null 7 Aug 15, 2022
Accordion Slider is a jQuery plugin that combines the functionality of a slider with that of an accordion.

Accordion Slider - jQuery slider plugin A responsive and touch-enabled jQuery accordion slider plugin that combines the functionality of a slider with

null 0 Dec 29, 2022
A pretty cool org-mode -> interactive blog post tool

Radish A kinda-cool org-mode -> interactive blog post tool written with and for Clojure(script). Here are two example posts created with this tool: Ra

adam-james 46 Dec 28, 2021
A cool tool that saves you time if you want to remove node_modules before running 'npm i'

rmnpm A cool tool that saves you time if you want to remove your node_modules folder before running the npm install command. How does it do it? By fir

null 4 Jul 16, 2022
Tool Cool Color Picker is a color picker library written in typescript and using web component technologies.

Tool Cool Color Picker Tool Cool Color Picker is a color picker library written in typescript and using web component technologies. Check out the demo

Tool Cool 13 Oct 23, 2022
Digispark Overmaster : free IDE TOOL allows to create and edit Digispark Scripts by the drag and drop technique,with cool GUI and easy to use it

Digispark_Overmaster Digispark Overmaster : free IDE TOOL allows to create and edit Digispark Scripts by the drag and drop technique,with cool GUI and

Yehia Elborma 5 Nov 14, 2022
Calculate the price range for property advertised on Domain and Real Estate.

Property Seeker Calculate the price range for property advertised on Domain and Real Estate. Install Chrome Firefox Edge Privacy All searches are perf

null 42 Dec 27, 2022
A modern, clean design can be customized and applied for a wide range of purposes

Gatsby starter for projects portfolio with Flotiq source Kick off your project with this hello-world boilerplate. This starter ships with the main Gat

flotiq 13 Oct 4, 2022
Set a range that will be played.

Spicetify part selector This extension play selected part. Usage: 'd' - start position, 'f' - end position, 'r' - reset. For first set end position(f)

null 4 Nov 27, 2022