@fab1o978/react-ui 0.1.8 → 0.2.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/dist/components/Badge/index.d.cts +3 -3
- package/dist/components/Badge/index.d.ts +3 -3
- package/dist/components/Button/index.d.cts +3 -3
- package/dist/components/Button/index.d.ts +3 -3
- package/dist/components/ColorPicker/index.d.cts +2 -2
- package/dist/components/ColorPicker/index.d.ts +2 -2
- package/dist/components/ColorWheel/index.cjs +671 -0
- package/dist/components/ColorWheel/index.cjs.map +1 -0
- package/dist/components/ColorWheel/index.css +494 -0
- package/dist/components/ColorWheel/index.css.map +1 -0
- package/dist/components/ColorWheel/index.d.cts +49 -0
- package/dist/components/ColorWheel/index.d.ts +49 -0
- package/dist/components/ColorWheel/index.js +669 -0
- package/dist/components/ColorWheel/index.js.map +1 -0
- package/dist/components/Input/index.d.cts +5 -5
- package/dist/components/Input/index.d.ts +5 -5
- package/dist/components/RainCanvas/index.d.cts +4 -4
- package/dist/components/RainCanvas/index.d.ts +4 -4
- package/dist/components/RichTextEditor/index.d.cts +6 -6
- package/dist/components/RichTextEditor/index.d.ts +6 -6
- package/dist/components/SliderControl/index.d.cts +4 -4
- package/dist/components/SliderControl/index.d.ts +4 -4
- package/dist/components/SlidingCounter/index.d.cts +2 -2
- package/dist/components/SlidingCounter/index.d.ts +2 -2
- package/dist/components/Timeline/index.d.cts +3 -3
- package/dist/components/Timeline/index.d.ts +3 -3
- package/dist/index.cjs +588 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +494 -0
- package/dist/index.css.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +589 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
import { useMemo, useState, useRef, useEffect } from 'react';
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
|
|
4
|
+
// src/components/ColorWheel/ColorWheel.tsx
|
|
5
|
+
|
|
6
|
+
// src/components/ColorWheel/ColorWheel.module.scss
|
|
7
|
+
var ColorWheel_module_default = {
|
|
8
|
+
root: "ColorWheel_module_root2",
|
|
9
|
+
recents: "ColorWheel_module_recents2",
|
|
10
|
+
chipWrap: "ColorWheel_module_chipWrap2",
|
|
11
|
+
chip: "ColorWheel_module_chip2",
|
|
12
|
+
chipRemove: "ColorWheel_module_chipRemove2",
|
|
13
|
+
picker: "ColorWheel_module_picker2",
|
|
14
|
+
glow: "ColorWheel_module_glow2",
|
|
15
|
+
modeToggle: "ColorWheel_module_modeToggle2",
|
|
16
|
+
modePill: "ColorWheel_module_modePill2",
|
|
17
|
+
modeButton: "ColorWheel_module_modeButton2",
|
|
18
|
+
active: "ColorWheel_module_active2",
|
|
19
|
+
stage: "ColorWheel_module_stage2",
|
|
20
|
+
stageShadow: "ColorWheel_module_stageShadow2",
|
|
21
|
+
ring: "ColorWheel_module_ring2",
|
|
22
|
+
hueHandlePivot: "ColorWheel_module_hueHandlePivot2",
|
|
23
|
+
hueHandle: "ColorWheel_module_hueHandle2",
|
|
24
|
+
alphaArcShadowWrap: "ColorWheel_module_alphaArcShadowWrap2",
|
|
25
|
+
alphaArcShadow: "ColorWheel_module_alphaArcShadow2",
|
|
26
|
+
alphaArc: "ColorWheel_module_alphaArc2",
|
|
27
|
+
alphaArcFill: "ColorWheel_module_alphaArcFill2",
|
|
28
|
+
alphaArcCap: "ColorWheel_module_alphaArcCap2",
|
|
29
|
+
alphaArcCapStart: "ColorWheel_module_alphaArcCapStart2",
|
|
30
|
+
alphaArcCapEnd: "ColorWheel_module_alphaArcCapEnd2",
|
|
31
|
+
alphaArcHandle: "ColorWheel_module_alphaArcHandle2",
|
|
32
|
+
currentDisc: "ColorWheel_module_currentDisc2",
|
|
33
|
+
discFill: "ColorWheel_module_discFill2",
|
|
34
|
+
currentHex: "ColorWheel_module_currentHex2",
|
|
35
|
+
currentHexInput: "ColorWheel_module_currentHexInput2",
|
|
36
|
+
copied: "ColorWheel_module_copied2",
|
|
37
|
+
discBadge: "ColorWheel_module_discBadge2",
|
|
38
|
+
discBadgeTop: "ColorWheel_module_discBadgeTop2",
|
|
39
|
+
pulse: "ColorWheel_module_pulse2",
|
|
40
|
+
discBadgeBottom: "ColorWheel_module_discBadgeBottom2"
|
|
41
|
+
};
|
|
42
|
+
function usePointerDrag(ref, onMove, onEnd, onDraggingChange) {
|
|
43
|
+
const onMoveRef = useRef(onMove);
|
|
44
|
+
onMoveRef.current = onMove;
|
|
45
|
+
const onEndRef = useRef(onEnd);
|
|
46
|
+
onEndRef.current = onEnd;
|
|
47
|
+
const onDraggingChangeRef = useRef(onDraggingChange);
|
|
48
|
+
onDraggingChangeRef.current = onDraggingChange;
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
const el = ref.current;
|
|
51
|
+
if (!el) return;
|
|
52
|
+
function pointerPos(e) {
|
|
53
|
+
return { x: e.clientX, y: e.clientY };
|
|
54
|
+
}
|
|
55
|
+
function move(e) {
|
|
56
|
+
onMoveRef.current(pointerPos(e));
|
|
57
|
+
}
|
|
58
|
+
function up() {
|
|
59
|
+
onDraggingChangeRef.current?.(false);
|
|
60
|
+
window.removeEventListener("pointermove", move);
|
|
61
|
+
window.removeEventListener("pointerup", up);
|
|
62
|
+
onEndRef.current?.();
|
|
63
|
+
}
|
|
64
|
+
function down(e) {
|
|
65
|
+
const pe = e;
|
|
66
|
+
pe.preventDefault();
|
|
67
|
+
pe.stopPropagation();
|
|
68
|
+
onDraggingChangeRef.current?.(true);
|
|
69
|
+
onMoveRef.current(pointerPos(pe));
|
|
70
|
+
window.addEventListener("pointermove", move);
|
|
71
|
+
window.addEventListener("pointerup", up);
|
|
72
|
+
}
|
|
73
|
+
el.addEventListener("pointerdown", down);
|
|
74
|
+
return () => {
|
|
75
|
+
el.removeEventListener("pointerdown", down);
|
|
76
|
+
window.removeEventListener("pointermove", move);
|
|
77
|
+
window.removeEventListener("pointerup", up);
|
|
78
|
+
};
|
|
79
|
+
}, [ref]);
|
|
80
|
+
}
|
|
81
|
+
function useUnwrappedAngle(targetDeg) {
|
|
82
|
+
const continuousRef = useRef(targetDeg);
|
|
83
|
+
const targetMod = (targetDeg % 360 + 360) % 360;
|
|
84
|
+
const currentMod = (continuousRef.current % 360 + 360) % 360;
|
|
85
|
+
let delta = targetMod - currentMod;
|
|
86
|
+
if (delta > 180) delta -= 360;
|
|
87
|
+
else if (delta < -180) delta += 360;
|
|
88
|
+
continuousRef.current += delta;
|
|
89
|
+
return continuousRef.current;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/utils/color.ts
|
|
93
|
+
function hexToHsl(hex) {
|
|
94
|
+
const r = parseInt(hex.slice(1, 3), 16) / 255;
|
|
95
|
+
const g = parseInt(hex.slice(3, 5), 16) / 255;
|
|
96
|
+
const b = parseInt(hex.slice(5, 7), 16) / 255;
|
|
97
|
+
const max = Math.max(r, g, b);
|
|
98
|
+
const min = Math.min(r, g, b);
|
|
99
|
+
const l = (max + min) / 2;
|
|
100
|
+
let h = 0;
|
|
101
|
+
let s = 0;
|
|
102
|
+
if (max !== min) {
|
|
103
|
+
const d = max - min;
|
|
104
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
105
|
+
switch (max) {
|
|
106
|
+
case r:
|
|
107
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
108
|
+
break;
|
|
109
|
+
case g:
|
|
110
|
+
h = ((b - r) / d + 2) / 6;
|
|
111
|
+
break;
|
|
112
|
+
case b:
|
|
113
|
+
h = ((r - g) / d + 4) / 6;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return [h * 360, s * 100, l * 100];
|
|
118
|
+
}
|
|
119
|
+
function hslToHex(h, s, l) {
|
|
120
|
+
const sl = s / 100;
|
|
121
|
+
const ll = l / 100;
|
|
122
|
+
const a = sl * Math.min(ll, 1 - ll);
|
|
123
|
+
const f = (n) => {
|
|
124
|
+
const k = (n + h / 30) % 12;
|
|
125
|
+
const color = ll - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
126
|
+
return Math.round(255 * color).toString(16).padStart(2, "0");
|
|
127
|
+
};
|
|
128
|
+
return `#${f(0)}${f(8)}${f(4)}`;
|
|
129
|
+
}
|
|
130
|
+
function hslToRgb(h, s, l) {
|
|
131
|
+
const sl = s / 100;
|
|
132
|
+
const ll = l / 100;
|
|
133
|
+
const a = sl * Math.min(ll, 1 - ll);
|
|
134
|
+
const f = (n) => {
|
|
135
|
+
const k = (n + h / 30) % 12;
|
|
136
|
+
return Math.round(255 * (ll - a * Math.max(Math.min(k - 3, 9 - k, 1), -1)));
|
|
137
|
+
};
|
|
138
|
+
return { r: f(0), g: f(8), b: f(4) };
|
|
139
|
+
}
|
|
140
|
+
function hslToRgba(h, s, l, alpha) {
|
|
141
|
+
const { r, g, b } = hslToRgb(h, s, l);
|
|
142
|
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
143
|
+
}
|
|
144
|
+
function deriveAccent(hex) {
|
|
145
|
+
const [h, s, l] = hexToHsl(hex);
|
|
146
|
+
return {
|
|
147
|
+
base: hex,
|
|
148
|
+
dot: hslToHex(h, s, Math.min(l + 12, 95)),
|
|
149
|
+
track: hex,
|
|
150
|
+
ring: hex,
|
|
151
|
+
glow: hslToRgba(h, s, l, 0.3),
|
|
152
|
+
light: hslToHex(h, Math.max(s - 20, 0), Math.min(l + 45, 96))
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function accentToCssVars(tokens, prefix) {
|
|
156
|
+
return {
|
|
157
|
+
[`--${prefix}-accent`]: tokens.base,
|
|
158
|
+
[`--${prefix}-accent-dot`]: tokens.dot,
|
|
159
|
+
[`--${prefix}-accent-track`]: tokens.track,
|
|
160
|
+
[`--${prefix}-accent-ring`]: tokens.ring,
|
|
161
|
+
[`--${prefix}-accent-glow`]: tokens.glow,
|
|
162
|
+
[`--${prefix}-accent-light`]: tokens.light
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/components/ColorWheel/colorMath.ts
|
|
167
|
+
function hsvToRgb(h, s, v) {
|
|
168
|
+
s /= 100;
|
|
169
|
+
v /= 100;
|
|
170
|
+
const c = v * s;
|
|
171
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
172
|
+
const m = v - c;
|
|
173
|
+
let r, g, b;
|
|
174
|
+
if (h < 60) {
|
|
175
|
+
r = c;
|
|
176
|
+
g = x;
|
|
177
|
+
b = 0;
|
|
178
|
+
} else if (h < 120) {
|
|
179
|
+
r = x;
|
|
180
|
+
g = c;
|
|
181
|
+
b = 0;
|
|
182
|
+
} else if (h < 180) {
|
|
183
|
+
r = 0;
|
|
184
|
+
g = c;
|
|
185
|
+
b = x;
|
|
186
|
+
} else if (h < 240) {
|
|
187
|
+
r = 0;
|
|
188
|
+
g = x;
|
|
189
|
+
b = c;
|
|
190
|
+
} else if (h < 300) {
|
|
191
|
+
r = x;
|
|
192
|
+
g = 0;
|
|
193
|
+
b = c;
|
|
194
|
+
} else {
|
|
195
|
+
r = c;
|
|
196
|
+
g = 0;
|
|
197
|
+
b = x;
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
r: Math.round((r + m) * 255),
|
|
201
|
+
g: Math.round((g + m) * 255),
|
|
202
|
+
b: Math.round((b + m) * 255)
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function rgbToHsv(r, g, b) {
|
|
206
|
+
r /= 255;
|
|
207
|
+
g /= 255;
|
|
208
|
+
b /= 255;
|
|
209
|
+
const max = Math.max(r, g, b);
|
|
210
|
+
const min = Math.min(r, g, b);
|
|
211
|
+
const d = max - min;
|
|
212
|
+
let h = 0;
|
|
213
|
+
if (d !== 0) {
|
|
214
|
+
if (max === r) h = (g - b) / d % 6;
|
|
215
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
216
|
+
else h = (r - g) / d + 4;
|
|
217
|
+
h *= 60;
|
|
218
|
+
if (h < 0) h += 360;
|
|
219
|
+
}
|
|
220
|
+
const s = max === 0 ? 0 : d / max;
|
|
221
|
+
return { h, s: s * 100, v: max * 100 };
|
|
222
|
+
}
|
|
223
|
+
function toHexByte(n) {
|
|
224
|
+
const h = Math.max(0, Math.min(255, Math.round(n))).toString(16);
|
|
225
|
+
return h.length === 1 ? "0" + h : h;
|
|
226
|
+
}
|
|
227
|
+
function rgbToHex(c) {
|
|
228
|
+
return ("#" + toHexByte(c.r) + toHexByte(c.g) + toHexByte(c.b)).toUpperCase();
|
|
229
|
+
}
|
|
230
|
+
function hsvaToHex(hsv, a) {
|
|
231
|
+
const rgb = hsvToRgb(hsv.h, hsv.s, hsv.v);
|
|
232
|
+
let hex = rgbToHex(rgb);
|
|
233
|
+
if (a < 1) hex += toHexByte(Math.round(a * 255));
|
|
234
|
+
return hex;
|
|
235
|
+
}
|
|
236
|
+
function hexToHsva(input) {
|
|
237
|
+
let clean = input.trim().replace(/^#/, "");
|
|
238
|
+
if (/^[0-9a-fA-F]{3}$/.test(clean)) {
|
|
239
|
+
clean = clean.split("").map((ch) => ch + ch).join("");
|
|
240
|
+
}
|
|
241
|
+
if (!/^[0-9a-fA-F]{6}$/.test(clean) && !/^[0-9a-fA-F]{8}$/.test(clean)) return null;
|
|
242
|
+
const r = parseInt(clean.slice(0, 2), 16);
|
|
243
|
+
const g = parseInt(clean.slice(2, 4), 16);
|
|
244
|
+
const b = parseInt(clean.slice(4, 6), 16);
|
|
245
|
+
const a = clean.length === 8 ? parseInt(clean.slice(6, 8), 16) / 255 : 1;
|
|
246
|
+
return { hsv: rgbToHsv(r, g, b), a };
|
|
247
|
+
}
|
|
248
|
+
function clamp(v, min, max) {
|
|
249
|
+
return Math.min(max, Math.max(min, v));
|
|
250
|
+
}
|
|
251
|
+
function round2(n) {
|
|
252
|
+
return Math.round(n * 100) / 100;
|
|
253
|
+
}
|
|
254
|
+
function angleDistance(a, b) {
|
|
255
|
+
const d = Math.abs(a - b) % 360;
|
|
256
|
+
return d > 180 ? 360 - d : d;
|
|
257
|
+
}
|
|
258
|
+
function relativeLuminance(c) {
|
|
259
|
+
return (0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b) / 255;
|
|
260
|
+
}
|
|
261
|
+
var RING_SPECTRUM = "conic-gradient(from 180deg, hsl(0 100% 50%) 0%, hsl(60 100% 50%) 16.667%, hsl(120 100% 50%) 33.333%, hsl(180 100% 50%) 50%, hsl(240 100% 50%) 66.667%, hsl(300 100% 50%) 83.333%, hsl(360 100% 50%) 100%)";
|
|
262
|
+
var RING_INNER_FRAC = 0.3382;
|
|
263
|
+
var RING_OUTER_FRAC = 0.5;
|
|
264
|
+
var ARC_A0 = 30;
|
|
265
|
+
var ARC_A1 = 150;
|
|
266
|
+
var ARC_HANDLE_R_FRAC = 152 / 380;
|
|
267
|
+
var MODES_HSV = ["h", "s", "v"];
|
|
268
|
+
var MODES_HSVA = ["h", "s", "v", "a"];
|
|
269
|
+
function ringGradient(mode, hsv) {
|
|
270
|
+
if (mode === "s") {
|
|
271
|
+
const grey = rgbToHex(hsvToRgb(hsv.h, 0, hsv.v));
|
|
272
|
+
const full = rgbToHex(hsvToRgb(hsv.h, 100, hsv.v));
|
|
273
|
+
return `conic-gradient(from 180deg, ${grey} 0deg, ${full} 360deg)`;
|
|
274
|
+
}
|
|
275
|
+
if (mode === "v") {
|
|
276
|
+
const bright = rgbToHex(hsvToRgb(hsv.h, hsv.s, 100));
|
|
277
|
+
return `conic-gradient(from 180deg, #000000 0deg, ${bright} 360deg)`;
|
|
278
|
+
}
|
|
279
|
+
if (mode === "a") {
|
|
280
|
+
const full = rgbToHex(hsvToRgb(hsv.h, hsv.s, hsv.v));
|
|
281
|
+
return `conic-gradient(from 180deg, transparent 0deg, ${full} 360deg)`;
|
|
282
|
+
}
|
|
283
|
+
return RING_SPECTRUM;
|
|
284
|
+
}
|
|
285
|
+
var EMPTY_FAVORITES = [];
|
|
286
|
+
function ColorWheel({
|
|
287
|
+
value,
|
|
288
|
+
onChange,
|
|
289
|
+
favorites = EMPTY_FAVORITES,
|
|
290
|
+
onFavoritesChange,
|
|
291
|
+
maxFavorites = 5,
|
|
292
|
+
showAlphaControl = true,
|
|
293
|
+
alphaMode = "arc",
|
|
294
|
+
theme = "system",
|
|
295
|
+
size = 1,
|
|
296
|
+
accent,
|
|
297
|
+
className
|
|
298
|
+
}) {
|
|
299
|
+
const ringIsFourWay = showAlphaControl && alphaMode === "ring";
|
|
300
|
+
const arcVisible = showAlphaControl && alphaMode === "arc";
|
|
301
|
+
const modes = ringIsFourWay ? MODES_HSVA : MODES_HSV;
|
|
302
|
+
const parsed = useMemo(() => hexToHsva(value), [value]);
|
|
303
|
+
const [hsv, setHsv] = useState(() => parsed?.hsv ?? { h: 0, s: 0, v: 100 });
|
|
304
|
+
const [alpha, setAlpha] = useState(() => parsed?.a ?? 1);
|
|
305
|
+
const [mode, setMode] = useState("h");
|
|
306
|
+
const [dragging, setDragging] = useState(false);
|
|
307
|
+
const [copied, setCopied] = useState(false);
|
|
308
|
+
const [pulseFavorite, setPulseFavorite] = useState(false);
|
|
309
|
+
const [editing, setEditing] = useState(false);
|
|
310
|
+
const [editValue, setEditValue] = useState("");
|
|
311
|
+
const [eyedropperSupported, setEyedropperSupported] = useState(false);
|
|
312
|
+
const accentVars = accent ? accentToCssVars(deriveAccent(accent), "colorwheel") : {};
|
|
313
|
+
const lastEmitted = useRef(value);
|
|
314
|
+
useEffect(() => {
|
|
315
|
+
if (value === lastEmitted.current) return;
|
|
316
|
+
const next = hexToHsva(value);
|
|
317
|
+
if (next) {
|
|
318
|
+
setHsv(next.hsv);
|
|
319
|
+
setAlpha(next.a);
|
|
320
|
+
}
|
|
321
|
+
lastEmitted.current = value;
|
|
322
|
+
}, [value]);
|
|
323
|
+
useEffect(() => {
|
|
324
|
+
setEyedropperSupported(typeof window !== "undefined" && "EyeDropper" in window);
|
|
325
|
+
}, []);
|
|
326
|
+
useEffect(() => {
|
|
327
|
+
if (!ringIsFourWay && mode === "a") setMode("h");
|
|
328
|
+
}, [ringIsFourWay, mode]);
|
|
329
|
+
const stageRef = useRef(null);
|
|
330
|
+
const alphaArcRef = useRef(null);
|
|
331
|
+
const modeToggleRef = useRef(null);
|
|
332
|
+
const hexInputRef = useRef(null);
|
|
333
|
+
function commit(nextHsv, nextAlpha) {
|
|
334
|
+
setHsv(nextHsv);
|
|
335
|
+
setAlpha(nextAlpha);
|
|
336
|
+
const hex = hsvaToHex(nextHsv, nextAlpha);
|
|
337
|
+
lastEmitted.current = hex;
|
|
338
|
+
onChange(hex);
|
|
339
|
+
}
|
|
340
|
+
const rgb = hsvToRgb(hsv.h, hsv.s, hsv.v);
|
|
341
|
+
const solidHex = rgbToHex(rgb);
|
|
342
|
+
const hexString = hsvaToHex(hsv, alpha);
|
|
343
|
+
const luminance = relativeLuminance(rgb);
|
|
344
|
+
const isLight = luminance > 0.58;
|
|
345
|
+
const discText = isLight ? "#1A1913" : "#FFFFFF";
|
|
346
|
+
const badgeBg = isLight ? "rgba(0,0,0,0.02)" : "rgba(255,255,255,0.03)";
|
|
347
|
+
const badgeGrooveDark = isLight ? "rgba(0,0,0,0.2)" : "rgba(0,0,0,0.32)";
|
|
348
|
+
const badgeGrooveLight = isLight ? "rgba(255,255,255,0.35)" : "rgba(255,255,255,0.12)";
|
|
349
|
+
const badgeIcon = isLight ? "rgba(0,0,0,0.72)" : "rgba(255,255,255,0.88)";
|
|
350
|
+
const badgeShadow = {
|
|
351
|
+
background: badgeBg,
|
|
352
|
+
color: badgeIcon,
|
|
353
|
+
boxShadow: `inset 0 1px 1px ${badgeGrooveDark}, inset 0 -1px 1px ${badgeGrooveLight}`
|
|
354
|
+
};
|
|
355
|
+
const ringLogicalAngle = mode === "h" ? hsv.h : mode === "a" ? alpha * 360 : (mode === "s" ? hsv.s : hsv.v) / 100 * 360;
|
|
356
|
+
const ringAngleFromTopDeg = useUnwrappedAngle(180 + ringLogicalAngle);
|
|
357
|
+
const arcLogicalAngle = ARC_A1 - alpha * (ARC_A1 - ARC_A0);
|
|
358
|
+
const arcAngleFromTop = (90 + arcLogicalAngle) * (Math.PI / 180);
|
|
359
|
+
const arcHandleR = ARC_HANDLE_R_FRAC * 380;
|
|
360
|
+
const arcHandleDx = arcHandleR * Math.sin(arcAngleFromTop);
|
|
361
|
+
const arcHandleDy = -arcHandleR * Math.cos(arcAngleFromTop);
|
|
362
|
+
function applyRingAngle(angle) {
|
|
363
|
+
if (mode === "h") commit({ ...hsv, h: angle }, alpha);
|
|
364
|
+
else if (mode === "s") commit({ ...hsv, s: angle / 360 * 100 }, alpha);
|
|
365
|
+
else if (mode === "a") commit(hsv, angle / 360);
|
|
366
|
+
else commit({ ...hsv, v: angle / 360 * 100 }, alpha);
|
|
367
|
+
}
|
|
368
|
+
const dragPrevAngleRef = useRef(null);
|
|
369
|
+
const dragAccumRef = useRef(0);
|
|
370
|
+
usePointerDrag(
|
|
371
|
+
stageRef,
|
|
372
|
+
(pos) => {
|
|
373
|
+
const rect = stageRef.current.getBoundingClientRect();
|
|
374
|
+
const cx = rect.left + rect.width / 2;
|
|
375
|
+
const cy = rect.top + rect.height / 2;
|
|
376
|
+
const dx = pos.x - cx;
|
|
377
|
+
const dy = pos.y - cy;
|
|
378
|
+
const dist = Math.hypot(dx, dy);
|
|
379
|
+
if (dist < rect.width * RING_INNER_FRAC || dist > rect.width * RING_OUTER_FRAC) return;
|
|
380
|
+
let angle = Math.atan2(dy, dx) * (180 / Math.PI) - 90;
|
|
381
|
+
angle = (angle % 360 + 360) % 360;
|
|
382
|
+
if (mode === "h") {
|
|
383
|
+
applyRingAngle(angle);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (dragPrevAngleRef.current === null) {
|
|
387
|
+
dragAccumRef.current = angle;
|
|
388
|
+
} else {
|
|
389
|
+
let delta = angle - dragPrevAngleRef.current;
|
|
390
|
+
if (delta > 180) delta -= 360;
|
|
391
|
+
else if (delta < -180) delta += 360;
|
|
392
|
+
dragAccumRef.current = clamp(dragAccumRef.current + delta, 0, 360);
|
|
393
|
+
}
|
|
394
|
+
dragPrevAngleRef.current = angle;
|
|
395
|
+
applyRingAngle(dragAccumRef.current);
|
|
396
|
+
},
|
|
397
|
+
void 0,
|
|
398
|
+
(dragging2) => {
|
|
399
|
+
if (!dragging2) dragPrevAngleRef.current = null;
|
|
400
|
+
setDragging(dragging2);
|
|
401
|
+
}
|
|
402
|
+
);
|
|
403
|
+
useEffect(() => {
|
|
404
|
+
const el = stageRef.current;
|
|
405
|
+
if (!el) return;
|
|
406
|
+
function onWheel(e) {
|
|
407
|
+
const rect = el.getBoundingClientRect();
|
|
408
|
+
const cx = rect.left + rect.width / 2;
|
|
409
|
+
const cy = rect.top + rect.height / 2;
|
|
410
|
+
const dist = Math.hypot(e.clientX - cx, e.clientY - cy);
|
|
411
|
+
if (dist < rect.width * RING_INNER_FRAC || dist > rect.width * RING_OUTER_FRAC) return;
|
|
412
|
+
e.preventDefault();
|
|
413
|
+
const dir = e.deltaY > 0 ? -1 : 1;
|
|
414
|
+
if (mode === "h") commit({ ...hsv, h: (hsv.h + dir * 4 + 360) % 360 }, alpha);
|
|
415
|
+
else if (mode === "s") commit({ ...hsv, s: clamp(hsv.s + dir * 3, 0, 100) }, alpha);
|
|
416
|
+
else if (mode === "a") commit(hsv, clamp(round2(alpha + dir * 0.03), 0, 1));
|
|
417
|
+
else commit({ ...hsv, v: clamp(hsv.v + dir * 3, 0, 100) }, alpha);
|
|
418
|
+
}
|
|
419
|
+
el.addEventListener("wheel", onWheel, { passive: false });
|
|
420
|
+
return () => el.removeEventListener("wheel", onWheel);
|
|
421
|
+
}, [mode, hsv, alpha]);
|
|
422
|
+
usePointerDrag(
|
|
423
|
+
alphaArcRef,
|
|
424
|
+
(pos) => {
|
|
425
|
+
const rect = alphaArcRef.current.getBoundingClientRect();
|
|
426
|
+
const cx = rect.left + rect.width / 2;
|
|
427
|
+
const cy = rect.top + rect.height / 2;
|
|
428
|
+
let angle = Math.atan2(pos.y - cy, pos.x - cx) * (180 / Math.PI);
|
|
429
|
+
if (angle < 0) angle += 360;
|
|
430
|
+
if (angle < ARC_A0 || angle > ARC_A1) {
|
|
431
|
+
angle = angleDistance(angle, ARC_A0) < angleDistance(angle, ARC_A1) ? ARC_A0 : ARC_A1;
|
|
432
|
+
}
|
|
433
|
+
commit(hsv, round2((ARC_A1 - angle) / (ARC_A1 - ARC_A0)));
|
|
434
|
+
},
|
|
435
|
+
void 0,
|
|
436
|
+
setDragging
|
|
437
|
+
);
|
|
438
|
+
useEffect(() => {
|
|
439
|
+
const el = alphaArcRef.current;
|
|
440
|
+
if (!el) return;
|
|
441
|
+
function onWheel(e) {
|
|
442
|
+
e.preventDefault();
|
|
443
|
+
const dir = e.deltaY > 0 ? -1 : 1;
|
|
444
|
+
commit(hsv, clamp(round2(alpha + dir * 0.05), 0, 1));
|
|
445
|
+
}
|
|
446
|
+
el.addEventListener("wheel", onWheel, { passive: false });
|
|
447
|
+
return () => el.removeEventListener("wheel", onWheel);
|
|
448
|
+
}, [hsv, alpha]);
|
|
449
|
+
useEffect(() => {
|
|
450
|
+
const el = modeToggleRef.current;
|
|
451
|
+
if (!el) return;
|
|
452
|
+
function onWheel(e) {
|
|
453
|
+
e.preventDefault();
|
|
454
|
+
const idx = modes.indexOf(mode);
|
|
455
|
+
const dir = e.deltaY > 0 ? -1 : 1;
|
|
456
|
+
setMode(modes[clamp(idx + dir, 0, modes.length - 1)]);
|
|
457
|
+
}
|
|
458
|
+
el.addEventListener("wheel", onWheel, { passive: false });
|
|
459
|
+
return () => el.removeEventListener("wheel", onWheel);
|
|
460
|
+
}, [mode, modes]);
|
|
461
|
+
function handleDiscClick() {
|
|
462
|
+
const done = () => {
|
|
463
|
+
setCopied(true);
|
|
464
|
+
setTimeout(() => setCopied(false), 900);
|
|
465
|
+
};
|
|
466
|
+
if (navigator.clipboard?.writeText) {
|
|
467
|
+
navigator.clipboard.writeText(hexString).then(done, done);
|
|
468
|
+
} else {
|
|
469
|
+
done();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function beginEdit(e) {
|
|
473
|
+
e.stopPropagation();
|
|
474
|
+
setEditValue(hexString);
|
|
475
|
+
setEditing(true);
|
|
476
|
+
requestAnimationFrame(() => {
|
|
477
|
+
hexInputRef.current?.focus();
|
|
478
|
+
hexInputRef.current?.setSelectionRange(1, hexString.length);
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
function commitEdit(cancelled) {
|
|
482
|
+
if (!cancelled) {
|
|
483
|
+
const next = hexToHsva(editValue);
|
|
484
|
+
if (next) commit(next.hsv, next.a);
|
|
485
|
+
}
|
|
486
|
+
setEditing(false);
|
|
487
|
+
}
|
|
488
|
+
function pickFromScreen() {
|
|
489
|
+
const dropper = new window.EyeDropper();
|
|
490
|
+
dropper.open().then((result) => {
|
|
491
|
+
const next = hexToHsva(result.sRGBHex);
|
|
492
|
+
if (next) commit(next.hsv, next.a);
|
|
493
|
+
}).catch(() => {
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
function saveFavorite() {
|
|
497
|
+
const next = [hexString, ...favorites.filter((h) => h !== hexString)].slice(0, maxFavorites);
|
|
498
|
+
onFavoritesChange?.(next);
|
|
499
|
+
setPulseFavorite(false);
|
|
500
|
+
requestAnimationFrame(() => setPulseFavorite(true));
|
|
501
|
+
}
|
|
502
|
+
function applyFavorite(hex) {
|
|
503
|
+
const next = hexToHsva(hex);
|
|
504
|
+
if (next) commit(next.hsv, next.a);
|
|
505
|
+
}
|
|
506
|
+
function removeFavorite(hex) {
|
|
507
|
+
onFavoritesChange?.(favorites.filter((h) => h !== hex));
|
|
508
|
+
}
|
|
509
|
+
return /* @__PURE__ */ jsxs(
|
|
510
|
+
"div",
|
|
511
|
+
{
|
|
512
|
+
className: [ColorWheel_module_default.root, className].filter(Boolean).join(" "),
|
|
513
|
+
"data-theme": theme === "system" ? void 0 : theme,
|
|
514
|
+
"data-dragging": dragging ? "true" : void 0,
|
|
515
|
+
style: {
|
|
516
|
+
...accentVars,
|
|
517
|
+
...size !== 1 ? { transform: `scale(${size})`, transformOrigin: "top center" } : void 0
|
|
518
|
+
},
|
|
519
|
+
children: [
|
|
520
|
+
favorites.length > 0 && /* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.recents, children: favorites.map((hex, i) => /* @__PURE__ */ jsxs("div", { className: ColorWheel_module_default.chipWrap, style: { animationDelay: `${i * 0.02}s` }, children: [
|
|
521
|
+
/* @__PURE__ */ jsx(
|
|
522
|
+
"button",
|
|
523
|
+
{
|
|
524
|
+
type: "button",
|
|
525
|
+
className: ColorWheel_module_default.chip,
|
|
526
|
+
style: { background: hex.length > 7 ? hex.slice(0, 7) : hex },
|
|
527
|
+
title: hex,
|
|
528
|
+
"aria-label": `Apply color ${hex}`,
|
|
529
|
+
onClick: () => applyFavorite(hex)
|
|
530
|
+
}
|
|
531
|
+
),
|
|
532
|
+
/* @__PURE__ */ jsx(
|
|
533
|
+
"button",
|
|
534
|
+
{
|
|
535
|
+
type: "button",
|
|
536
|
+
className: ColorWheel_module_default.chipRemove,
|
|
537
|
+
"aria-label": `Remove ${hex} from favorites`,
|
|
538
|
+
onClick: (e) => {
|
|
539
|
+
e.stopPropagation();
|
|
540
|
+
removeFavorite(hex);
|
|
541
|
+
},
|
|
542
|
+
children: /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 3, strokeLinecap: "round", children: /* @__PURE__ */ jsx("path", { d: "M6 6l12 12M18 6L6 18" }) })
|
|
543
|
+
}
|
|
544
|
+
)
|
|
545
|
+
] }, hex + i)) }),
|
|
546
|
+
/* @__PURE__ */ jsxs("div", { className: ColorWheel_module_default.picker, children: [
|
|
547
|
+
/* @__PURE__ */ jsx(
|
|
548
|
+
"div",
|
|
549
|
+
{
|
|
550
|
+
className: ColorWheel_module_default.glow,
|
|
551
|
+
style: { background: `radial-gradient(closest-side, ${solidHex}, transparent 72%)` }
|
|
552
|
+
}
|
|
553
|
+
),
|
|
554
|
+
/* @__PURE__ */ jsxs(
|
|
555
|
+
"div",
|
|
556
|
+
{
|
|
557
|
+
className: ColorWheel_module_default.modeToggle,
|
|
558
|
+
ref: modeToggleRef,
|
|
559
|
+
style: { width: modes.length * 28, gridTemplateColumns: `repeat(${modes.length}, 1fr)` },
|
|
560
|
+
children: [
|
|
561
|
+
/* @__PURE__ */ jsx(
|
|
562
|
+
"div",
|
|
563
|
+
{
|
|
564
|
+
className: ColorWheel_module_default.modePill,
|
|
565
|
+
style: {
|
|
566
|
+
width: `calc(${100 / modes.length}% - 2px)`,
|
|
567
|
+
transform: `translateX(calc(${modes.indexOf(mode)} * (100% + 1px)))`
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
),
|
|
571
|
+
modes.map((m) => /* @__PURE__ */ jsx(
|
|
572
|
+
"button",
|
|
573
|
+
{
|
|
574
|
+
type: "button",
|
|
575
|
+
className: [ColorWheel_module_default.modeButton, mode === m ? ColorWheel_module_default.active : ""].join(" "),
|
|
576
|
+
onClick: () => setMode(m),
|
|
577
|
+
children: m.toUpperCase()
|
|
578
|
+
},
|
|
579
|
+
m
|
|
580
|
+
))
|
|
581
|
+
]
|
|
582
|
+
}
|
|
583
|
+
),
|
|
584
|
+
/* @__PURE__ */ jsxs("div", { className: ColorWheel_module_default.stage, ref: stageRef, children: [
|
|
585
|
+
/* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.stageShadow }),
|
|
586
|
+
/* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.ring, style: { background: ringGradient(mode, hsv) } }),
|
|
587
|
+
/* @__PURE__ */ jsxs("div", { className: ColorWheel_module_default.currentDisc, onClick: handleDiscClick, children: [
|
|
588
|
+
/* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.discFill, style: { background: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})` } }),
|
|
589
|
+
!editing && /* @__PURE__ */ jsx("span", { className: [ColorWheel_module_default.currentHex, copied ? ColorWheel_module_default.copied : ""].join(" "), style: { color: discText }, onClick: beginEdit, children: copied ? "Copied" : hexString }),
|
|
590
|
+
editing && /* @__PURE__ */ jsx(
|
|
591
|
+
"input",
|
|
592
|
+
{
|
|
593
|
+
ref: hexInputRef,
|
|
594
|
+
className: ColorWheel_module_default.currentHexInput,
|
|
595
|
+
style: { color: discText },
|
|
596
|
+
type: "text",
|
|
597
|
+
inputMode: "text",
|
|
598
|
+
autoComplete: "off",
|
|
599
|
+
spellCheck: false,
|
|
600
|
+
maxLength: 9,
|
|
601
|
+
value: editValue,
|
|
602
|
+
onClick: (e) => e.stopPropagation(),
|
|
603
|
+
onChange: (e) => setEditValue(e.target.value),
|
|
604
|
+
onKeyDown: (e) => {
|
|
605
|
+
if (e.key === "Enter") hexInputRef.current?.blur();
|
|
606
|
+
else if (e.key === "Escape") {
|
|
607
|
+
commitEdit(true);
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
onBlur: () => commitEdit(false)
|
|
611
|
+
}
|
|
612
|
+
)
|
|
613
|
+
] }),
|
|
614
|
+
eyedropperSupported && /* @__PURE__ */ jsx(
|
|
615
|
+
"button",
|
|
616
|
+
{
|
|
617
|
+
type: "button",
|
|
618
|
+
className: [ColorWheel_module_default.discBadge, ColorWheel_module_default.discBadgeBottom].join(" "),
|
|
619
|
+
style: badgeShadow,
|
|
620
|
+
"aria-label": "Pick color from screen",
|
|
621
|
+
onClick: pickFromScreen,
|
|
622
|
+
children: /* @__PURE__ */ jsx("svg", { viewBox: "0 0 32 32", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M27.7,3.3c-1.5-1.5-3.9-1.5-5.4,0L17,8.6l-1.3-1.3c-0.4-0.4-1-0.4-1.4,0s-0.4,1,0,1.4l1.3,1.3L5,20.6c-0.6,0.6-1,1.4-1.1,2.3C3.3,23.4,3,24.2,3,25c0,1.7,1.3,3,3,3c0.8,0,1.6-0.3,2.2-0.9C9,27,9.8,26.6,10.4,26L21,15.4l1.3,1.3c0.2,0.2,0.5,0.3,0.7,0.3s0.5-0.1,0.7-0.3c0.4-0.4,0.4-1,0-1.4L22.4,14l5.3-5.3C29.2,7.2,29.2,4.8,27.7,3.3z M9,24.6c-0.4,0.4-0.8,0.6-1.3,0.5c-0.4,0-0.7,0.2-0.9,0.5C6.7,25.8,6.3,26,6,26c-0.6,0-1-0.4-1-1c0-0.3,0.2-0.7,0.5-0.8c0.3-0.2,0.5-0.5,0.5-0.9c0-0.5,0.2-1,0.5-1.3L17,11.4l2.6,2.6L9,24.6z" }) })
|
|
623
|
+
}
|
|
624
|
+
),
|
|
625
|
+
/* @__PURE__ */ jsx(
|
|
626
|
+
"button",
|
|
627
|
+
{
|
|
628
|
+
type: "button",
|
|
629
|
+
className: [ColorWheel_module_default.discBadge, ColorWheel_module_default.discBadgeTop, pulseFavorite ? ColorWheel_module_default.pulse : ""].join(" "),
|
|
630
|
+
style: badgeShadow,
|
|
631
|
+
"aria-label": "Save current color to favorites",
|
|
632
|
+
onClick: saveFavorite,
|
|
633
|
+
onAnimationEnd: () => setPulseFavorite(false),
|
|
634
|
+
children: /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.2, strokeLinecap: "round", strokeLinejoin: "round", children: /* @__PURE__ */ jsx("path", { d: "M12 5v14M5 12h14" }) })
|
|
635
|
+
}
|
|
636
|
+
),
|
|
637
|
+
/* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.hueHandlePivot, style: { transform: `translate(-50%, -50%) rotate(${ringAngleFromTopDeg}deg)` }, children: /* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.hueHandle, style: { background: solidHex } }) })
|
|
638
|
+
] }),
|
|
639
|
+
/* @__PURE__ */ jsxs("div", { style: { display: arcVisible ? "contents" : "none" }, children: [
|
|
640
|
+
/* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.alphaArcShadowWrap, children: /* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.alphaArcShadow }) }),
|
|
641
|
+
/* @__PURE__ */ jsx("div", { className: ColorWheel_module_default.alphaArc, ref: alphaArcRef, children: /* @__PURE__ */ jsx(
|
|
642
|
+
"div",
|
|
643
|
+
{
|
|
644
|
+
className: ColorWheel_module_default.alphaArcFill,
|
|
645
|
+
style: { background: `conic-gradient(from 90deg, ${solidHex} 30deg, transparent 150deg)` }
|
|
646
|
+
}
|
|
647
|
+
) }),
|
|
648
|
+
/* @__PURE__ */ jsx("div", { className: [ColorWheel_module_default.alphaArcCap, ColorWheel_module_default.alphaArcCapStart].join(" "), style: { background: solidHex } }),
|
|
649
|
+
/* @__PURE__ */ jsx("div", { className: [ColorWheel_module_default.alphaArcCap, ColorWheel_module_default.alphaArcCapEnd].join(" ") }),
|
|
650
|
+
/* @__PURE__ */ jsx(
|
|
651
|
+
"div",
|
|
652
|
+
{
|
|
653
|
+
className: ColorWheel_module_default.alphaArcHandle,
|
|
654
|
+
style: {
|
|
655
|
+
transform: `translate(calc(-50% + ${arcHandleDx}px), calc(-50% + ${arcHandleDy}px))`,
|
|
656
|
+
background: solidHex
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
)
|
|
660
|
+
] })
|
|
661
|
+
] })
|
|
662
|
+
]
|
|
663
|
+
}
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
export { ColorWheel };
|
|
668
|
+
//# sourceMappingURL=index.js.map
|
|
669
|
+
//# sourceMappingURL=index.js.map
|