@designa8/vibedesign-linear-internal-v1-b737 0.4.0 → 0.6.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 +1 -1
- package/dist/index.cjs +1604 -1168
- package/dist/index.js +1632 -1196
- package/dist/styles.css +34 -0
- package/package.json +11 -1
- package/src/atoms/CodePreview/CodePreview.tsx +161 -0
- package/src/atoms/CodePreview/index.ts +2 -0
- package/src/atoms/CodePreview/preview.tsx +74 -0
- package/src/atoms/ColorPicker/ColorPicker.tsx +394 -0
- package/src/atoms/ColorPicker/index.ts +7 -0
- package/src/atoms/ColorPicker/preview.tsx +182 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { Badge } from "../Badge";
|
|
3
|
+
import { Input } from "../Input";
|
|
4
|
+
|
|
5
|
+
/* ------------------------------------------------------------------ */
|
|
6
|
+
/* Colour math */
|
|
7
|
+
/* ------------------------------------------------------------------ */
|
|
8
|
+
|
|
9
|
+
export interface Hsv {
|
|
10
|
+
h: number;
|
|
11
|
+
s: number;
|
|
12
|
+
v: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const clamp = (n: number, min: number, max: number) => Math.min(max, Math.max(min, n));
|
|
16
|
+
|
|
17
|
+
const cn = (...parts: Array<string | false | null | undefined>) =>
|
|
18
|
+
parts.filter(Boolean).join(" ");
|
|
19
|
+
|
|
20
|
+
/** Returns "#RRGGBB" uppercase, or null when the text is not a valid hex. */
|
|
21
|
+
export function parseHex(text: string, allowShort: boolean): string | null {
|
|
22
|
+
const t = text.trim().replace(/^#/, "");
|
|
23
|
+
if (/^[0-9a-fA-F]{6}$/.test(t)) return `#${t.toUpperCase()}`;
|
|
24
|
+
if (allowShort && /^[0-9a-fA-F]{3}$/.test(t)) {
|
|
25
|
+
return `#${t
|
|
26
|
+
.split("")
|
|
27
|
+
.map((c) => c + c)
|
|
28
|
+
.join("")
|
|
29
|
+
.toUpperCase()}`;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function hexToRgb(hex: string): [number, number, number] {
|
|
35
|
+
const n = parseInt(hex.replace("#", ""), 16);
|
|
36
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function rgbToHex(r: number, g: number, b: number): string {
|
|
40
|
+
const to = (x: number) => Math.round(clamp(x, 0, 255)).toString(16).padStart(2, "0");
|
|
41
|
+
return `#${to(r)}${to(g)}${to(b)}`.toUpperCase();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function hexToHsv(hex: string): Hsv {
|
|
45
|
+
const [r, g, b] = hexToRgb(hex).map((c) => c / 255);
|
|
46
|
+
const max = Math.max(r, g, b);
|
|
47
|
+
const min = Math.min(r, g, b);
|
|
48
|
+
const d = max - min;
|
|
49
|
+
let h = 0;
|
|
50
|
+
if (d !== 0) {
|
|
51
|
+
if (max === r) h = ((g - b) / d) % 6;
|
|
52
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
53
|
+
else h = (r - g) / d + 4;
|
|
54
|
+
h *= 60;
|
|
55
|
+
if (h < 0) h += 360;
|
|
56
|
+
}
|
|
57
|
+
return { h, s: max === 0 ? 0 : (d / max) * 100, v: max * 100 };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function hsvToHex({ h, s, v }: Hsv): string {
|
|
61
|
+
const S = s / 100;
|
|
62
|
+
const V = v / 100;
|
|
63
|
+
const c = V * S;
|
|
64
|
+
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
65
|
+
const m = V - c;
|
|
66
|
+
let rgb: [number, number, number] = [0, 0, 0];
|
|
67
|
+
if (h < 60) rgb = [c, x, 0];
|
|
68
|
+
else if (h < 120) rgb = [x, c, 0];
|
|
69
|
+
else if (h < 180) rgb = [0, c, x];
|
|
70
|
+
else if (h < 240) rgb = [0, x, c];
|
|
71
|
+
else if (h < 300) rgb = [x, 0, c];
|
|
72
|
+
else rgb = [c, 0, x];
|
|
73
|
+
return rgbToHex((rgb[0] + m) * 255, (rgb[1] + m) * 255, (rgb[2] + m) * 255);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function luminance(hex: string): number {
|
|
77
|
+
const [r, g, b] = hexToRgb(hex).map((c) => {
|
|
78
|
+
const x = c / 255;
|
|
79
|
+
return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
|
|
80
|
+
});
|
|
81
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function contrastRatio(a: string, b: string): number {
|
|
85
|
+
const la = luminance(a);
|
|
86
|
+
const lb = luminance(b);
|
|
87
|
+
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type ContrastTone = "pass" | "large" | "fail";
|
|
91
|
+
|
|
92
|
+
export function contrastRating(ratio: number): { label: string; tone: ContrastTone } {
|
|
93
|
+
const text = `${ratio.toFixed(1)}:1`;
|
|
94
|
+
if (ratio >= 7) return { label: `AAA ${text}`, tone: "pass" };
|
|
95
|
+
if (ratio >= 4.5) return { label: `AA ${text}`, tone: "pass" };
|
|
96
|
+
if (ratio >= 3) return { label: `AA Large ${text}`, tone: "large" };
|
|
97
|
+
return { label: `Fails AA ${text}`, tone: "fail" };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* Colour-math constants, not design values: the hue wheel, the pure white /
|
|
101
|
+
black overlays that turn a hue into a saturation/brightness square, and the
|
|
102
|
+
white thumb ring that has to stay legible on any sampled colour. */
|
|
103
|
+
const HUE_GRADIENT =
|
|
104
|
+
"linear-gradient(to right, hsl(0 100% 50%), hsl(60 100% 50%), hsl(120 100% 50%), hsl(180 100% 50%), hsl(240 100% 50%), hsl(300 100% 50%), hsl(360 100% 50%))";
|
|
105
|
+
const SAT_OVERLAY = "linear-gradient(to right, hsl(0 0% 100%), hsl(0 0% 100% / 0))";
|
|
106
|
+
const VAL_OVERLAY = "linear-gradient(to top, hsl(0 0% 0%), hsl(0 0% 0% / 0))";
|
|
107
|
+
const THUMB_RING = "hsl(0 0% 100%)";
|
|
108
|
+
|
|
109
|
+
/* ------------------------------------------------------------------ */
|
|
110
|
+
/* Contrast badge */
|
|
111
|
+
/* ------------------------------------------------------------------ */
|
|
112
|
+
|
|
113
|
+
export interface ContrastBadgeProps extends React.HTMLAttributes<HTMLSpanElement> {
|
|
114
|
+
hex: string;
|
|
115
|
+
against: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const ContrastBadge = React.forwardRef<HTMLSpanElement, ContrastBadgeProps>(
|
|
119
|
+
({ hex, against, className, ...rest }, ref) => {
|
|
120
|
+
const rating = contrastRating(contrastRatio(hex, against));
|
|
121
|
+
const tone =
|
|
122
|
+
rating.tone === "pass" ? "success" : rating.tone === "large" ? "warning" : "error";
|
|
123
|
+
return (
|
|
124
|
+
<Badge
|
|
125
|
+
ref={ref}
|
|
126
|
+
size="lg"
|
|
127
|
+
tone={tone}
|
|
128
|
+
aria-label={`Contrast ${rating.label} against ${against}`}
|
|
129
|
+
className={cn("transition-colors duration-150 ease-out", className)}
|
|
130
|
+
{...rest}
|
|
131
|
+
>
|
|
132
|
+
{rating.label}
|
|
133
|
+
</Badge>
|
|
134
|
+
);
|
|
135
|
+
},
|
|
136
|
+
);
|
|
137
|
+
ContrastBadge.displayName = "ContrastBadge";
|
|
138
|
+
|
|
139
|
+
/* ------------------------------------------------------------------ */
|
|
140
|
+
/* Hue slider */
|
|
141
|
+
/* ------------------------------------------------------------------ */
|
|
142
|
+
|
|
143
|
+
interface HueSliderProps {
|
|
144
|
+
label: string;
|
|
145
|
+
value: number;
|
|
146
|
+
onChange: (h: number) => void;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function HueSlider({ label, value, onChange }: HueSliderProps) {
|
|
150
|
+
const trackRef = React.useRef<HTMLDivElement>(null);
|
|
151
|
+
const [dragging, setDragging] = React.useState(false);
|
|
152
|
+
const ratio = clamp(value / 360, 0, 1);
|
|
153
|
+
const emit = (raw: number) => onChange(clamp(Math.round(raw), 0, 360));
|
|
154
|
+
|
|
155
|
+
const fromPointer = (clientX: number) => {
|
|
156
|
+
const rect = trackRef.current?.getBoundingClientRect();
|
|
157
|
+
if (!rect || rect.width === 0) return;
|
|
158
|
+
emit(clamp((clientX - rect.left) / rect.width, 0, 1) * 360);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
162
|
+
const step = e.shiftKey ? 10 : 1;
|
|
163
|
+
if (e.key === "ArrowRight" || e.key === "ArrowUp") emit(value + step);
|
|
164
|
+
else if (e.key === "ArrowLeft" || e.key === "ArrowDown") emit(value - step);
|
|
165
|
+
else if (e.key === "Home") emit(0);
|
|
166
|
+
else if (e.key === "End") emit(360);
|
|
167
|
+
else return;
|
|
168
|
+
e.preventDefault();
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<div
|
|
173
|
+
ref={trackRef}
|
|
174
|
+
role="slider"
|
|
175
|
+
tabIndex={0}
|
|
176
|
+
aria-label={label}
|
|
177
|
+
aria-valuemin={0}
|
|
178
|
+
aria-valuemax={360}
|
|
179
|
+
aria-valuenow={Math.round(value)}
|
|
180
|
+
aria-valuetext={`Hue ${Math.round(value)} degrees`}
|
|
181
|
+
aria-orientation="horizontal"
|
|
182
|
+
onKeyDown={onKeyDown}
|
|
183
|
+
onPointerDown={(e) => {
|
|
184
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
185
|
+
setDragging(true);
|
|
186
|
+
fromPointer(e.clientX);
|
|
187
|
+
}}
|
|
188
|
+
onPointerMove={(e) => {
|
|
189
|
+
if (dragging) fromPointer(e.clientX);
|
|
190
|
+
}}
|
|
191
|
+
onPointerUp={() => setDragging(false)}
|
|
192
|
+
onPointerCancel={() => setDragging(false)}
|
|
193
|
+
className="relative h-6 w-full cursor-pointer touch-none rounded-full focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus-blue"
|
|
194
|
+
>
|
|
195
|
+
<span
|
|
196
|
+
aria-hidden="true"
|
|
197
|
+
className="absolute inset-x-0 top-1/2 h-3 -translate-y-1/2 rounded-full"
|
|
198
|
+
style={{ backgroundImage: HUE_GRADIENT }}
|
|
199
|
+
/>
|
|
200
|
+
<span
|
|
201
|
+
aria-hidden="true"
|
|
202
|
+
data-dragging={dragging}
|
|
203
|
+
className="absolute top-1/2 size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-canvas shadow-md transition-transform duration-150 ease-out data-[dragging=true]:scale-110"
|
|
204
|
+
style={{ left: `${ratio * 100}%`, backgroundColor: `hsl(${value} 100% 50%)` }}
|
|
205
|
+
/>
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/* ------------------------------------------------------------------ */
|
|
211
|
+
/* Saturation / brightness square */
|
|
212
|
+
/* ------------------------------------------------------------------ */
|
|
213
|
+
|
|
214
|
+
interface SvSquareProps {
|
|
215
|
+
label: string;
|
|
216
|
+
hsv: Hsv;
|
|
217
|
+
onChange: (next: { s: number; v: number }) => void;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function SvSquare({ label, hsv, onChange }: SvSquareProps) {
|
|
221
|
+
const areaRef = React.useRef<HTMLDivElement>(null);
|
|
222
|
+
const [dragging, setDragging] = React.useState(false);
|
|
223
|
+
|
|
224
|
+
const fromPointer = (x: number, y: number) => {
|
|
225
|
+
const rect = areaRef.current?.getBoundingClientRect();
|
|
226
|
+
if (!rect || rect.width === 0 || rect.height === 0) return;
|
|
227
|
+
onChange({
|
|
228
|
+
s: clamp(((x - rect.left) / rect.width) * 100, 0, 100),
|
|
229
|
+
v: clamp((1 - (y - rect.top) / rect.height) * 100, 0, 100),
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
234
|
+
const step = e.shiftKey ? 10 : 1;
|
|
235
|
+
let s = hsv.s;
|
|
236
|
+
let v = hsv.v;
|
|
237
|
+
if (e.key === "ArrowRight") s += step;
|
|
238
|
+
else if (e.key === "ArrowLeft") s -= step;
|
|
239
|
+
else if (e.key === "ArrowUp") v += step;
|
|
240
|
+
else if (e.key === "ArrowDown") v -= step;
|
|
241
|
+
else return;
|
|
242
|
+
e.preventDefault();
|
|
243
|
+
onChange({ s: clamp(s, 0, 100), v: clamp(v, 0, 100) });
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<div
|
|
248
|
+
ref={areaRef}
|
|
249
|
+
role="slider"
|
|
250
|
+
tabIndex={0}
|
|
251
|
+
aria-label={label}
|
|
252
|
+
aria-valuemin={0}
|
|
253
|
+
aria-valuemax={100}
|
|
254
|
+
aria-valuenow={Math.round(hsv.s)}
|
|
255
|
+
aria-valuetext={`Saturation ${Math.round(hsv.s)}%, brightness ${Math.round(hsv.v)}%`}
|
|
256
|
+
aria-orientation="horizontal"
|
|
257
|
+
onKeyDown={onKeyDown}
|
|
258
|
+
onPointerDown={(e) => {
|
|
259
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
260
|
+
setDragging(true);
|
|
261
|
+
fromPointer(e.clientX, e.clientY);
|
|
262
|
+
}}
|
|
263
|
+
onPointerMove={(e) => {
|
|
264
|
+
if (dragging) fromPointer(e.clientX, e.clientY);
|
|
265
|
+
}}
|
|
266
|
+
onPointerUp={() => setDragging(false)}
|
|
267
|
+
onPointerCancel={() => setDragging(false)}
|
|
268
|
+
className="relative h-24 w-28 shrink-0 cursor-crosshair touch-none overflow-hidden rounded-md shadow-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus-blue"
|
|
269
|
+
>
|
|
270
|
+
<span
|
|
271
|
+
aria-hidden="true"
|
|
272
|
+
className="absolute inset-0"
|
|
273
|
+
style={{ backgroundColor: `hsl(${hsv.h} 100% 50%)` }}
|
|
274
|
+
/>
|
|
275
|
+
<span aria-hidden="true" className="absolute inset-0" style={{ backgroundImage: SAT_OVERLAY }} />
|
|
276
|
+
<span aria-hidden="true" className="absolute inset-0" style={{ backgroundImage: VAL_OVERLAY }} />
|
|
277
|
+
<span
|
|
278
|
+
aria-hidden="true"
|
|
279
|
+
data-dragging={dragging}
|
|
280
|
+
className="absolute size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 shadow-md transition-transform duration-150 ease-out data-[dragging=true]:scale-110"
|
|
281
|
+
style={{
|
|
282
|
+
left: `${hsv.s}%`,
|
|
283
|
+
top: `${100 - hsv.v}%`,
|
|
284
|
+
borderColor: THUMB_RING,
|
|
285
|
+
}}
|
|
286
|
+
/>
|
|
287
|
+
</div>
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/* ------------------------------------------------------------------ */
|
|
292
|
+
/* ColorPicker */
|
|
293
|
+
/* ------------------------------------------------------------------ */
|
|
294
|
+
|
|
295
|
+
export interface ColorPickerProps {
|
|
296
|
+
/** Current colour as "#RRGGBB". */
|
|
297
|
+
value: string;
|
|
298
|
+
/** Fires with a new "#RRGGBB" while dragging the square or the hue slider. */
|
|
299
|
+
onChange: (hex: string) => void;
|
|
300
|
+
/** Hex field text. Defaults to `value`; pass a parent draft to allow partial input. */
|
|
301
|
+
text?: string;
|
|
302
|
+
onTextChange?: (text: string) => void;
|
|
303
|
+
onTextBlur?: () => void;
|
|
304
|
+
/** Paints the hex field in its error state. */
|
|
305
|
+
invalid?: boolean;
|
|
306
|
+
/** When set, shows a WCAG contrast Badge against this "#RRGGBB". */
|
|
307
|
+
contrastAgainst?: string;
|
|
308
|
+
/** Name used to build the controls' accessible labels (e.g. "Primary"). */
|
|
309
|
+
label: string;
|
|
310
|
+
/** Helper line under the picker. */
|
|
311
|
+
description?: React.ReactNode;
|
|
312
|
+
className?: string;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export const ColorPicker = React.forwardRef<HTMLDivElement, ColorPickerProps>(
|
|
316
|
+
(
|
|
317
|
+
{
|
|
318
|
+
value,
|
|
319
|
+
onChange,
|
|
320
|
+
text,
|
|
321
|
+
onTextChange,
|
|
322
|
+
onTextBlur,
|
|
323
|
+
invalid = false,
|
|
324
|
+
contrastAgainst,
|
|
325
|
+
label,
|
|
326
|
+
description,
|
|
327
|
+
className,
|
|
328
|
+
},
|
|
329
|
+
ref,
|
|
330
|
+
) => {
|
|
331
|
+
const [hsv, setHsv] = React.useState<Hsv>(() => hexToHsv(value));
|
|
332
|
+
|
|
333
|
+
// Keep hue/saturation stable while dragging; resync on outside changes.
|
|
334
|
+
React.useEffect(() => {
|
|
335
|
+
setHsv((prev) => (hsvToHex(prev) === value ? prev : hexToHsv(value)));
|
|
336
|
+
}, [value]);
|
|
337
|
+
|
|
338
|
+
const update = (patch: Partial<Hsv>) => {
|
|
339
|
+
const next = { ...hsv, ...patch };
|
|
340
|
+
setHsv(next);
|
|
341
|
+
onChange(hsvToHex(next));
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
return (
|
|
345
|
+
<div
|
|
346
|
+
ref={ref}
|
|
347
|
+
className={cn("flex w-full flex-col gap-md", className)}
|
|
348
|
+
>
|
|
349
|
+
<div className="flex w-full gap-md">
|
|
350
|
+
<SvSquare
|
|
351
|
+
label={`${label} saturation and brightness`}
|
|
352
|
+
hsv={hsv}
|
|
353
|
+
onChange={(next) => update(next)}
|
|
354
|
+
/>
|
|
355
|
+
<div className="flex min-w-0 flex-1 flex-col justify-between gap-sm">
|
|
356
|
+
<HueSlider
|
|
357
|
+
label={`${label} hue`}
|
|
358
|
+
value={Math.round(hsv.h)}
|
|
359
|
+
onChange={(h) => update({ h })}
|
|
360
|
+
/>
|
|
361
|
+
<div className="flex items-center gap-sm">
|
|
362
|
+
<div className="w-24 shrink-0">
|
|
363
|
+
<Input
|
|
364
|
+
size="sm"
|
|
365
|
+
aria-label={`${label} hex value`}
|
|
366
|
+
value={text ?? value}
|
|
367
|
+
maxLength={7}
|
|
368
|
+
spellCheck={false}
|
|
369
|
+
autoComplete="off"
|
|
370
|
+
invalid={invalid}
|
|
371
|
+
onChange={(e) => {
|
|
372
|
+
if (onTextChange) {
|
|
373
|
+
onTextChange(e.target.value);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const hex = parseHex(e.target.value, false);
|
|
377
|
+
if (hex) onChange(hex);
|
|
378
|
+
}}
|
|
379
|
+
onBlur={onTextBlur}
|
|
380
|
+
className="font-mono uppercase"
|
|
381
|
+
/>
|
|
382
|
+
</div>
|
|
383
|
+
{contrastAgainst ? (
|
|
384
|
+
<ContrastBadge hex={value} against={contrastAgainst} />
|
|
385
|
+
) : null}
|
|
386
|
+
</div>
|
|
387
|
+
</div>
|
|
388
|
+
</div>
|
|
389
|
+
{description ? <p className="text-caption text-muted">{description}</p> : null}
|
|
390
|
+
</div>
|
|
391
|
+
);
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
ColorPicker.displayName = "ColorPicker";
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { ColorPicker } from "./ColorPicker";
|
|
3
|
+
|
|
4
|
+
/* ------------------------------------------------------------------ */
|
|
5
|
+
/* Resolve token colours from rendered swatches. Utilities give us the */
|
|
6
|
+
/* palette without literals; the computed values are then read back. */
|
|
7
|
+
/* ------------------------------------------------------------------ */
|
|
8
|
+
|
|
9
|
+
const SWATCHES = [
|
|
10
|
+
{ key: "primary", className: "bg-primary" },
|
|
11
|
+
{ key: "warning", className: "bg-semantic-warning" },
|
|
12
|
+
{ key: "warningSoft", className: "bg-semantic-warning-soft" },
|
|
13
|
+
{ key: "success", className: "bg-semantic-success" },
|
|
14
|
+
{ key: "canvas", className: "bg-canvas" },
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
type SwatchKey = (typeof SWATCHES)[number]["key"];
|
|
18
|
+
type SwatchColors = Record<SwatchKey, string>;
|
|
19
|
+
|
|
20
|
+
function toHex(n: number): string {
|
|
21
|
+
return Math.round(Math.min(255, Math.max(0, n)))
|
|
22
|
+
.toString(16)
|
|
23
|
+
.padStart(2, "0");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ParsedColor {
|
|
27
|
+
hex: string;
|
|
28
|
+
alpha: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseCssColor(value: string): ParsedColor | null {
|
|
32
|
+
const trimmed = value.trim();
|
|
33
|
+
const long = trimmed.match(/^#([0-9a-f]{6})$/i);
|
|
34
|
+
if (long) return { hex: `#${long[1]}`.toUpperCase(), alpha: 1 };
|
|
35
|
+
const short = trimmed.match(/^#([0-9a-f]{3})$/i);
|
|
36
|
+
if (short) {
|
|
37
|
+
const [r, g, b] = short[1];
|
|
38
|
+
return { hex: `#${r}${r}${g}${g}${b}${b}`.toUpperCase(), alpha: 1 };
|
|
39
|
+
}
|
|
40
|
+
const rgb = trimmed.match(/rgba?\(([^)]+)\)/i);
|
|
41
|
+
if (!rgb) return null;
|
|
42
|
+
const parts = rgb[1]
|
|
43
|
+
.split(/[,\s/]+/)
|
|
44
|
+
.filter(Boolean)
|
|
45
|
+
.map((part) => Number(part));
|
|
46
|
+
if (parts.length < 3 || parts.slice(0, 3).some((part) => Number.isNaN(part))) return null;
|
|
47
|
+
const alpha = parts.length > 3 && !Number.isNaN(parts[3]) ? parts[3] : 1;
|
|
48
|
+
return { hex: `#${toHex(parts[0])}${toHex(parts[1])}${toHex(parts[2])}`.toUpperCase(), alpha };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function readSwatches(holder: HTMLElement): SwatchColors | null {
|
|
52
|
+
const inherited = parseCssColor(getComputedStyle(document.body).color)?.hex ?? null;
|
|
53
|
+
const next: Partial<SwatchColors> = {};
|
|
54
|
+
|
|
55
|
+
Array.from(holder.children).forEach((child, index) => {
|
|
56
|
+
const key = SWATCHES[index]?.key;
|
|
57
|
+
if (!key) return;
|
|
58
|
+
const parsed = parseCssColor(getComputedStyle(child as HTMLElement).backgroundColor);
|
|
59
|
+
// A utility that failed to resolve leaves the swatch transparent; one that
|
|
60
|
+
// answered with the inherited text colour is not a real background either.
|
|
61
|
+
if (!parsed || parsed.alpha === 0) return;
|
|
62
|
+
if (inherited && parsed.hex === inherited) return;
|
|
63
|
+
next[key] = parsed.hex;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return SWATCHES.every((swatch) => Boolean(next[swatch.key])) ? (next as SwatchColors) : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function useSwatchColors() {
|
|
70
|
+
const holderRef = React.useRef<HTMLDivElement>(null);
|
|
71
|
+
const [colors, setColors] = React.useState<SwatchColors | null>(null);
|
|
72
|
+
|
|
73
|
+
React.useEffect(() => {
|
|
74
|
+
let frame = 0;
|
|
75
|
+
let attempts = 0;
|
|
76
|
+
|
|
77
|
+
const run = () => {
|
|
78
|
+
attempts += 1;
|
|
79
|
+
const holder = holderRef.current;
|
|
80
|
+
const found = holder ? readSwatches(holder) : null;
|
|
81
|
+
if (found) {
|
|
82
|
+
setColors(found);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (attempts < 30) frame = window.requestAnimationFrame(run);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
frame = window.requestAnimationFrame(run);
|
|
89
|
+
return () => window.cancelAnimationFrame(frame);
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
return { colors, holderRef };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* ------------------------------------------------------------------ */
|
|
96
|
+
/* Preview */
|
|
97
|
+
/* ------------------------------------------------------------------ */
|
|
98
|
+
|
|
99
|
+
const Panel = ({ title, children }: { title: string; children: React.ReactNode }) => (
|
|
100
|
+
<section className="w-full rounded-lg border border-hairline bg-canvas p-base shadow-card">
|
|
101
|
+
<h3 className="mb-md text-heading-sm text-ink">{title}</h3>
|
|
102
|
+
{children}
|
|
103
|
+
</section>
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
function PickerDemos({ colors }: { colors: SwatchColors }) {
|
|
107
|
+
const [brand, setBrand] = React.useState<string>(colors.primary);
|
|
108
|
+
const [warning, setWarning] = React.useState<string>(colors.warning);
|
|
109
|
+
const [warningText, setWarningText] = React.useState<string>(colors.warning);
|
|
110
|
+
const [success, setSuccess] = React.useState<string>(colors.success);
|
|
111
|
+
const [successText, setSuccessText] = React.useState<string>(colors.success.slice(0, 4));
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<div className="mx-auto w-full max-w-3xl space-y-lg p-lg">
|
|
115
|
+
<Panel title="Brand colour, rated against the canvas">
|
|
116
|
+
<ColorPicker
|
|
117
|
+
label="Primary token"
|
|
118
|
+
value={brand}
|
|
119
|
+
onChange={setBrand}
|
|
120
|
+
contrastAgainst={colors.canvas}
|
|
121
|
+
description="Drag the square or the hue slider — the badge re-rates contrast against the page canvas."
|
|
122
|
+
/>
|
|
123
|
+
</Panel>
|
|
124
|
+
|
|
125
|
+
<Panel title="Parent-controlled hex draft">
|
|
126
|
+
<ColorPicker
|
|
127
|
+
label="Warning token"
|
|
128
|
+
value={warning}
|
|
129
|
+
onChange={(hex) => {
|
|
130
|
+
setWarning(hex);
|
|
131
|
+
setWarningText(hex);
|
|
132
|
+
}}
|
|
133
|
+
text={warningText}
|
|
134
|
+
onTextChange={setWarningText}
|
|
135
|
+
onTextBlur={() => setWarningText(warning)}
|
|
136
|
+
contrastAgainst={colors.warningSoft}
|
|
137
|
+
description="Type a partial hex and blur the field to snap the draft back to the committed colour."
|
|
138
|
+
/>
|
|
139
|
+
</Panel>
|
|
140
|
+
|
|
141
|
+
<Panel title="Invalid hex">
|
|
142
|
+
<ColorPicker
|
|
143
|
+
label="Success token"
|
|
144
|
+
value={success}
|
|
145
|
+
onChange={setSuccess}
|
|
146
|
+
text={successText}
|
|
147
|
+
onTextChange={setSuccessText}
|
|
148
|
+
onTextBlur={() => setSuccessText(success)}
|
|
149
|
+
invalid={successText.length !== 7}
|
|
150
|
+
description="The field reports its error state while the hex is incomplete."
|
|
151
|
+
/>
|
|
152
|
+
</Panel>
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export default function ColorPickerPreview() {
|
|
158
|
+
const { colors, holderRef } = useSwatchColors();
|
|
159
|
+
|
|
160
|
+
const swatches = (
|
|
161
|
+
<div
|
|
162
|
+
ref={holderRef}
|
|
163
|
+
aria-hidden="true"
|
|
164
|
+
className="pointer-events-none absolute top-0 left-0 -z-10 h-px w-px overflow-hidden opacity-0"
|
|
165
|
+
>
|
|
166
|
+
{SWATCHES.map((swatch) => (
|
|
167
|
+
<span key={swatch.key} className={`block h-px w-px ${swatch.className}`} />
|
|
168
|
+
))}
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
if (!colors) {
|
|
173
|
+
return <div className="mx-auto w-full max-w-3xl p-lg">{swatches}</div>;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<>
|
|
178
|
+
{swatches}
|
|
179
|
+
<PickerDemos colors={colors} />
|
|
180
|
+
</>
|
|
181
|
+
);
|
|
182
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,8 @@ export * from "./atoms/ChatWindow";
|
|
|
21
21
|
export * from "./atoms/Checkbox";
|
|
22
22
|
export * from "./atoms/Chip";
|
|
23
23
|
export * from "./atoms/CodeEditor";
|
|
24
|
+
export * from "./atoms/CodePreview";
|
|
25
|
+
export * from "./atoms/ColorPicker";
|
|
24
26
|
export * from "./atoms/Combobox";
|
|
25
27
|
export * from "./atoms/CommentThread";
|
|
26
28
|
export * from "./atoms/DataTable";
|