@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,710 @@
1
+ "use client";
2
+
3
+ import {
4
+ useCallback,
5
+ useEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ type ChangeEvent,
10
+ type CSSProperties,
11
+ type KeyboardEvent,
12
+ type PointerEvent as ReactPointerEvent,
13
+ type RefObject,
14
+ } from "react";
15
+ import {
16
+ InputGroup,
17
+ InputGroupAddon,
18
+ InputGroupInput,
19
+ InputGroupText,
20
+ } from "../../primitives/input-group";
21
+ import { StaticSelect } from "../select";
22
+ import {
23
+ getColorChannels,
24
+ getEditableChannelHex,
25
+ type ColorChannels,
26
+ type ColorFormatMode,
27
+ type ColorSurfaceModel,
28
+ } from "./style-guide-color-picker-channel-utils";
29
+ import { type HsvColor } from "../../../lib/style-guide-color-utils";
30
+ import { cn } from "../../../lib/utils";
31
+ import { type ColorSurfacePosition } from "./style-guide-color-picker-logic";
32
+
33
+ export { getColorChannels } from "./style-guide-color-picker-channel-utils";
34
+
35
+ const HUE_RAIL_BACKGROUND =
36
+ "linear-gradient(90deg, #ff0000 0%, #ffff00 16.67%, #00ff00 33.33%, #00ffff 50%, #0000ff 66.67%, #ff00ff 83.33%, #ff0000 100%)";
37
+ const RGB_BLUE_RAIL_BACKGROUND = "linear-gradient(90deg, rgb(0 0 0), rgb(0 0 255))";
38
+
39
+ function clampSliderValue(value: number, max: number): number {
40
+ return Math.min(max, Math.max(0, value));
41
+ }
42
+
43
+ function getSliderValueFromClientX(clientX: number, bounds: DOMRect, max: number): number {
44
+ if (bounds.width === 0) return 0;
45
+
46
+ return clampSliderValue(Math.round(((clientX - bounds.left) / bounds.width) * max), max);
47
+ }
48
+
49
+ type ColorSurfaceProps = {
50
+ surfaceRef: RefObject<HTMLDivElement | null>;
51
+ surfaceLabel: string;
52
+ surfaceClassName?: string;
53
+ disabled: boolean;
54
+ hueColor: string;
55
+ currentColorHex: string;
56
+ colorModel: ColorSurfaceModel;
57
+ optimisticColor: HsvColor;
58
+ surfacePosition: ColorSurfacePosition | null;
59
+ isSurfaceDragging: boolean;
60
+ onPointerDown: (event: ReactPointerEvent<HTMLDivElement>) => void;
61
+ onThumbPointerDown: (event: ReactPointerEvent<HTMLDivElement>) => void;
62
+ };
63
+
64
+ function getRgbCss([red, green, blue]: [number, number, number]): string {
65
+ return `rgb(${red} ${green} ${blue})`;
66
+ }
67
+
68
+ export function getColorSurfaceThumbPosition({
69
+ colorModel,
70
+ currentColorHex,
71
+ optimisticColor,
72
+ surfacePosition,
73
+ }: {
74
+ colorModel: ColorSurfaceModel;
75
+ currentColorHex: string;
76
+ optimisticColor: HsvColor;
77
+ surfacePosition?: ColorSurfacePosition | null;
78
+ }): { left: string; top: string } {
79
+ if (surfacePosition) {
80
+ return {
81
+ left: `${surfacePosition.x * 100}%`,
82
+ top: `${surfacePosition.y * 100}%`,
83
+ };
84
+ }
85
+
86
+ const channels = getColorChannels(currentColorHex);
87
+
88
+ if (colorModel === "rgb") {
89
+ const [red, green] = channels.rgb;
90
+
91
+ return {
92
+ left: `${(red / 255) * 100}%`,
93
+ top: `${(1 - green / 255) * 100}%`,
94
+ };
95
+ }
96
+
97
+ if (colorModel === "hsl") {
98
+ const [, saturation, lightness] = channels.hsl;
99
+
100
+ return {
101
+ left: `${saturation}%`,
102
+ top: `${100 - lightness}%`,
103
+ };
104
+ }
105
+
106
+ return {
107
+ left: `${optimisticColor.s * 100}%`,
108
+ top: `${(1 - optimisticColor.v) * 100}%`,
109
+ };
110
+ }
111
+
112
+ export function getColorSurfaceStyle({
113
+ colorModel,
114
+ currentColorHex,
115
+ hue,
116
+ hueColor,
117
+ }: {
118
+ colorModel: ColorSurfaceModel;
119
+ currentColorHex: string;
120
+ hue?: number;
121
+ hueColor: string;
122
+ }): CSSProperties {
123
+ const channels = getColorChannels(currentColorHex);
124
+
125
+ if (colorModel === "rgb") {
126
+ const blue = channels.rgb[2];
127
+
128
+ return {
129
+ backgroundColor: getRgbCss([0, 0, blue]),
130
+ backgroundImage: [
131
+ "linear-gradient(to right, rgb(0 0 0), rgb(255 0 0))",
132
+ "linear-gradient(to top, rgb(0 0 0), rgb(0 255 0))",
133
+ ].join(", "),
134
+ backgroundBlendMode: "screen",
135
+ };
136
+ }
137
+
138
+ if (colorModel === "hsl") {
139
+ const [fallbackHue] = channels.hsl;
140
+ const resolvedHue = hue ?? fallbackHue;
141
+
142
+ return {
143
+ backgroundImage: [
144
+ "linear-gradient(to bottom, #fff 0%, transparent 50%, #000 100%)",
145
+ `linear-gradient(to right, hsl(${resolvedHue} 0% 50%), hsl(${resolvedHue} 100% 50%))`,
146
+ ].join(", "),
147
+ };
148
+ }
149
+
150
+ return {
151
+ backgroundColor: hueColor,
152
+ };
153
+ }
154
+
155
+ export function getColorSurfaceSliderConfig({
156
+ colorModel,
157
+ currentColorHex,
158
+ hueLabel,
159
+ optimisticColor,
160
+ }: {
161
+ colorModel: ColorSurfaceModel;
162
+ currentColorHex: string;
163
+ hueLabel: string;
164
+ optimisticColor: HsvColor;
165
+ }): {
166
+ label: string;
167
+ max: number;
168
+ railBackground: string;
169
+ value: number;
170
+ } {
171
+ if (colorModel === "rgb") {
172
+ const [, , blue] = getColorChannels(currentColorHex).rgb;
173
+
174
+ return {
175
+ label: "RGB blue channel",
176
+ max: 255,
177
+ railBackground: RGB_BLUE_RAIL_BACKGROUND,
178
+ value: blue,
179
+ };
180
+ }
181
+
182
+ return {
183
+ label: hueLabel,
184
+ max: 360,
185
+ railBackground: HUE_RAIL_BACKGROUND,
186
+ value: optimisticColor.h,
187
+ };
188
+ }
189
+
190
+ export function ColorSurface({
191
+ surfaceRef,
192
+ surfaceLabel,
193
+ surfaceClassName,
194
+ disabled,
195
+ hueColor,
196
+ currentColorHex,
197
+ colorModel,
198
+ optimisticColor,
199
+ surfacePosition,
200
+ isSurfaceDragging,
201
+ onPointerDown,
202
+ onThumbPointerDown,
203
+ }: ColorSurfaceProps) {
204
+ const thumbPosition = getColorSurfaceThumbPosition({
205
+ colorModel,
206
+ currentColorHex,
207
+ optimisticColor,
208
+ surfacePosition,
209
+ });
210
+
211
+ return (
212
+ <div
213
+ ref={surfaceRef}
214
+ data-slot="style-guide-color-surface"
215
+ data-color-model={colorModel}
216
+ aria-label={surfaceLabel}
217
+ className={cn(
218
+ "group/surface relative aspect-square w-full shrink-0 touch-none rounded-t-[8px]",
219
+ surfaceClassName,
220
+ disabled && "cursor-not-allowed opacity-60",
221
+ )}
222
+ style={getColorSurfaceStyle({
223
+ colorModel,
224
+ currentColorHex,
225
+ hue: optimisticColor.h,
226
+ hueColor,
227
+ })}
228
+ onPointerDown={onPointerDown}
229
+ >
230
+ <div className="pointer-events-none absolute inset-0 overflow-hidden rounded-t-[8px]">
231
+ {colorModel === "hsb" ? (
232
+ <>
233
+ <div className="absolute inset-0 bg-linear-to-r from-white to-transparent" />
234
+ <div className="absolute inset-0 bg-linear-to-t from-black to-transparent" />
235
+ </>
236
+ ) : null}
237
+ <div
238
+ data-slot="style-guide-color-surface-divider"
239
+ aria-hidden
240
+ className="absolute inset-x-0 bottom-0 h-px bg-[color:color-mix(in_oklab,var(--border)_6%,transparent)]"
241
+ />
242
+ </div>
243
+ <div
244
+ data-slot="style-guide-color-surface-thumb"
245
+ aria-hidden
246
+ className={cn(
247
+ "absolute size-[14px] -translate-x-1/2 -translate-y-1/2 cursor-pointer rounded-full border-2 border-foreground shadow-[0_1px_4px_rgba(0,0,0,0.35)] transition-transform duration-200 ease-out group-hover/surface:scale-[1.4286]",
248
+ isSurfaceDragging && "scale-[1.4286]",
249
+ )}
250
+ style={{
251
+ backgroundColor: currentColorHex,
252
+ left: thumbPosition.left,
253
+ top: thumbPosition.top,
254
+ }}
255
+ onPointerDown={onThumbPointerDown}
256
+ />
257
+ </div>
258
+ );
259
+ }
260
+
261
+ type ColorModelSliderProps = {
262
+ label: string;
263
+ disabled: boolean;
264
+ max: number;
265
+ railBackground: string;
266
+ value: number;
267
+ onDragStateChange: (nextIsDragging: boolean) => void;
268
+ onPreviewChange: (nextValue: number) => void;
269
+ onCommit: (nextValue: number) => void;
270
+ };
271
+
272
+ export function ColorModelSlider({
273
+ label,
274
+ disabled,
275
+ max,
276
+ railBackground,
277
+ value,
278
+ onDragStateChange,
279
+ onPreviewChange,
280
+ onCommit,
281
+ }: ColorModelSliderProps) {
282
+ const sliderRef = useRef<HTMLDivElement | null>(null);
283
+ const dragBoundsRef = useRef<DOMRect | null>(null);
284
+ const latestDragValueRef = useRef(value);
285
+ const callbacksRef = useRef({ onCommit, onDragStateChange, onPreviewChange });
286
+ const [isDragging, setIsDragging] = useState(false);
287
+ const activeValue = isDragging ? latestDragValueRef.current : value;
288
+ const valuePercent = max === 0 ? 0 : (clampSliderValue(activeValue, max) / max) * 100;
289
+
290
+ useEffect(() => {
291
+ callbacksRef.current = { onCommit, onDragStateChange, onPreviewChange };
292
+ }, [onCommit, onDragStateChange, onPreviewChange]);
293
+
294
+ useEffect(() => {
295
+ if (!isDragging) {
296
+ latestDragValueRef.current = value;
297
+ }
298
+ }, [isDragging, value]);
299
+
300
+ const previewFromClientX = useCallback(
301
+ (clientX: number) => {
302
+ const bounds = dragBoundsRef.current;
303
+ if (!bounds) return;
304
+
305
+ const nextValue = getSliderValueFromClientX(clientX, bounds, max);
306
+ latestDragValueRef.current = nextValue;
307
+ callbacksRef.current.onPreviewChange(nextValue);
308
+ },
309
+ [max],
310
+ );
311
+
312
+ const finishDrag = useCallback((shouldCommit: boolean) => {
313
+ const nextValue = latestDragValueRef.current;
314
+
315
+ dragBoundsRef.current = null;
316
+ setIsDragging(false);
317
+ callbacksRef.current.onDragStateChange(false);
318
+
319
+ if (shouldCommit) {
320
+ callbacksRef.current.onCommit(nextValue);
321
+ }
322
+ }, []);
323
+
324
+ useEffect(() => {
325
+ if (!isDragging) return;
326
+
327
+ const handlePointerMove = (event: PointerEvent) => {
328
+ event.preventDefault();
329
+ previewFromClientX(event.clientX);
330
+ };
331
+ const handlePointerUp = (event: PointerEvent) => {
332
+ event.preventDefault();
333
+ previewFromClientX(event.clientX);
334
+ finishDrag(true);
335
+ };
336
+ const handlePointerCancel = () => finishDrag(false);
337
+
338
+ window.addEventListener("pointermove", handlePointerMove);
339
+ window.addEventListener("pointerup", handlePointerUp);
340
+ window.addEventListener("pointercancel", handlePointerCancel);
341
+ window.addEventListener("blur", handlePointerCancel);
342
+
343
+ return () => {
344
+ window.removeEventListener("pointermove", handlePointerMove);
345
+ window.removeEventListener("pointerup", handlePointerUp);
346
+ window.removeEventListener("pointercancel", handlePointerCancel);
347
+ window.removeEventListener("blur", handlePointerCancel);
348
+ };
349
+ }, [finishDrag, isDragging, previewFromClientX]);
350
+
351
+ const beginDrag = useCallback(
352
+ (event: ReactPointerEvent<HTMLDivElement>) => {
353
+ if (disabled) return;
354
+
355
+ const bounds = sliderRef.current?.getBoundingClientRect();
356
+ if (!bounds || bounds.width === 0) return;
357
+
358
+ event.preventDefault();
359
+ dragBoundsRef.current = bounds;
360
+ setIsDragging(true);
361
+ callbacksRef.current.onDragStateChange(true);
362
+ previewFromClientX(event.clientX);
363
+ },
364
+ [disabled, previewFromClientX],
365
+ );
366
+
367
+ const handleKeyDown = useCallback(
368
+ (event: KeyboardEvent<HTMLDivElement>) => {
369
+ if (disabled) return;
370
+
371
+ const step = event.shiftKey ? 10 : 1;
372
+ let nextValue: number | null = null;
373
+
374
+ if (event.key === "ArrowLeft" || event.key === "ArrowDown") {
375
+ nextValue = clampSliderValue(activeValue - step, max);
376
+ } else if (event.key === "ArrowRight" || event.key === "ArrowUp") {
377
+ nextValue = clampSliderValue(activeValue + step, max);
378
+ } else if (event.key === "Home") {
379
+ nextValue = 0;
380
+ } else if (event.key === "End") {
381
+ nextValue = max;
382
+ }
383
+
384
+ if (nextValue == null) return;
385
+
386
+ event.preventDefault();
387
+ latestDragValueRef.current = nextValue;
388
+ callbacksRef.current.onPreviewChange(nextValue);
389
+ callbacksRef.current.onCommit(nextValue);
390
+ },
391
+ [activeValue, disabled, max],
392
+ );
393
+
394
+ return (
395
+ <div
396
+ data-slot="style-guide-color-hue"
397
+ className={cn(
398
+ "relative w-full",
399
+ disabled && "cursor-not-allowed opacity-60",
400
+ )}
401
+ >
402
+ <div
403
+ data-slot="style-guide-color-hue-rail"
404
+ aria-hidden
405
+ className="pointer-events-none absolute inset-x-0 top-1/2 h-px -translate-y-1/2"
406
+ style={{ background: railBackground }}
407
+ />
408
+ <div
409
+ aria-label={label}
410
+ aria-valuemax={max}
411
+ aria-valuemin={0}
412
+ aria-valuenow={activeValue}
413
+ className={cn(
414
+ "relative h-[18px] w-full touch-none cursor-pointer select-none [--slider-active-color:var(--foreground)] [--slider-track-color:transparent]",
415
+ disabled && "cursor-not-allowed",
416
+ )}
417
+ data-slot="slider"
418
+ data-variant="continuous"
419
+ onKeyDown={handleKeyDown}
420
+ onPointerDown={beginDrag}
421
+ ref={sliderRef}
422
+ role="slider"
423
+ tabIndex={disabled ? -1 : 0}
424
+ >
425
+ <div
426
+ className="group/slider-control relative flex h-[18px] w-full touch-none items-center select-none"
427
+ data-orientation="horizontal"
428
+ data-slot="slider-control"
429
+ >
430
+ <div
431
+ className="group/slider-track relative h-px w-full grow overflow-visible rounded-full bg-[color:var(--slider-track-color)] select-none"
432
+ data-slot="slider-track"
433
+ />
434
+ <div
435
+ className="group/slider-thumb absolute top-1/2 block size-[9px] -translate-x-1/2 -translate-y-1/2 cursor-pointer rounded-full select-none before:absolute before:top-1/2 before:left-1/2 before:block before:size-[18px] before:-translate-x-1/2 before:-translate-y-1/2 before:content-['']"
436
+ data-dragging={isDragging ? "" : undefined}
437
+ data-slot="slider-thumb"
438
+ style={{ left: `${valuePercent}%` }}
439
+ >
440
+ <span
441
+ aria-hidden
442
+ className={cn(
443
+ "pointer-events-none absolute inset-0 block rounded-full bg-[color:var(--slider-active-color)] transition-[scale,background-color] duration-200 ease-out motion-reduce:transition-none",
444
+ disabled
445
+ ? null
446
+ : "group-hover/slider-thumb:scale-[1.4] group-data-[dragging]/slider-thumb:scale-[1.4]",
447
+ )}
448
+ data-slot="slider-dot"
449
+ />
450
+ </div>
451
+ </div>
452
+ </div>
453
+ </div>
454
+ );
455
+ }
456
+
457
+ type ColorFooterProps = {
458
+ resolvedHexInputId: string;
459
+ hexInputLabel: string;
460
+ disabled: boolean;
461
+ draftHexValue: string;
462
+ onHexFocus: () => void;
463
+ onHexChange: (event: ChangeEvent<HTMLInputElement>) => void;
464
+ onHexBlur: () => void;
465
+ onHexKeyDown: (event: KeyboardEvent<HTMLInputElement>) => void;
466
+ onColorValueFocus: () => void;
467
+ onColorValueChange: (nextHex: string) => void;
468
+ onColorValueBlur: () => void;
469
+ mode: ColorFormatMode;
470
+ onModeChange: (nextMode: ColorFormatMode) => void;
471
+ showOpacity?: boolean;
472
+ };
473
+
474
+ const COLOR_FORMAT_MODES = [
475
+ { label: "Hex", value: "hex" },
476
+ { label: "RGB", value: "rgb" },
477
+ { label: "HSL", value: "hsl" },
478
+ { label: "HSB", value: "hsb" },
479
+ ] as const satisfies ReadonlyArray<{ label: string; value: ColorFormatMode }>;
480
+
481
+ const colorFormatSelectWidth = `calc(${Math.max(
482
+ ...COLOR_FORMAT_MODES.map((formatMode) => formatMode.label.length),
483
+ )}ch + 2rem)`;
484
+
485
+ const colorValueInputGroupClassName = "h-6 min-w-0 flex-1";
486
+
487
+ const colorValueInputClassName =
488
+ "min-w-0 px-1 text-center text-xs font-mono";
489
+
490
+ const colorValueCellSeparatorClassName =
491
+ "pointer-events-none absolute top-0 bottom-0 w-px bg-[color:color-mix(in_oklab,var(--border)_12%,transparent)]";
492
+
493
+ function toColorFormatMode(value: unknown): ColorFormatMode | null {
494
+ const candidate =
495
+ typeof value === "string"
496
+ ? value
497
+ : value && typeof value === "object" && "value" in value
498
+ ? String(value.value)
499
+ : null;
500
+
501
+ return COLOR_FORMAT_MODES.some((formatMode) => formatMode.value === candidate)
502
+ ? (candidate as ColorFormatMode)
503
+ : null;
504
+ }
505
+
506
+ function ColorFormatSelect({
507
+ disabled,
508
+ mode,
509
+ onModeChange,
510
+ }: {
511
+ disabled: boolean;
512
+ mode: ColorFormatMode;
513
+ onModeChange: (nextMode: ColorFormatMode) => void;
514
+ }) {
515
+ return (
516
+ <div className="min-w-0 shrink-0" style={{ width: colorFormatSelectWidth }}>
517
+ <StaticSelect
518
+ disabled={disabled}
519
+ options={COLOR_FORMAT_MODES}
520
+ scrollFadeValue={false}
521
+ size="sm"
522
+ triggerClassName="text-[11px]"
523
+ value={mode}
524
+ onValueChange={(nextMode) => {
525
+ const resolvedMode = toColorFormatMode(nextMode);
526
+
527
+ if (resolvedMode) onModeChange(resolvedMode);
528
+ }}
529
+ />
530
+ </div>
531
+ );
532
+ }
533
+
534
+ function ColorValueCells({
535
+ channels,
536
+ disabled,
537
+ mode,
538
+ onColorValueBlur,
539
+ onColorValueChange,
540
+ onColorValueFocus,
541
+ showOpacity,
542
+ }: {
543
+ channels: ColorChannels;
544
+ disabled: boolean;
545
+ mode: ColorFormatMode;
546
+ onColorValueBlur: () => void;
547
+ onColorValueChange: (nextHex: string) => void;
548
+ onColorValueFocus: () => void;
549
+ showOpacity: boolean;
550
+ }) {
551
+ if (mode === "css") {
552
+ const [red, green, blue] = channels.rgb;
553
+
554
+ return (
555
+ <InputGroup
556
+ data-slot="style-guide-color-value-cells"
557
+ className={colorValueInputGroupClassName}
558
+ size="sm"
559
+ >
560
+ <InputGroupInput
561
+ aria-label="CSS color value"
562
+ className="min-w-0 px-2 font-mono text-xs"
563
+ disabled={disabled}
564
+ readOnly
565
+ value={`rgb(${red} ${green} ${blue})`}
566
+ />
567
+ </InputGroup>
568
+ );
569
+ }
570
+
571
+ const colorValues =
572
+ mode === "rgb"
573
+ ? channels.rgb
574
+ : mode === "hsl"
575
+ ? channels.hsl
576
+ : channels.hsb;
577
+ const values = showOpacity ? [...colorValues, 100] : colorValues;
578
+
579
+ return (
580
+ <InputGroup
581
+ data-slot="style-guide-color-value-cells"
582
+ className={colorValueInputGroupClassName}
583
+ size="sm"
584
+ >
585
+ <div className="relative flex h-full min-w-0 flex-1">
586
+ {values.slice(1).map((_, index) => (
587
+ <span
588
+ aria-hidden
589
+ className={colorValueCellSeparatorClassName}
590
+ key={`${mode}-separator-${index}`}
591
+ style={{ left: `${((index + 1) / values.length) * 100}%` }}
592
+ />
593
+ ))}
594
+ {values.map((value, index) => {
595
+ const isAlphaChannel = index === 3;
596
+
597
+ return (
598
+ <InputGroupInput
599
+ aria-label={`${mode.toUpperCase()} channel ${index + 1}`}
600
+ className={colorValueInputClassName}
601
+ disabled={disabled}
602
+ inputMode="numeric"
603
+ key={`${mode}-${index}`}
604
+ readOnly={isAlphaChannel}
605
+ value={String(value)}
606
+ onBlur={isAlphaChannel ? undefined : onColorValueBlur}
607
+ onChange={(event) => {
608
+ const nextHex = getEditableChannelHex({
609
+ channels,
610
+ channelIndex: index,
611
+ mode,
612
+ rawValue: event.target.value,
613
+ });
614
+
615
+ if (nextHex) onColorValueChange(nextHex);
616
+ }}
617
+ onFocus={
618
+ isAlphaChannel
619
+ ? undefined
620
+ : () => {
621
+ onColorValueFocus();
622
+ }
623
+ }
624
+ onKeyDown={(event) => {
625
+ if (event.key === "Enter" || event.key === "Escape") {
626
+ event.currentTarget.blur();
627
+ }
628
+ }}
629
+ />
630
+ );
631
+ })}
632
+ </div>
633
+ {showOpacity ? (
634
+ <InputGroupAddon align="inline-end" className="pr-1.5 pl-0">
635
+ <InputGroupText>%</InputGroupText>
636
+ </InputGroupAddon>
637
+ ) : null}
638
+ </InputGroup>
639
+ );
640
+ }
641
+
642
+ export function ColorFooter({
643
+ resolvedHexInputId,
644
+ hexInputLabel,
645
+ disabled,
646
+ draftHexValue,
647
+ onHexFocus,
648
+ onHexChange,
649
+ onHexBlur,
650
+ onHexKeyDown,
651
+ onColorValueFocus,
652
+ onColorValueChange,
653
+ onColorValueBlur,
654
+ mode,
655
+ onModeChange,
656
+ showOpacity = false,
657
+ }: ColorFooterProps) {
658
+ const channels = useMemo(
659
+ () => getColorChannels(draftHexValue),
660
+ [draftHexValue],
661
+ );
662
+
663
+ return (
664
+ <div
665
+ data-slot="style-guide-color-footer"
666
+ className="flex w-full shrink-0 items-center border-t border-[color:color-mix(in_oklab,var(--border)_6%,transparent)] px-2 py-3"
667
+ >
668
+ <div
669
+ data-slot="style-guide-color-footer-row"
670
+ className="flex w-full min-w-0 items-center gap-1.5"
671
+ >
672
+ <ColorFormatSelect
673
+ disabled={disabled}
674
+ mode={mode}
675
+ onModeChange={onModeChange}
676
+ />
677
+ {mode === "hex" ? (
678
+ <InputGroup className="h-6 min-w-0 flex-1" size="sm">
679
+ <InputGroupInput
680
+ id={resolvedHexInputId}
681
+ type="text"
682
+ inputMode="text"
683
+ spellCheck={false}
684
+ autoCapitalize="characters"
685
+ autoCorrect="off"
686
+ disabled={disabled}
687
+ aria-label={hexInputLabel}
688
+ className="min-w-0 font-mono text-xs"
689
+ value={draftHexValue}
690
+ onFocus={onHexFocus}
691
+ onChange={onHexChange}
692
+ onBlur={onHexBlur}
693
+ onKeyDown={onHexKeyDown}
694
+ />
695
+ </InputGroup>
696
+ ) : (
697
+ <ColorValueCells
698
+ channels={channels}
699
+ disabled={disabled}
700
+ mode={mode}
701
+ onColorValueBlur={onColorValueBlur}
702
+ onColorValueChange={onColorValueChange}
703
+ onColorValueFocus={onColorValueFocus}
704
+ showOpacity={showOpacity}
705
+ />
706
+ )}
707
+ </div>
708
+ </div>
709
+ );
710
+ }