@pixel-point/toolcraft 0.0.2

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.
Files changed (257) hide show
  1. package/LICENSE.md +98 -0
  2. package/README.md +41 -0
  3. package/bin/create-toolcraft-app.mjs +8 -0
  4. package/bin/toolcraft.mjs +8 -0
  5. package/package.json +24 -0
  6. package/scripts/prepare-pack.mjs +29 -0
  7. package/src/cli.mjs +392 -0
  8. package/src/cli.test.mjs +284 -0
  9. package/src/copy-recursive.mjs +86 -0
  10. package/src/generate.mjs +212 -0
  11. package/src/generate.test.mjs +322 -0
  12. package/src/import-map.mjs +14 -0
  13. package/src/package-json.mjs +80 -0
  14. package/src/package-json.test.mjs +67 -0
  15. package/src/rewrite-imports.mjs +85 -0
  16. package/src/rewrite-imports.test.mjs +58 -0
  17. package/templates/runtime/contracts/component-contracts.test.ts +1165 -0
  18. package/templates/runtime/contracts/component-contracts.ts +1340 -0
  19. package/templates/runtime/contracts/decision-contracts.test.ts +206 -0
  20. package/templates/runtime/contracts/decision-contracts.ts +283 -0
  21. package/templates/runtime/contracts/index.test.ts +14 -0
  22. package/templates/runtime/contracts/index.ts +3 -0
  23. package/templates/runtime/contracts/types.ts +56 -0
  24. package/templates/runtime/export/export.test.ts +203 -0
  25. package/templates/runtime/export/export.ts +132 -0
  26. package/templates/runtime/export/index.ts +1 -0
  27. package/templates/runtime/index.ts +14 -0
  28. package/templates/runtime/react/canvas-shell.test.tsx +424 -0
  29. package/templates/runtime/react/canvas-shell.tsx +408 -0
  30. package/templates/runtime/react/control-renderers.ts +31 -0
  31. package/templates/runtime/react/controls-panel.test.tsx +3736 -0
  32. package/templates/runtime/react/controls-panel.tsx +2327 -0
  33. package/templates/runtime/react/curve-geometry.test.ts +70 -0
  34. package/templates/runtime/react/index.ts +15 -0
  35. package/templates/runtime/react/layer-tree.ts +96 -0
  36. package/templates/runtime/react/layers-panel.test.tsx +487 -0
  37. package/templates/runtime/react/layers-panel.tsx +1348 -0
  38. package/templates/runtime/react/media-file.ts +82 -0
  39. package/templates/runtime/react/panel-host-config.ts +80 -0
  40. package/templates/runtime/react/panel-host-geometry.test.ts +66 -0
  41. package/templates/runtime/react/panel-host-geometry.ts +109 -0
  42. package/templates/runtime/react/panel-host-types.ts +74 -0
  43. package/templates/runtime/react/panel-host.test.tsx +102 -0
  44. package/templates/runtime/react/panel-host.tsx +353 -0
  45. package/templates/runtime/react/runtime-public-api.test.tsx +132 -0
  46. package/templates/runtime/react/settings-transfer.test.ts +150 -0
  47. package/templates/runtime/react/settings-transfer.ts +279 -0
  48. package/templates/runtime/react/storage-key-migration.ts +48 -0
  49. package/templates/runtime/react/theme-runtime.tsx +177 -0
  50. package/templates/runtime/react/timeline-panel.test.tsx +668 -0
  51. package/templates/runtime/react/timeline-panel.tsx +2953 -0
  52. package/templates/runtime/react/toolbar-panel.test.tsx +212 -0
  53. package/templates/runtime/react/toolbar-panel.tsx +205 -0
  54. package/templates/runtime/react/toolcraft-app.integration.test.tsx +350 -0
  55. package/templates/runtime/react/toolcraft-app.test.tsx +339 -0
  56. package/templates/runtime/react/toolcraft-app.tsx +81 -0
  57. package/templates/runtime/react/toolcraft-root.test.tsx +347 -0
  58. package/templates/runtime/react/toolcraft-root.tsx +203 -0
  59. package/templates/runtime/react/use-toolcraft.ts +41 -0
  60. package/templates/runtime/schema/define-toolcraft.test.ts +1524 -0
  61. package/templates/runtime/schema/define-toolcraft.ts +1442 -0
  62. package/templates/runtime/schema/keyframe-capability.test.ts +90 -0
  63. package/templates/runtime/schema/keyframe-capability.ts +51 -0
  64. package/templates/runtime/schema/runtime-targets.ts +40 -0
  65. package/templates/runtime/schema/types.ts +370 -0
  66. package/templates/runtime/state/canvas-zoom.ts +8 -0
  67. package/templates/runtime/state/create-template-state.test.ts +242 -0
  68. package/templates/runtime/state/create-template-state.ts +95 -0
  69. package/templates/runtime/state/keyframe-evaluation.test.ts +141 -0
  70. package/templates/runtime/state/keyframe-evaluation.ts +203 -0
  71. package/templates/runtime/state/persistence.test.ts +217 -0
  72. package/templates/runtime/state/persistence.ts +511 -0
  73. package/templates/runtime/state/reducer.test.ts +937 -0
  74. package/templates/runtime/state/reducer.ts +1212 -0
  75. package/templates/runtime/state/timeline-readiness.ts +43 -0
  76. package/templates/runtime/state/types.ts +242 -0
  77. package/templates/runtime/styles.css +125 -0
  78. package/templates/runtime/testing/performance.test.ts +1058 -0
  79. package/templates/runtime/testing/performance.ts +1078 -0
  80. package/templates/starter/AGENTS.md +186 -0
  81. package/templates/starter/LICENSE.md +98 -0
  82. package/templates/starter/NOTICE.md +8 -0
  83. package/templates/starter/docs/toolcraft/README.md +41 -0
  84. package/templates/starter/docs/toolcraft/acceptance-testing.md +205 -0
  85. package/templates/starter/docs/toolcraft/agent-worklog.md +81 -0
  86. package/templates/starter/docs/toolcraft/assembly-workflow.md +206 -0
  87. package/templates/starter/docs/toolcraft/component-rules.md +299 -0
  88. package/templates/starter/docs/toolcraft/custom-controls.md +71 -0
  89. package/templates/starter/docs/toolcraft/decision-contract.md +71 -0
  90. package/templates/starter/docs/toolcraft/performance.md +112 -0
  91. package/templates/starter/docs/toolcraft/renderer-technique.md +48 -0
  92. package/templates/starter/docs/toolcraft/schema-reference.md +265 -0
  93. package/templates/starter/docs/toolcraft/workflow.md +87 -0
  94. package/templates/starter/e2e/app-browser-acceptance.spec.ts +785 -0
  95. package/templates/starter/e2e/app-controls.spec.ts +41 -0
  96. package/templates/starter/e2e/app-performance.spec.ts +326 -0
  97. package/templates/starter/e2e/canvas-handle-helpers.ts +244 -0
  98. package/templates/starter/e2e/performance-helpers.ts +612 -0
  99. package/templates/starter/e2e/product-observable-helpers.ts +170 -0
  100. package/templates/starter/index.html +12 -0
  101. package/templates/starter/package.json +52 -0
  102. package/templates/starter/playwright.config.ts +43 -0
  103. package/templates/starter/scripts/check-ai-skills.mjs +95 -0
  104. package/templates/starter/scripts/check-toolcraft-docs.mjs +159 -0
  105. package/templates/starter/scripts/check-toolcraft-integrity.mjs +232 -0
  106. package/templates/starter/scripts/run-vite-on-free-port.mjs +48 -0
  107. package/templates/starter/scripts/toolcraft-port.mjs +54 -0
  108. package/templates/starter/scripts/toolcraft-port.test.mjs +73 -0
  109. package/templates/starter/src/app/starter-acceptance.test.ts +5959 -0
  110. package/templates/starter/src/app/starter-acceptance.ts +2646 -0
  111. package/templates/starter/src/app/starter-performance.test.ts +1390 -0
  112. package/templates/starter/src/app/starter-performance.ts +12 -0
  113. package/templates/starter/src/app/starter-schema.test.ts +70 -0
  114. package/templates/starter/src/app/starter-schema.ts +15 -0
  115. package/templates/starter/src/main.tsx +18 -0
  116. package/templates/starter/src/router.tsx +16 -0
  117. package/templates/starter/src/routes/index.tsx +7 -0
  118. package/templates/starter/src/routes/root.tsx +19 -0
  119. package/templates/starter/src/styles.css +120 -0
  120. package/templates/starter/tsconfig.json +11 -0
  121. package/templates/starter/vite.config.ts +13 -0
  122. package/templates/ui/components/composites/accordion.tsx +73 -0
  123. package/templates/ui/components/composites/alert-dialog.tsx +190 -0
  124. package/templates/ui/components/composites/alert.tsx +74 -0
  125. package/templates/ui/components/composites/aspect-ratio.tsx +22 -0
  126. package/templates/ui/components/composites/avatar.tsx +98 -0
  127. package/templates/ui/components/composites/badge.tsx +69 -0
  128. package/templates/ui/components/composites/breadcrumb.tsx +106 -0
  129. package/templates/ui/components/composites/card.tsx +91 -0
  130. package/templates/ui/components/composites/combobox.tsx +486 -0
  131. package/templates/ui/components/composites/command.tsx +296 -0
  132. package/templates/ui/components/composites/context-menu.tsx +247 -0
  133. package/templates/ui/components/composites/dialog.tsx +282 -0
  134. package/templates/ui/components/composites/dropdown-menu.tsx +299 -0
  135. package/templates/ui/components/composites/empty.tsx +110 -0
  136. package/templates/ui/components/composites/hover-card.tsx +44 -0
  137. package/templates/ui/components/composites/index.ts +30 -0
  138. package/templates/ui/components/composites/menubar.tsx +214 -0
  139. package/templates/ui/components/composites/navigation-menu.tsx +167 -0
  140. package/templates/ui/components/composites/pagination.tsx +131 -0
  141. package/templates/ui/components/composites/progress.tsx +72 -0
  142. package/templates/ui/components/composites/radio-group.tsx +84 -0
  143. package/templates/ui/components/composites/resizable.tsx +42 -0
  144. package/templates/ui/components/composites/sheet.tsx +153 -0
  145. package/templates/ui/components/composites/sidebar-structural.tsx +310 -0
  146. package/templates/ui/components/composites/sidebar.tsx +431 -0
  147. package/templates/ui/components/composites/sonner.tsx +35 -0
  148. package/templates/ui/components/composites/spinner.tsx +43 -0
  149. package/templates/ui/components/composites/table.tsx +108 -0
  150. package/templates/ui/components/composites/tabs.tsx +83 -0
  151. package/templates/ui/components/control-layout/index.tsx +437 -0
  152. package/templates/ui/components/controls/actions/actions-control.tsx +139 -0
  153. package/templates/ui/components/controls/actions/index.ts +9 -0
  154. package/templates/ui/components/controls/anchor-grid/anchor-grid-control.tsx +107 -0
  155. package/templates/ui/components/controls/anchor-grid/index.ts +4 -0
  156. package/templates/ui/components/controls/boolean/boolean-controls.tsx +79 -0
  157. package/templates/ui/components/controls/boolean/index.ts +4 -0
  158. package/templates/ui/components/controls/channel-mixer/channel-mixer-control.tsx +95 -0
  159. package/templates/ui/components/controls/channel-mixer/index.ts +4 -0
  160. package/templates/ui/components/controls/channel-tabs/channel-tabs.tsx +42 -0
  161. package/templates/ui/components/controls/channel-tabs/index.ts +6 -0
  162. package/templates/ui/components/controls/code-textarea/code-textarea-control.tsx +90 -0
  163. package/templates/ui/components/controls/code-textarea/index.ts +4 -0
  164. package/templates/ui/components/controls/color/color-control.tsx +571 -0
  165. package/templates/ui/components/controls/color/color-picker-popover.tsx +104 -0
  166. package/templates/ui/components/controls/color/index.ts +41 -0
  167. package/templates/ui/components/controls/color/palette-control-data.ts +436 -0
  168. package/templates/ui/components/controls/color/palette-control.tsx +535 -0
  169. package/templates/ui/components/controls/color/style-guide-color-picker-channel-utils.ts +162 -0
  170. package/templates/ui/components/controls/color/style-guide-color-picker-interactions.ts +190 -0
  171. package/templates/ui/components/controls/color/style-guide-color-picker-logic.ts +485 -0
  172. package/templates/ui/components/controls/color/style-guide-color-picker-parts.tsx +710 -0
  173. package/templates/ui/components/controls/color/style-guide-color-picker.tsx +503 -0
  174. package/templates/ui/components/controls/control-types.ts +43 -0
  175. package/templates/ui/components/controls/curves/curve-geometry.ts +355 -0
  176. package/templates/ui/components/controls/curves/curve-graph.tsx +390 -0
  177. package/templates/ui/components/controls/curves/curves-control.tsx +445 -0
  178. package/templates/ui/components/controls/curves/index.ts +6 -0
  179. package/templates/ui/components/controls/file-drop/file-drop-control.tsx +191 -0
  180. package/templates/ui/components/controls/file-drop/index.ts +5 -0
  181. package/templates/ui/components/controls/font-picker/font-catalog.json +15360 -0
  182. package/templates/ui/components/controls/font-picker/font-catalog.ts +116 -0
  183. package/templates/ui/components/controls/font-picker/font-picker-control.tsx +1202 -0
  184. package/templates/ui/components/controls/font-picker/font-preview-loader.ts +336 -0
  185. package/templates/ui/components/controls/font-picker/index.ts +24 -0
  186. package/templates/ui/components/controls/font-picker/use-hover-intent.ts +46 -0
  187. package/templates/ui/components/controls/gradient/gradient-control-utils.ts +190 -0
  188. package/templates/ui/components/controls/gradient/gradient-control.tsx +612 -0
  189. package/templates/ui/components/controls/gradient/gradient-stop-list.tsx +400 -0
  190. package/templates/ui/components/controls/gradient/gradient-toolbar.tsx +152 -0
  191. package/templates/ui/components/controls/gradient/index.ts +4 -0
  192. package/templates/ui/components/controls/image-picker/image-picker-control.tsx +139 -0
  193. package/templates/ui/components/controls/image-picker/index.ts +7 -0
  194. package/templates/ui/components/controls/index.ts +192 -0
  195. package/templates/ui/components/controls/range-input/index.ts +4 -0
  196. package/templates/ui/components/controls/range-input/range-input-control.tsx +173 -0
  197. package/templates/ui/components/controls/range-slider/index.ts +4 -0
  198. package/templates/ui/components/controls/range-slider/range-slider-control.tsx +122 -0
  199. package/templates/ui/components/controls/range-slider/range-slider-value.ts +61 -0
  200. package/templates/ui/components/controls/segmented/index.ts +8 -0
  201. package/templates/ui/components/controls/segmented/segmented-control.tsx +94 -0
  202. package/templates/ui/components/controls/select/index.ts +4 -0
  203. package/templates/ui/components/controls/select/select-control.tsx +223 -0
  204. package/templates/ui/components/controls/slider/index.ts +4 -0
  205. package/templates/ui/components/controls/slider/slider-control.tsx +150 -0
  206. package/templates/ui/components/controls/slider/slider-value.ts +56 -0
  207. package/templates/ui/components/controls/text-input/index.ts +4 -0
  208. package/templates/ui/components/controls/text-input/text-input-control.tsx +158 -0
  209. package/templates/ui/components/controls/use-measured-element-width.ts +42 -0
  210. package/templates/ui/components/controls/vector/index.ts +8 -0
  211. package/templates/ui/components/controls/vector/vector-control.tsx +401 -0
  212. package/templates/ui/components/panel/index.ts +19 -0
  213. package/templates/ui/components/panel/panel-actions.tsx +165 -0
  214. package/templates/ui/components/panel/panel-header.tsx +61 -0
  215. package/templates/ui/components/panel/panel-icon-button.tsx +96 -0
  216. package/templates/ui/components/panel/panel-section.tsx +168 -0
  217. package/templates/ui/components/panel/panel-surface.tsx +206 -0
  218. package/templates/ui/components/panel/panel.tsx +210 -0
  219. package/templates/ui/components/primitives/animated-loader.tsx +61 -0
  220. package/templates/ui/components/primitives/button-group.tsx +134 -0
  221. package/templates/ui/components/primitives/button.tsx +429 -0
  222. package/templates/ui/components/primitives/checkbox.tsx +62 -0
  223. package/templates/ui/components/primitives/editable-slider-value-label.tsx +337 -0
  224. package/templates/ui/components/primitives/field.tsx +225 -0
  225. package/templates/ui/components/primitives/index.ts +82 -0
  226. package/templates/ui/components/primitives/input-group.tsx +298 -0
  227. package/templates/ui/components/primitives/input.tsx +61 -0
  228. package/templates/ui/components/primitives/internal/button-loading.tsx +178 -0
  229. package/templates/ui/components/primitives/label.tsx +16 -0
  230. package/templates/ui/components/primitives/popover.tsx +126 -0
  231. package/templates/ui/components/primitives/portal-layer-context.tsx +33 -0
  232. package/templates/ui/components/primitives/primitive-arrow-icon.tsx +38 -0
  233. package/templates/ui/components/primitives/scroll-fade-logic.ts +441 -0
  234. package/templates/ui/components/primitives/scroll-fade-render.tsx +75 -0
  235. package/templates/ui/components/primitives/scroll-fade-types.ts +41 -0
  236. package/templates/ui/components/primitives/scroll-fade.tsx +72 -0
  237. package/templates/ui/components/primitives/select.tsx +408 -0
  238. package/templates/ui/components/primitives/selection-state.ts +31 -0
  239. package/templates/ui/components/primitives/separator.tsx +21 -0
  240. package/templates/ui/components/primitives/slider/index.ts +4 -0
  241. package/templates/ui/components/primitives/slider/slider-interaction.tsx +96 -0
  242. package/templates/ui/components/primitives/slider/slider-parts.tsx +303 -0
  243. package/templates/ui/components/primitives/slider/slider-reset.ts +152 -0
  244. package/templates/ui/components/primitives/slider/slider-value.ts +114 -0
  245. package/templates/ui/components/primitives/slider/slider.tsx +511 -0
  246. package/templates/ui/components/primitives/switch.tsx +35 -0
  247. package/templates/ui/components/primitives/textarea.tsx +49 -0
  248. package/templates/ui/components/primitives/toggle-group.tsx +114 -0
  249. package/templates/ui/components/primitives/toggle.tsx +46 -0
  250. package/templates/ui/components/primitives/tooltip.tsx +100 -0
  251. package/templates/ui/hooks/use-mobile.ts +21 -0
  252. package/templates/ui/index.ts +31 -0
  253. package/templates/ui/lib/control-outline.ts +3 -0
  254. package/templates/ui/lib/input-control-style.ts +131 -0
  255. package/templates/ui/lib/style-guide-color-utils.ts +111 -0
  256. package/templates/ui/lib/utils.ts +6 -0
  257. package/templates/ui/styles.css +291 -0
