react-circular-range 1.0.0
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 +21 -0
- package/README.md +194 -0
- package/dist/index.css +110 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +159 -0
- package/dist/index.mjs +137 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yan
|
|
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,194 @@
|
|
|
1
|
+
# react-circular-range
|
|
2
|
+
|
|
3
|
+
A compact, touch- and mouse-friendly circular (rotary) range knob React component with CSS-module theming. Supports double-click to reset, and works well inside instrument UIs (pedals, mixers, synth panels).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
* Double-click to reset to mid value
|
|
8
|
+
* Themeable via CSS modules
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install react-circular-range
|
|
14
|
+
# or
|
|
15
|
+
yarn add react-circular-range
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Types
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
type StyleSlots =
|
|
22
|
+
| 'knob'
|
|
23
|
+
| 'knobRing'
|
|
24
|
+
| 'knobBackground'
|
|
25
|
+
| 'knobBackgroundPath'
|
|
26
|
+
| 'knobIndicator'
|
|
27
|
+
| 'knobCenter'
|
|
28
|
+
| 'active';
|
|
29
|
+
|
|
30
|
+
export type CircularRangeThemeProps = Partial<Record<StyleSlots, string>>;
|
|
31
|
+
|
|
32
|
+
export interface CircularRangeProps {
|
|
33
|
+
// defaults: 0 / 100
|
|
34
|
+
min?: number;
|
|
35
|
+
max?: number;
|
|
36
|
+
|
|
37
|
+
value?: number;
|
|
38
|
+
step?: number;
|
|
39
|
+
|
|
40
|
+
onChange?: (value: number) => void;
|
|
41
|
+
|
|
42
|
+
// optional CSS module object mapping slot names (knob, knobRing, knobBackground, etc.) to class names
|
|
43
|
+
theme?: CircularRangeThemeProps;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Simple example with pedal
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { CircularRange } from "react-circular-range";
|
|
51
|
+
import React, { useState } from "react";
|
|
52
|
+
|
|
53
|
+
export const SimplePedal: React.FC = () => {
|
|
54
|
+
const [params, setParams] = useState({
|
|
55
|
+
gain: 50,
|
|
56
|
+
freq: 1000,
|
|
57
|
+
min: 25,
|
|
58
|
+
drive: 0,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const update = (key: keyof typeof params) => (v: number) => setParams(p => ({ ...p, [key]: v }));
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div style={{ display: 'flex', gap: '3rem', justifyContent: 'center', padding: '4rem' }}>
|
|
65
|
+
<Knob label="GAIN" value={params.gain} unit="%" onChange={update('gain')}/>
|
|
66
|
+
<Knob label="FREQ" value={params.freq} unit="Hz" onChange={update('freq')} format={f => f >= 1000 ? `${(f/1000).toFixed(1)}k` : f.toString()} />
|
|
67
|
+
<Knob label="MIN" value={params.min} unit="%" onChange={update('min')} />
|
|
68
|
+
<Knob label="DRIVE" value={params.drive} unit="dB" onChange={update('drive')} format={d => `${d > 0 ? '+' : ''}${d}`} />
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const Knob: React.FC<{ label: string; value: number; unit: string; onChange: (v: number) => void; format?: (v: number) => string }> = ({ label, value, unit, onChange, format = String }) => (
|
|
74
|
+
<div style={{ textAlign: 'center' }}>
|
|
75
|
+
<CircularRange
|
|
76
|
+
min={unit === 'Hz' ? 20 : unit === 'dB' ? -20 : 0}
|
|
77
|
+
max={unit === 'Hz' ? 20000 : unit === 'dB' ? 20 : 100}
|
|
78
|
+
step={unit === 'Hz' ? 10 : 1}
|
|
79
|
+
value={value}
|
|
80
|
+
onChange={onChange}
|
|
81
|
+
/>
|
|
82
|
+
<div style={{ marginTop: '1rem', color: '#fff', fontFamily: 'monospace' }}>
|
|
83
|
+
<div style={{ fontSize: '0.75rem', opacity: 0.7 }}>{label}</div>
|
|
84
|
+
<div style={{ fontSize: '1.1rem', fontWeight: 'bold' }}>{format(value)}{unit}</div>
|
|
85
|
+
</div>
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
export const App = () => {
|
|
90
|
+
return <SimplePedal />
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Example with style override (neon)
|
|
95
|
+
|
|
96
|
+
```css
|
|
97
|
+
/* neon.module.css */
|
|
98
|
+
.knob {
|
|
99
|
+
width: 70px;
|
|
100
|
+
height: 70px;
|
|
101
|
+
position: relative;
|
|
102
|
+
cursor: pointer;
|
|
103
|
+
}
|
|
104
|
+
.knobRing {
|
|
105
|
+
width: 100%;
|
|
106
|
+
height: 100%;
|
|
107
|
+
border: 2px solid #00ffcc;
|
|
108
|
+
border-radius: 50%;
|
|
109
|
+
background: #0a0a0a;
|
|
110
|
+
box-shadow: 0 0 10px rgba(0, 255, 204, 0.5);
|
|
111
|
+
}
|
|
112
|
+
.knobBackground {
|
|
113
|
+
position: absolute;
|
|
114
|
+
top: 0;
|
|
115
|
+
left: 0;
|
|
116
|
+
width: 100%;
|
|
117
|
+
height: 100%;
|
|
118
|
+
pointer-events: none;
|
|
119
|
+
transform: rotate(-135deg);
|
|
120
|
+
}
|
|
121
|
+
.knobBackgroundPath {
|
|
122
|
+
fill: none;
|
|
123
|
+
stroke: #00ffcc;
|
|
124
|
+
stroke-width: 3;
|
|
125
|
+
stroke-linecap: round;
|
|
126
|
+
stroke-dasharray: 113.1;
|
|
127
|
+
stroke-dashoffset: 113.1;
|
|
128
|
+
filter: drop-shadow(0 0 3px rgba(0, 255, 204, 0.9));
|
|
129
|
+
}
|
|
130
|
+
.knobIndicator {
|
|
131
|
+
position: absolute;
|
|
132
|
+
width: 4px;
|
|
133
|
+
height: 22px;
|
|
134
|
+
background: linear-gradient(to bottom, #00ffcc, #00b3ff);
|
|
135
|
+
top: 4px;
|
|
136
|
+
left: 50%;
|
|
137
|
+
transform-origin: center 31px;
|
|
138
|
+
transform: translateX(-50%) rotate(-135deg);
|
|
139
|
+
border-radius: 1px;
|
|
140
|
+
box-shadow: 0 0 8px rgba(0, 255, 204, 0.8);
|
|
141
|
+
}
|
|
142
|
+
.knobCenter {
|
|
143
|
+
position: absolute;
|
|
144
|
+
width: 20px;
|
|
145
|
+
height: 20px;
|
|
146
|
+
background: #000000;
|
|
147
|
+
border-radius: 50%;
|
|
148
|
+
top: 50%;
|
|
149
|
+
left: 50%;
|
|
150
|
+
transform: translate(-50%, -50%);
|
|
151
|
+
box-shadow: 0 0 5px rgba(0, 255, 204, 0.5);
|
|
152
|
+
}
|
|
153
|
+
/* Hover states */
|
|
154
|
+
.knob:hover .knobRing {
|
|
155
|
+
border-color: #00ff88;
|
|
156
|
+
box-shadow: 0 0 15px rgba(0, 255, 204, 0.7);
|
|
157
|
+
}
|
|
158
|
+
.knob:hover .knobIndicator {
|
|
159
|
+
background: linear-gradient(to bottom, #00ff88, #00ccff);
|
|
160
|
+
box-shadow: 0 0 12px rgba(0, 255, 204, 1);
|
|
161
|
+
}
|
|
162
|
+
/* Active states */
|
|
163
|
+
.knob.active .knobRing {
|
|
164
|
+
border-color: #00ff88;
|
|
165
|
+
box-shadow: 0 0 20px rgba(0, 255, 204, 0.9);
|
|
166
|
+
}
|
|
167
|
+
.knob.active .knobIndicator {
|
|
168
|
+
background: linear-gradient(to bottom, #00ff88, #00ccff);
|
|
169
|
+
box-shadow: 0 0 15px rgba(0, 255, 204, 1);
|
|
170
|
+
}
|
|
171
|
+
.knob.active .knobBackgroundPath {
|
|
172
|
+
stroke: #00ff88;
|
|
173
|
+
filter: drop-shadow(0 0 5px rgba(0, 255, 204, 1));
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Import and pass as a prop
|
|
178
|
+
|
|
179
|
+
```tsx
|
|
180
|
+
import neonStyles from "./neon.module.css"
|
|
181
|
+
|
|
182
|
+
// ...
|
|
183
|
+
|
|
184
|
+
<CircularRange
|
|
185
|
+
min={unit === 'Hz' ? 20 : unit === 'dB' ? -20 : 0}
|
|
186
|
+
max={unit === 'Hz' ? 20000 : unit === 'dB' ? 20 : 100}
|
|
187
|
+
step={unit === 'Hz' ? 10 : 1}
|
|
188
|
+
value={value}
|
|
189
|
+
onChange={onChange}
|
|
190
|
+
|
|
191
|
+
theme={neonStyles} // <---
|
|
192
|
+
/>
|
|
193
|
+
|
|
194
|
+
```
|
package/dist/index.css
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/* src/CircularRange.module.css */
|
|
2
|
+
.knob {
|
|
3
|
+
width: 65px;
|
|
4
|
+
height: 65px;
|
|
5
|
+
position: relative;
|
|
6
|
+
cursor: pointer;
|
|
7
|
+
user-select: none;
|
|
8
|
+
}
|
|
9
|
+
.knobRing {
|
|
10
|
+
width: 100%;
|
|
11
|
+
height: 100%;
|
|
12
|
+
border: 3px solid #333;
|
|
13
|
+
border-radius: 50%;
|
|
14
|
+
background:
|
|
15
|
+
linear-gradient(
|
|
16
|
+
145deg,
|
|
17
|
+
#1a1a1a,
|
|
18
|
+
#252525);
|
|
19
|
+
box-shadow:
|
|
20
|
+
inset 2px 2px 5px rgba(0, 0, 0, 0.5),
|
|
21
|
+
inset -2px -2px 5px rgba(60, 60, 60, 0.1),
|
|
22
|
+
0 4px 8px rgba(0, 0, 0, 0.3);
|
|
23
|
+
transition: border-color 0.2s ease, box-shadow 0.2s ease;
|
|
24
|
+
}
|
|
25
|
+
.knobBackground {
|
|
26
|
+
position: absolute;
|
|
27
|
+
top: 0;
|
|
28
|
+
left: 0;
|
|
29
|
+
width: 100%;
|
|
30
|
+
height: 100%;
|
|
31
|
+
pointer-events: none;
|
|
32
|
+
transform: rotate(-135deg);
|
|
33
|
+
}
|
|
34
|
+
.knobBackgroundPath {
|
|
35
|
+
fill: none;
|
|
36
|
+
stroke: #8b5cf6;
|
|
37
|
+
stroke-width: 3.5;
|
|
38
|
+
stroke-linecap: round;
|
|
39
|
+
stroke-dasharray: 113.1;
|
|
40
|
+
stroke-dashoffset: 113.1;
|
|
41
|
+
transition: stroke-dashoffset 0.1s ease-out;
|
|
42
|
+
filter: drop-shadow(0 0 3px rgba(139, 92, 246, 0.7));
|
|
43
|
+
}
|
|
44
|
+
.knobIndicator {
|
|
45
|
+
position: absolute;
|
|
46
|
+
width: 4px;
|
|
47
|
+
height: 20px;
|
|
48
|
+
background:
|
|
49
|
+
linear-gradient(
|
|
50
|
+
to bottom,
|
|
51
|
+
#8b5cf6,
|
|
52
|
+
#c084fc);
|
|
53
|
+
top: 5px;
|
|
54
|
+
left: 50%;
|
|
55
|
+
transform-origin: center 27.5px;
|
|
56
|
+
transform: translateX(-50%) rotate(-135deg);
|
|
57
|
+
border-radius: 2px;
|
|
58
|
+
z-index: 2;
|
|
59
|
+
box-shadow: 0 0 6px rgba(139, 92, 246, 0.8);
|
|
60
|
+
}
|
|
61
|
+
.knobCenter {
|
|
62
|
+
position: absolute;
|
|
63
|
+
width: 18px;
|
|
64
|
+
height: 18px;
|
|
65
|
+
background:
|
|
66
|
+
radial-gradient(
|
|
67
|
+
circle,
|
|
68
|
+
#0a0a0a,
|
|
69
|
+
#1a1a1a);
|
|
70
|
+
border-radius: 50%;
|
|
71
|
+
top: 50%;
|
|
72
|
+
left: 50%;
|
|
73
|
+
transform: translate(-50%, -50%);
|
|
74
|
+
box-shadow: inset 1px 1px 3px rgba(0, 0, 0, 0.7), 0 1px 2px rgba(139, 92, 246, 0.3);
|
|
75
|
+
z-index: 3;
|
|
76
|
+
}
|
|
77
|
+
.knob:hover .knobRing {
|
|
78
|
+
border-color: #8b5cf6;
|
|
79
|
+
box-shadow:
|
|
80
|
+
inset 2px 2px 5px rgba(0, 0, 0, 0.5),
|
|
81
|
+
inset -2px -2px 5px rgba(60, 60, 60, 0.1),
|
|
82
|
+
0 4px 12px rgba(139, 92, 246, 0.4);
|
|
83
|
+
}
|
|
84
|
+
.knob:hover .knobIndicator {
|
|
85
|
+
background:
|
|
86
|
+
linear-gradient(
|
|
87
|
+
to bottom,
|
|
88
|
+
#a78bfa,
|
|
89
|
+
#c084fc);
|
|
90
|
+
box-shadow: 0 0 8px rgba(167, 139, 250, 0.9);
|
|
91
|
+
}
|
|
92
|
+
.knob.active .knobRing {
|
|
93
|
+
border-color: #a78bfa;
|
|
94
|
+
box-shadow:
|
|
95
|
+
inset 2px 2px 5px rgba(0, 0, 0, 0.6),
|
|
96
|
+
inset -2px -2px 5px rgba(60, 60, 60, 0.15),
|
|
97
|
+
0 4px 16px rgba(167, 139, 250, 0.5);
|
|
98
|
+
}
|
|
99
|
+
.knob.active .knobIndicator {
|
|
100
|
+
background:
|
|
101
|
+
linear-gradient(
|
|
102
|
+
to bottom,
|
|
103
|
+
#a78bfa,
|
|
104
|
+
#c084fc);
|
|
105
|
+
box-shadow: 0 0 10px rgba(167, 139, 250, 1);
|
|
106
|
+
}
|
|
107
|
+
.knob.active .knobBackgroundPath {
|
|
108
|
+
stroke: #a78bfa;
|
|
109
|
+
filter: drop-shadow(0 0 4px rgba(167, 139, 250, 0.9));
|
|
110
|
+
}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type StyleSlots = 'knob' | 'knobRing' | 'knobBackground' | 'knobBackgroundPath' | 'knobIndicator' | 'knobCenter' | 'active';
|
|
4
|
+
type CircularRangeThemeProps = Partial<Record<StyleSlots, string>>;
|
|
5
|
+
interface CircularRangeProps {
|
|
6
|
+
min?: number;
|
|
7
|
+
max?: number;
|
|
8
|
+
value?: number;
|
|
9
|
+
step?: number;
|
|
10
|
+
onChange?: (value: number) => void;
|
|
11
|
+
theme?: CircularRangeThemeProps;
|
|
12
|
+
}
|
|
13
|
+
declare const CircularRange: React.FC<CircularRangeProps>;
|
|
14
|
+
|
|
15
|
+
export { CircularRange, type CircularRangeProps, type CircularRangeThemeProps, type StyleSlots };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type StyleSlots = 'knob' | 'knobRing' | 'knobBackground' | 'knobBackgroundPath' | 'knobIndicator' | 'knobCenter' | 'active';
|
|
4
|
+
type CircularRangeThemeProps = Partial<Record<StyleSlots, string>>;
|
|
5
|
+
interface CircularRangeProps {
|
|
6
|
+
min?: number;
|
|
7
|
+
max?: number;
|
|
8
|
+
value?: number;
|
|
9
|
+
step?: number;
|
|
10
|
+
onChange?: (value: number) => void;
|
|
11
|
+
theme?: CircularRangeThemeProps;
|
|
12
|
+
}
|
|
13
|
+
declare const CircularRange: React.FC<CircularRangeProps>;
|
|
14
|
+
|
|
15
|
+
export { CircularRange, type CircularRangeProps, type CircularRangeThemeProps, type StyleSlots };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
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
|
+
CircularRange: () => CircularRange
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/CircularRange.tsx
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
|
|
30
|
+
// src/CircularRange.module.css
|
|
31
|
+
var CircularRange_default = {};
|
|
32
|
+
|
|
33
|
+
// src/CircularRange.tsx
|
|
34
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
35
|
+
var ARC_LENGTH = 113.1;
|
|
36
|
+
var CircularRange = ({
|
|
37
|
+
min = 0,
|
|
38
|
+
max = 100,
|
|
39
|
+
value = 0,
|
|
40
|
+
step = 0.01,
|
|
41
|
+
onChange,
|
|
42
|
+
theme = CircularRange_default
|
|
43
|
+
}) => {
|
|
44
|
+
const knobRef = (0, import_react.useRef)(null);
|
|
45
|
+
const [internalValue, setInternalValue] = (0, import_react.useState)(value);
|
|
46
|
+
const [isActive, setIsActive] = (0, import_react.useState)(false);
|
|
47
|
+
const startY = (0, import_react.useRef)(0);
|
|
48
|
+
const startValue = (0, import_react.useRef)(0);
|
|
49
|
+
const sensitivity = 0.01;
|
|
50
|
+
(0, import_react.useEffect)(() => {
|
|
51
|
+
setInternalValue(value);
|
|
52
|
+
}, [value]);
|
|
53
|
+
const clamp = (v) => Math.min(max, Math.max(min, v));
|
|
54
|
+
const getNormalizedValue = (0, import_react.useCallback)(
|
|
55
|
+
(v) => {
|
|
56
|
+
return (v - min) / (max - min);
|
|
57
|
+
},
|
|
58
|
+
[min, max]
|
|
59
|
+
);
|
|
60
|
+
const setValue = (0, import_react.useCallback)(
|
|
61
|
+
(v) => {
|
|
62
|
+
let next = clamp(v);
|
|
63
|
+
if (step > 0) {
|
|
64
|
+
next = Math.round(next / step) * step;
|
|
65
|
+
}
|
|
66
|
+
setInternalValue(next);
|
|
67
|
+
onChange?.(next);
|
|
68
|
+
},
|
|
69
|
+
[min, max, step, onChange]
|
|
70
|
+
);
|
|
71
|
+
const handleStart = (e) => {
|
|
72
|
+
e.preventDefault();
|
|
73
|
+
setIsActive(true);
|
|
74
|
+
const clientY = "touches" in e ? e.touches[0].clientY : e.clientY;
|
|
75
|
+
startY.current = clientY;
|
|
76
|
+
startValue.current = internalValue;
|
|
77
|
+
};
|
|
78
|
+
const handleMove = (e) => {
|
|
79
|
+
if (!isActive) return;
|
|
80
|
+
e.preventDefault();
|
|
81
|
+
const clientY = "touches" in e ? e.touches[0].clientY : e.clientY;
|
|
82
|
+
const deltaY = startY.current - clientY;
|
|
83
|
+
const deltaValue = deltaY * sensitivity * (max - min);
|
|
84
|
+
setValue(startValue.current + deltaValue);
|
|
85
|
+
};
|
|
86
|
+
const handleEnd = () => {
|
|
87
|
+
setIsActive(false);
|
|
88
|
+
};
|
|
89
|
+
const handleDoubleClick = () => {
|
|
90
|
+
setValue((min + max) / 2);
|
|
91
|
+
};
|
|
92
|
+
(0, import_react.useEffect)(() => {
|
|
93
|
+
const el = knobRef.current;
|
|
94
|
+
if (!el) return;
|
|
95
|
+
el.addEventListener("mousedown", handleStart);
|
|
96
|
+
el.addEventListener("touchstart", handleStart);
|
|
97
|
+
document.addEventListener("mousemove", handleMove);
|
|
98
|
+
document.addEventListener("touchmove", handleMove);
|
|
99
|
+
document.addEventListener("mouseup", handleEnd);
|
|
100
|
+
document.addEventListener("touchend", handleEnd);
|
|
101
|
+
el.addEventListener("dblclick", handleDoubleClick);
|
|
102
|
+
return () => {
|
|
103
|
+
el.removeEventListener("mousedown", handleStart);
|
|
104
|
+
el.removeEventListener("touchstart", handleStart);
|
|
105
|
+
document.removeEventListener("mousemove", handleMove);
|
|
106
|
+
document.removeEventListener("touchmove", handleMove);
|
|
107
|
+
document.removeEventListener("mouseup", handleEnd);
|
|
108
|
+
document.removeEventListener("touchend", handleEnd);
|
|
109
|
+
el.removeEventListener("dblclick", handleDoubleClick);
|
|
110
|
+
};
|
|
111
|
+
}, [handleMove, isActive]);
|
|
112
|
+
const normalized = getNormalizedValue(internalValue);
|
|
113
|
+
const rotation = normalized * 270 - 135;
|
|
114
|
+
const dashOffset = ARC_LENGTH - normalized * ARC_LENGTH;
|
|
115
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
116
|
+
"div",
|
|
117
|
+
{
|
|
118
|
+
ref: knobRef,
|
|
119
|
+
className: `${theme.knob} ${isActive ? theme.active : ""}`,
|
|
120
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: `${theme.knobRing}`, children: [
|
|
121
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
122
|
+
"svg",
|
|
123
|
+
{
|
|
124
|
+
className: `${theme.knobBackground}`,
|
|
125
|
+
viewBox: "0 0 60 60",
|
|
126
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
127
|
+
"path",
|
|
128
|
+
{
|
|
129
|
+
className: `${theme.knobBackgroundPath}`,
|
|
130
|
+
d: "M 30 6 A 24 24 0 1 1 6 30",
|
|
131
|
+
strokeDasharray: ARC_LENGTH,
|
|
132
|
+
strokeDashoffset: dashOffset
|
|
133
|
+
}
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
),
|
|
137
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
138
|
+
"div",
|
|
139
|
+
{
|
|
140
|
+
className: `${theme.knobIndicator}`,
|
|
141
|
+
style: {
|
|
142
|
+
transform: `translateX(-50%) rotate(${rotation}deg)`
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
),
|
|
146
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
147
|
+
"div",
|
|
148
|
+
{
|
|
149
|
+
className: `${theme.knobCenter}`
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
] })
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
157
|
+
0 && (module.exports = {
|
|
158
|
+
CircularRange
|
|
159
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// src/CircularRange.tsx
|
|
2
|
+
import {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useRef,
|
|
6
|
+
useState
|
|
7
|
+
} from "react";
|
|
8
|
+
|
|
9
|
+
// src/CircularRange.module.css
|
|
10
|
+
var CircularRange_default = {};
|
|
11
|
+
|
|
12
|
+
// src/CircularRange.tsx
|
|
13
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
14
|
+
var ARC_LENGTH = 113.1;
|
|
15
|
+
var CircularRange = ({
|
|
16
|
+
min = 0,
|
|
17
|
+
max = 100,
|
|
18
|
+
value = 0,
|
|
19
|
+
step = 0.01,
|
|
20
|
+
onChange,
|
|
21
|
+
theme = CircularRange_default
|
|
22
|
+
}) => {
|
|
23
|
+
const knobRef = useRef(null);
|
|
24
|
+
const [internalValue, setInternalValue] = useState(value);
|
|
25
|
+
const [isActive, setIsActive] = useState(false);
|
|
26
|
+
const startY = useRef(0);
|
|
27
|
+
const startValue = useRef(0);
|
|
28
|
+
const sensitivity = 0.01;
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
setInternalValue(value);
|
|
31
|
+
}, [value]);
|
|
32
|
+
const clamp = (v) => Math.min(max, Math.max(min, v));
|
|
33
|
+
const getNormalizedValue = useCallback(
|
|
34
|
+
(v) => {
|
|
35
|
+
return (v - min) / (max - min);
|
|
36
|
+
},
|
|
37
|
+
[min, max]
|
|
38
|
+
);
|
|
39
|
+
const setValue = useCallback(
|
|
40
|
+
(v) => {
|
|
41
|
+
let next = clamp(v);
|
|
42
|
+
if (step > 0) {
|
|
43
|
+
next = Math.round(next / step) * step;
|
|
44
|
+
}
|
|
45
|
+
setInternalValue(next);
|
|
46
|
+
onChange?.(next);
|
|
47
|
+
},
|
|
48
|
+
[min, max, step, onChange]
|
|
49
|
+
);
|
|
50
|
+
const handleStart = (e) => {
|
|
51
|
+
e.preventDefault();
|
|
52
|
+
setIsActive(true);
|
|
53
|
+
const clientY = "touches" in e ? e.touches[0].clientY : e.clientY;
|
|
54
|
+
startY.current = clientY;
|
|
55
|
+
startValue.current = internalValue;
|
|
56
|
+
};
|
|
57
|
+
const handleMove = (e) => {
|
|
58
|
+
if (!isActive) return;
|
|
59
|
+
e.preventDefault();
|
|
60
|
+
const clientY = "touches" in e ? e.touches[0].clientY : e.clientY;
|
|
61
|
+
const deltaY = startY.current - clientY;
|
|
62
|
+
const deltaValue = deltaY * sensitivity * (max - min);
|
|
63
|
+
setValue(startValue.current + deltaValue);
|
|
64
|
+
};
|
|
65
|
+
const handleEnd = () => {
|
|
66
|
+
setIsActive(false);
|
|
67
|
+
};
|
|
68
|
+
const handleDoubleClick = () => {
|
|
69
|
+
setValue((min + max) / 2);
|
|
70
|
+
};
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const el = knobRef.current;
|
|
73
|
+
if (!el) return;
|
|
74
|
+
el.addEventListener("mousedown", handleStart);
|
|
75
|
+
el.addEventListener("touchstart", handleStart);
|
|
76
|
+
document.addEventListener("mousemove", handleMove);
|
|
77
|
+
document.addEventListener("touchmove", handleMove);
|
|
78
|
+
document.addEventListener("mouseup", handleEnd);
|
|
79
|
+
document.addEventListener("touchend", handleEnd);
|
|
80
|
+
el.addEventListener("dblclick", handleDoubleClick);
|
|
81
|
+
return () => {
|
|
82
|
+
el.removeEventListener("mousedown", handleStart);
|
|
83
|
+
el.removeEventListener("touchstart", handleStart);
|
|
84
|
+
document.removeEventListener("mousemove", handleMove);
|
|
85
|
+
document.removeEventListener("touchmove", handleMove);
|
|
86
|
+
document.removeEventListener("mouseup", handleEnd);
|
|
87
|
+
document.removeEventListener("touchend", handleEnd);
|
|
88
|
+
el.removeEventListener("dblclick", handleDoubleClick);
|
|
89
|
+
};
|
|
90
|
+
}, [handleMove, isActive]);
|
|
91
|
+
const normalized = getNormalizedValue(internalValue);
|
|
92
|
+
const rotation = normalized * 270 - 135;
|
|
93
|
+
const dashOffset = ARC_LENGTH - normalized * ARC_LENGTH;
|
|
94
|
+
return /* @__PURE__ */ jsx(
|
|
95
|
+
"div",
|
|
96
|
+
{
|
|
97
|
+
ref: knobRef,
|
|
98
|
+
className: `${theme.knob} ${isActive ? theme.active : ""}`,
|
|
99
|
+
children: /* @__PURE__ */ jsxs("div", { className: `${theme.knobRing}`, children: [
|
|
100
|
+
/* @__PURE__ */ jsx(
|
|
101
|
+
"svg",
|
|
102
|
+
{
|
|
103
|
+
className: `${theme.knobBackground}`,
|
|
104
|
+
viewBox: "0 0 60 60",
|
|
105
|
+
children: /* @__PURE__ */ jsx(
|
|
106
|
+
"path",
|
|
107
|
+
{
|
|
108
|
+
className: `${theme.knobBackgroundPath}`,
|
|
109
|
+
d: "M 30 6 A 24 24 0 1 1 6 30",
|
|
110
|
+
strokeDasharray: ARC_LENGTH,
|
|
111
|
+
strokeDashoffset: dashOffset
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
),
|
|
116
|
+
/* @__PURE__ */ jsx(
|
|
117
|
+
"div",
|
|
118
|
+
{
|
|
119
|
+
className: `${theme.knobIndicator}`,
|
|
120
|
+
style: {
|
|
121
|
+
transform: `translateX(-50%) rotate(${rotation}deg)`
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
),
|
|
125
|
+
/* @__PURE__ */ jsx(
|
|
126
|
+
"div",
|
|
127
|
+
{
|
|
128
|
+
className: `${theme.knobCenter}`
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
] })
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
};
|
|
135
|
+
export {
|
|
136
|
+
CircularRange
|
|
137
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-circular-range",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple VST-like circular slider/range.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/executable1210/react-circular-range.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/executable1210/react-circular-range",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "exec",
|
|
13
|
+
"url": "https://github.com/executable1210"
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.cjs",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"keywords": [
|
|
29
|
+
"slider",
|
|
30
|
+
"round-slider",
|
|
31
|
+
"round-range",
|
|
32
|
+
"circular-slider",
|
|
33
|
+
"circular-range",
|
|
34
|
+
"circle-slider",
|
|
35
|
+
"circe-range"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup src/index.ts --format esm,cjs --dts"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=16 <20",
|
|
42
|
+
"react-dom": ">=16 <20"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/react": "^19.2.7",
|
|
46
|
+
"@types/react-dom": "^19.2.3",
|
|
47
|
+
"tsup": "^8.5.1",
|
|
48
|
+
"typescript": "^5.9.3"
|
|
49
|
+
}
|
|
50
|
+
}
|