@behavioral-agentic-design-system/core 0.18.0 → 0.19.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@behavioral-agentic-design-system/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "BADS core — tokens, shadcn/ui components, and the BADS template shell (app routes, customize tab, theme hooks). Forks consume this and re-export, keeping client repos to ~30 lines of glue.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Matthew Stephens <work@matthewstephens.com> (https://github.com/matthewlarn)",
|
|
@@ -1903,58 +1903,6 @@ export default function ComponentsPreview() {
|
|
|
1903
1903
|
</div>
|
|
1904
1904
|
</div>
|
|
1905
1905
|
</Section>
|
|
1906
|
-
<Section title="Code">
|
|
1907
|
-
<div className="space-y-6">
|
|
1908
|
-
<div className="space-y-2">
|
|
1909
|
-
<p className="text-sm font-medium text-muted-foreground">Inline (default)</p>
|
|
1910
|
-
<p className="text-sm text-foreground leading-relaxed max-w-sm">
|
|
1911
|
-
Set the primary brand color via{" "}
|
|
1912
|
-
<Code>--primary</Code> in{" "}
|
|
1913
|
-
<Code>tokens.json</Code> and run{" "}
|
|
1914
|
-
<Code>pnpm build:tokens</Code> to regenerate.
|
|
1915
|
-
</p>
|
|
1916
|
-
</div>
|
|
1917
|
-
<div className="space-y-2">
|
|
1918
|
-
<p className="text-sm font-medium text-muted-foreground">Block variant</p>
|
|
1919
|
-
<div className="w-[480px]">
|
|
1920
|
-
<Code variant="block">
|
|
1921
|
-
{`// tokens.json — semantic layer\n{\n "theme": {\n "light": {\n "primary": "#1a1a1a",\n "primary-foreground": "#ffffff"\n }\n }\n}`}
|
|
1922
|
-
</Code>
|
|
1923
|
-
</div>
|
|
1924
|
-
</div>
|
|
1925
|
-
<div className="space-y-2">
|
|
1926
|
-
<p className="text-sm font-medium text-muted-foreground">Sizes — sm / default / lg</p>
|
|
1927
|
-
<div className="flex flex-col gap-3">
|
|
1928
|
-
<div className="flex items-center gap-3">
|
|
1929
|
-
<Code size="sm">pnpm a11y</Code>
|
|
1930
|
-
<Code size="default">pnpm wcag:audit</Code>
|
|
1931
|
-
<Code size="lg">design-tokens</Code>
|
|
1932
|
-
</div>
|
|
1933
|
-
<div className="w-[440px] space-y-1.5">
|
|
1934
|
-
<Code variant="block" size="sm">{`78 pass · 0 fail · 0 warn`}</Code>
|
|
1935
|
-
<Code variant="block" size="default">{`✓ primary on background 12.6:1 AAA`}</Code>
|
|
1936
|
-
<Code variant="block" size="lg">{`@behavioral-agentic-design-system/core@0.16.0`}</Code>
|
|
1937
|
-
</div>
|
|
1938
|
-
</div>
|
|
1939
|
-
</div>
|
|
1940
|
-
<div className="space-y-2">
|
|
1941
|
-
<p className="text-sm font-medium text-muted-foreground">Realistic use</p>
|
|
1942
|
-
<div className="max-w-md space-y-4 text-sm text-foreground leading-relaxed">
|
|
1943
|
-
<div className="space-y-1">
|
|
1944
|
-
<p className="font-medium text-foreground">Install the package</p>
|
|
1945
|
-
<Code variant="block">{`pnpm add @behavioral-agentic-design-system/core`}</Code>
|
|
1946
|
-
</div>
|
|
1947
|
-
<p>
|
|
1948
|
-
Import tokens via <Code>import "@behavioral-agentic-design-system/core/styles.css"</Code>{" "}
|
|
1949
|
-
in your root layout, then use any component:
|
|
1950
|
-
</p>
|
|
1951
|
-
<Code variant="block">
|
|
1952
|
-
{`import { Button } from "@behavioral-agentic-design-system/core/components/ui/button"\n\nexport function Example() {\n return <Button variant="default">Get started</Button>\n}`}
|
|
1953
|
-
</Code>
|
|
1954
|
-
</div>
|
|
1955
|
-
</div>
|
|
1956
|
-
</div>
|
|
1957
|
-
</Section>
|
|
1958
1906
|
<Section title="Combobox">
|
|
1959
1907
|
<div className="space-y-6">
|
|
1960
1908
|
<div className="grid grid-cols-1 gap-6 sm:grid-cols-2">
|
|
@@ -2491,52 +2439,6 @@ export default function ComponentsPreview() {
|
|
|
2491
2439
|
</div>
|
|
2492
2440
|
</Section>
|
|
2493
2441
|
|
|
2494
|
-
<Section title="Native Select">
|
|
2495
|
-
<div className="grid gap-6 sm:grid-cols-2">
|
|
2496
|
-
<div className="space-y-2">
|
|
2497
|
-
<Label htmlFor="ns-focus">Design Focus Area</Label>
|
|
2498
|
-
<NativeSelect id="ns-focus" defaultValue="components">
|
|
2499
|
-
<option value="">Choose a focus area</option>
|
|
2500
|
-
<option value="tokens">Design Tokens</option>
|
|
2501
|
-
<option value="components">Components</option>
|
|
2502
|
-
<option value="patterns">Patterns</option>
|
|
2503
|
-
<option value="a11y">Accessibility</option>
|
|
2504
|
-
<option value="motion">Motion & Animation</option>
|
|
2505
|
-
</NativeSelect>
|
|
2506
|
-
</div>
|
|
2507
|
-
<div className="space-y-2">
|
|
2508
|
-
<Label htmlFor="ns-disabled">Disabled</Label>
|
|
2509
|
-
<NativeSelect id="ns-disabled" disabled defaultValue="components">
|
|
2510
|
-
<option value="components">Components (locked)</option>
|
|
2511
|
-
</NativeSelect>
|
|
2512
|
-
</div>
|
|
2513
|
-
<div className="space-y-2">
|
|
2514
|
-
<Label htmlFor="ns-invalid">Invalid</Label>
|
|
2515
|
-
<NativeSelect id="ns-invalid" aria-invalid defaultValue="">
|
|
2516
|
-
<option value="">Select a valid option</option>
|
|
2517
|
-
<option value="tokens">Design Tokens</option>
|
|
2518
|
-
</NativeSelect>
|
|
2519
|
-
</div>
|
|
2520
|
-
<div className="space-y-2">
|
|
2521
|
-
<p className="text-sm font-medium text-foreground">Sizes</p>
|
|
2522
|
-
<div className="flex flex-col gap-2">
|
|
2523
|
-
<NativeSelect size="sm" defaultValue="sm">
|
|
2524
|
-
<option value="sm">Small (sm)</option>
|
|
2525
|
-
</NativeSelect>
|
|
2526
|
-
<NativeSelect size="default" defaultValue="default">
|
|
2527
|
-
<option value="default">Default</option>
|
|
2528
|
-
</NativeSelect>
|
|
2529
|
-
<NativeSelect size="lg" defaultValue="lg">
|
|
2530
|
-
<option value="lg">Large (lg)</option>
|
|
2531
|
-
</NativeSelect>
|
|
2532
|
-
<NativeSelect size="xl" defaultValue="xl">
|
|
2533
|
-
<option value="xl">Extra Large (xl)</option>
|
|
2534
|
-
</NativeSelect>
|
|
2535
|
-
</div>
|
|
2536
|
-
</div>
|
|
2537
|
-
</div>
|
|
2538
|
-
</Section>
|
|
2539
|
-
|
|
2540
2442
|
<Section title="Textarea">
|
|
2541
2443
|
<div className="grid gap-6 sm:grid-cols-2">
|
|
2542
2444
|
<div className="space-y-2">
|
|
@@ -2933,6 +2835,26 @@ export default function ComponentsPreview() {
|
|
|
2933
2835
|
|
|
2934
2836
|
<Section title="Color Picker">
|
|
2935
2837
|
<div className="flex flex-wrap gap-8 items-start">
|
|
2838
|
+
<div className="space-y-1.5">
|
|
2839
|
+
<p className="text-xs font-medium text-muted-foreground">Color wheel</p>
|
|
2840
|
+
<ColorPicker
|
|
2841
|
+
wheel
|
|
2842
|
+
defaultValue="#3b82f6"
|
|
2843
|
+
aria-label="Pick a color from the wheel"
|
|
2844
|
+
/>
|
|
2845
|
+
</div>
|
|
2846
|
+
<div className="space-y-1.5">
|
|
2847
|
+
<p className="text-xs font-medium text-muted-foreground">Wheel + swatches</p>
|
|
2848
|
+
<ColorPicker
|
|
2849
|
+
wheel
|
|
2850
|
+
swatches={[
|
|
2851
|
+
"#ef4444", "#f97316", "#eab308", "#22c55e",
|
|
2852
|
+
"#06b6d4", "#3b82f6", "#8b5cf6", "#ec4899",
|
|
2853
|
+
]}
|
|
2854
|
+
defaultValue="#8b5cf6"
|
|
2855
|
+
aria-label="Pick a color from the wheel or a swatch"
|
|
2856
|
+
/>
|
|
2857
|
+
</div>
|
|
2936
2858
|
<div className="space-y-1.5">
|
|
2937
2859
|
<p className="text-xs font-medium text-muted-foreground">Swatches</p>
|
|
2938
2860
|
<ColorPicker
|
|
@@ -2970,6 +2892,105 @@ export default function ComponentsPreview() {
|
|
|
2970
2892
|
</div>
|
|
2971
2893
|
</div>
|
|
2972
2894
|
</Section>
|
|
2895
|
+
|
|
2896
|
+
<Section title="Code">
|
|
2897
|
+
<div className="space-y-6">
|
|
2898
|
+
<div className="space-y-2">
|
|
2899
|
+
<p className="text-sm font-medium text-muted-foreground">Inline (default)</p>
|
|
2900
|
+
<p className="text-sm text-foreground leading-relaxed max-w-sm">
|
|
2901
|
+
Set the primary brand color via{" "}
|
|
2902
|
+
<Code>--primary</Code> in{" "}
|
|
2903
|
+
<Code>tokens.json</Code> and run{" "}
|
|
2904
|
+
<Code>pnpm build:tokens</Code> to regenerate.
|
|
2905
|
+
</p>
|
|
2906
|
+
</div>
|
|
2907
|
+
<div className="space-y-2">
|
|
2908
|
+
<p className="text-sm font-medium text-muted-foreground">Block variant</p>
|
|
2909
|
+
<div className="w-[480px]">
|
|
2910
|
+
<Code variant="block">
|
|
2911
|
+
{`// tokens.json — semantic layer\n{\n "theme": {\n "light": {\n "primary": "#1a1a1a",\n "primary-foreground": "#ffffff"\n }\n }\n}`}
|
|
2912
|
+
</Code>
|
|
2913
|
+
</div>
|
|
2914
|
+
</div>
|
|
2915
|
+
<div className="space-y-2">
|
|
2916
|
+
<p className="text-sm font-medium text-muted-foreground">Sizes — sm / default / lg</p>
|
|
2917
|
+
<div className="flex flex-col gap-3">
|
|
2918
|
+
<div className="flex items-center gap-3">
|
|
2919
|
+
<Code size="sm">pnpm a11y</Code>
|
|
2920
|
+
<Code size="default">pnpm wcag:audit</Code>
|
|
2921
|
+
<Code size="lg">design-tokens</Code>
|
|
2922
|
+
</div>
|
|
2923
|
+
<div className="w-[440px] space-y-1.5">
|
|
2924
|
+
<Code variant="block" size="sm">{`78 pass · 0 fail · 0 warn`}</Code>
|
|
2925
|
+
<Code variant="block" size="default">{`✓ primary on background 12.6:1 AAA`}</Code>
|
|
2926
|
+
<Code variant="block" size="lg">{`@behavioral-agentic-design-system/core@0.16.0`}</Code>
|
|
2927
|
+
</div>
|
|
2928
|
+
</div>
|
|
2929
|
+
</div>
|
|
2930
|
+
<div className="space-y-2">
|
|
2931
|
+
<p className="text-sm font-medium text-muted-foreground">Realistic use</p>
|
|
2932
|
+
<div className="max-w-md space-y-4 text-sm text-foreground leading-relaxed">
|
|
2933
|
+
<div className="space-y-1">
|
|
2934
|
+
<p className="font-medium text-foreground">Install the package</p>
|
|
2935
|
+
<Code variant="block">{`pnpm add @behavioral-agentic-design-system/core`}</Code>
|
|
2936
|
+
</div>
|
|
2937
|
+
<p>
|
|
2938
|
+
Import tokens via <Code>import "@behavioral-agentic-design-system/core/styles.css"</Code>{" "}
|
|
2939
|
+
in your root layout, then use any component:
|
|
2940
|
+
</p>
|
|
2941
|
+
<Code variant="block">
|
|
2942
|
+
{`import { Button } from "@behavioral-agentic-design-system/core/components/ui/button"\n\nexport function Example() {\n return <Button variant="default">Get started</Button>\n}`}
|
|
2943
|
+
</Code>
|
|
2944
|
+
</div>
|
|
2945
|
+
</div>
|
|
2946
|
+
</div>
|
|
2947
|
+
</Section>
|
|
2948
|
+
|
|
2949
|
+
<Section title="Native Select">
|
|
2950
|
+
<div className="grid gap-6 sm:grid-cols-2">
|
|
2951
|
+
<div className="space-y-2">
|
|
2952
|
+
<Label htmlFor="ns-focus">Design Focus Area</Label>
|
|
2953
|
+
<NativeSelect id="ns-focus" defaultValue="components">
|
|
2954
|
+
<option value="">Choose a focus area</option>
|
|
2955
|
+
<option value="tokens">Design Tokens</option>
|
|
2956
|
+
<option value="components">Components</option>
|
|
2957
|
+
<option value="patterns">Patterns</option>
|
|
2958
|
+
<option value="a11y">Accessibility</option>
|
|
2959
|
+
<option value="motion">Motion & Animation</option>
|
|
2960
|
+
</NativeSelect>
|
|
2961
|
+
</div>
|
|
2962
|
+
<div className="space-y-2">
|
|
2963
|
+
<Label htmlFor="ns-disabled">Disabled</Label>
|
|
2964
|
+
<NativeSelect id="ns-disabled" disabled defaultValue="components">
|
|
2965
|
+
<option value="components">Components (locked)</option>
|
|
2966
|
+
</NativeSelect>
|
|
2967
|
+
</div>
|
|
2968
|
+
<div className="space-y-2">
|
|
2969
|
+
<Label htmlFor="ns-invalid">Invalid</Label>
|
|
2970
|
+
<NativeSelect id="ns-invalid" aria-invalid defaultValue="">
|
|
2971
|
+
<option value="">Select a valid option</option>
|
|
2972
|
+
<option value="tokens">Design Tokens</option>
|
|
2973
|
+
</NativeSelect>
|
|
2974
|
+
</div>
|
|
2975
|
+
<div className="space-y-2">
|
|
2976
|
+
<p className="text-sm font-medium text-foreground">Sizes</p>
|
|
2977
|
+
<div className="flex flex-col gap-2">
|
|
2978
|
+
<NativeSelect size="sm" defaultValue="sm">
|
|
2979
|
+
<option value="sm">Small (sm)</option>
|
|
2980
|
+
</NativeSelect>
|
|
2981
|
+
<NativeSelect size="default" defaultValue="default">
|
|
2982
|
+
<option value="default">Default</option>
|
|
2983
|
+
</NativeSelect>
|
|
2984
|
+
<NativeSelect size="lg" defaultValue="lg">
|
|
2985
|
+
<option value="lg">Large (lg)</option>
|
|
2986
|
+
</NativeSelect>
|
|
2987
|
+
<NativeSelect size="xl" defaultValue="xl">
|
|
2988
|
+
<option value="xl">Extra Large (xl)</option>
|
|
2989
|
+
</NativeSelect>
|
|
2990
|
+
</div>
|
|
2991
|
+
</div>
|
|
2992
|
+
</div>
|
|
2993
|
+
</Section>
|
|
2973
2994
|
</div>
|
|
2974
2995
|
</div>
|
|
2975
2996
|
);
|
|
@@ -8,12 +8,24 @@ import { cn } from "../../lib/utils";
|
|
|
8
8
|
/* Helpers */
|
|
9
9
|
/* -------------------------------------------------------------------------- */
|
|
10
10
|
|
|
11
|
+
const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
|
|
12
|
+
|
|
13
|
+
function hexToRgb(hex: string): [number, number, number] | null {
|
|
14
|
+
if (!isValidHex(hex)) return null;
|
|
15
|
+
let h = hex.replace("#", "");
|
|
16
|
+
if (h.length === 3) h = h.split("").map((c) => c + c).join("");
|
|
17
|
+
return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function rgbToHex(r: number, g: number, b: number): string {
|
|
21
|
+
const to = (v: number) => Math.round(clamp(v, 0, 255)).toString(16).padStart(2, "0");
|
|
22
|
+
return `#${to(r)}${to(g)}${to(b)}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
function luminance(hex: string): number {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
: [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];
|
|
16
|
-
const [r, g, b] = full.map((v) => {
|
|
26
|
+
const rgb = hexToRgb(hex);
|
|
27
|
+
if (!rgb) return 1;
|
|
28
|
+
const [r, g, b] = rgb.map((v) => {
|
|
17
29
|
const s = v / 255;
|
|
18
30
|
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
19
31
|
});
|
|
@@ -21,7 +33,7 @@ function luminance(hex: string): number {
|
|
|
21
33
|
}
|
|
22
34
|
|
|
23
35
|
function contrastColor(hex: string): string {
|
|
24
|
-
if (
|
|
36
|
+
if (!isValidHex(hex)) return "#ffffff";
|
|
25
37
|
return luminance(hex) > 0.179 ? "#000000" : "#ffffff";
|
|
26
38
|
}
|
|
27
39
|
|
|
@@ -29,6 +41,281 @@ function isValidHex(v: string): boolean {
|
|
|
29
41
|
return /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(v);
|
|
30
42
|
}
|
|
31
43
|
|
|
44
|
+
/** hex -> {h: 0-360, s: 0-100, v: 0-100} */
|
|
45
|
+
function hexToHsv(hex: string): { h: number; s: number; v: number } {
|
|
46
|
+
const rgb = hexToRgb(hex);
|
|
47
|
+
if (!rgb) return { h: 0, s: 0, v: 100 };
|
|
48
|
+
const [r, g, b] = rgb.map((v) => v / 255);
|
|
49
|
+
const max = Math.max(r, g, b);
|
|
50
|
+
const min = Math.min(r, g, b);
|
|
51
|
+
const d = max - min;
|
|
52
|
+
let h = 0;
|
|
53
|
+
if (d !== 0) {
|
|
54
|
+
if (max === r) h = ((g - b) / d) % 6;
|
|
55
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
56
|
+
else h = (r - g) / d + 4;
|
|
57
|
+
h *= 60;
|
|
58
|
+
if (h < 0) h += 360;
|
|
59
|
+
}
|
|
60
|
+
const s = max === 0 ? 0 : d / max;
|
|
61
|
+
return { h, s: s * 100, v: max * 100 };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** {h: 0-360, s: 0-100, v: 0-100} -> hex */
|
|
65
|
+
function hsvToHex(h: number, s: number, v: number): string {
|
|
66
|
+
const sn = s / 100;
|
|
67
|
+
const vn = v / 100;
|
|
68
|
+
const c = vn * sn;
|
|
69
|
+
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
70
|
+
const m = vn - c;
|
|
71
|
+
let r = 0, g = 0, b = 0;
|
|
72
|
+
if (h < 60) [r, g, b] = [c, x, 0];
|
|
73
|
+
else if (h < 120) [r, g, b] = [x, c, 0];
|
|
74
|
+
else if (h < 180) [r, g, b] = [0, c, x];
|
|
75
|
+
else if (h < 240) [r, g, b] = [0, x, c];
|
|
76
|
+
else if (h < 300) [r, g, b] = [x, 0, c];
|
|
77
|
+
else [r, g, b] = [c, 0, x];
|
|
78
|
+
return rgbToHex((r + m) * 255, (g + m) * 255, (b + m) * 255);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* -------------------------------------------------------------------------- */
|
|
82
|
+
/* Color wheel (hue ring + saturation/brightness square) */
|
|
83
|
+
/* -------------------------------------------------------------------------- */
|
|
84
|
+
|
|
85
|
+
const WHEEL_SIZE = 208;
|
|
86
|
+
const RING_THICKNESS = 22;
|
|
87
|
+
const HOLE = WHEEL_SIZE - RING_THICKNESS * 2;
|
|
88
|
+
const SQUARE = Math.round(HOLE / Math.SQRT2);
|
|
89
|
+
const CENTER = WHEEL_SIZE / 2;
|
|
90
|
+
const RING_RADIUS = CENTER - RING_THICKNESS / 2;
|
|
91
|
+
|
|
92
|
+
const HUE_CONIC =
|
|
93
|
+
"conic-gradient(from 0deg, 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%))";
|
|
94
|
+
|
|
95
|
+
function ColorWheel({
|
|
96
|
+
value,
|
|
97
|
+
onChange,
|
|
98
|
+
disabled,
|
|
99
|
+
}: {
|
|
100
|
+
value: string;
|
|
101
|
+
onChange: (hex: string) => void;
|
|
102
|
+
disabled?: boolean;
|
|
103
|
+
}) {
|
|
104
|
+
const ringRef = React.useRef<HTMLDivElement>(null);
|
|
105
|
+
const squareRef = React.useRef<HTMLDivElement>(null);
|
|
106
|
+
|
|
107
|
+
const { h, s, v } = isValidHex(value) ? hexToHsv(value) : { h: 0, s: 0, v: 100 };
|
|
108
|
+
|
|
109
|
+
const commitHsv = React.useCallback(
|
|
110
|
+
(nh: number, ns: number, nv: number) => {
|
|
111
|
+
if (disabled) return;
|
|
112
|
+
onChange(hsvToHex(((nh % 360) + 360) % 360, clamp(ns, 0, 100), clamp(nv, 0, 100)));
|
|
113
|
+
},
|
|
114
|
+
[disabled, onChange]
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const pointerFromRing = (clientX: number, clientY: number) => {
|
|
118
|
+
const el = ringRef.current;
|
|
119
|
+
if (!el) return;
|
|
120
|
+
const r = el.getBoundingClientRect();
|
|
121
|
+
const dx = clientX - (r.left + r.width / 2);
|
|
122
|
+
const dy = clientY - (r.top + r.height / 2);
|
|
123
|
+
let ang = (Math.atan2(dx, -dy) * 180) / Math.PI; // 0 = top, clockwise
|
|
124
|
+
if (ang < 0) ang += 360;
|
|
125
|
+
commitHsv(ang, s, v);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const pointerFromSquare = (clientX: number, clientY: number) => {
|
|
129
|
+
const el = squareRef.current;
|
|
130
|
+
if (!el) return;
|
|
131
|
+
const r = el.getBoundingClientRect();
|
|
132
|
+
const ns = clamp((clientX - r.left) / r.width, 0, 1) * 100;
|
|
133
|
+
const nv = (1 - clamp((clientY - r.top) / r.height, 0, 1)) * 100;
|
|
134
|
+
commitHsv(h, ns, nv);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const dragRing = (e: React.PointerEvent) => {
|
|
138
|
+
if (disabled) return;
|
|
139
|
+
e.preventDefault();
|
|
140
|
+
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
|
141
|
+
pointerFromRing(e.clientX, e.clientY);
|
|
142
|
+
};
|
|
143
|
+
const moveRing = (e: React.PointerEvent) => {
|
|
144
|
+
if (disabled || !(e.currentTarget as HTMLElement).hasPointerCapture(e.pointerId)) return;
|
|
145
|
+
pointerFromRing(e.clientX, e.clientY);
|
|
146
|
+
};
|
|
147
|
+
const dragSquare = (e: React.PointerEvent) => {
|
|
148
|
+
if (disabled) return;
|
|
149
|
+
e.preventDefault();
|
|
150
|
+
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
|
151
|
+
pointerFromSquare(e.clientX, e.clientY);
|
|
152
|
+
};
|
|
153
|
+
const moveSquare = (e: React.PointerEvent) => {
|
|
154
|
+
if (disabled || !(e.currentTarget as HTMLElement).hasPointerCapture(e.pointerId)) return;
|
|
155
|
+
pointerFromSquare(e.clientX, e.clientY);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const ringKey = (e: React.KeyboardEvent) => {
|
|
159
|
+
if (disabled) return;
|
|
160
|
+
const big = e.key === "PageUp" || e.key === "PageDown";
|
|
161
|
+
const step = big ? 10 : 1;
|
|
162
|
+
let nh = h;
|
|
163
|
+
switch (e.key) {
|
|
164
|
+
case "ArrowRight":
|
|
165
|
+
case "ArrowUp":
|
|
166
|
+
case "PageUp":
|
|
167
|
+
nh = h + step;
|
|
168
|
+
break;
|
|
169
|
+
case "ArrowLeft":
|
|
170
|
+
case "ArrowDown":
|
|
171
|
+
case "PageDown":
|
|
172
|
+
nh = h - step;
|
|
173
|
+
break;
|
|
174
|
+
case "Home":
|
|
175
|
+
nh = 0;
|
|
176
|
+
break;
|
|
177
|
+
case "End":
|
|
178
|
+
nh = 359;
|
|
179
|
+
break;
|
|
180
|
+
default:
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
e.preventDefault();
|
|
184
|
+
commitHsv(nh, s, v);
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const squareKey = (e: React.KeyboardEvent) => {
|
|
188
|
+
if (disabled) return;
|
|
189
|
+
let ns = s;
|
|
190
|
+
let nv = v;
|
|
191
|
+
switch (e.key) {
|
|
192
|
+
case "ArrowRight":
|
|
193
|
+
ns = s + 2;
|
|
194
|
+
break;
|
|
195
|
+
case "ArrowLeft":
|
|
196
|
+
ns = s - 2;
|
|
197
|
+
break;
|
|
198
|
+
case "ArrowUp":
|
|
199
|
+
nv = v + 2;
|
|
200
|
+
break;
|
|
201
|
+
case "ArrowDown":
|
|
202
|
+
nv = v - 2;
|
|
203
|
+
break;
|
|
204
|
+
case "Home":
|
|
205
|
+
ns = 0;
|
|
206
|
+
break;
|
|
207
|
+
case "End":
|
|
208
|
+
ns = 100;
|
|
209
|
+
break;
|
|
210
|
+
default:
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
e.preventDefault();
|
|
214
|
+
commitHsv(h, ns, nv);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// thumb positions
|
|
218
|
+
const rad = (h * Math.PI) / 180;
|
|
219
|
+
const hueX = CENTER + RING_RADIUS * Math.sin(rad);
|
|
220
|
+
const hueY = CENTER - RING_RADIUS * Math.cos(rad);
|
|
221
|
+
const svX = (s / 100) * SQUARE;
|
|
222
|
+
const svY = (1 - v / 100) * SQUARE;
|
|
223
|
+
|
|
224
|
+
const thumb =
|
|
225
|
+
"absolute size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white pointer-events-none shadow-[0_0_0_1px_rgba(0,0,0,0.45)]";
|
|
226
|
+
|
|
227
|
+
return (
|
|
228
|
+
<div
|
|
229
|
+
className={cn("relative select-none touch-none", disabled && "opacity-50")}
|
|
230
|
+
style={{ width: WHEEL_SIZE, height: WHEEL_SIZE }}
|
|
231
|
+
>
|
|
232
|
+
{/* Hue ring */}
|
|
233
|
+
<div
|
|
234
|
+
ref={ringRef}
|
|
235
|
+
role="slider"
|
|
236
|
+
aria-label="Hue"
|
|
237
|
+
aria-valuemin={0}
|
|
238
|
+
aria-valuemax={360}
|
|
239
|
+
aria-valuenow={Math.round(h)}
|
|
240
|
+
aria-valuetext={`${Math.round(h)} degrees`}
|
|
241
|
+
aria-disabled={disabled || undefined}
|
|
242
|
+
tabIndex={disabled ? -1 : 0}
|
|
243
|
+
onPointerDown={dragRing}
|
|
244
|
+
onPointerMove={moveRing}
|
|
245
|
+
onKeyDown={ringKey}
|
|
246
|
+
className={cn(
|
|
247
|
+
"absolute inset-0 rounded-full",
|
|
248
|
+
!disabled && "cursor-pointer",
|
|
249
|
+
"outline-none focus-visible:ring-[3px] focus-visible:ring-ring/70 focus-visible:ring-offset-2 focus-visible:ring-offset-background"
|
|
250
|
+
)}
|
|
251
|
+
style={{ background: HUE_CONIC }}
|
|
252
|
+
/>
|
|
253
|
+
|
|
254
|
+
{/* Center hole masks the conic so only the ring band shows */}
|
|
255
|
+
<div
|
|
256
|
+
aria-hidden
|
|
257
|
+
className="absolute rounded-full bg-background pointer-events-none"
|
|
258
|
+
style={{ width: HOLE, height: HOLE, left: RING_THICKNESS, top: RING_THICKNESS }}
|
|
259
|
+
/>
|
|
260
|
+
|
|
261
|
+
{/* Saturation / brightness square */}
|
|
262
|
+
<div
|
|
263
|
+
ref={squareRef}
|
|
264
|
+
role="slider"
|
|
265
|
+
aria-label="Saturation and brightness"
|
|
266
|
+
aria-valuemin={0}
|
|
267
|
+
aria-valuemax={100}
|
|
268
|
+
aria-valuenow={Math.round(s)}
|
|
269
|
+
aria-valuetext={`Saturation ${Math.round(s)} percent, brightness ${Math.round(v)} percent`}
|
|
270
|
+
aria-disabled={disabled || undefined}
|
|
271
|
+
tabIndex={disabled ? -1 : 0}
|
|
272
|
+
onPointerDown={dragSquare}
|
|
273
|
+
onPointerMove={moveSquare}
|
|
274
|
+
onKeyDown={squareKey}
|
|
275
|
+
className={cn(
|
|
276
|
+
"absolute rounded-md",
|
|
277
|
+
!disabled && "cursor-crosshair",
|
|
278
|
+
"outline-none focus-visible:ring-[3px] focus-visible:ring-ring/70 focus-visible:ring-offset-2 focus-visible:ring-offset-background"
|
|
279
|
+
)}
|
|
280
|
+
style={{
|
|
281
|
+
width: SQUARE,
|
|
282
|
+
height: SQUARE,
|
|
283
|
+
left: CENTER - SQUARE / 2,
|
|
284
|
+
top: CENTER - SQUARE / 2,
|
|
285
|
+
background: `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, hsl(${Math.round(h)} 100% 50%))`,
|
|
286
|
+
}}
|
|
287
|
+
>
|
|
288
|
+
<span
|
|
289
|
+
aria-hidden
|
|
290
|
+
className={thumb}
|
|
291
|
+
style={{
|
|
292
|
+
left: svX,
|
|
293
|
+
top: svY,
|
|
294
|
+
backgroundColor: isValidHex(value) ? value : "#ffffff",
|
|
295
|
+
transitionProperty: "box-shadow",
|
|
296
|
+
transitionDuration: "var(--duration-fast)",
|
|
297
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
298
|
+
}}
|
|
299
|
+
/>
|
|
300
|
+
</div>
|
|
301
|
+
|
|
302
|
+
{/* Hue thumb (rendered last so it sits above the ring) */}
|
|
303
|
+
<span
|
|
304
|
+
aria-hidden
|
|
305
|
+
className={thumb}
|
|
306
|
+
style={{
|
|
307
|
+
left: hueX,
|
|
308
|
+
top: hueY,
|
|
309
|
+
backgroundColor: `hsl(${Math.round(h)} 100% 50%)`,
|
|
310
|
+
transitionProperty: "box-shadow",
|
|
311
|
+
transitionDuration: "var(--duration-fast)",
|
|
312
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
313
|
+
}}
|
|
314
|
+
/>
|
|
315
|
+
</div>
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
32
319
|
/* -------------------------------------------------------------------------- */
|
|
33
320
|
/* ColorPicker */
|
|
34
321
|
/* -------------------------------------------------------------------------- */
|
|
@@ -43,6 +330,13 @@ export interface ColorPickerProps {
|
|
|
43
330
|
swatches?: string[];
|
|
44
331
|
/** Show a hex text input for custom colors */
|
|
45
332
|
allowCustom?: boolean;
|
|
333
|
+
/**
|
|
334
|
+
* Show a hue-ring + saturation/brightness color wheel above the swatches.
|
|
335
|
+
* The wheel is a pointer/keyboard enhancement; the swatches + hex field
|
|
336
|
+
* remain the primary keyboard/screen-reader path. Enabling the wheel always
|
|
337
|
+
* surfaces the hex field too, so an exact value can still be typed.
|
|
338
|
+
*/
|
|
339
|
+
wheel?: boolean;
|
|
46
340
|
disabled?: boolean;
|
|
47
341
|
/** Accessible label for the radiogroup */
|
|
48
342
|
"aria-label"?: string;
|
|
@@ -55,6 +349,7 @@ export function ColorPicker({
|
|
|
55
349
|
onValueChange,
|
|
56
350
|
swatches = [],
|
|
57
351
|
allowCustom = false,
|
|
352
|
+
wheel = false,
|
|
58
353
|
disabled = false,
|
|
59
354
|
"aria-label": ariaLabel = "Color picker",
|
|
60
355
|
className,
|
|
@@ -64,7 +359,7 @@ export function ColorPicker({
|
|
|
64
359
|
const value = isControlled ? (valueProp ?? "") : valueState;
|
|
65
360
|
|
|
66
361
|
const [customInput, setCustomInput] = React.useState<string>(() =>
|
|
67
|
-
value && !swatches.includes(value) ? value : ""
|
|
362
|
+
value && (wheel || !swatches.includes(value)) ? value : ""
|
|
68
363
|
);
|
|
69
364
|
|
|
70
365
|
const swatchRefs = React.useRef<(HTMLButtonElement | null)[]>([]);
|
|
@@ -78,6 +373,15 @@ export function ColorPicker({
|
|
|
78
373
|
[disabled, isControlled, onValueChange]
|
|
79
374
|
);
|
|
80
375
|
|
|
376
|
+
// Keep the hex field mirrored when the wheel (or a swatch) drives the value.
|
|
377
|
+
const commitFromControl = React.useCallback(
|
|
378
|
+
(color: string) => {
|
|
379
|
+
commit(color);
|
|
380
|
+
setCustomInput(color);
|
|
381
|
+
},
|
|
382
|
+
[commit]
|
|
383
|
+
);
|
|
384
|
+
|
|
81
385
|
const handleKeyDown = React.useCallback(
|
|
82
386
|
(e: React.KeyboardEvent<HTMLButtonElement>, index: number) => {
|
|
83
387
|
const btns = swatchRefs.current.filter((b): b is HTMLButtonElement => !!b);
|
|
@@ -116,9 +420,14 @@ export function ColorPicker({
|
|
|
116
420
|
};
|
|
117
421
|
|
|
118
422
|
const selectedSwatchIdx = swatches.indexOf(value);
|
|
423
|
+
const showHex = allowCustom || wheel;
|
|
119
424
|
|
|
120
425
|
return (
|
|
121
|
-
<div className={cn("space-y-
|
|
426
|
+
<div className={cn("space-y-3", className)}>
|
|
427
|
+
{wheel && (
|
|
428
|
+
<ColorWheel value={value} onChange={commitFromControl} disabled={disabled} />
|
|
429
|
+
)}
|
|
430
|
+
|
|
122
431
|
{swatches.length > 0 && (
|
|
123
432
|
<div
|
|
124
433
|
role="radiogroup"
|
|
@@ -176,7 +485,7 @@ export function ColorPicker({
|
|
|
176
485
|
</div>
|
|
177
486
|
)}
|
|
178
487
|
|
|
179
|
-
{
|
|
488
|
+
{showHex && (
|
|
180
489
|
<div className="flex items-center gap-2">
|
|
181
490
|
<div
|
|
182
491
|
role="presentation"
|
|
@@ -46,6 +46,37 @@ export const WithCustomInput: Story = {
|
|
|
46
46
|
),
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
+
export const Wheel: Story = {
|
|
50
|
+
render: function WheelPicker() {
|
|
51
|
+
const [color, setColor] = React.useState("#3b82f6");
|
|
52
|
+
return (
|
|
53
|
+
<div className="space-y-3">
|
|
54
|
+
<ColorPicker
|
|
55
|
+
value={color}
|
|
56
|
+
onValueChange={setColor}
|
|
57
|
+
wheel
|
|
58
|
+
aria-label="Pick a color from the wheel"
|
|
59
|
+
/>
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const WheelWithSwatches: Story = {
|
|
66
|
+
render: function WheelSwatchesPicker() {
|
|
67
|
+
const [color, setColor] = React.useState("#8b5cf6");
|
|
68
|
+
return (
|
|
69
|
+
<ColorPicker
|
|
70
|
+
value={color}
|
|
71
|
+
onValueChange={setColor}
|
|
72
|
+
wheel
|
|
73
|
+
swatches={BRAND_SWATCHES}
|
|
74
|
+
aria-label="Pick a color from the wheel or a swatch"
|
|
75
|
+
/>
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
49
80
|
export const PastelPalette: Story = {
|
|
50
81
|
render: () => (
|
|
51
82
|
<ColorPicker
|