@@ -0,0 +1,571 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+
5
+ import { ControlFieldLabel } from "../../control-layout";
6
+ import {
7
+ ButtonGroup,
8
+ Field,
9
+ Input,
10
+ InputGroup,
11
+ InputGroupAddon,
12
+ InputGroupInput,
13
+ InputGroupText,
14
+ } from "../../primitives";
15
+ import { cn } from "../../../lib/utils";
16
+ import { ColorPickerPopover } from "./color-picker-popover";
17
+ import {
18
+ createControlHistoryGroupId,
19
+ type ControlChangeMeta,
20
+ type ControlValueChangeHandler,
21
+ } from "../control-types";
22
+
23
+ export type ColorControlInput = {
24
+ hex?: string;
25
+ name: string;
26
+ onValueChange?: ControlValueChangeHandler<{ hex: string }>;
27
+ showLabel?: boolean;
28
+ };
29
+
30
+ export type ColorOpacityValue = {
31
+ hex: string;
32
+ opacity: number;
33
+ };
34
+
35
+ export type ColorOpacityControlProps = {
36
+ hex?: string;
37
+ name: string;
38
+ onValueChange?: ControlValueChangeHandler<ColorOpacityValue>;
39
+ opacity?: number;
40
+ showLabel?: boolean;
41
+ };
42
+
43
+ export type ColorControlInputPair = readonly [
44
+ ColorControlInput,
45
+ ColorControlInput,
46
+ ];
47
+
48
+ type ColorControlSingleProps = ColorControlInput & {
49
+ inputs?: never;
50
+ };
51
+
52
+ type ColorControlGroupProps = {
53
+ inputs: ColorControlInputPair;
54
+ };
55
+
56
+ export type ColorControlProps =
57
+ | ColorControlSingleProps
58
+ | ColorControlGroupProps;
59
+
60
+ type ColorValueControlProps = {
61
+ children?: React.ReactNode;
62
+ className?: string;
63
+ color: string;
64
+ inputName?: string;
65
+ label: string;
66
+ nativeInputName?: string;
67
+ onColorChange: ControlValueChangeHandler<string>;
68
+ showHash?: boolean;
69
+ size?: "default" | "sm";
70
+ };
71
+
72
+ const colorValueButtonGroupClassName =
73
+ "w-full has-[[data-slot=button][aria-expanded=true]]:[&>input]:!border-l-[color:color-mix(in_oklab,var(--border)_30%,transparent)] has-[[data-slot=button][data-open]]:[&>input]:!border-l-[color:color-mix(in_oklab,var(--border)_30%,transparent)] has-[[data-slot=button][data-popup-open]]:[&>input]:!border-l-[color:color-mix(in_oklab,var(--border)_30%,transparent)] has-[[data-slot=button][data-state=open]]:[&>input]:!border-l-[color:color-mix(in_oklab,var(--border)_30%,transparent)]";
74
+
75
+ function isColorControlGroupProps(
76
+ props: ColorControlProps,
77
+ ): props is ColorControlGroupProps {
78
+ return Array.isArray((props as ColorControlGroupProps).inputs);
79
+ }
80
+
81
+ function resolveCssVariableColor(color: string): string {
82
+ const variableMatch = /^var\(\s*(--[\w-]+)\s*(?:,\s*(.+))?\)$/i.exec(
83
+ color.trim(),
84
+ );
85
+
86
+ if (!variableMatch?.[1] || typeof window === "undefined") {
87
+ return color;
88
+ }
89
+
90
+ const resolvedValue = window
91
+ .getComputedStyle(document.documentElement)
92
+ .getPropertyValue(variableMatch[1]);
93
+ const fallbackValue = variableMatch[2]?.trim();
94
+ const nextColor = resolvedValue.trim() || fallbackValue;
95
+
96
+ return nextColor ? resolveCssVariableColor(nextColor) : color;
97
+ }
98
+
99
+ function formatHexChannel(value: number): string {
100
+ return Math.round(Math.min(1, Math.max(0, value)) * 255)
101
+ .toString(16)
102
+ .padStart(2, "0")
103
+ .toUpperCase();
104
+ }
105
+
106
+ function linearSrgbToSrgb(value: number): number {
107
+ return value >= 0.0031308
108
+ ? 1.055 * value ** (1 / 2.4) - 0.055
109
+ : 12.92 * value;
110
+ }
111
+
112
+ function oklchToHex(color: string): string | null {
113
+ const match =
114
+ /^oklch\(\s*([+-]?\d*\.?\d+%?)\s+([+-]?\d*\.?\d+)\s+([+-]?\d*\.?\d+)(?:deg)?(?:\s*\/\s*[+-]?\d*\.?\d+%?)?\s*\)$/i.exec(
115
+ color.trim(),
116
+ );
117
+
118
+ if (!match) {
119
+ return null;
120
+ }
121
+
122
+ const rawLightness = match[1] ?? "";
123
+ const lightness = rawLightness.endsWith("%")
124
+ ? Number.parseFloat(rawLightness) / 100
125
+ : Number.parseFloat(rawLightness);
126
+ const chroma = Number.parseFloat(match[2] ?? "");
127
+ const hue = (Number.parseFloat(match[3] ?? "") * Math.PI) / 180;
128
+
129
+ if (
130
+ !Number.isFinite(lightness) ||
131
+ !Number.isFinite(chroma) ||
132
+ !Number.isFinite(hue)
133
+ ) {
134
+ return null;
135
+ }
136
+
137
+ const a = chroma * Math.cos(hue);
138
+ const b = chroma * Math.sin(hue);
139
+ const lCone = lightness + 0.3963377774 * a + 0.2158037573 * b;
140
+ const mCone = lightness - 0.1055613458 * a - 0.0638541728 * b;
141
+ const sCone = lightness - 0.0894841775 * a - 1.291485548 * b;
142
+ const l = lCone ** 3;
143
+ const m = mCone ** 3;
144
+ const s = sCone ** 3;
145
+ const red = linearSrgbToSrgb(
146
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
147
+ );
148
+ const green = linearSrgbToSrgb(
149
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
150
+ );
151
+ const blue = linearSrgbToSrgb(
152
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
153
+ );
154
+
155
+ return `#${formatHexChannel(red)}${formatHexChannel(green)}${formatHexChannel(blue)}`;
156
+ }
157
+
158
+ function rgbToHex(color: string): string | null {
159
+ const match =
160
+ /^rgba?\(\s*([+-]?\d*\.?\d+%?)\s*[,\s]\s*([+-]?\d*\.?\d+%?)\s*[,\s]\s*([+-]?\d*\.?\d+%?)/i.exec(
161
+ color.trim(),
162
+ );
163
+
164
+ if (!match) {
165
+ return null;
166
+ }
167
+
168
+ const channels = [match[1] ?? "", match[2] ?? "", match[3] ?? ""].map(
169
+ (channel) => {
170
+ const parsed = Number.parseFloat(channel);
171
+ return channel.endsWith("%") ? parsed / 100 : parsed / 255;
172
+ },
173
+ );
174
+
175
+ if (channels.some((channel) => !Number.isFinite(channel))) {
176
+ return null;
177
+ }
178
+
179
+ const [red = 0, green = 0, blue = 0] = channels;
180
+
181
+ return `#${formatHexChannel(red)}${formatHexChannel(green)}${formatHexChannel(blue)}`;
182
+ }
183
+
184
+ function getNativeColorPickerValue(color: string): string {
185
+ const trimmedColor = resolveCssVariableColor(color).trim();
186
+ const bareHexMatch = /^([0-9a-f]{6})$/i.exec(trimmedColor);
187
+
188
+ if (bareHexMatch?.[1]) {
189
+ return `#${bareHexMatch[1].toUpperCase()}`;
190
+ }
191
+
192
+ const hexMatch = /^#([0-9a-f]{6})$/i.exec(trimmedColor);
193
+
194
+ if (hexMatch?.[1]) {
195
+ return `#${hexMatch[1].toUpperCase()}`;
196
+ }
197
+
198
+ const shortHexMatch = /^#([0-9a-f]{3})$/i.exec(trimmedColor);
199
+
200
+ if (shortHexMatch?.[1]) {
201
+ const [red = "0", green = "0", blue = "0"] = shortHexMatch[1];
202
+ return `#${red}${red}${green}${green}${blue}${blue}`.toUpperCase();
203
+ }
204
+
205
+ const parsedFunctionalColor =
206
+ oklchToHex(trimmedColor) ?? rgbToHex(trimmedColor);
207
+
208
+ if (parsedFunctionalColor) {
209
+ return parsedFunctionalColor;
210
+ }
211
+
212
+ return "#000000";
213
+ }
214
+
215
+ function getSwatchColorValue(color: string): string {
216
+ const trimmedColor = color.trim();
217
+
218
+ if (trimmedColor.startsWith("var(") || trimmedColor.includes("(")) {
219
+ return trimmedColor;
220
+ }
221
+
222
+ return getNativeColorPickerValue(trimmedColor);
223
+ }
224
+
225
+ function getHexDraftValue(color: string, showHash: boolean): string {
226
+ const hexColor = getNativeColorPickerValue(color);
227
+
228
+ return showHash ? hexColor : hexColor.slice(1);
229
+ }
230
+
231
+ function getSanitizedHexDraft(value: string, showHash: boolean): string {
232
+ const hex = value
233
+ .replace(/[^\da-f]/gi, "")
234
+ .slice(0, 6)
235
+ .toUpperCase();
236
+
237
+ return showHash ? `#${hex}` : hex;
238
+ }
239
+
240
+ function getCommittedHexColor(value: string): string | null {
241
+ const hex = value
242
+ .replace(/[^\da-f]/gi, "")
243
+ .slice(0, 6)
244
+ .toUpperCase();
245
+
246
+ return hex.length === 6 ? `#${hex}` : null;
247
+ }
248
+
249
+ function parseOpacityValue(opacity: number | undefined): number {
250
+ return Math.min(100, Math.max(0, Math.round(opacity ?? 100)));
251
+ }
252
+
253
+ function normalizeOpacityInput(value: string): number {
254
+ const parsedValue = Number.parseFloat(value);
255
+
256
+ return Number.isFinite(parsedValue)
257
+ ? Math.min(100, Math.max(0, Math.round(parsedValue)))
258
+ : 100;
259
+ }
260
+
261
+ function ColorOpacityInput({
262
+ label,
263
+ name,
264
+ onOpacityChange,
265
+ opacity,
266
+ }: {
267
+ label: string;
268
+ name?: string;
269
+ onOpacityChange: (nextOpacity: number, meta?: ControlChangeMeta) => void;
270
+ opacity: number;
271
+ }): React.JSX.Element {
272
+ const committedOpacity = String(parseOpacityValue(opacity));
273
+ const [draftOpacity, setDraftOpacity] = React.useState(committedOpacity);
274
+
275
+ React.useEffect(() => {
276
+ setDraftOpacity(committedOpacity);
277
+ }, [committedOpacity]);
278
+
279
+ function commitOpacity(nextDraft = draftOpacity): void {
280
+ const trimmedDraft = nextDraft.trim();
281
+
282
+ if (trimmedDraft === "" || !Number.isFinite(Number.parseFloat(trimmedDraft))) {
283
+ setDraftOpacity(committedOpacity);
284
+ return;
285
+ }
286
+
287
+ const nextOpacity = normalizeOpacityInput(trimmedDraft);
288
+
289
+ setDraftOpacity(String(nextOpacity));
290
+
291
+ if (nextOpacity !== parseOpacityValue(opacity)) {
292
+ onOpacityChange(nextOpacity);
293
+ }
294
+ }
295
+
296
+ return (
297
+ <InputGroup className="w-14 flex-none rounded-l-none [&:not(:focus-within):hover]:!border-[color:color-mix(in_oklab,var(--border)_20%,transparent)] [&:not(:focus-within):hover]:text-[color:var(--foreground)]">
298
+ <InputGroupInput
299
+ aria-label={`${label} opacity`}
300
+ autoComplete="off"
301
+ className="pl-2 pr-1 text-right font-mono"
302
+ inputMode="numeric"
303
+ name={name}
304
+ onBlur={() => commitOpacity()}
305
+ onChange={(event) => setDraftOpacity(event.target.value)}
306
+ onKeyDown={(event) => {
307
+ if (event.key === "Enter") {
308
+ event.preventDefault();
309
+ commitOpacity(event.currentTarget.value);
310
+ event.currentTarget.blur();
311
+ return;
312
+ }
313
+
314
+ if (event.key === "Escape") {
315
+ event.preventDefault();
316
+ setDraftOpacity(committedOpacity);
317
+ event.currentTarget.blur();
318
+ }
319
+ }}
320
+ type="text"
321
+ value={draftOpacity}
322
+ />
323
+ <InputGroupAddon align="inline-end" className="pr-1.5 pl-0">
324
+ <InputGroupText className="group-hover/input-group:text-[color:var(--foreground)]">
325
+ %
326
+ </InputGroupText>
327
+ </InputGroupAddon>
328
+ </InputGroup>
329
+ );
330
+ }
331
+
332
+ export function ColorValueControl({
333
+ children,
334
+ className,
335
+ color,
336
+ inputName,
337
+ label,
338
+ nativeInputName,
339
+ onColorChange,
340
+ showHash = true,
341
+ size = "default",
342
+ }: ColorValueControlProps): React.JSX.Element {
343
+ const [draftColor, setDraftColor] = React.useState(() =>
344
+ getHexDraftValue(color, showHash),
345
+ );
346
+ const [previewColor, setPreviewColor] = React.useState(color);
347
+ const liveHistoryGroupRef = React.useRef<string | null>(null);
348
+
349
+ React.useEffect(() => {
350
+ setDraftColor(getHexDraftValue(color, showHash));
351
+ setPreviewColor(color);
352
+ }, [color, showHash]);
353
+
354
+ function getLiveHistoryMeta(): ControlChangeMeta {
355
+ liveHistoryGroupRef.current ??= createControlHistoryGroupId(`color:${label}`);
356
+
357
+ return {
358
+ history: "merge",
359
+ historyGroup: liveHistoryGroupRef.current,
360
+ };
361
+ }
362
+
363
+ function finishLiveHistoryGroup(): void {
364
+ liveHistoryGroupRef.current = null;
365
+ }
366
+
367
+ function commitColor(nextColor: string, meta?: ControlChangeMeta): void {
368
+ const normalizedColor = nextColor.toUpperCase();
369
+ setPreviewColor(normalizedColor);
370
+ setDraftColor(getHexDraftValue(normalizedColor, showHash));
371
+ onColorChange(normalizedColor, meta);
372
+ }
373
+
374
+ function updateDraft(nextValue: string): void {
375
+ const nextDraft = getSanitizedHexDraft(nextValue, showHash);
376
+
377
+ setDraftColor(nextDraft);
378
+
379
+ const committedColor = getCommittedHexColor(nextDraft);
380
+ if (committedColor) {
381
+ setPreviewColor(committedColor);
382
+ }
383
+ }
384
+
385
+ function handleDraftBlur(): void {
386
+ const committedColor = getCommittedHexColor(draftColor);
387
+
388
+ if (committedColor) {
389
+ commitColor(
390
+ committedColor,
391
+ liveHistoryGroupRef.current ? getLiveHistoryMeta() : undefined,
392
+ );
393
+ finishLiveHistoryGroup();
394
+ return;
395
+ }
396
+
397
+ setDraftColor(getHexDraftValue(color, showHash));
398
+ setPreviewColor(color);
399
+ finishLiveHistoryGroup();
400
+ }
401
+
402
+ return (
403
+ <>
404
+ <ButtonGroup
405
+ adjacentBorderTone="subtle"
406
+ className={cn(colorValueButtonGroupClassName, className)}
407
+ >
408
+ <ColorPickerPopover
409
+ label={label}
410
+ pickerValue={getNativeColorPickerValue(previewColor)}
411
+ showOpacity={Boolean(children)}
412
+ size={size}
413
+ swatchColor={getSwatchColorValue(previewColor)}
414
+ onColorChange={(nextColor) => commitColor(nextColor, getLiveHistoryMeta())}
415
+ onCommit={handleDraftBlur}
416
+ />
417
+ <Input
418
+ aria-label={`${label} hex`}
419
+ autoComplete="off"
420
+ className="font-mono"
421
+ name={inputName}
422
+ onBlur={handleDraftBlur}
423
+ onChange={(event) => updateDraft(event.target.value)}
424
+ onKeyDown={(event) => {
425
+ if (event.key === "Enter") {
426
+ event.preventDefault();
427
+ handleDraftBlur();
428
+ event.currentTarget.blur();
429
+ return;
430
+ }
431
+
432
+ if (event.key === "Escape") {
433
+ event.preventDefault();
434
+ setDraftColor(getHexDraftValue(color, showHash));
435
+ setPreviewColor(color);
436
+ finishLiveHistoryGroup();
437
+ event.currentTarget.blur();
438
+ }
439
+ }}
440
+ size={size}
441
+ spellCheck={false}
442
+ type="text"
443
+ value={draftColor}
444
+ />
445
+ {children}
446
+ </ButtonGroup>
447
+ {nativeInputName ? (
448
+ <input name={nativeInputName} type="hidden" value={previewColor} />
449
+ ) : null}
450
+ </>
451
+ );
452
+ }
453
+
454
+ function ColorControlField({
455
+ fullWidth = false,
456
+ hex,
457
+ name,
458
+ onValueChange,
459
+ showLabel = false,
460
+ }: ColorControlInput & { fullWidth?: boolean }): React.JSX.Element {
461
+ const activeColor = hex ?? "var(--foreground)";
462
+
463
+ function updateColor(nextColor: string, meta?: ControlChangeMeta): void {
464
+ const nextValue = {
465
+ hex: nextColor,
466
+ };
467
+
468
+ if (meta) {
469
+ onValueChange?.(nextValue, meta);
470
+ return;
471
+ }
472
+
473
+ onValueChange?.(nextValue);
474
+ }
475
+
476
+ return (
477
+ <Field className="h-fit min-w-0 justify-start gap-2">
478
+ {showLabel ? <ControlFieldLabel>{name}</ControlFieldLabel> : null}
479
+ <div className={cn("min-w-0", fullWidth ? "w-full" : "w-1/2 shrink-0")}>
480
+ <ColorValueControl
481
+ color={activeColor}
482
+ label={name}
483
+ onColorChange={updateColor}
484
+ />
485
+ </div>
486
+ </Field>
487
+ );
488
+ }
489
+
490
+ export function ColorControl(props: ColorControlProps): React.JSX.Element {
491
+ if (isColorControlGroupProps(props)) {
492
+ return (
493
+ <div
494
+ className="grid min-w-0 gap-[10px]"
495
+ data-slot="color-control-grid"
496
+ style={{
497
+ gridTemplateColumns: "repeat(2, minmax(0, 1fr))",
498
+ }}
499
+ >
500
+ {props.inputs.map((input, index) => (
501
+ <ColorControlField
502
+ fullWidth
503
+ key={`${input.name}-${index}`}
504
+ {...input}
505
+ />
506
+ ))}
507
+ </div>
508
+ );
509
+ }
510
+
511
+ return <ColorControlField {...props} />;
512
+ }
513
+
514
+ export function ColorOpacityControl({
515
+ hex,
516
+ name,
517
+ onValueChange,
518
+ opacity,
519
+ showLabel = false,
520
+ }: ColorOpacityControlProps): React.JSX.Element {
521
+ const activeColor = hex ?? "var(--foreground)";
522
+ const activeOpacity = parseOpacityValue(opacity);
523
+
524
+ function updateColor(nextColor: string, meta?: ControlChangeMeta): void {
525
+ const nextValue = {
526
+ hex: nextColor,
527
+ opacity: activeOpacity,
528
+ };
529
+
530
+ if (meta) {
531
+ onValueChange?.(nextValue, meta);
532
+ return;
533
+ }
534
+
535
+ onValueChange?.(nextValue);
536
+ }
537
+
538
+ function updateOpacity(nextOpacity: number, meta?: ControlChangeMeta): void {
539
+ const nextValue = {
540
+ hex: activeColor,
541
+ opacity: nextOpacity,
542
+ };
543
+
544
+ if (meta) {
545
+ onValueChange?.(nextValue, meta);
546
+ return;
547
+ }
548
+
549
+ onValueChange?.(nextValue);
550
+ }
551
+
552
+ return (
553
+ <Field className="h-fit min-w-0 justify-start gap-2">
554
+ {showLabel ? <ControlFieldLabel>{name}</ControlFieldLabel> : null}
555
+ <div className="min-w-0 w-full">
556
+ <ColorValueControl
557
+ color={activeColor}
558
+ label={name}
559
+ onColorChange={updateColor}
560
+ >
561
+ <ColorOpacityInput
562
+ label={name}
563
+ name={`${name.toLowerCase().replace(/\s+/g, "-")}-opacity`}
564
+ onOpacityChange={updateOpacity}
565
+ opacity={activeOpacity}
566
+ />
567
+ </ColorValueControl>
568
+ </div>
569
+ </Field>
570
+ );
571
+ }
@@ -0,0 +1,104 @@
1
+ "use client";
2
+
3
+ import {
4
+ Popover,
5
+ PopoverContent,
6
+ PopoverTrigger,
7
+ } from "../../primitives/popover";
8
+ import { StyleGuideColorPicker } from "./style-guide-color-picker";
9
+ import { cn } from "../../../lib/utils";
10
+ import { useCallback, useRef, useState } from "react";
11
+
12
+ type ColorPickerPopoverSize = "default" | "sm";
13
+
14
+ type ColorPickerPopoverProps = {
15
+ disabled?: boolean;
16
+ label: string;
17
+ pickerValue: string;
18
+ showOpacity?: boolean;
19
+ size?: ColorPickerPopoverSize;
20
+ swatchColor: string;
21
+ onColorChange: (nextColor: string) => void;
22
+ onCommit?: () => void;
23
+ };
24
+
25
+ export function ColorPickerPopover({
26
+ disabled = false,
27
+ label,
28
+ pickerValue,
29
+ showOpacity = false,
30
+ size = "default",
31
+ swatchColor,
32
+ onColorChange,
33
+ onCommit,
34
+ }: ColorPickerPopoverProps): React.JSX.Element {
35
+ const [open, setOpen] = useState(false);
36
+ const triggerRef = useRef<HTMLButtonElement | null>(null);
37
+ const commitColor = useCallback(() => {
38
+ onCommit?.();
39
+ }, [onCommit]);
40
+ const clearTriggerFocus = useCallback(() => {
41
+ const blurTrigger = () => {
42
+ if (document.activeElement === triggerRef.current) {
43
+ triggerRef.current?.blur();
44
+ }
45
+ };
46
+
47
+ blurTrigger();
48
+ window.requestAnimationFrame(blurTrigger);
49
+ window.setTimeout(blurTrigger, 0);
50
+ }, []);
51
+ const handleOpenChange = useCallback(
52
+ (nextOpen: boolean) => {
53
+ setOpen(nextOpen);
54
+ if (!nextOpen) {
55
+ commitColor();
56
+ clearTriggerFocus();
57
+ }
58
+ },
59
+ [clearTriggerFocus, commitColor],
60
+ );
61
+
62
+ return (
63
+ <Popover onOpenChange={handleOpenChange} open={open}>
64
+ <PopoverTrigger
65
+ aria-label={`Pick ${label}`}
66
+ className={cn(
67
+ "group/button inline-flex shrink-0 cursor-pointer items-center justify-center rounded-lg border font-medium whitespace-nowrap transition-colors outline-none select-none focus-visible:border-[color:var(--ring)] focus-visible:ring-2 focus-visible:ring-[color:color-mix(in_oklab,var(--ring)_30%,transparent)]",
68
+ "border-[color:color-mix(in_oklab,var(--border)_12%,transparent)] bg-[color:color-mix(in_oklab,var(--input)_5%,transparent)]",
69
+ size === "sm"
70
+ ? "size-6 text-xs/relaxed"
71
+ : "size-7 text-[13px] leading-[1.125rem]",
72
+ "relative aspect-square flex-none p-0 [&:not(:focus):not([aria-expanded=true]):not([data-open]):not([data-popup-open]):not([data-state=open]):hover]:!border-[color:color-mix(in_oklab,var(--border)_20%,transparent)] [&:not(:focus):not([aria-expanded=true]):not([data-open]):not([data-popup-open]):not([data-state=open]):hover]:text-[color:var(--foreground)] aria-expanded:border-[color:color-mix(in_oklab,var(--border)_30%,transparent)] data-open:border-[color:color-mix(in_oklab,var(--border)_30%,transparent)] data-popup-open:border-[color:color-mix(in_oklab,var(--border)_30%,transparent)] data-[state=open]:border-[color:color-mix(in_oklab,var(--border)_30%,transparent)] disabled:pointer-events-none disabled:opacity-50",
73
+ )}
74
+ data-slot="button"
75
+ disabled={disabled}
76
+ ref={triggerRef}
77
+ type="button"
78
+ >
79
+ <span
80
+ aria-hidden="true"
81
+ className={cn(
82
+ "aspect-square shrink-0 rounded-[3px] border border-[color:color-mix(in_oklab,var(--border)_10%,transparent)]",
83
+ size === "sm" ? "size-3.5" : "size-4",
84
+ )}
85
+ style={{ backgroundColor: swatchColor }}
86
+ />
87
+ </PopoverTrigger>
88
+ <PopoverContent
89
+ align="start"
90
+ className="w-[236px] gap-0 overflow-visible p-0"
91
+ finalFocus={false}
92
+ sideOffset={6}
93
+ >
94
+ <StyleGuideColorPicker
95
+ value={pickerValue}
96
+ disabled={disabled}
97
+ showOpacity={showOpacity}
98
+ onChange={onColorChange}
99
+ onCommit={commitColor}
100
+ />
101
+ </PopoverContent>
102
+ </Popover>
103
+ );
104
+ }
@@ -0,0 +1,41 @@
1
+ "use client";
2
+
3
+ export {
4
+ ColorControl,
5
+ ColorOpacityControl,
6
+ ColorOpacityControl as ColorOpacity,
7
+ ColorValueControl,
8
+ } from "./color-control";
9
+ export type {
10
+ ColorControlInput,
11
+ ColorControlInputPair,
12
+ ColorControlProps,
13
+ ColorOpacityControlProps,
14
+ ColorOpacityValue,
15
+ } from "./color-control";
16
+ export {
17
+ getColorSurfaceModel,
18
+ type ColorFormatMode,
19
+ type ColorSurfaceModel,
20
+ } from "./style-guide-color-picker-channel-utils";
21
+ export { DEFAULT_COLOR_FORMAT_MODE } from "./style-guide-color-picker";
22
+ export {
23
+ getColorSurfaceSliderConfig,
24
+ getColorSurfaceStyle,
25
+ getColorSurfaceThumbPosition,
26
+ } from "./style-guide-color-picker-parts";
27
+ export { getSurfaceHsvColor } from "./style-guide-color-picker-logic";
28
+ export {
29
+ PALETTE_SHADE_STEPS,
30
+ PaletteControl,
31
+ STYLE_GUIDE_PRIMARY_FAMILY_OPTIONS,
32
+ TAILWIND_COLOR_PALETTE,
33
+ getPaletteHex,
34
+ } from "./palette-control";
35
+ export type {
36
+ PaletteColorFamily,
37
+ PaletteControlChangeMeta,
38
+ PaletteControlProps,
39
+ PaletteControlValue,
40
+ PaletteShadeStep,
41
+ } from "./palette-control";