Lightweight JavaScript range slider with no dependencies
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for creating and initializing slider instances on DOM elements with comprehensive configuration options.
Creates a new slider instance on the specified DOM element.
/**
* Creates a new slider instance on the target element
* @param target - DOM element to convert to slider
* @param options - Configuration options for the slider
* @returns Slider API object with methods and properties
* @throws Error if target is invalid or already initialized
*/
noUiSlider.create(target: HTMLElement, options: SliderOptions): SliderAPI;Usage Examples:
import noUiSlider from "nouislider";
// Basic single-handle slider
const singleSlider = document.getElementById('single-slider');
noUiSlider.create(singleSlider, {
start: 50,
range: {
'min': 0,
'max': 100
}
});
// Dual-handle range slider
const rangeSlider = document.getElementById('range-slider');
noUiSlider.create(rangeSlider, {
start: [20, 80],
connect: true,
range: {
'min': 0,
'max': 100
}
});
// Advanced slider with stepping and formatting
const advancedSlider = document.getElementById('advanced-slider');
noUiSlider.create(advancedSlider, {
start: [1000, 3000],
connect: true,
step: 100,
range: {
'min': 0,
'max': 5000
},
format: {
to: function(value) {
return '$' + Math.round(value);
},
from: function(value) {
return Number(value.replace('$', ''));
}
}
});Initial value(s) for the slider handles.
start: number | number[];start: 50start: [20, 80]Defines the minimum and maximum values, with optional stepping.
range: RangeConfig;
interface RangeConfig {
min: number | [number, number]; // Minimum value, optionally with step
max: number | [number, number]; // Maximum value, optionally with step
[position: string]: number | [number, number]; // Intermediate positions
}Examples:
// Basic linear range
range: {
'min': 0,
'max': 100
}
// Range with stepping
range: {
'min': [0, 10], // Min value 0, step size 10
'50%': [500, 50], // At 50% position, value 500, step size 50
'max': 1000
}
// Non-linear range
range: {
'min': 0,
'10%': 50,
'50%': 500,
'max': 2000
}Controls visual connection between handles.
connect?: boolean | string | boolean[];false: No connectionstrue: Connect all handles'lower': Connect from start to first handle'upper': Connect from last handle to end[false, true, false] - Connect specific segmentsText direction affecting slider orientation.
direction?: 'ltr' | 'rtl'; // Default: 'ltr'Physical orientation of the slider.
orientation?: 'horizontal' | 'vertical'; // Default: 'horizontal'Defines interaction behaviors as space-separated string.
behaviour?: string; // Default: 'tap'Available behaviors:
'tap': Click to move handles'drag': Drag handles and ranges'fixed': Fixed distance between handles'snap': Snap to step values'hover': Fire hover events'unconstrained': Allow handles to crossExamples:
behaviour: 'tap' // Click only
behaviour: 'tap-drag' // Click and drag
behaviour: 'tap-drag-fixed' // Fixed distance dragging
behaviour: 'unconstrained-tap' // Handles can crossStep size for value increments.
step?: number;Minimum distance between handles.
margin?: number;Maximum distance between handles.
limit?: number;Padding from slider edges.
padding?: number | number[]; // Single value or [start, end]Force values to snap to steps.
snap?: boolean; // Default: falseEnable smooth animations.
animate?: boolean; // Default: trueAnimation duration in milliseconds.
animationDuration?: number; // Default: 300Value formatting for display.
format?: Formatter;
interface Formatter {
to(value: number): string; // Format for display
from(value: string): number; // Parse from string
}Accessibility formatting (defaults to format if not provided).
ariaFormat?: Formatter;Example:
format: {
to: function(value) {
return value.toFixed(2) + '%';
},
from: function(value) {
return parseFloat(value.replace('%', ''));
}
}Enable and configure tooltips.
tooltips?: boolean | Formatter | (boolean | Formatter)[];true: Enable default tooltipsfalse: Disable tooltipsScale markers configuration.
pips?: PipsConfig;
interface PipsConfig {
mode: 'range' | 'steps' | 'positions' | 'count' | 'values';
values?: number[];
stepped?: boolean;
density?: number;
filter?: (value: number, type: number) => number;
format?: Formatter;
}Enable keyboard navigation.
keyboardSupport?: boolean; // Default: trueMultiplier for page up/down keys.
keyboardPageMultiplier?: number; // Default: 5Default step percentage for keyboard navigation.
keyboardDefaultStep?: number; // Default: 10CSS class prefix for all elements.
cssPrefix?: string | false; // Default: 'noUi-'Custom CSS class names.
cssClasses?: CSSClasses;Custom document element for event handling.
documentElement?: HTMLElement;Removes all slider functionality and cleans up DOM elements.
/**
* Destroy the slider instance and clean up
* @returns void
*/
slider.noUiSlider.destroy(): void;Usage Examples:
// Clean up slider before removing element
const slider = document.getElementById('my-slider');
noUiSlider.create(slider, options);
// Later, when no longer needed
slider.noUiSlider.destroy();
// Now the element can be safely removed or reused
slider.remove();
// Or create a new slider on the same element
noUiSlider.create(slider, newOptions);What destroy() does:
noUiSlider property from the target elementThe create method throws errors for invalid configurations:
// Invalid target
noUiSlider.create(null, options); // Error: create requires a single element
// Already initialized
noUiSlider.create(element, options); // First call succeeds
noUiSlider.create(element, options); // Error: Slider was already initialized
// Invalid range
noUiSlider.create(element, {
start: 50,
range: { min: 100, max: 0 } // Error: Invalid range configuration
});Install with Tessl CLI
npx tessl i tessl/npm-nouislider