noUiSlider: Lightweight JavaScript Range Slider
noUiSlider is an unopinionated, lightweight, and accessible JavaScript range slider library designed for modern web applications. It offers GPU-animated performance, ensuring smooth operation even on older devices, and supports all modern browsers including IE > 9. Its key differentiators include having no external dependencies, full responsiveness, multi-touch support for various mobile platforms, and comprehensive accessibility features with ARIA and keyboard navigation. The current stable version is 15.8.1. The project maintains a regular release cadence, often issuing patch and minor releases to address bug fixes, add new features, and refine TypeScript definitions, as seen in its recent release history (e.g., 15.8.1, 15.8.0, 15.7.2).
Common errors
-
TypeError: noUiSlider.create is not a function
cause Attempting to call `create` on an `undefined` or incorrect import of `noUiSlider`, often due to a wrong import statement or attempting a default import where a namespace import is required.fixEnsure you are using `import * as noUiSlider from 'nouislider';` in ESM or `const noUiSlider = require('nouislider');` in CJS. Also verify the package is correctly installed. -
Slider appears, but is unstyled or broken visually (e.g., handles are not positioned correctly).
cause The noUiSlider CSS stylesheet has not been imported or is not correctly loaded by the bundler.fixAdd `import 'nouislider/dist/nouislider.css';` (or the minified version) to your application's entry point or the component where the slider is used. -
Argument of type '(...)' is not assignable to parameter of type 'Pips'.
cause TypeScript type mismatch when providing options to `noUiSlider.create` or `set`, specifically for the `pips` or `range` options, indicating incorrect structure or missing properties.fixRefer to the noUiSlider documentation and the TypeScript definitions for the correct structure of options like `pips`, `range`, or `connect`. Ensure all required properties are present and correctly typed.
Warnings
- breaking Version 15.5.0 introduced a fix for compositing issues in Safari, which could potentially be a breaking change when using a custom stylesheet. Styles might need adjustment if relying on previous Safari rendering quirks.
- gotcha When using TypeScript, remember that `noUiSlider.create` returns an API object (type `API`). Ensure your imports and variable typings correctly reflect this for proper type inference and checking.
- gotcha noUiSlider requires its CSS file to be imported for correct visual rendering. Forgetting to import the CSS will result in an unstyled, unusable slider element.
- deprecated Older versions of noUiSlider might have been imported using default imports, but modern versions, especially with ESM, explicitly export a namespace object. Default imports can lead to undefined errors.
Install
-
npm install nouislider -
yarn add nouislider -
pnpm add nouislider
Imports
- noUiSlider
import noUiSlider from 'nouislider';
import * as noUiSlider from 'nouislider'; import 'nouislider/dist/nouislider.css';
- nouislider.min.css
import 'nouislider/dist/nouislider.min.css';
- nouislider
const noUiSlider = require('nouislider'); - nouislider type
import type { noUiSliderOptions, API } from 'nouislider';
Quickstart
import * as noUiSlider from 'nouislider';
import 'nouislider/dist/nouislider.css';
const sliderElement = document.createElement('div');
sliderElement.id = 'mySlider';
document.body.appendChild(sliderElement);
const slider = noUiSlider.create(sliderElement, {
start: [20, 80],
connect: true,
range: {
'min': 0,
'max': 100
},
tooltips: true,
pips: {
mode: 'steps',
stepped: true,
density: 4
}
});
slider.on('update', (values, handle) => {
console.log(`Slider updated: Handle ${handle} value: ${values[handle]}`);
});
// Example of setting new values programmatically
setTimeout(() => {
slider.set([30, 70]);
console.log('Slider values set to [30, 70] after 2 seconds.');
}, 2000);