@comitor/ui 3.13.6 → 3.15.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/README.md +42 -6
- package/dist/components/composite/gantt.js +1 -1
- package/dist/components/composite/gantt.js.map +1 -1
- package/dist/components/composite/status-config.d.ts +7 -5
- package/dist/components/composite/status-config.js +2 -2
- package/dist/components/composite/status-config.js.map +1 -1
- package/dist/components/ui/badge.js +2 -2
- package/dist/components/ui/badge.js.map +1 -1
- package/dist/components/ui/button.js +3 -3
- package/dist/components/ui/button.js.map +1 -1
- package/dist/components/ui/circular-progress.js +1 -1
- package/dist/components/ui/circular-progress.js.map +1 -1
- package/dist/components/ui/color-picker.d.ts +111 -0
- package/dist/components/ui/color-picker.js +1444 -0
- package/dist/components/ui/color-picker.js.map +1 -0
- package/dist/components/ui/color-swatch.d.ts +22 -0
- package/dist/components/ui/color-swatch.js +82 -0
- package/dist/components/ui/color-swatch.js.map +1 -0
- package/dist/components/ui/compare-slider.d.ts +61 -0
- package/dist/components/ui/compare-slider.js +448 -0
- package/dist/components/ui/compare-slider.js.map +1 -0
- package/dist/components/ui/pagination.js +1 -1
- package/dist/components/ui/pagination.js.map +1 -1
- package/dist/editor/fixed-toolbar.js +4 -0
- package/dist/editor/fixed-toolbar.js.map +1 -1
- package/dist/editor/markdown-editor-constants.d.ts +4 -0
- package/dist/editor/markdown-editor-constants.js.map +1 -1
- package/dist/editor/markdown-extensions.js +45 -3
- package/dist/editor/markdown-extensions.js.map +1 -1
- package/dist/editor/markdown-syntax.d.ts +39 -1
- package/dist/editor/markdown-syntax.js +31 -0
- package/dist/editor/markdown-syntax.js.map +1 -1
- package/dist/editor/text-align.d.ts +42 -0
- package/dist/editor/text-align.js +98 -0
- package/dist/editor/text-align.js.map +1 -0
- package/dist/editor/toolbar-actions.d.ts +2 -1
- package/dist/editor/toolbar-actions.js +31 -0
- package/dist/editor/toolbar-actions.js.map +1 -1
- package/dist/i18n/locale-provider.d.ts +13 -0
- package/dist/i18n/messages.d.ts +16 -0
- package/dist/i18n/messages.js +9 -0
- package/dist/i18n/messages.js.map +1 -1
- package/dist/i18n/namespaces/color-picker.d.ts +44 -0
- package/dist/i18n/namespaces/color-picker.js +49 -0
- package/dist/i18n/namespaces/color-picker.js.map +1 -0
- package/dist/i18n/namespaces/color-swatch.d.ts +11 -0
- package/dist/i18n/namespaces/color-swatch.js +13 -0
- package/dist/i18n/namespaces/color-swatch.js.map +1 -0
- package/dist/i18n/namespaces/compare-slider.d.ts +27 -0
- package/dist/i18n/namespaces/compare-slider.js +18 -0
- package/dist/i18n/namespaces/compare-slider.js.map +1 -0
- package/dist/i18n/namespaces/display-axis.d.ts +20 -0
- package/dist/i18n/namespaces/display-axis.js +22 -2
- package/dist/i18n/namespaces/display-axis.js.map +1 -1
- package/dist/i18n/namespaces/markdown-editor.js +6 -0
- package/dist/i18n/namespaces/markdown-editor.js.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/color-swatch-background.d.ts +27 -0
- package/dist/lib/color-swatch-background.js +32 -0
- package/dist/lib/color-swatch-background.js.map +1 -0
- package/dist/lib/script-literal.d.ts +13 -0
- package/dist/lib/script-literal.js +7 -0
- package/dist/lib/script-literal.js.map +1 -0
- package/dist/shell/app-brand.d.ts +22 -0
- package/dist/shell/app-brand.js +63 -0
- package/dist/shell/app-brand.js.map +1 -0
- package/dist/shell/app-shell.d.ts +3 -0
- package/dist/shell/app-shell.js +3 -2
- package/dist/shell/app-shell.js.map +1 -1
- package/dist/shell/contrast-provider.js +2 -1
- package/dist/shell/contrast-provider.js.map +1 -1
- package/dist/shell/display-axis-constants.d.ts +20 -1
- package/dist/shell/display-axis-constants.js +14 -1
- package/dist/shell/display-axis-constants.js.map +1 -1
- package/dist/shell/display-axis-controls.d.ts +33 -2
- package/dist/shell/display-axis-controls.js +60 -3
- package/dist/shell/display-axis-controls.js.map +1 -1
- package/dist/shell/display-axis-provider.d.ts +66 -4
- package/dist/shell/display-axis-provider.js +34 -5
- package/dist/shell/display-axis-provider.js.map +1 -1
- package/dist/shell/display-prefs-seed.d.ts +9 -5
- package/dist/shell/display-prefs-seed.js +7 -3
- package/dist/shell/display-prefs-seed.js.map +1 -1
- package/dist/shell/index.d.ts +7 -4
- package/dist/shell/index.js +1 -0
- package/dist/shell/index.js.map +1 -1
- package/dist/shell/mobile-menu.js +3 -2
- package/dist/shell/mobile-menu.js.map +1 -1
- package/dist/shell/shell-context.d.ts +9 -2
- package/dist/shell/shell-context.js +3 -0
- package/dist/shell/shell-context.js.map +1 -1
- package/dist/shell/sidebar.d.ts +6 -2
- package/dist/shell/sidebar.js +5 -3
- package/dist/shell/sidebar.js.map +1 -1
- package/dist/shell/types.d.ts +29 -2
- package/dist/shell/types.js.map +1 -1
- package/dist/tokens.d.ts +311 -1
- package/dist/tokens.js +291 -0
- package/dist/tokens.js.map +1 -1
- package/package.json +3 -2
- package/styles.css +405 -20
|
@@ -0,0 +1,1444 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useDirection } from "@radix-ui/react-direction";
|
|
4
|
+
import * as SliderPrimitive from "@radix-ui/react-slider";
|
|
5
|
+
import { Slot } from "@radix-ui/react-slot";
|
|
6
|
+
import { cva } from "class-variance-authority";
|
|
7
|
+
import { Pipette } from "lucide-react";
|
|
8
|
+
import * as React from "react";
|
|
9
|
+
import { useComitorLabels } from "../../i18n/locale-provider.js";
|
|
10
|
+
import { cn } from "../../lib/cn.js";
|
|
11
|
+
import { getCheckerboardBackground, getSwatchBackground } from "../../lib/color-swatch-background.js";
|
|
12
|
+
import { useComposedRefs } from "../../lib/compose-refs.js";
|
|
13
|
+
import { useAsRef } from "../../lib/use-as-ref.js";
|
|
14
|
+
import { useIsomorphicLayoutEffect } from "../../lib/use-isomorphic-layout-effect.js";
|
|
15
|
+
import { useLazyRef } from "../../lib/use-lazy-ref.js";
|
|
16
|
+
import { VisuallyHiddenInput } from "../../lib/visually-hidden-input.js";
|
|
17
|
+
import { Button } from "./button.js";
|
|
18
|
+
import { Input } from "./input.js";
|
|
19
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./popover.js";
|
|
20
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select.js";
|
|
21
|
+
const ROOT_NAME = "ColorPicker";
|
|
22
|
+
const ROOT_IMPL_NAME = "ColorPickerImpl";
|
|
23
|
+
const TRIGGER_NAME = "ColorPickerTrigger";
|
|
24
|
+
const CONTENT_NAME = "ColorPickerContent";
|
|
25
|
+
const AREA_NAME = "ColorPickerArea";
|
|
26
|
+
const HUE_SLIDER_NAME = "ColorPickerHueSlider";
|
|
27
|
+
const ALPHA_SLIDER_NAME = "ColorPickerAlphaSlider";
|
|
28
|
+
const SWATCH_NAME = "ColorPickerSwatch";
|
|
29
|
+
const EYE_DROPPER_NAME = "ColorPickerEyeDropper";
|
|
30
|
+
const FORMAT_SELECT_NAME = "ColorPickerFormatSelect";
|
|
31
|
+
const INPUT_NAME = "ColorPickerInput";
|
|
32
|
+
const colorFormats = ["hex", "rgb", "hsl", "hsb"];
|
|
33
|
+
const HUE_TRACK_CLASS = "bg-[linear-gradient(to_right,#ff0000_0%,#ffff00_16.66%,#00ff00_33.33%,#00ffff_50%,#0000ff_66.66%,#ff00ff_83.33%,#ff0000_100%)]";
|
|
34
|
+
const THUMB_CLASS = "block rounded-full border-2 border-paper shadow-sm ring-1 ring-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring";
|
|
35
|
+
function hexToRgb(hex, alpha) {
|
|
36
|
+
const trimmed = hex.trim();
|
|
37
|
+
const short = /^#?([a-f\d])([a-f\d])([a-f\d])$/i.exec(trimmed);
|
|
38
|
+
const full = short ? `${short[1]}${short[1]}${short[2]}${short[2]}${short[3]}${short[3]}` : trimmed;
|
|
39
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(full);
|
|
40
|
+
return result ? {
|
|
41
|
+
r: Number.parseInt(result[1] ?? "0", 16),
|
|
42
|
+
g: Number.parseInt(result[2] ?? "0", 16),
|
|
43
|
+
b: Number.parseInt(result[3] ?? "0", 16),
|
|
44
|
+
a: alpha ?? 1
|
|
45
|
+
} : { r: 0, g: 0, b: 0, a: alpha ?? 1 };
|
|
46
|
+
}
|
|
47
|
+
function toHex(n) {
|
|
48
|
+
const hex = Math.round(n).toString(16);
|
|
49
|
+
return hex.length === 1 ? `0${hex}` : hex;
|
|
50
|
+
}
|
|
51
|
+
function rgbToHex(color) {
|
|
52
|
+
return `#${toHex(color.r)}${toHex(color.g)}${toHex(color.b)}`;
|
|
53
|
+
}
|
|
54
|
+
function rgbToHsv(color) {
|
|
55
|
+
const r = color.r / 255;
|
|
56
|
+
const g = color.g / 255;
|
|
57
|
+
const b = color.b / 255;
|
|
58
|
+
const max = Math.max(r, g, b);
|
|
59
|
+
const min = Math.min(r, g, b);
|
|
60
|
+
const diff = max - min;
|
|
61
|
+
let h = 0;
|
|
62
|
+
if (diff !== 0) {
|
|
63
|
+
switch (max) {
|
|
64
|
+
case r:
|
|
65
|
+
h = (g - b) / diff % 6;
|
|
66
|
+
break;
|
|
67
|
+
case g:
|
|
68
|
+
h = (b - r) / diff + 2;
|
|
69
|
+
break;
|
|
70
|
+
case b:
|
|
71
|
+
h = (r - g) / diff + 4;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
h = Math.round(h * 60);
|
|
76
|
+
if (h < 0) h += 360;
|
|
77
|
+
const s = max === 0 ? 0 : diff / max;
|
|
78
|
+
const v = max;
|
|
79
|
+
return {
|
|
80
|
+
h,
|
|
81
|
+
s: Math.round(s * 100),
|
|
82
|
+
v: Math.round(v * 100),
|
|
83
|
+
a: color.a
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function hsvToRgb(hsv) {
|
|
87
|
+
const h = hsv.h / 360;
|
|
88
|
+
const s = hsv.s / 100;
|
|
89
|
+
const v = hsv.v / 100;
|
|
90
|
+
const i = Math.floor(h * 6);
|
|
91
|
+
const f = h * 6 - i;
|
|
92
|
+
const p = v * (1 - s);
|
|
93
|
+
const q = v * (1 - f * s);
|
|
94
|
+
const t = v * (1 - (1 - f) * s);
|
|
95
|
+
let r;
|
|
96
|
+
let g;
|
|
97
|
+
let b;
|
|
98
|
+
switch (i % 6) {
|
|
99
|
+
case 0: {
|
|
100
|
+
r = v;
|
|
101
|
+
g = t;
|
|
102
|
+
b = p;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case 1: {
|
|
106
|
+
r = q;
|
|
107
|
+
g = v;
|
|
108
|
+
b = p;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
case 2: {
|
|
112
|
+
r = p;
|
|
113
|
+
g = v;
|
|
114
|
+
b = t;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case 3: {
|
|
118
|
+
r = p;
|
|
119
|
+
g = q;
|
|
120
|
+
b = v;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case 4: {
|
|
124
|
+
r = t;
|
|
125
|
+
g = p;
|
|
126
|
+
b = v;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case 5: {
|
|
130
|
+
r = v;
|
|
131
|
+
g = p;
|
|
132
|
+
b = q;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
default: {
|
|
136
|
+
r = 0;
|
|
137
|
+
g = 0;
|
|
138
|
+
b = 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
r: Math.round(r * 255),
|
|
143
|
+
g: Math.round(g * 255),
|
|
144
|
+
b: Math.round(b * 255),
|
|
145
|
+
a: hsv.a
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function colorToString(color, format = "hex") {
|
|
149
|
+
switch (format) {
|
|
150
|
+
case "hex":
|
|
151
|
+
return rgbToHex(color);
|
|
152
|
+
case "rgb":
|
|
153
|
+
return color.a < 1 ? `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})` : `rgb(${color.r}, ${color.g}, ${color.b})`;
|
|
154
|
+
case "hsl": {
|
|
155
|
+
const hsl = rgbToHsl(color);
|
|
156
|
+
return color.a < 1 ? `hsla(${hsl.h}, ${hsl.s}%, ${hsl.l}%, ${color.a})` : `hsl(${hsl.h}, ${hsl.s}%, ${hsl.l}%)`;
|
|
157
|
+
}
|
|
158
|
+
case "hsb": {
|
|
159
|
+
const hsv = rgbToHsv(color);
|
|
160
|
+
return color.a < 1 ? `hsba(${hsv.h}, ${hsv.s}%, ${hsv.v}%, ${color.a})` : `hsb(${hsv.h}, ${hsv.s}%, ${hsv.v}%)`;
|
|
161
|
+
}
|
|
162
|
+
default:
|
|
163
|
+
return rgbToHex(color);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function rgbToHsl(color) {
|
|
167
|
+
const r = color.r / 255;
|
|
168
|
+
const g = color.g / 255;
|
|
169
|
+
const b = color.b / 255;
|
|
170
|
+
const max = Math.max(r, g, b);
|
|
171
|
+
const min = Math.min(r, g, b);
|
|
172
|
+
const diff = max - min;
|
|
173
|
+
const sum = max + min;
|
|
174
|
+
const l = sum / 2;
|
|
175
|
+
let h = 0;
|
|
176
|
+
let s = 0;
|
|
177
|
+
if (diff !== 0) {
|
|
178
|
+
s = l > 0.5 ? diff / (2 - sum) : diff / sum;
|
|
179
|
+
if (max === r) {
|
|
180
|
+
h = (g - b) / diff + (g < b ? 6 : 0);
|
|
181
|
+
} else if (max === g) {
|
|
182
|
+
h = (b - r) / diff + 2;
|
|
183
|
+
} else if (max === b) {
|
|
184
|
+
h = (r - g) / diff + 4;
|
|
185
|
+
}
|
|
186
|
+
h /= 6;
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
h: Math.round(h * 360),
|
|
190
|
+
s: Math.round(s * 100),
|
|
191
|
+
l: Math.round(l * 100)
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function hslToRgb(hsl, alpha = 1) {
|
|
195
|
+
const h = hsl.h / 360;
|
|
196
|
+
const s = hsl.s / 100;
|
|
197
|
+
const l = hsl.l / 100;
|
|
198
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
199
|
+
const x = c * (1 - Math.abs(h * 6 % 2 - 1));
|
|
200
|
+
const m = l - c / 2;
|
|
201
|
+
let r = 0;
|
|
202
|
+
let g = 0;
|
|
203
|
+
let b = 0;
|
|
204
|
+
if (h >= 0 && h < 1 / 6) {
|
|
205
|
+
r = c;
|
|
206
|
+
g = x;
|
|
207
|
+
b = 0;
|
|
208
|
+
} else if (h >= 1 / 6 && h < 2 / 6) {
|
|
209
|
+
r = x;
|
|
210
|
+
g = c;
|
|
211
|
+
b = 0;
|
|
212
|
+
} else if (h >= 2 / 6 && h < 3 / 6) {
|
|
213
|
+
r = 0;
|
|
214
|
+
g = c;
|
|
215
|
+
b = x;
|
|
216
|
+
} else if (h >= 3 / 6 && h < 4 / 6) {
|
|
217
|
+
r = 0;
|
|
218
|
+
g = x;
|
|
219
|
+
b = c;
|
|
220
|
+
} else if (h >= 4 / 6 && h < 5 / 6) {
|
|
221
|
+
r = x;
|
|
222
|
+
g = 0;
|
|
223
|
+
b = c;
|
|
224
|
+
} else if (h >= 5 / 6 && h < 1) {
|
|
225
|
+
r = c;
|
|
226
|
+
g = 0;
|
|
227
|
+
b = x;
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
r: Math.round((r + m) * 255),
|
|
231
|
+
g: Math.round((g + m) * 255),
|
|
232
|
+
b: Math.round((b + m) * 255),
|
|
233
|
+
a: alpha
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function parseColorString(value) {
|
|
237
|
+
const trimmed = value.trim();
|
|
238
|
+
if (trimmed.startsWith("#")) {
|
|
239
|
+
const hexMatch = trimmed.match(/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/);
|
|
240
|
+
if (hexMatch) {
|
|
241
|
+
return hexToRgb(trimmed);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const rgbMatch = trimmed.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/);
|
|
245
|
+
if (rgbMatch) {
|
|
246
|
+
return {
|
|
247
|
+
r: Number.parseInt(rgbMatch[1] ?? "0", 10),
|
|
248
|
+
g: Number.parseInt(rgbMatch[2] ?? "0", 10),
|
|
249
|
+
b: Number.parseInt(rgbMatch[3] ?? "0", 10),
|
|
250
|
+
a: rgbMatch[4] ? Number.parseFloat(rgbMatch[4]) : 1
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
const hslMatch = trimmed.match(/^hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(?:,\s*([\d.]+))?\s*\)$/);
|
|
254
|
+
if (hslMatch) {
|
|
255
|
+
const h = Number.parseInt(hslMatch[1] ?? "0", 10);
|
|
256
|
+
const s = Number.parseInt(hslMatch[2] ?? "0", 10) / 100;
|
|
257
|
+
const l = Number.parseInt(hslMatch[3] ?? "0", 10) / 100;
|
|
258
|
+
const a = hslMatch[4] ? Number.parseFloat(hslMatch[4]) : 1;
|
|
259
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
260
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
261
|
+
const m = l - c / 2;
|
|
262
|
+
let r = 0;
|
|
263
|
+
let g = 0;
|
|
264
|
+
let b = 0;
|
|
265
|
+
if (h >= 0 && h < 60) {
|
|
266
|
+
r = c;
|
|
267
|
+
g = x;
|
|
268
|
+
b = 0;
|
|
269
|
+
} else if (h >= 60 && h < 120) {
|
|
270
|
+
r = x;
|
|
271
|
+
g = c;
|
|
272
|
+
b = 0;
|
|
273
|
+
} else if (h >= 120 && h < 180) {
|
|
274
|
+
r = 0;
|
|
275
|
+
g = c;
|
|
276
|
+
b = x;
|
|
277
|
+
} else if (h >= 180 && h < 240) {
|
|
278
|
+
r = 0;
|
|
279
|
+
g = x;
|
|
280
|
+
b = c;
|
|
281
|
+
} else if (h >= 240 && h < 300) {
|
|
282
|
+
r = x;
|
|
283
|
+
g = 0;
|
|
284
|
+
b = c;
|
|
285
|
+
} else if (h >= 300 && h < 360) {
|
|
286
|
+
r = c;
|
|
287
|
+
g = 0;
|
|
288
|
+
b = x;
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
r: Math.round((r + m) * 255),
|
|
292
|
+
g: Math.round((g + m) * 255),
|
|
293
|
+
b: Math.round((b + m) * 255),
|
|
294
|
+
a
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const hsbMatch = trimmed.match(/^hsba?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(?:,\s*([\d.]+))?\s*\)$/);
|
|
298
|
+
if (hsbMatch) {
|
|
299
|
+
const h = Number.parseInt(hsbMatch[1] ?? "0", 10);
|
|
300
|
+
const s = Number.parseInt(hsbMatch[2] ?? "0", 10);
|
|
301
|
+
const v = Number.parseInt(hsbMatch[3] ?? "0", 10);
|
|
302
|
+
const a = hsbMatch[4] ? Number.parseFloat(hsbMatch[4]) : 1;
|
|
303
|
+
return hsvToRgb({ h, s, v, a });
|
|
304
|
+
}
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
function parseColorProp(value, fallbackAlpha) {
|
|
308
|
+
const trimmed = value.trim();
|
|
309
|
+
const parsed = parseColorString(trimmed);
|
|
310
|
+
if (!parsed) return hexToRgb(trimmed, fallbackAlpha);
|
|
311
|
+
return trimmed.startsWith("#") ? { ...parsed, a: fallbackAlpha } : parsed;
|
|
312
|
+
}
|
|
313
|
+
function isSameColor(a, b) {
|
|
314
|
+
return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a;
|
|
315
|
+
}
|
|
316
|
+
function clampPercent(value) {
|
|
317
|
+
return Math.max(0, Math.min(100, value));
|
|
318
|
+
}
|
|
319
|
+
const StoreContext = React.createContext(null);
|
|
320
|
+
function useStoreContext(consumerName) {
|
|
321
|
+
const context = React.useContext(StoreContext);
|
|
322
|
+
if (!context) {
|
|
323
|
+
throw new Error(`\`${consumerName}\` ph\u1EA3i n\u1EB1m b\xEAn trong \`${ROOT_NAME}\``);
|
|
324
|
+
}
|
|
325
|
+
return context;
|
|
326
|
+
}
|
|
327
|
+
function useStore(selector) {
|
|
328
|
+
const store = useStoreContext("useColorPicker");
|
|
329
|
+
const getSnapshot = React.useCallback(() => selector(store.getState()), [store, selector]);
|
|
330
|
+
return React.useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
|
|
331
|
+
}
|
|
332
|
+
const ColorPickerContext = React.createContext(null);
|
|
333
|
+
function useColorPickerContext(consumerName) {
|
|
334
|
+
const context = React.useContext(ColorPickerContext);
|
|
335
|
+
if (!context) {
|
|
336
|
+
throw new Error(`\`${consumerName}\` ph\u1EA3i n\u1EB1m b\xEAn trong \`${ROOT_NAME}\``);
|
|
337
|
+
}
|
|
338
|
+
return context;
|
|
339
|
+
}
|
|
340
|
+
function ColorPicker(props) {
|
|
341
|
+
const {
|
|
342
|
+
value: valueProp,
|
|
343
|
+
defaultValue = "#000000",
|
|
344
|
+
onValueChange,
|
|
345
|
+
format: formatProp,
|
|
346
|
+
defaultFormat = "hex",
|
|
347
|
+
onFormatChange,
|
|
348
|
+
defaultOpen,
|
|
349
|
+
open: openProp,
|
|
350
|
+
onOpenChange,
|
|
351
|
+
name,
|
|
352
|
+
disabled,
|
|
353
|
+
inline,
|
|
354
|
+
readOnly,
|
|
355
|
+
required,
|
|
356
|
+
...rootProps
|
|
357
|
+
} = props;
|
|
358
|
+
const listenersRef = useLazyRef(() => /* @__PURE__ */ new Set());
|
|
359
|
+
const stateRef = useLazyRef(() => {
|
|
360
|
+
const colorString = valueProp ?? defaultValue;
|
|
361
|
+
const color = parseColorProp(colorString, 1);
|
|
362
|
+
return {
|
|
363
|
+
color,
|
|
364
|
+
hsv: rgbToHsv(color),
|
|
365
|
+
open: openProp ?? defaultOpen ?? false,
|
|
366
|
+
format: formatProp ?? defaultFormat
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
const lastValueRef = React.useRef(valueProp ?? null);
|
|
370
|
+
const propsRef = useAsRef({
|
|
371
|
+
onValueChange,
|
|
372
|
+
onOpenChange,
|
|
373
|
+
onFormatChange
|
|
374
|
+
});
|
|
375
|
+
const store = React.useMemo(() => {
|
|
376
|
+
function notify() {
|
|
377
|
+
for (const cb of listenersRef.current) {
|
|
378
|
+
cb();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function emit(color, format) {
|
|
382
|
+
const colorString = colorToString(color, format);
|
|
383
|
+
if (colorString === lastValueRef.current) return;
|
|
384
|
+
lastValueRef.current = colorString;
|
|
385
|
+
propsRef.current.onValueChange?.(colorString);
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
subscribe: (cb) => {
|
|
389
|
+
listenersRef.current.add(cb);
|
|
390
|
+
return () => listenersRef.current.delete(cb);
|
|
391
|
+
},
|
|
392
|
+
getState: () => stateRef.current,
|
|
393
|
+
setColor: (value) => {
|
|
394
|
+
if (Object.is(stateRef.current.color, value)) return;
|
|
395
|
+
const prevState = { ...stateRef.current };
|
|
396
|
+
stateRef.current.color = value;
|
|
397
|
+
emit(value, prevState.format);
|
|
398
|
+
notify();
|
|
399
|
+
},
|
|
400
|
+
setHsv: (value) => {
|
|
401
|
+
if (Object.is(stateRef.current.hsv, value)) return;
|
|
402
|
+
const prevState = { ...stateRef.current };
|
|
403
|
+
stateRef.current.hsv = value;
|
|
404
|
+
emit(hsvToRgb(value), prevState.format);
|
|
405
|
+
notify();
|
|
406
|
+
},
|
|
407
|
+
setOpen: (value) => {
|
|
408
|
+
if (Object.is(stateRef.current.open, value)) return;
|
|
409
|
+
stateRef.current.open = value;
|
|
410
|
+
if (propsRef.current.onOpenChange) {
|
|
411
|
+
propsRef.current.onOpenChange(value);
|
|
412
|
+
}
|
|
413
|
+
notify();
|
|
414
|
+
},
|
|
415
|
+
setFormat: (value) => {
|
|
416
|
+
if (Object.is(stateRef.current.format, value)) return;
|
|
417
|
+
stateRef.current.format = value;
|
|
418
|
+
if (propsRef.current.onFormatChange) {
|
|
419
|
+
propsRef.current.onFormatChange(value);
|
|
420
|
+
}
|
|
421
|
+
notify();
|
|
422
|
+
},
|
|
423
|
+
syncValue: (color, hsv) => {
|
|
424
|
+
stateRef.current.color = color;
|
|
425
|
+
stateRef.current.hsv = hsv;
|
|
426
|
+
lastValueRef.current = colorToString(color, stateRef.current.format);
|
|
427
|
+
notify();
|
|
428
|
+
},
|
|
429
|
+
getLastValue: () => lastValueRef.current,
|
|
430
|
+
notify
|
|
431
|
+
};
|
|
432
|
+
}, [listenersRef, stateRef, propsRef]);
|
|
433
|
+
return /* @__PURE__ */ jsx(StoreContext.Provider, { value: store, children: /* @__PURE__ */ jsx(
|
|
434
|
+
ColorPickerImpl,
|
|
435
|
+
{
|
|
436
|
+
...rootProps,
|
|
437
|
+
value: valueProp,
|
|
438
|
+
defaultOpen,
|
|
439
|
+
open: openProp,
|
|
440
|
+
name,
|
|
441
|
+
disabled,
|
|
442
|
+
inline,
|
|
443
|
+
readOnly,
|
|
444
|
+
required
|
|
445
|
+
}
|
|
446
|
+
) });
|
|
447
|
+
}
|
|
448
|
+
function ColorPickerImpl(props) {
|
|
449
|
+
const {
|
|
450
|
+
value: valueProp,
|
|
451
|
+
dir: dirProp,
|
|
452
|
+
defaultOpen,
|
|
453
|
+
open: openProp,
|
|
454
|
+
name,
|
|
455
|
+
ref,
|
|
456
|
+
asChild,
|
|
457
|
+
disabled,
|
|
458
|
+
inline,
|
|
459
|
+
modal,
|
|
460
|
+
readOnly,
|
|
461
|
+
required,
|
|
462
|
+
labels: labelsProp,
|
|
463
|
+
...rootProps
|
|
464
|
+
} = props;
|
|
465
|
+
const store = useStoreContext(ROOT_IMPL_NAME);
|
|
466
|
+
const labels = useComitorLabels("colorPicker", labelsProp);
|
|
467
|
+
const dir = useDirection(dirProp);
|
|
468
|
+
const [formTrigger, setFormTrigger] = React.useState(null);
|
|
469
|
+
const composedRef = useComposedRefs(ref, (node) => setFormTrigger(node));
|
|
470
|
+
const isFormControl = formTrigger ? !!formTrigger.closest("form") : true;
|
|
471
|
+
useIsomorphicLayoutEffect(() => {
|
|
472
|
+
if (valueProp !== void 0) {
|
|
473
|
+
if (valueProp === store.getLastValue()) return;
|
|
474
|
+
const currentState = store.getState();
|
|
475
|
+
const color = parseColorProp(valueProp, currentState.color.a);
|
|
476
|
+
if (isSameColor(color, currentState.color)) return;
|
|
477
|
+
store.syncValue(color, rgbToHsv(color));
|
|
478
|
+
}
|
|
479
|
+
}, [valueProp, store]);
|
|
480
|
+
useIsomorphicLayoutEffect(() => {
|
|
481
|
+
if (openProp !== void 0) {
|
|
482
|
+
store.setOpen(openProp);
|
|
483
|
+
}
|
|
484
|
+
}, [openProp, store]);
|
|
485
|
+
const contextValue = React.useMemo(
|
|
486
|
+
() => ({
|
|
487
|
+
dir,
|
|
488
|
+
disabled,
|
|
489
|
+
inline,
|
|
490
|
+
readOnly,
|
|
491
|
+
required,
|
|
492
|
+
labels
|
|
493
|
+
}),
|
|
494
|
+
[dir, disabled, inline, readOnly, required, labels]
|
|
495
|
+
);
|
|
496
|
+
const value = useStore((state) => rgbToHex(state.color));
|
|
497
|
+
const open = useStore((state) => state.open);
|
|
498
|
+
const RootPrimitive = asChild ? Slot : "div";
|
|
499
|
+
if (inline) {
|
|
500
|
+
return /* @__PURE__ */ jsxs(ColorPickerContext.Provider, { value: contextValue, children: [
|
|
501
|
+
/* @__PURE__ */ jsx(RootPrimitive, { ...rootProps, ref: composedRef }),
|
|
502
|
+
isFormControl && /* @__PURE__ */ jsx(
|
|
503
|
+
VisuallyHiddenInput,
|
|
504
|
+
{
|
|
505
|
+
type: "hidden",
|
|
506
|
+
control: formTrigger,
|
|
507
|
+
name,
|
|
508
|
+
value,
|
|
509
|
+
disabled,
|
|
510
|
+
readOnly,
|
|
511
|
+
required
|
|
512
|
+
}
|
|
513
|
+
)
|
|
514
|
+
] });
|
|
515
|
+
}
|
|
516
|
+
return /* @__PURE__ */ jsx(ColorPickerContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxs(Popover, { defaultOpen, open, onOpenChange: store.setOpen, modal, children: [
|
|
517
|
+
/* @__PURE__ */ jsx(RootPrimitive, { ...rootProps, ref: composedRef }),
|
|
518
|
+
isFormControl && /* @__PURE__ */ jsx(
|
|
519
|
+
VisuallyHiddenInput,
|
|
520
|
+
{
|
|
521
|
+
type: "hidden",
|
|
522
|
+
control: formTrigger,
|
|
523
|
+
name,
|
|
524
|
+
value,
|
|
525
|
+
disabled,
|
|
526
|
+
readOnly,
|
|
527
|
+
required
|
|
528
|
+
}
|
|
529
|
+
)
|
|
530
|
+
] }) });
|
|
531
|
+
}
|
|
532
|
+
function ColorPickerTrigger(props) {
|
|
533
|
+
const { asChild, disabled, variant, ...triggerProps } = props;
|
|
534
|
+
const context = useColorPickerContext(TRIGGER_NAME);
|
|
535
|
+
const isDisabled = disabled || context.disabled;
|
|
536
|
+
if (asChild) {
|
|
537
|
+
return /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, disabled: isDisabled, children: /* @__PURE__ */ jsx(Slot, { "data-slot": "color-picker-trigger", ...triggerProps }) });
|
|
538
|
+
}
|
|
539
|
+
return /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, disabled: isDisabled, children: /* @__PURE__ */ jsx(Button, { "data-slot": "color-picker-trigger", variant: variant ?? "outline", ...triggerProps }) });
|
|
540
|
+
}
|
|
541
|
+
function ColorPickerContent(props) {
|
|
542
|
+
const { asChild, className, children, ...popoverContentProps } = props;
|
|
543
|
+
const context = useColorPickerContext(CONTENT_NAME);
|
|
544
|
+
if (context.inline) {
|
|
545
|
+
const ContentPrimitive = asChild ? Slot : "div";
|
|
546
|
+
return /* @__PURE__ */ jsx(
|
|
547
|
+
ContentPrimitive,
|
|
548
|
+
{
|
|
549
|
+
"data-slot": "color-picker-content",
|
|
550
|
+
...popoverContentProps,
|
|
551
|
+
className: cn("flex w-85 flex-col gap-4 p-4", className),
|
|
552
|
+
children
|
|
553
|
+
}
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
return /* @__PURE__ */ jsx(
|
|
557
|
+
PopoverContent,
|
|
558
|
+
{
|
|
559
|
+
"data-slot": "color-picker-content",
|
|
560
|
+
asChild,
|
|
561
|
+
...popoverContentProps,
|
|
562
|
+
className: cn("flex w-85 flex-col gap-4 p-4", className),
|
|
563
|
+
children
|
|
564
|
+
}
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
function ColorPickerArea(props) {
|
|
568
|
+
const {
|
|
569
|
+
asChild,
|
|
570
|
+
onPointerDown: onPointerDownProp,
|
|
571
|
+
onPointerMove: onPointerMoveProp,
|
|
572
|
+
onPointerUp: onPointerUpProp,
|
|
573
|
+
"aria-label": ariaLabel,
|
|
574
|
+
"aria-labelledby": ariaLabelledby,
|
|
575
|
+
className,
|
|
576
|
+
ref,
|
|
577
|
+
...areaProps
|
|
578
|
+
} = props;
|
|
579
|
+
const propsRef = useAsRef({
|
|
580
|
+
onPointerDown: onPointerDownProp,
|
|
581
|
+
onPointerMove: onPointerMoveProp,
|
|
582
|
+
onPointerUp: onPointerUpProp
|
|
583
|
+
});
|
|
584
|
+
const context = useColorPickerContext(AREA_NAME);
|
|
585
|
+
const store = useStoreContext(AREA_NAME);
|
|
586
|
+
const hsv = useStore((state) => state.hsv);
|
|
587
|
+
const isDraggingRef = React.useRef(false);
|
|
588
|
+
const areaRef = React.useRef(null);
|
|
589
|
+
const thumbRef = React.useRef(null);
|
|
590
|
+
const composedRef = useComposedRefs(ref, areaRef);
|
|
591
|
+
const isLocked = context.disabled || context.readOnly;
|
|
592
|
+
const commitHsv = React.useCallback(
|
|
593
|
+
(newHsv) => {
|
|
594
|
+
store.setHsv(newHsv);
|
|
595
|
+
store.setColor(hsvToRgb(newHsv));
|
|
596
|
+
},
|
|
597
|
+
[store]
|
|
598
|
+
);
|
|
599
|
+
const updateColorFromPosition = React.useCallback(
|
|
600
|
+
(clientX, clientY) => {
|
|
601
|
+
if (!areaRef.current) return;
|
|
602
|
+
const rect = areaRef.current.getBoundingClientRect();
|
|
603
|
+
const x = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
|
|
604
|
+
const y = Math.max(0, Math.min(1, 1 - (clientY - rect.top) / rect.height));
|
|
605
|
+
commitHsv({
|
|
606
|
+
h: hsv?.h ?? 0,
|
|
607
|
+
s: Math.round(x * 100),
|
|
608
|
+
v: Math.round(y * 100),
|
|
609
|
+
a: hsv?.a ?? 1
|
|
610
|
+
});
|
|
611
|
+
},
|
|
612
|
+
[hsv, commitHsv]
|
|
613
|
+
);
|
|
614
|
+
const onPointerDown = React.useCallback(
|
|
615
|
+
(event) => {
|
|
616
|
+
if (isLocked) return;
|
|
617
|
+
propsRef.current.onPointerDown?.(event);
|
|
618
|
+
if (event.defaultPrevented) return;
|
|
619
|
+
isDraggingRef.current = true;
|
|
620
|
+
areaRef.current?.setPointerCapture(event.pointerId);
|
|
621
|
+
event.preventDefault();
|
|
622
|
+
thumbRef.current?.focus({ preventScroll: true });
|
|
623
|
+
updateColorFromPosition(event.clientX, event.clientY);
|
|
624
|
+
},
|
|
625
|
+
[isLocked, updateColorFromPosition, propsRef]
|
|
626
|
+
);
|
|
627
|
+
const onPointerMove = React.useCallback(
|
|
628
|
+
(event) => {
|
|
629
|
+
propsRef.current.onPointerMove?.(event);
|
|
630
|
+
if (event.defaultPrevented) return;
|
|
631
|
+
if (isDraggingRef.current) {
|
|
632
|
+
updateColorFromPosition(event.clientX, event.clientY);
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
[updateColorFromPosition, propsRef]
|
|
636
|
+
);
|
|
637
|
+
const onPointerUp = React.useCallback(
|
|
638
|
+
(event) => {
|
|
639
|
+
propsRef.current.onPointerUp?.(event);
|
|
640
|
+
if (event.defaultPrevented) return;
|
|
641
|
+
isDraggingRef.current = false;
|
|
642
|
+
areaRef.current?.releasePointerCapture(event.pointerId);
|
|
643
|
+
},
|
|
644
|
+
[propsRef]
|
|
645
|
+
);
|
|
646
|
+
const onThumbKeyDown = React.useCallback(
|
|
647
|
+
(event) => {
|
|
648
|
+
if (isLocked) return;
|
|
649
|
+
const step = event.shiftKey ? 10 : 1;
|
|
650
|
+
let s = hsv?.s ?? 0;
|
|
651
|
+
let v = hsv?.v ?? 0;
|
|
652
|
+
switch (event.key) {
|
|
653
|
+
case "ArrowLeft":
|
|
654
|
+
s -= step;
|
|
655
|
+
break;
|
|
656
|
+
case "ArrowRight":
|
|
657
|
+
s += step;
|
|
658
|
+
break;
|
|
659
|
+
case "ArrowUp":
|
|
660
|
+
v += step;
|
|
661
|
+
break;
|
|
662
|
+
case "ArrowDown":
|
|
663
|
+
v -= step;
|
|
664
|
+
break;
|
|
665
|
+
case "PageUp":
|
|
666
|
+
v += 10;
|
|
667
|
+
break;
|
|
668
|
+
case "PageDown":
|
|
669
|
+
v -= 10;
|
|
670
|
+
break;
|
|
671
|
+
case "Home":
|
|
672
|
+
s = 0;
|
|
673
|
+
break;
|
|
674
|
+
case "End":
|
|
675
|
+
s = 100;
|
|
676
|
+
break;
|
|
677
|
+
default:
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
event.preventDefault();
|
|
681
|
+
commitHsv({ h: hsv?.h ?? 0, s: clampPercent(s), v: clampPercent(v), a: hsv?.a ?? 1 });
|
|
682
|
+
},
|
|
683
|
+
[hsv, isLocked, commitHsv]
|
|
684
|
+
);
|
|
685
|
+
const hue = hsv?.h ?? 0;
|
|
686
|
+
const backgroundHue = hsvToRgb({ h: hue, s: 100, v: 100, a: 1 });
|
|
687
|
+
const saturation = hsv?.s ?? 0;
|
|
688
|
+
const brightness = hsv?.v ?? 0;
|
|
689
|
+
const AreaPrimitive = asChild ? Slot : "div";
|
|
690
|
+
return /* @__PURE__ */ jsxs(
|
|
691
|
+
AreaPrimitive,
|
|
692
|
+
{
|
|
693
|
+
"data-slot": "color-picker-area",
|
|
694
|
+
...areaProps,
|
|
695
|
+
className: cn(
|
|
696
|
+
"relative h-40 w-full cursor-crosshair touch-none rounded-sm border border-input",
|
|
697
|
+
context.disabled && "pointer-events-none opacity-50",
|
|
698
|
+
className
|
|
699
|
+
),
|
|
700
|
+
ref: composedRef,
|
|
701
|
+
onPointerDown,
|
|
702
|
+
onPointerMove,
|
|
703
|
+
onPointerUp,
|
|
704
|
+
children: [
|
|
705
|
+
/* @__PURE__ */ jsxs("div", { className: "absolute inset-0 overflow-hidden rounded-sm", children: [
|
|
706
|
+
/* @__PURE__ */ jsx(
|
|
707
|
+
"div",
|
|
708
|
+
{
|
|
709
|
+
className: "absolute inset-0",
|
|
710
|
+
style: {
|
|
711
|
+
backgroundColor: `rgb(${backgroundHue.r}, ${backgroundHue.g}, ${backgroundHue.b})`
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
),
|
|
715
|
+
/* @__PURE__ */ jsx(
|
|
716
|
+
"div",
|
|
717
|
+
{
|
|
718
|
+
className: "absolute inset-0",
|
|
719
|
+
style: {
|
|
720
|
+
backgroundImage: "linear-gradient(to right, #fff, transparent)"
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
),
|
|
724
|
+
/* @__PURE__ */ jsx(
|
|
725
|
+
"div",
|
|
726
|
+
{
|
|
727
|
+
className: "absolute inset-0",
|
|
728
|
+
style: {
|
|
729
|
+
backgroundImage: "linear-gradient(to bottom, transparent, #000)"
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
)
|
|
733
|
+
] }),
|
|
734
|
+
/* @__PURE__ */ jsx(
|
|
735
|
+
"div",
|
|
736
|
+
{
|
|
737
|
+
ref: thumbRef,
|
|
738
|
+
role: "slider",
|
|
739
|
+
tabIndex: context.disabled ? void 0 : 0,
|
|
740
|
+
"aria-label": ariaLabelledby ? void 0 : ariaLabel ?? context.labels.area,
|
|
741
|
+
"aria-labelledby": ariaLabelledby,
|
|
742
|
+
"aria-roledescription": context.labels.areaRoleDescription,
|
|
743
|
+
"aria-valuemin": 0,
|
|
744
|
+
"aria-valuemax": 100,
|
|
745
|
+
"aria-valuenow": saturation,
|
|
746
|
+
"aria-valuetext": context.labels.areaValueText(saturation, brightness),
|
|
747
|
+
"aria-disabled": context.disabled || void 0,
|
|
748
|
+
"aria-readonly": context.readOnly || void 0,
|
|
749
|
+
"data-slot": "color-picker-area-thumb",
|
|
750
|
+
className: cn(THUMB_CLASS, "absolute size-3 -translate-1/2"),
|
|
751
|
+
style: {
|
|
752
|
+
left: `${saturation}%`,
|
|
753
|
+
top: `${100 - brightness}%`
|
|
754
|
+
},
|
|
755
|
+
onKeyDown: onThumbKeyDown
|
|
756
|
+
}
|
|
757
|
+
)
|
|
758
|
+
]
|
|
759
|
+
}
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
function ColorPickerHueSlider(props) {
|
|
763
|
+
const { className, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, ...sliderProps } = props;
|
|
764
|
+
const context = useColorPickerContext(HUE_SLIDER_NAME);
|
|
765
|
+
const store = useStoreContext(HUE_SLIDER_NAME);
|
|
766
|
+
const hsv = useStore((state) => state.hsv);
|
|
767
|
+
const onValueChange = React.useCallback(
|
|
768
|
+
(values) => {
|
|
769
|
+
if (context.readOnly) return;
|
|
770
|
+
const newHsv = {
|
|
771
|
+
h: values[0] ?? 0,
|
|
772
|
+
s: hsv?.s ?? 0,
|
|
773
|
+
v: hsv?.v ?? 0,
|
|
774
|
+
a: hsv?.a ?? 1
|
|
775
|
+
};
|
|
776
|
+
store.setHsv(newHsv);
|
|
777
|
+
store.setColor(hsvToRgb(newHsv));
|
|
778
|
+
},
|
|
779
|
+
[hsv, store, context.readOnly]
|
|
780
|
+
);
|
|
781
|
+
const hue = hsv?.h ?? 0;
|
|
782
|
+
return /* @__PURE__ */ jsxs(
|
|
783
|
+
SliderPrimitive.Root,
|
|
784
|
+
{
|
|
785
|
+
"data-slot": "color-picker-hue-slider",
|
|
786
|
+
...sliderProps,
|
|
787
|
+
max: 360,
|
|
788
|
+
step: 1,
|
|
789
|
+
className: cn("relative flex w-full touch-none items-center select-none data-[disabled]:opacity-50", className),
|
|
790
|
+
value: [hue],
|
|
791
|
+
onValueChange,
|
|
792
|
+
disabled: context.disabled,
|
|
793
|
+
children: [
|
|
794
|
+
/* @__PURE__ */ jsx(SliderPrimitive.Track, { className: cn("relative h-3 w-full grow overflow-hidden rounded-full", HUE_TRACK_CLASS), children: /* @__PURE__ */ jsx(SliderPrimitive.Range, { className: "absolute h-full" }) }),
|
|
795
|
+
/* @__PURE__ */ jsx(
|
|
796
|
+
SliderPrimitive.Thumb,
|
|
797
|
+
{
|
|
798
|
+
"aria-label": ariaLabelledby ? void 0 : ariaLabel ?? context.labels.hueSlider,
|
|
799
|
+
"aria-labelledby": ariaLabelledby,
|
|
800
|
+
"aria-valuetext": context.labels.hueValueText(hue),
|
|
801
|
+
"aria-readonly": context.readOnly || void 0,
|
|
802
|
+
className: cn(THUMB_CLASS, "size-4")
|
|
803
|
+
}
|
|
804
|
+
)
|
|
805
|
+
]
|
|
806
|
+
}
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
function ColorPickerAlphaSlider(props) {
|
|
810
|
+
const { className, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, ...sliderProps } = props;
|
|
811
|
+
const context = useColorPickerContext(ALPHA_SLIDER_NAME);
|
|
812
|
+
const store = useStoreContext(ALPHA_SLIDER_NAME);
|
|
813
|
+
const color = useStore((state) => state.color);
|
|
814
|
+
const hsv = useStore((state) => state.hsv);
|
|
815
|
+
const onValueChange = React.useCallback(
|
|
816
|
+
(values) => {
|
|
817
|
+
if (context.readOnly) return;
|
|
818
|
+
const alpha = (values[0] ?? 0) / 100;
|
|
819
|
+
const newColor = { ...color, a: alpha };
|
|
820
|
+
const newHsv = { ...hsv, a: alpha };
|
|
821
|
+
store.setColor(newColor);
|
|
822
|
+
store.setHsv(newHsv);
|
|
823
|
+
},
|
|
824
|
+
[color, hsv, store, context.readOnly]
|
|
825
|
+
);
|
|
826
|
+
const gradientColor = `rgb(${color?.r ?? 0}, ${color?.g ?? 0}, ${color?.b ?? 0})`;
|
|
827
|
+
const alphaPercent = Math.round((color?.a ?? 1) * 100);
|
|
828
|
+
const checkerboard = getCheckerboardBackground(8);
|
|
829
|
+
return /* @__PURE__ */ jsxs(
|
|
830
|
+
SliderPrimitive.Root,
|
|
831
|
+
{
|
|
832
|
+
"data-slot": "color-picker-alpha-slider",
|
|
833
|
+
...sliderProps,
|
|
834
|
+
max: 100,
|
|
835
|
+
step: 1,
|
|
836
|
+
disabled: context.disabled,
|
|
837
|
+
className: cn("relative flex w-full touch-none items-center select-none data-[disabled]:opacity-50", className),
|
|
838
|
+
value: [alphaPercent],
|
|
839
|
+
onValueChange,
|
|
840
|
+
children: [
|
|
841
|
+
/* @__PURE__ */ jsxs(
|
|
842
|
+
SliderPrimitive.Track,
|
|
843
|
+
{
|
|
844
|
+
className: "relative h-3 w-full grow overflow-hidden rounded-full",
|
|
845
|
+
style: {
|
|
846
|
+
backgroundImage: checkerboard.backgroundImage,
|
|
847
|
+
backgroundSize: checkerboard.backgroundSize,
|
|
848
|
+
backgroundPosition: checkerboard.backgroundPosition
|
|
849
|
+
},
|
|
850
|
+
children: [
|
|
851
|
+
/* @__PURE__ */ jsx(
|
|
852
|
+
"div",
|
|
853
|
+
{
|
|
854
|
+
className: "absolute inset-0 rounded-full",
|
|
855
|
+
style: {
|
|
856
|
+
backgroundImage: `linear-gradient(to right, transparent, ${gradientColor})`
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
),
|
|
860
|
+
/* @__PURE__ */ jsx(SliderPrimitive.Range, { className: "absolute h-full" })
|
|
861
|
+
]
|
|
862
|
+
}
|
|
863
|
+
),
|
|
864
|
+
/* @__PURE__ */ jsx(
|
|
865
|
+
SliderPrimitive.Thumb,
|
|
866
|
+
{
|
|
867
|
+
"aria-label": ariaLabelledby ? void 0 : ariaLabel ?? context.labels.alphaSlider,
|
|
868
|
+
"aria-labelledby": ariaLabelledby,
|
|
869
|
+
"aria-valuetext": context.labels.alphaValueText(alphaPercent),
|
|
870
|
+
"aria-readonly": context.readOnly || void 0,
|
|
871
|
+
className: cn(THUMB_CLASS, "size-4")
|
|
872
|
+
}
|
|
873
|
+
)
|
|
874
|
+
]
|
|
875
|
+
}
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
function ColorPickerSwatch(props) {
|
|
879
|
+
const { asChild, className, ...swatchProps } = props;
|
|
880
|
+
const context = useColorPickerContext(SWATCH_NAME);
|
|
881
|
+
const color = useStore((state) => state.color);
|
|
882
|
+
const format = useStore((state) => state.format);
|
|
883
|
+
const background = React.useMemo(
|
|
884
|
+
() => getSwatchBackground(color ? `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})` : null, {
|
|
885
|
+
showTransparency: !!color && color.a < 1,
|
|
886
|
+
cell: 8
|
|
887
|
+
}),
|
|
888
|
+
[color]
|
|
889
|
+
);
|
|
890
|
+
const ariaLabel = !color ? context.labels.noColor : context.labels.swatchLabel(colorToString(color, format));
|
|
891
|
+
const SwatchPrimitive = asChild ? Slot : "div";
|
|
892
|
+
return /* @__PURE__ */ jsx(
|
|
893
|
+
SwatchPrimitive,
|
|
894
|
+
{
|
|
895
|
+
role: "img",
|
|
896
|
+
"aria-label": ariaLabel,
|
|
897
|
+
"data-slot": "color-picker-swatch",
|
|
898
|
+
...swatchProps,
|
|
899
|
+
className: cn(
|
|
900
|
+
// ⚠ Khác thượng nguồn: viền `border-input` — cùng lý do với `ColorSwatch`.
|
|
901
|
+
"box-border size-8 rounded-sm border border-input shadow-sm",
|
|
902
|
+
context.disabled && "opacity-50",
|
|
903
|
+
className
|
|
904
|
+
),
|
|
905
|
+
style: {
|
|
906
|
+
backgroundColor: background.backgroundColor,
|
|
907
|
+
backgroundImage: background.backgroundImage,
|
|
908
|
+
backgroundSize: background.backgroundSize,
|
|
909
|
+
backgroundPosition: background.backgroundPosition,
|
|
910
|
+
backgroundRepeat: background.backgroundRepeat,
|
|
911
|
+
forcedColorAdjust: "none"
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
function subscribeToEyeDropper() {
|
|
917
|
+
return () => {
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
function getEyeDropperSnapshot() {
|
|
921
|
+
return !!window.EyeDropper;
|
|
922
|
+
}
|
|
923
|
+
function getEyeDropperServerSnapshot() {
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
function ColorPickerEyeDropper(props) {
|
|
927
|
+
const { size: sizeProp, children, disabled, ...buttonProps } = props;
|
|
928
|
+
const context = useColorPickerContext(EYE_DROPPER_NAME);
|
|
929
|
+
const store = useStoreContext(EYE_DROPPER_NAME);
|
|
930
|
+
const color = useStore((state) => state.color);
|
|
931
|
+
const isDisabled = disabled || context.disabled || context.readOnly;
|
|
932
|
+
const onEyeDropper = React.useCallback(async () => {
|
|
933
|
+
if (!window.EyeDropper) return;
|
|
934
|
+
try {
|
|
935
|
+
const eyeDropper = new window.EyeDropper();
|
|
936
|
+
const result = await eyeDropper.open();
|
|
937
|
+
if (result.sRGBHex) {
|
|
938
|
+
const currentAlpha = color?.a ?? 1;
|
|
939
|
+
const newColor = hexToRgb(result.sRGBHex, currentAlpha);
|
|
940
|
+
const newHsv = rgbToHsv(newColor);
|
|
941
|
+
store.setColor(newColor);
|
|
942
|
+
store.setHsv(newHsv);
|
|
943
|
+
}
|
|
944
|
+
} catch (error) {
|
|
945
|
+
if (error instanceof DOMException && error.name === "AbortError") return;
|
|
946
|
+
console.warn("L\u1ED7i EyeDropper:", error);
|
|
947
|
+
}
|
|
948
|
+
}, [color, store]);
|
|
949
|
+
const hasEyeDropper = React.useSyncExternalStore(
|
|
950
|
+
subscribeToEyeDropper,
|
|
951
|
+
getEyeDropperSnapshot,
|
|
952
|
+
getEyeDropperServerSnapshot
|
|
953
|
+
);
|
|
954
|
+
if (!hasEyeDropper) return null;
|
|
955
|
+
const size = sizeProp ?? (children ? "default" : "icon");
|
|
956
|
+
return /* @__PURE__ */ jsx(
|
|
957
|
+
Button,
|
|
958
|
+
{
|
|
959
|
+
"data-slot": "color-picker-eye-dropper",
|
|
960
|
+
"aria-label": children ? void 0 : context.labels.eyeDropper,
|
|
961
|
+
...buttonProps,
|
|
962
|
+
variant: "outline",
|
|
963
|
+
size,
|
|
964
|
+
onClick: onEyeDropper,
|
|
965
|
+
disabled: isDisabled,
|
|
966
|
+
children: children ?? /* @__PURE__ */ jsx(Pipette, { className: "size-4", "aria-hidden": "true" })
|
|
967
|
+
}
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
function ColorPickerFormatSelect(props) {
|
|
971
|
+
const { size, disabled, className, "aria-label": ariaLabel, ...selectProps } = props;
|
|
972
|
+
const context = useColorPickerContext(FORMAT_SELECT_NAME);
|
|
973
|
+
const store = useStoreContext(FORMAT_SELECT_NAME);
|
|
974
|
+
const isDisabled = disabled || context.disabled;
|
|
975
|
+
const format = useStore((state) => state.format);
|
|
976
|
+
const onFormatChange = React.useCallback(
|
|
977
|
+
(value) => {
|
|
978
|
+
store.setFormat(value);
|
|
979
|
+
},
|
|
980
|
+
[store]
|
|
981
|
+
);
|
|
982
|
+
return /* @__PURE__ */ jsxs(
|
|
983
|
+
Select,
|
|
984
|
+
{
|
|
985
|
+
"data-slot": "color-picker-format-select",
|
|
986
|
+
...selectProps,
|
|
987
|
+
value: format,
|
|
988
|
+
onValueChange: onFormatChange,
|
|
989
|
+
disabled: isDisabled,
|
|
990
|
+
children: [
|
|
991
|
+
/* @__PURE__ */ jsx(
|
|
992
|
+
SelectTrigger,
|
|
993
|
+
{
|
|
994
|
+
"data-slot": "color-picker-format-select-trigger",
|
|
995
|
+
"aria-label": ariaLabel ?? context.labels.formatSelect,
|
|
996
|
+
size: size ?? "sm",
|
|
997
|
+
className: cn(className),
|
|
998
|
+
children: /* @__PURE__ */ jsx(SelectValue, {})
|
|
999
|
+
}
|
|
1000
|
+
),
|
|
1001
|
+
/* @__PURE__ */ jsx(SelectContent, { children: colorFormats.map((format2) => /* @__PURE__ */ jsx(SelectItem, { value: format2, children: format2.toUpperCase() }, format2)) })
|
|
1002
|
+
]
|
|
1003
|
+
}
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
function ColorPickerInput(props) {
|
|
1007
|
+
const store = useStoreContext(INPUT_NAME);
|
|
1008
|
+
const context = useColorPickerContext(INPUT_NAME);
|
|
1009
|
+
const color = useStore((state) => state.color);
|
|
1010
|
+
const format = useStore((state) => state.format);
|
|
1011
|
+
const hsv = useStore((state) => state.hsv);
|
|
1012
|
+
const onColorChange = React.useCallback(
|
|
1013
|
+
(newColor) => {
|
|
1014
|
+
const newHsv = rgbToHsv(newColor);
|
|
1015
|
+
store.setColor(newColor);
|
|
1016
|
+
store.setHsv(newHsv);
|
|
1017
|
+
},
|
|
1018
|
+
[store]
|
|
1019
|
+
);
|
|
1020
|
+
if (format === "hex") {
|
|
1021
|
+
return /* @__PURE__ */ jsx(HexInput, { color, onColorChange, context, ...props });
|
|
1022
|
+
}
|
|
1023
|
+
if (format === "rgb") {
|
|
1024
|
+
return /* @__PURE__ */ jsx(RgbInput, { color, onColorChange, context, ...props });
|
|
1025
|
+
}
|
|
1026
|
+
if (format === "hsl") {
|
|
1027
|
+
return /* @__PURE__ */ jsx(HslInput, { color, onColorChange, context, ...props });
|
|
1028
|
+
}
|
|
1029
|
+
if (format === "hsb") {
|
|
1030
|
+
return /* @__PURE__ */ jsx(HsbInput, { hsv, onColorChange, context, ...props });
|
|
1031
|
+
}
|
|
1032
|
+
return null;
|
|
1033
|
+
}
|
|
1034
|
+
const inputGroupItemVariants = cva("h-8 focus:z-10", {
|
|
1035
|
+
variants: {
|
|
1036
|
+
position: {
|
|
1037
|
+
first: "rounded-r-none",
|
|
1038
|
+
middle: "-ml-px rounded-none border-l-0",
|
|
1039
|
+
last: "-ml-px rounded-l-none border-l-0",
|
|
1040
|
+
isolated: ""
|
|
1041
|
+
}
|
|
1042
|
+
},
|
|
1043
|
+
defaultVariants: {
|
|
1044
|
+
position: "isolated"
|
|
1045
|
+
}
|
|
1046
|
+
});
|
|
1047
|
+
function InputGroupItem({ className, position, value, onChange, onBlur, ...props }) {
|
|
1048
|
+
const [draft, setDraft] = React.useState(null);
|
|
1049
|
+
return /* @__PURE__ */ jsx(
|
|
1050
|
+
Input,
|
|
1051
|
+
{
|
|
1052
|
+
"data-slot": "color-picker-input",
|
|
1053
|
+
className: cn(inputGroupItemVariants({ position, className })),
|
|
1054
|
+
...props,
|
|
1055
|
+
value: draft ?? value,
|
|
1056
|
+
onChange: (event) => {
|
|
1057
|
+
setDraft(event.target.value);
|
|
1058
|
+
onChange?.(event);
|
|
1059
|
+
},
|
|
1060
|
+
onBlur: (event) => {
|
|
1061
|
+
setDraft(null);
|
|
1062
|
+
onBlur?.(event);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
);
|
|
1066
|
+
}
|
|
1067
|
+
function HexInput(props) {
|
|
1068
|
+
const { color, onColorChange, context, withoutAlpha, className, ...inputProps } = props;
|
|
1069
|
+
const hexValue = rgbToHex(color);
|
|
1070
|
+
const alphaValue = Math.round((color?.a ?? 1) * 100);
|
|
1071
|
+
const onHexChange = React.useCallback(
|
|
1072
|
+
(event) => {
|
|
1073
|
+
const raw = event.target.value.trim();
|
|
1074
|
+
const value = /^(?:[a-f\d]{3}|[a-f\d]{6})$/i.test(raw) ? `#${raw}` : raw;
|
|
1075
|
+
const parsedColor = parseColorString(value);
|
|
1076
|
+
if (parsedColor) {
|
|
1077
|
+
onColorChange({ ...parsedColor, a: color?.a ?? 1 });
|
|
1078
|
+
}
|
|
1079
|
+
},
|
|
1080
|
+
[color, onColorChange]
|
|
1081
|
+
);
|
|
1082
|
+
const onAlphaChange = React.useCallback(
|
|
1083
|
+
(event) => {
|
|
1084
|
+
const value = Number.parseInt(event.target.value, 10);
|
|
1085
|
+
if (!Number.isNaN(value) && value >= 0 && value <= 100) {
|
|
1086
|
+
onColorChange({ ...color, a: value / 100 });
|
|
1087
|
+
}
|
|
1088
|
+
},
|
|
1089
|
+
[color, onColorChange]
|
|
1090
|
+
);
|
|
1091
|
+
if (withoutAlpha) {
|
|
1092
|
+
return /* @__PURE__ */ jsx(
|
|
1093
|
+
InputGroupItem,
|
|
1094
|
+
{
|
|
1095
|
+
"aria-label": context.labels.hexInput,
|
|
1096
|
+
position: "isolated",
|
|
1097
|
+
...inputProps,
|
|
1098
|
+
placeholder: "#000000",
|
|
1099
|
+
className: cn("font-mono", className),
|
|
1100
|
+
value: hexValue,
|
|
1101
|
+
onChange: onHexChange,
|
|
1102
|
+
disabled: context.disabled,
|
|
1103
|
+
readOnly: context.readOnly
|
|
1104
|
+
}
|
|
1105
|
+
);
|
|
1106
|
+
}
|
|
1107
|
+
return /* @__PURE__ */ jsxs("div", { "data-slot": "color-picker-input-wrapper", className: cn("flex items-center", className), children: [
|
|
1108
|
+
/* @__PURE__ */ jsx(
|
|
1109
|
+
InputGroupItem,
|
|
1110
|
+
{
|
|
1111
|
+
"aria-label": context.labels.hexInput,
|
|
1112
|
+
position: "first",
|
|
1113
|
+
...inputProps,
|
|
1114
|
+
placeholder: "#000000",
|
|
1115
|
+
className: "flex-1 font-mono",
|
|
1116
|
+
value: hexValue,
|
|
1117
|
+
onChange: onHexChange,
|
|
1118
|
+
disabled: context.disabled,
|
|
1119
|
+
readOnly: context.readOnly
|
|
1120
|
+
}
|
|
1121
|
+
),
|
|
1122
|
+
/* @__PURE__ */ jsx(
|
|
1123
|
+
InputGroupItem,
|
|
1124
|
+
{
|
|
1125
|
+
"aria-label": context.labels.alphaInput,
|
|
1126
|
+
position: "last",
|
|
1127
|
+
...inputProps,
|
|
1128
|
+
placeholder: "100",
|
|
1129
|
+
inputMode: "numeric",
|
|
1130
|
+
pattern: "[0-9]*",
|
|
1131
|
+
min: "0",
|
|
1132
|
+
max: "100",
|
|
1133
|
+
className: "w-14",
|
|
1134
|
+
value: alphaValue,
|
|
1135
|
+
onChange: onAlphaChange,
|
|
1136
|
+
disabled: context.disabled,
|
|
1137
|
+
readOnly: context.readOnly
|
|
1138
|
+
}
|
|
1139
|
+
)
|
|
1140
|
+
] });
|
|
1141
|
+
}
|
|
1142
|
+
function RgbInput(props) {
|
|
1143
|
+
const { color, onColorChange, context, withoutAlpha, className, ...inputProps } = props;
|
|
1144
|
+
const rValue = Math.round(color?.r ?? 0);
|
|
1145
|
+
const gValue = Math.round(color?.g ?? 0);
|
|
1146
|
+
const bValue = Math.round(color?.b ?? 0);
|
|
1147
|
+
const alphaValue = Math.round((color?.a ?? 1) * 100);
|
|
1148
|
+
const onChannelChange = React.useCallback(
|
|
1149
|
+
(channel, max, isAlpha = false) => (event) => {
|
|
1150
|
+
const value = Number.parseInt(event.target.value, 10);
|
|
1151
|
+
if (!Number.isNaN(value) && value >= 0 && value <= max) {
|
|
1152
|
+
const newValue = isAlpha ? value / 100 : value;
|
|
1153
|
+
onColorChange({ ...color, [channel]: newValue });
|
|
1154
|
+
}
|
|
1155
|
+
},
|
|
1156
|
+
[color, onColorChange]
|
|
1157
|
+
);
|
|
1158
|
+
return /* @__PURE__ */ jsxs("div", { "data-slot": "color-picker-input-wrapper", className: cn("flex items-center", className), children: [
|
|
1159
|
+
/* @__PURE__ */ jsx(
|
|
1160
|
+
InputGroupItem,
|
|
1161
|
+
{
|
|
1162
|
+
"aria-label": context.labels.redInput,
|
|
1163
|
+
position: "first",
|
|
1164
|
+
...inputProps,
|
|
1165
|
+
placeholder: "0",
|
|
1166
|
+
inputMode: "numeric",
|
|
1167
|
+
pattern: "[0-9]*",
|
|
1168
|
+
min: "0",
|
|
1169
|
+
max: "255",
|
|
1170
|
+
className: "w-14",
|
|
1171
|
+
value: rValue,
|
|
1172
|
+
onChange: onChannelChange("r", 255),
|
|
1173
|
+
disabled: context.disabled,
|
|
1174
|
+
readOnly: context.readOnly
|
|
1175
|
+
}
|
|
1176
|
+
),
|
|
1177
|
+
/* @__PURE__ */ jsx(
|
|
1178
|
+
InputGroupItem,
|
|
1179
|
+
{
|
|
1180
|
+
"aria-label": context.labels.greenInput,
|
|
1181
|
+
position: "middle",
|
|
1182
|
+
...inputProps,
|
|
1183
|
+
placeholder: "0",
|
|
1184
|
+
inputMode: "numeric",
|
|
1185
|
+
pattern: "[0-9]*",
|
|
1186
|
+
min: "0",
|
|
1187
|
+
max: "255",
|
|
1188
|
+
className: "w-14",
|
|
1189
|
+
value: gValue,
|
|
1190
|
+
onChange: onChannelChange("g", 255),
|
|
1191
|
+
disabled: context.disabled,
|
|
1192
|
+
readOnly: context.readOnly
|
|
1193
|
+
}
|
|
1194
|
+
),
|
|
1195
|
+
/* @__PURE__ */ jsx(
|
|
1196
|
+
InputGroupItem,
|
|
1197
|
+
{
|
|
1198
|
+
"aria-label": context.labels.blueInput,
|
|
1199
|
+
position: withoutAlpha ? "last" : "middle",
|
|
1200
|
+
...inputProps,
|
|
1201
|
+
placeholder: "0",
|
|
1202
|
+
inputMode: "numeric",
|
|
1203
|
+
pattern: "[0-9]*",
|
|
1204
|
+
min: "0",
|
|
1205
|
+
max: "255",
|
|
1206
|
+
className: "w-14",
|
|
1207
|
+
value: bValue,
|
|
1208
|
+
onChange: onChannelChange("b", 255),
|
|
1209
|
+
disabled: context.disabled,
|
|
1210
|
+
readOnly: context.readOnly
|
|
1211
|
+
}
|
|
1212
|
+
),
|
|
1213
|
+
!withoutAlpha && /* @__PURE__ */ jsx(
|
|
1214
|
+
InputGroupItem,
|
|
1215
|
+
{
|
|
1216
|
+
"aria-label": context.labels.alphaInput,
|
|
1217
|
+
position: "last",
|
|
1218
|
+
...inputProps,
|
|
1219
|
+
placeholder: "100",
|
|
1220
|
+
inputMode: "numeric",
|
|
1221
|
+
pattern: "[0-9]*",
|
|
1222
|
+
min: "0",
|
|
1223
|
+
max: "100",
|
|
1224
|
+
className: "w-14",
|
|
1225
|
+
value: alphaValue,
|
|
1226
|
+
onChange: onChannelChange("a", 100, true),
|
|
1227
|
+
disabled: context.disabled,
|
|
1228
|
+
readOnly: context.readOnly
|
|
1229
|
+
}
|
|
1230
|
+
)
|
|
1231
|
+
] });
|
|
1232
|
+
}
|
|
1233
|
+
function HslInput(props) {
|
|
1234
|
+
const { color, onColorChange, context, withoutAlpha, className, ...inputProps } = props;
|
|
1235
|
+
const hsl = React.useMemo(() => rgbToHsl(color), [color]);
|
|
1236
|
+
const alphaValue = Math.round((color?.a ?? 1) * 100);
|
|
1237
|
+
const onHslChannelChange = React.useCallback(
|
|
1238
|
+
(channel, max) => (event) => {
|
|
1239
|
+
const value = Number.parseInt(event.target.value, 10);
|
|
1240
|
+
if (!Number.isNaN(value) && value >= 0 && value <= max) {
|
|
1241
|
+
const newHsl = { ...hsl, [channel]: value };
|
|
1242
|
+
const newColor = hslToRgb(newHsl, color?.a ?? 1);
|
|
1243
|
+
onColorChange(newColor);
|
|
1244
|
+
}
|
|
1245
|
+
},
|
|
1246
|
+
[hsl, color, onColorChange]
|
|
1247
|
+
);
|
|
1248
|
+
const onAlphaChange = React.useCallback(
|
|
1249
|
+
(event) => {
|
|
1250
|
+
const value = Number.parseInt(event.target.value, 10);
|
|
1251
|
+
if (!Number.isNaN(value) && value >= 0 && value <= 100) {
|
|
1252
|
+
onColorChange({ ...color, a: value / 100 });
|
|
1253
|
+
}
|
|
1254
|
+
},
|
|
1255
|
+
[color, onColorChange]
|
|
1256
|
+
);
|
|
1257
|
+
return /* @__PURE__ */ jsxs("div", { "data-slot": "color-picker-input-wrapper", className: cn("flex items-center", className), children: [
|
|
1258
|
+
/* @__PURE__ */ jsx(
|
|
1259
|
+
InputGroupItem,
|
|
1260
|
+
{
|
|
1261
|
+
"aria-label": context.labels.hueInput,
|
|
1262
|
+
position: "first",
|
|
1263
|
+
...inputProps,
|
|
1264
|
+
placeholder: "0",
|
|
1265
|
+
inputMode: "numeric",
|
|
1266
|
+
pattern: "[0-9]*",
|
|
1267
|
+
min: "0",
|
|
1268
|
+
max: "360",
|
|
1269
|
+
className: "w-14",
|
|
1270
|
+
value: hsl.h,
|
|
1271
|
+
onChange: onHslChannelChange("h", 360),
|
|
1272
|
+
disabled: context.disabled,
|
|
1273
|
+
readOnly: context.readOnly
|
|
1274
|
+
}
|
|
1275
|
+
),
|
|
1276
|
+
/* @__PURE__ */ jsx(
|
|
1277
|
+
InputGroupItem,
|
|
1278
|
+
{
|
|
1279
|
+
"aria-label": context.labels.saturationInput,
|
|
1280
|
+
position: "middle",
|
|
1281
|
+
...inputProps,
|
|
1282
|
+
placeholder: "0",
|
|
1283
|
+
inputMode: "numeric",
|
|
1284
|
+
pattern: "[0-9]*",
|
|
1285
|
+
min: "0",
|
|
1286
|
+
max: "100",
|
|
1287
|
+
className: "w-14",
|
|
1288
|
+
value: hsl.s,
|
|
1289
|
+
onChange: onHslChannelChange("s", 100),
|
|
1290
|
+
disabled: context.disabled,
|
|
1291
|
+
readOnly: context.readOnly
|
|
1292
|
+
}
|
|
1293
|
+
),
|
|
1294
|
+
/* @__PURE__ */ jsx(
|
|
1295
|
+
InputGroupItem,
|
|
1296
|
+
{
|
|
1297
|
+
"aria-label": context.labels.lightnessInput,
|
|
1298
|
+
position: withoutAlpha ? "last" : "middle",
|
|
1299
|
+
...inputProps,
|
|
1300
|
+
placeholder: "0",
|
|
1301
|
+
inputMode: "numeric",
|
|
1302
|
+
pattern: "[0-9]*",
|
|
1303
|
+
min: "0",
|
|
1304
|
+
max: "100",
|
|
1305
|
+
className: "w-14",
|
|
1306
|
+
value: hsl.l,
|
|
1307
|
+
onChange: onHslChannelChange("l", 100),
|
|
1308
|
+
disabled: context.disabled,
|
|
1309
|
+
readOnly: context.readOnly
|
|
1310
|
+
}
|
|
1311
|
+
),
|
|
1312
|
+
!withoutAlpha && /* @__PURE__ */ jsx(
|
|
1313
|
+
InputGroupItem,
|
|
1314
|
+
{
|
|
1315
|
+
"aria-label": context.labels.alphaInput,
|
|
1316
|
+
position: "last",
|
|
1317
|
+
...inputProps,
|
|
1318
|
+
placeholder: "100",
|
|
1319
|
+
inputMode: "numeric",
|
|
1320
|
+
pattern: "[0-9]*",
|
|
1321
|
+
min: "0",
|
|
1322
|
+
max: "100",
|
|
1323
|
+
className: "w-14",
|
|
1324
|
+
value: alphaValue,
|
|
1325
|
+
onChange: onAlphaChange,
|
|
1326
|
+
disabled: context.disabled,
|
|
1327
|
+
readOnly: context.readOnly
|
|
1328
|
+
}
|
|
1329
|
+
)
|
|
1330
|
+
] });
|
|
1331
|
+
}
|
|
1332
|
+
function HsbInput(props) {
|
|
1333
|
+
const { hsv, onColorChange, context, withoutAlpha, className, ...inputProps } = props;
|
|
1334
|
+
const alphaValue = Math.round((hsv?.a ?? 1) * 100);
|
|
1335
|
+
const onHsvChannelChange = React.useCallback(
|
|
1336
|
+
(channel, max) => (event) => {
|
|
1337
|
+
const value = Number.parseInt(event.target.value, 10);
|
|
1338
|
+
if (!Number.isNaN(value) && value >= 0 && value <= max) {
|
|
1339
|
+
const newHsv = { ...hsv, [channel]: value };
|
|
1340
|
+
const newColor = hsvToRgb(newHsv);
|
|
1341
|
+
onColorChange(newColor);
|
|
1342
|
+
}
|
|
1343
|
+
},
|
|
1344
|
+
[hsv, onColorChange]
|
|
1345
|
+
);
|
|
1346
|
+
const onAlphaChange = React.useCallback(
|
|
1347
|
+
(event) => {
|
|
1348
|
+
const value = Number.parseInt(event.target.value, 10);
|
|
1349
|
+
if (!Number.isNaN(value) && value >= 0 && value <= 100) {
|
|
1350
|
+
const currentColor = hsvToRgb(hsv);
|
|
1351
|
+
onColorChange({ ...currentColor, a: value / 100 });
|
|
1352
|
+
}
|
|
1353
|
+
},
|
|
1354
|
+
[hsv, onColorChange]
|
|
1355
|
+
);
|
|
1356
|
+
return /* @__PURE__ */ jsxs("div", { "data-slot": "color-picker-input-wrapper", className: cn("flex items-center", className), children: [
|
|
1357
|
+
/* @__PURE__ */ jsx(
|
|
1358
|
+
InputGroupItem,
|
|
1359
|
+
{
|
|
1360
|
+
"aria-label": context.labels.hueInput,
|
|
1361
|
+
position: "first",
|
|
1362
|
+
...inputProps,
|
|
1363
|
+
placeholder: "0",
|
|
1364
|
+
inputMode: "numeric",
|
|
1365
|
+
pattern: "[0-9]*",
|
|
1366
|
+
min: "0",
|
|
1367
|
+
max: "360",
|
|
1368
|
+
className: "w-14",
|
|
1369
|
+
value: hsv?.h ?? 0,
|
|
1370
|
+
onChange: onHsvChannelChange("h", 360),
|
|
1371
|
+
disabled: context.disabled,
|
|
1372
|
+
readOnly: context.readOnly
|
|
1373
|
+
}
|
|
1374
|
+
),
|
|
1375
|
+
/* @__PURE__ */ jsx(
|
|
1376
|
+
InputGroupItem,
|
|
1377
|
+
{
|
|
1378
|
+
"aria-label": context.labels.saturationInput,
|
|
1379
|
+
position: "middle",
|
|
1380
|
+
...inputProps,
|
|
1381
|
+
placeholder: "0",
|
|
1382
|
+
inputMode: "numeric",
|
|
1383
|
+
pattern: "[0-9]*",
|
|
1384
|
+
min: "0",
|
|
1385
|
+
max: "100",
|
|
1386
|
+
className: "w-14",
|
|
1387
|
+
value: hsv?.s ?? 0,
|
|
1388
|
+
onChange: onHsvChannelChange("s", 100),
|
|
1389
|
+
disabled: context.disabled,
|
|
1390
|
+
readOnly: context.readOnly
|
|
1391
|
+
}
|
|
1392
|
+
),
|
|
1393
|
+
/* @__PURE__ */ jsx(
|
|
1394
|
+
InputGroupItem,
|
|
1395
|
+
{
|
|
1396
|
+
"aria-label": context.labels.brightnessInput,
|
|
1397
|
+
position: withoutAlpha ? "last" : "middle",
|
|
1398
|
+
...inputProps,
|
|
1399
|
+
placeholder: "0",
|
|
1400
|
+
inputMode: "numeric",
|
|
1401
|
+
pattern: "[0-9]*",
|
|
1402
|
+
min: "0",
|
|
1403
|
+
max: "100",
|
|
1404
|
+
className: "w-14",
|
|
1405
|
+
value: hsv?.v ?? 0,
|
|
1406
|
+
onChange: onHsvChannelChange("v", 100),
|
|
1407
|
+
disabled: context.disabled,
|
|
1408
|
+
readOnly: context.readOnly
|
|
1409
|
+
}
|
|
1410
|
+
),
|
|
1411
|
+
!withoutAlpha && /* @__PURE__ */ jsx(
|
|
1412
|
+
InputGroupItem,
|
|
1413
|
+
{
|
|
1414
|
+
"aria-label": context.labels.alphaInput,
|
|
1415
|
+
position: "last",
|
|
1416
|
+
...inputProps,
|
|
1417
|
+
placeholder: "100",
|
|
1418
|
+
inputMode: "numeric",
|
|
1419
|
+
pattern: "[0-9]*",
|
|
1420
|
+
min: "0",
|
|
1421
|
+
max: "100",
|
|
1422
|
+
className: "w-14",
|
|
1423
|
+
value: alphaValue,
|
|
1424
|
+
onChange: onAlphaChange,
|
|
1425
|
+
disabled: context.disabled,
|
|
1426
|
+
readOnly: context.readOnly
|
|
1427
|
+
}
|
|
1428
|
+
)
|
|
1429
|
+
] });
|
|
1430
|
+
}
|
|
1431
|
+
export {
|
|
1432
|
+
ColorPicker,
|
|
1433
|
+
ColorPickerAlphaSlider,
|
|
1434
|
+
ColorPickerArea,
|
|
1435
|
+
ColorPickerContent,
|
|
1436
|
+
ColorPickerEyeDropper,
|
|
1437
|
+
ColorPickerFormatSelect,
|
|
1438
|
+
ColorPickerHueSlider,
|
|
1439
|
+
ColorPickerInput,
|
|
1440
|
+
ColorPickerSwatch,
|
|
1441
|
+
ColorPickerTrigger,
|
|
1442
|
+
useStore as useColorPicker
|
|
1443
|
+
};
|
|
1444
|
+
//# sourceMappingURL=color-picker.js.map
|