react-slider-range 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Nihar Mondal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # React Range Slider
2
+
3
+ > A lightweight, accessible React range slider component with dual-thumb support.
4
+ > Built with React + TypeScript, supports controlled and uncontrolled usage, keyboard navigation, touch/mouse interaction, and full styling control.
5
+
6
+ This package is designed as a modern alternative to react-slider with minimal API surface and zero external dependencies.
7
+
8
+ ## ✨ Features
9
+
10
+ - Dual-thumb range slider
11
+ - Controlled & uncontrolled modes
12
+ - Keyboard accessible (ARIA compliant)
13
+ - Mouse & touch support
14
+ - Customizable styles via class names
15
+ - Optional tooltip support
16
+ - Written in TypeScript
17
+ - No external UI dependencies
18
+
19
+ ### 📦 Installation
20
+
21
+ ```bash
22
+ npm install react-slider-input
23
+ # or
24
+ yarn add react-slider-input
25
+ # or
26
+ pnpm add react-slider-input
27
+ ```
28
+
29
+ ### 🚀 Usage
30
+
31
+ **1. Basic Example**
32
+
33
+ ```bash
34
+ import { Slider } from "react-slider-input";
35
+
36
+ export default function Example() {
37
+ return (
38
+ <Slider
39
+ min={0}
40
+ max={100}
41
+ defaultValue={[20, 80]}
42
+ onChange={(value) => console.log(value)}
43
+ />
44
+ );
45
+ }
46
+ ```
47
+
48
+ **2.Controlled Example**
49
+
50
+ ```bash
51
+ import { useState } from "react";
52
+ import { Slider } from "react-slider-input";
53
+
54
+ export default function ControlledExample() {
55
+ const [value, setValue] = useState<[number, number]>([30, 70]);
56
+
57
+ return (
58
+ <Slider
59
+ min={0}
60
+ max={100}
61
+ value={value}
62
+ onChange={setValue}
63
+ onChangeCommitted={(val) => console.log("Committed:", val)}
64
+ />
65
+ );
66
+ }
67
+ ```
68
+
69
+ ### 🧩 Props
70
+
71
+ | Prop | Type | Default | Description |
72
+ | ------------------- | ------------------- | ------------------ | ---------------------------- |
73
+ | `min` | `number` | `0` | Minimum slider value |
74
+ | `max` | `number` | `100` | Maximum slider value |
75
+ | `step` | `number` | `1` | Step increment |
76
+ | `defaultValue` | `[number, number]` | `[min, max]` | Initial range (uncontrolled) |
77
+ | `value` | `[number, number]` | — | Controlled value |
78
+ | `onChange` | `(value) => void` | — | Fires on value change |
79
+ | `onChangeCommitted` | `(value) => void` | — | Fires on drag end |
80
+ | `disabled` | `boolean` | `false` | Disable slider |
81
+ | `className` | `string` | `""` | Wrapper class |
82
+ | `trackClassName` | `string` | `""` | Track class |
83
+ | `rangeClassName` | `string` | `""` | Active range class |
84
+ | `thumbClassName` | `string` | `""` | Thumb class |
85
+ | `showTooltip` | `boolean` | `false` | Show value tooltip |
86
+ | `formatTooltip` | `(value) => string` | `value.toString()` | Tooltip formatter |
87
+
88
+ ### ♿ Accessibility
89
+
90
+ - Fully keyboard navigable (ArrowLeft / ArrowRight)
91
+ - Proper role="slider" and ARIA attributes
92
+ - Screen-reader friendly
93
+ - Focusable thumbs
94
+
95
+ ### 🧠 Why This Slider?
96
+
97
+ - Smaller API than react-slider
98
+ - No external dependencies
99
+ - Better TypeScript support
100
+ - Easier styling
101
+ - Built for modern React (hooks, forwardRef)
102
+
103
+ ### 🛠 Tech Stack
104
+
105
+ - React
106
+ - TypeScript
107
+ - Hooks
108
+ - Forward refs
109
+ - ARIA accessibility
110
+
111
+ ### 📄 License
112
+
113
+ MIT © Nihar Mondal
package/dist/index.cjs ADDED
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Slider: () => Slider
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/Slider.tsx
28
+ var import_react = require("react");
29
+ var import_jsx_runtime = require("react/jsx-runtime");
30
+ var Slider = (0, import_react.forwardRef)(
31
+ ({
32
+ min = 0,
33
+ max = 100,
34
+ step = 1,
35
+ defaultValue = [min, max],
36
+ value,
37
+ onChange,
38
+ onChangeCommitted,
39
+ disabled = false,
40
+ className = "",
41
+ trackClassName = "",
42
+ rangeClassName = "",
43
+ thumbClassName = "",
44
+ showTooltip = false,
45
+ formatTooltip = (val) => val.toString()
46
+ }, ref) => {
47
+ const isControlled = value !== void 0;
48
+ const [localValue, setLocalValue] = (0, import_react.useState)(
49
+ isControlled ? value : defaultValue
50
+ );
51
+ const currentValue = isControlled ? value : localValue;
52
+ const [activeThumb, setActiveThumb] = (0, import_react.useState)(null);
53
+ const trackRef = (0, import_react.useRef)(null);
54
+ (0, import_react.useEffect)(() => {
55
+ if (isControlled) setLocalValue(value);
56
+ }, [isControlled, value]);
57
+ const clamp = (0, import_react.useCallback)(
58
+ (val) => Math.min(max, Math.max(min, val)),
59
+ [min, max]
60
+ );
61
+ const getPercentage = (val) => (val - min) / (max - min) * 100;
62
+ const updateValue = (0, import_react.useCallback)(
63
+ (next) => {
64
+ const sorted = [
65
+ Math.min(next[0], next[1]),
66
+ Math.max(next[0], next[1])
67
+ ];
68
+ if (!isControlled) setLocalValue(sorted);
69
+ onChange?.(sorted);
70
+ },
71
+ [isControlled, onChange]
72
+ );
73
+ const commit = (0, import_react.useCallback)(() => {
74
+ onChangeCommitted?.(currentValue);
75
+ }, [onChangeCommitted, currentValue]);
76
+ const startDrag = (index) => {
77
+ if (!disabled) setActiveThumb(index);
78
+ };
79
+ const handlePointerMove = (0, import_react.useCallback)(
80
+ (clientX) => {
81
+ if (activeThumb === null) return;
82
+ const getValueFromPosition = (clientX2) => {
83
+ if (!trackRef.current) return min;
84
+ const rect = trackRef.current.getBoundingClientRect();
85
+ const percent = (clientX2 - rect.left) / rect.width;
86
+ const raw = percent * (max - min) + min;
87
+ return clamp(Math.round(raw / step) * step);
88
+ };
89
+ const updated = [...currentValue];
90
+ updated[activeThumb] = getValueFromPosition(clientX);
91
+ updateValue(updated);
92
+ },
93
+ [activeThumb, currentValue, updateValue, min, max, step, clamp]
94
+ );
95
+ (0, import_react.useEffect)(() => {
96
+ if (activeThumb === null) return;
97
+ const onMove = (e) => handlePointerMove(e.clientX);
98
+ const onTouchMove = (e) => handlePointerMove(e.touches[0].clientX);
99
+ const onEnd = () => {
100
+ commit();
101
+ setActiveThumb(null);
102
+ };
103
+ document.addEventListener("mousemove", onMove);
104
+ document.addEventListener("mouseup", onEnd);
105
+ document.addEventListener("touchmove", onTouchMove);
106
+ document.addEventListener("touchend", onEnd);
107
+ return () => {
108
+ document.removeEventListener("mousemove", onMove);
109
+ document.removeEventListener("mouseup", onEnd);
110
+ document.removeEventListener("touchmove", onTouchMove);
111
+ document.removeEventListener("touchend", onEnd);
112
+ };
113
+ }, [activeThumb, handlePointerMove, commit]);
114
+ const handleKeyDown = (index) => (e) => {
115
+ if (disabled) return;
116
+ let delta = 0;
117
+ if (e.key === "ArrowRight") delta = step;
118
+ if (e.key === "ArrowLeft") delta = -step;
119
+ if (delta !== 0) {
120
+ e.preventDefault();
121
+ const updated = [...currentValue];
122
+ updated[index] = clamp(updated[index] + delta);
123
+ updateValue(updated);
124
+ commit();
125
+ }
126
+ };
127
+ const left = getPercentage(currentValue[0]);
128
+ const right = getPercentage(currentValue[1]);
129
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref, className: `range-slider ${className}`, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
130
+ "div",
131
+ {
132
+ ref: trackRef,
133
+ className: `range-slider-track ${trackClassName}`,
134
+ children: [
135
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
136
+ "div",
137
+ {
138
+ className: `range-slider-range ${rangeClassName}`,
139
+ style: { left: `${left}%`, width: `${right - left}%` }
140
+ }
141
+ ),
142
+ [0, 1].map((i) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
143
+ "div",
144
+ {
145
+ className: `range-slider-thumb ${thumbClassName}`,
146
+ style: {
147
+ left: `${getPercentage(currentValue[i])}%`,
148
+ transform: "translateX(-50%)"
149
+ },
150
+ role: "slider",
151
+ tabIndex: disabled ? -1 : 0,
152
+ "aria-valuemin": min,
153
+ "aria-valuemax": max,
154
+ "aria-valuenow": currentValue[i],
155
+ "aria-disabled": disabled,
156
+ onMouseDown: () => startDrag(i),
157
+ onTouchStart: () => startDrag(i),
158
+ onKeyDown: handleKeyDown(i),
159
+ children: showTooltip && activeThumb === i && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "range-slider-tooltip", children: formatTooltip(currentValue[i]) })
160
+ },
161
+ i
162
+ ))
163
+ ]
164
+ }
165
+ ) });
166
+ }
167
+ );
168
+ // Annotate the CommonJS export names for ESM import in node:
169
+ 0 && (module.exports = {
170
+ Slider
171
+ });
@@ -0,0 +1,21 @@
1
+ import * as react from 'react';
2
+
3
+ interface SliderProps {
4
+ min?: number;
5
+ max?: number;
6
+ step?: number;
7
+ defaultValue?: [number, number];
8
+ value?: [number, number];
9
+ onChange?: (value: [number, number]) => void;
10
+ onChangeCommitted?: (value: [number, number]) => void;
11
+ disabled?: boolean;
12
+ className?: string;
13
+ trackClassName?: string;
14
+ rangeClassName?: string;
15
+ thumbClassName?: string;
16
+ showTooltip?: boolean;
17
+ formatTooltip?: (value: number) => string;
18
+ }
19
+ declare const Slider: react.ForwardRefExoticComponent<SliderProps & react.RefAttributes<HTMLDivElement>>;
20
+
21
+ export { Slider, type SliderProps };
@@ -0,0 +1,21 @@
1
+ import * as react from 'react';
2
+
3
+ interface SliderProps {
4
+ min?: number;
5
+ max?: number;
6
+ step?: number;
7
+ defaultValue?: [number, number];
8
+ value?: [number, number];
9
+ onChange?: (value: [number, number]) => void;
10
+ onChangeCommitted?: (value: [number, number]) => void;
11
+ disabled?: boolean;
12
+ className?: string;
13
+ trackClassName?: string;
14
+ rangeClassName?: string;
15
+ thumbClassName?: string;
16
+ showTooltip?: boolean;
17
+ formatTooltip?: (value: number) => string;
18
+ }
19
+ declare const Slider: react.ForwardRefExoticComponent<SliderProps & react.RefAttributes<HTMLDivElement>>;
20
+
21
+ export { Slider, type SliderProps };
package/dist/index.js ADDED
@@ -0,0 +1,144 @@
1
+ // src/Slider.tsx
2
+ import { useState, useRef, useEffect, useCallback, forwardRef } from "react";
3
+ import { jsx, jsxs } from "react/jsx-runtime";
4
+ var Slider = forwardRef(
5
+ ({
6
+ min = 0,
7
+ max = 100,
8
+ step = 1,
9
+ defaultValue = [min, max],
10
+ value,
11
+ onChange,
12
+ onChangeCommitted,
13
+ disabled = false,
14
+ className = "",
15
+ trackClassName = "",
16
+ rangeClassName = "",
17
+ thumbClassName = "",
18
+ showTooltip = false,
19
+ formatTooltip = (val) => val.toString()
20
+ }, ref) => {
21
+ const isControlled = value !== void 0;
22
+ const [localValue, setLocalValue] = useState(
23
+ isControlled ? value : defaultValue
24
+ );
25
+ const currentValue = isControlled ? value : localValue;
26
+ const [activeThumb, setActiveThumb] = useState(null);
27
+ const trackRef = useRef(null);
28
+ useEffect(() => {
29
+ if (isControlled) setLocalValue(value);
30
+ }, [isControlled, value]);
31
+ const clamp = useCallback(
32
+ (val) => Math.min(max, Math.max(min, val)),
33
+ [min, max]
34
+ );
35
+ const getPercentage = (val) => (val - min) / (max - min) * 100;
36
+ const updateValue = useCallback(
37
+ (next) => {
38
+ const sorted = [
39
+ Math.min(next[0], next[1]),
40
+ Math.max(next[0], next[1])
41
+ ];
42
+ if (!isControlled) setLocalValue(sorted);
43
+ onChange?.(sorted);
44
+ },
45
+ [isControlled, onChange]
46
+ );
47
+ const commit = useCallback(() => {
48
+ onChangeCommitted?.(currentValue);
49
+ }, [onChangeCommitted, currentValue]);
50
+ const startDrag = (index) => {
51
+ if (!disabled) setActiveThumb(index);
52
+ };
53
+ const handlePointerMove = useCallback(
54
+ (clientX) => {
55
+ if (activeThumb === null) return;
56
+ const getValueFromPosition = (clientX2) => {
57
+ if (!trackRef.current) return min;
58
+ const rect = trackRef.current.getBoundingClientRect();
59
+ const percent = (clientX2 - rect.left) / rect.width;
60
+ const raw = percent * (max - min) + min;
61
+ return clamp(Math.round(raw / step) * step);
62
+ };
63
+ const updated = [...currentValue];
64
+ updated[activeThumb] = getValueFromPosition(clientX);
65
+ updateValue(updated);
66
+ },
67
+ [activeThumb, currentValue, updateValue, min, max, step, clamp]
68
+ );
69
+ useEffect(() => {
70
+ if (activeThumb === null) return;
71
+ const onMove = (e) => handlePointerMove(e.clientX);
72
+ const onTouchMove = (e) => handlePointerMove(e.touches[0].clientX);
73
+ const onEnd = () => {
74
+ commit();
75
+ setActiveThumb(null);
76
+ };
77
+ document.addEventListener("mousemove", onMove);
78
+ document.addEventListener("mouseup", onEnd);
79
+ document.addEventListener("touchmove", onTouchMove);
80
+ document.addEventListener("touchend", onEnd);
81
+ return () => {
82
+ document.removeEventListener("mousemove", onMove);
83
+ document.removeEventListener("mouseup", onEnd);
84
+ document.removeEventListener("touchmove", onTouchMove);
85
+ document.removeEventListener("touchend", onEnd);
86
+ };
87
+ }, [activeThumb, handlePointerMove, commit]);
88
+ const handleKeyDown = (index) => (e) => {
89
+ if (disabled) return;
90
+ let delta = 0;
91
+ if (e.key === "ArrowRight") delta = step;
92
+ if (e.key === "ArrowLeft") delta = -step;
93
+ if (delta !== 0) {
94
+ e.preventDefault();
95
+ const updated = [...currentValue];
96
+ updated[index] = clamp(updated[index] + delta);
97
+ updateValue(updated);
98
+ commit();
99
+ }
100
+ };
101
+ const left = getPercentage(currentValue[0]);
102
+ const right = getPercentage(currentValue[1]);
103
+ return /* @__PURE__ */ jsx("div", { ref, className: `range-slider ${className}`, children: /* @__PURE__ */ jsxs(
104
+ "div",
105
+ {
106
+ ref: trackRef,
107
+ className: `range-slider-track ${trackClassName}`,
108
+ children: [
109
+ /* @__PURE__ */ jsx(
110
+ "div",
111
+ {
112
+ className: `range-slider-range ${rangeClassName}`,
113
+ style: { left: `${left}%`, width: `${right - left}%` }
114
+ }
115
+ ),
116
+ [0, 1].map((i) => /* @__PURE__ */ jsx(
117
+ "div",
118
+ {
119
+ className: `range-slider-thumb ${thumbClassName}`,
120
+ style: {
121
+ left: `${getPercentage(currentValue[i])}%`,
122
+ transform: "translateX(-50%)"
123
+ },
124
+ role: "slider",
125
+ tabIndex: disabled ? -1 : 0,
126
+ "aria-valuemin": min,
127
+ "aria-valuemax": max,
128
+ "aria-valuenow": currentValue[i],
129
+ "aria-disabled": disabled,
130
+ onMouseDown: () => startDrag(i),
131
+ onTouchStart: () => startDrag(i),
132
+ onKeyDown: handleKeyDown(i),
133
+ children: showTooltip && activeThumb === i && /* @__PURE__ */ jsx("div", { className: "range-slider-tooltip", children: formatTooltip(currentValue[i]) })
134
+ },
135
+ i
136
+ ))
137
+ ]
138
+ }
139
+ ) });
140
+ }
141
+ );
142
+ export {
143
+ Slider
144
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "react-slider-range",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "description": "A small, configurable React range slider component.",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/NiharMondal/react-slider-input.git"
15
+ },
16
+ "homepage": "https://github.com/NiharMondal/react-slider-input#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/NiharMondal/react-slider-input/issues"
19
+ },
20
+ "keywords": [
21
+ "react",
22
+ "slider",
23
+ "range",
24
+ "component",
25
+ "react-slider",
26
+ "react-range",
27
+ "range-slider",
28
+ "slider-range",
29
+ "multi-slider",
30
+ "ui",
31
+ "component-library"
32
+ ],
33
+ "author": "Nihar Mondal <developernihar@gmail.com>",
34
+ "scripts": {
35
+ "dev": "vite",
36
+ "build": "tsup src/index.ts --format esm,cjs --dts --tsconfig tsconfig.app.json",
37
+ "lint": "eslint .",
38
+ "preview": "vite preview"
39
+ },
40
+ "dependencies": {
41
+ "@tailwindcss/vite": "^4.1.16",
42
+ "react": "^19.1.1",
43
+ "react-dom": "^19.1.1",
44
+ "tailwindcss": "^4.1.16"
45
+ },
46
+ "devDependencies": {
47
+ "@eslint/js": "^9.36.0",
48
+ "@types/node": "^24.6.0",
49
+ "@types/react": "^19.1.16",
50
+ "@types/react-dom": "^19.1.9",
51
+ "@vitejs/plugin-react": "^5.0.4",
52
+ "eslint": "^9.36.0",
53
+ "eslint-plugin-react-hooks": "^5.2.0",
54
+ "eslint-plugin-react-refresh": "^0.4.22",
55
+ "globals": "^16.4.0",
56
+ "tsup": "^8.5.0",
57
+ "typescript": "~5.9.3",
58
+ "typescript-eslint": "^8.45.0",
59
+ "vite": "^7.1.7"
60
+ }
61
+ }