@perkyzz/emvb 0.1.0 → 0.2.1

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 (154) hide show
  1. package/README.md +6 -4
  2. package/THIRD_PARTY_NOTICES.md +32 -0
  3. package/licenses/fontawesome-free.LICENSE +165 -0
  4. package/licenses/lucide-static.LICENSE +43 -0
  5. package/licenses/remixicon.LICENSE +201 -0
  6. package/licenses/tabler-icons.LICENSE +21 -0
  7. package/package.json +9 -5
  8. package/src/admin/api.ts +27 -0
  9. package/src/admin/content-api.ts +72 -9
  10. package/src/admin/editor/Editor.tsx +89 -14
  11. package/src/admin/editor/EditorOverlay.tsx +12 -0
  12. package/src/admin/editor/SmallScreenNotice.tsx +4 -3
  13. package/src/admin/editor/TopBar.tsx +76 -12
  14. package/src/admin/editor/canvas/CanvasFrame.tsx +224 -43
  15. package/src/admin/editor/canvas/DeviceBar.tsx +43 -2
  16. package/src/admin/editor/canvas/SelectionOverlay.tsx +60 -6
  17. package/src/admin/editor/canvas/canvas-events.ts +20 -7
  18. package/src/admin/editor/canvas/tab-reveal.ts +66 -0
  19. package/src/admin/editor/canvas/vnode-react.tsx +21 -1
  20. package/src/admin/editor/dialogs.tsx +39 -0
  21. package/src/admin/editor/dnd/drop-target.ts +63 -16
  22. package/src/admin/editor/dnd/new-element.ts +76 -1
  23. package/src/admin/editor/editor-css.ts +101 -15
  24. package/src/admin/editor/host.ts +30 -0
  25. package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
  26. package/src/admin/editor/icons/library.ts +243 -0
  27. package/src/admin/editor/icons/set-format.ts +29 -0
  28. package/src/admin/editor/icons/sets/fontawesome.json +1 -0
  29. package/src/admin/editor/icons/sets/lucide.json +1 -0
  30. package/src/admin/editor/icons/sets/remix.json +1 -0
  31. package/src/admin/editor/icons/sets/tabler.json +1 -0
  32. package/src/admin/editor/panels/AddPanel.tsx +7 -3
  33. package/src/admin/editor/panels/ClassesSection.tsx +44 -6
  34. package/src/admin/editor/panels/ColorControl.tsx +268 -21
  35. package/src/admin/editor/panels/ElementPanel.tsx +237 -93
  36. package/src/admin/editor/panels/LayersPanel.tsx +303 -66
  37. package/src/admin/editor/panels/LeftPanel.tsx +4 -0
  38. package/src/admin/editor/panels/PageSettings.tsx +134 -5
  39. package/src/admin/editor/panels/SettingsPanel.tsx +12 -0
  40. package/src/admin/editor/panels/SiteStylesDrawer.tsx +158 -2
  41. package/src/admin/editor/panels/TriggersEditor.tsx +19 -2
  42. package/src/admin/editor/panels/VariableSection.tsx +47 -9
  43. package/src/admin/editor/panels/color-space.ts +195 -0
  44. package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
  45. package/src/admin/editor/panels/settings/BackgroundControls.tsx +153 -55
  46. package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
  47. package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
  48. package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
  49. package/src/admin/editor/panels/settings/ClassChipInput.tsx +104 -2
  50. package/src/admin/editor/panels/settings/DraftTextField.tsx +44 -3
  51. package/src/admin/editor/panels/settings/EffectsControls.tsx +25 -16
  52. package/src/admin/editor/panels/settings/FieldBindControl.tsx +53 -17
  53. package/src/admin/editor/panels/settings/FieldControl.tsx +129 -9
  54. package/src/admin/editor/panels/settings/FormBindControl.tsx +14 -5
  55. package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
  56. package/src/admin/editor/panels/settings/IconPicker.tsx +133 -72
  57. package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
  58. package/src/admin/editor/panels/settings/LengthRow.tsx +97 -61
  59. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +10 -4
  60. package/src/admin/editor/panels/settings/MediaPicker.tsx +4 -2
  61. package/src/admin/editor/panels/settings/StateSwitcher.tsx +1 -1
  62. package/src/admin/editor/panels/settings/StyleRow.tsx +158 -75
  63. package/src/admin/editor/panels/settings/VariableBinding.tsx +5 -1
  64. package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
  65. package/src/admin/editor/panels/settings/background-mode.ts +112 -0
  66. package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
  67. package/src/admin/editor/panels/settings/box-sides.ts +153 -0
  68. package/src/admin/editor/panels/settings/button-url.ts +27 -0
  69. package/src/admin/editor/panels/settings/class-suggest.ts +5 -3
  70. package/src/admin/editor/panels/settings/form-names.ts +38 -0
  71. package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
  72. package/src/admin/editor/panels/settings/image-alt.ts +25 -0
  73. package/src/admin/editor/panels/settings/length-units.ts +40 -7
  74. package/src/admin/editor/panels/settings/loop-context.ts +26 -0
  75. package/src/admin/editor/panels/settings/style-sections.ts +114 -11
  76. package/src/admin/editor/panels/settings/svg-images.ts +14 -0
  77. package/src/admin/editor/panels/site-list.tsx +3 -0
  78. package/src/admin/editor/restore-deleted.ts +80 -0
  79. package/src/admin/editor/shortcuts.ts +1 -1
  80. package/src/admin/editor/store.ts +35 -7
  81. package/src/admin/editor/useEditorCommands.ts +66 -5
  82. package/src/admin/editor/useEditorData.ts +29 -3
  83. package/src/admin/editor/useEditorShortcuts.ts +34 -2
  84. package/src/admin/editor/useNodeActions.ts +68 -6
  85. package/src/admin/editor/useSave.ts +60 -3
  86. package/src/admin/entry-status.ts +19 -0
  87. package/src/admin/icon-uploads-api.ts +44 -0
  88. package/src/admin/pages/PageList.tsx +22 -2
  89. package/src/admin/pages/ThemePartList.tsx +23 -4
  90. package/src/admin/pages/list-kit.tsx +52 -2
  91. package/src/admin/setup/spec.ts +7 -0
  92. package/src/admin/theme-api.ts +61 -14
  93. package/src/admin/ui.ts +17 -4
  94. package/src/astro/EmVBPage.astro +28 -2
  95. package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
  96. package/src/astro/floats/index.ts +1 -0
  97. package/src/astro/floats/runtime.ts +78 -0
  98. package/src/astro/forms-definitions.ts +22 -0
  99. package/src/astro/index.ts +1 -0
  100. package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
  101. package/src/astro/menu/index.ts +1 -0
  102. package/src/astro/menu/runtime.ts +37 -0
  103. package/src/astro/popups/runtime.ts +54 -4
  104. package/src/astro/render.ts +47 -13
  105. package/src/astro/resolve-theme.ts +168 -24
  106. package/src/astro/tabs/runtime.ts +6 -1
  107. package/src/astro/theme-context.ts +25 -5
  108. package/src/astro/theme-posts.ts +19 -1
  109. package/src/constants.ts +1 -1
  110. package/src/core/arrange.ts +98 -10
  111. package/src/core/clipboard.ts +91 -9
  112. package/src/core/css/generate.ts +21 -4
  113. package/src/core/design/classes.ts +95 -9
  114. package/src/core/design/transfer.ts +101 -2
  115. package/src/core/design/variables.ts +51 -11
  116. package/src/core/elements/dynamic-elements.ts +57 -5
  117. package/src/core/elements/field-id.ts +6 -0
  118. package/src/core/elements/form-elements.ts +23 -8
  119. package/src/core/elements/index.ts +267 -49
  120. package/src/core/elements/menu.ts +190 -0
  121. package/src/core/forms/definition.ts +11 -0
  122. package/src/core/index.ts +67 -3
  123. package/src/core/limits.ts +2 -0
  124. package/src/core/migrate/index.ts +51 -1
  125. package/src/core/nesting.ts +51 -0
  126. package/src/core/render/context.ts +7 -0
  127. package/src/core/render/dynamic-post.ts +39 -11
  128. package/src/core/render/extras.ts +22 -3
  129. package/src/core/render/form.ts +134 -36
  130. package/src/core/render/index.ts +123 -25
  131. package/src/core/render/loop.ts +10 -3
  132. package/src/core/render/pagination.ts +76 -0
  133. package/src/core/render/section.ts +5 -1
  134. package/src/core/render/tabs.ts +8 -4
  135. package/src/core/render/vnode.ts +1 -1
  136. package/src/core/sanitize/css.ts +197 -17
  137. package/src/core/sanitize/embed-url.ts +64 -7
  138. package/src/core/sanitize/svg-upload.ts +203 -0
  139. package/src/core/sanitize/svg.ts +102 -21
  140. package/src/core/schema/descriptors.ts +4 -0
  141. package/src/core/schema/design.ts +7 -2
  142. package/src/core/schema/layout.ts +210 -19
  143. package/src/core/schema/style.ts +87 -8
  144. package/src/core/tabs/presence.ts +9 -0
  145. package/src/core/theme/conditions.ts +4 -1
  146. package/src/core/theme/dynamic.ts +10 -0
  147. package/src/core/theme/float.ts +113 -0
  148. package/src/core/theme/pagination.ts +97 -0
  149. package/src/core/theme/part-types.ts +2 -0
  150. package/src/core/theme/popup-markup.ts +3 -1
  151. package/src/core/theme/popup-rules.ts +4 -3
  152. package/src/server/hooks.ts +20 -0
  153. package/src/server/icon-routes.ts +79 -0
  154. package/src/server/plugin.ts +5 -1
@@ -1,5 +1,6 @@
1
1
  import { Input } from "@cloudflare/kumo";
2
2
  import {
3
+ ArrowsLeftRightIcon,
3
4
  LinkSimpleIcon,
4
5
  MinusIcon,
5
6
  RectangleIcon,
@@ -9,6 +10,7 @@ import {
9
10
  TextHIcon,
10
11
  TextTIcon,
11
12
  ListBulletsIcon,
13
+ CaretDownIcon,
12
14
  CursorClickIcon,
13
15
  ImageIcon,
14
16
  GraphIcon,
@@ -21,6 +23,7 @@ import {
21
23
  PaperPlaneTiltIcon,
22
24
  ClipboardTextIcon,
23
25
  NotePencilIcon,
26
+ RowsIcon,
24
27
  } from "@phosphor-icons/react";
25
28
  import * as React from "react";
26
29
  import { ELEMENT_DESCRIPTORS, type ElementType } from "../../../core/index.ts";
@@ -31,6 +34,7 @@ import { dragStash } from "../dnd/drag-stash.ts";
31
34
  const ICONS: Record<string, typeof TextHIcon> = {
32
35
  container: SquareIcon,
33
36
  "div-block": SquareIcon,
37
+ "layout-section": RowsIcon,
34
38
  flexbox: SquaresFourIcon,
35
39
  spacer: RectangleIcon,
36
40
  divider: MinusIcon,
@@ -45,6 +49,8 @@ const ICONS: Record<string, typeof TextHIcon> = {
45
49
  svg: GraphIcon,
46
50
  tabs: BrowserIcon,
47
51
  accordion: ListBulletsIcon,
52
+ menu: CaretDownIcon,
53
+ "menu-item": CaretDownIcon,
48
54
  video: YoutubeLogoIcon,
49
55
  form: ClipboardTextIcon,
50
56
  "text-input": TextboxIcon,
@@ -61,6 +67,7 @@ const ICONS: Record<string, typeof TextHIcon> = {
61
67
  "post-date": TextTIcon,
62
68
  "post-author": TextTIcon,
63
69
  loop: ListBulletsIcon,
70
+ pagination: ArrowsLeftRightIcon,
64
71
  };
65
72
 
66
73
  const GROUPS = [
@@ -123,9 +130,6 @@ export function AddPanel({
123
130
  dragStash.new(tile.type);
124
131
  }}
125
132
  onClick={() => onAdd(tile.type as ElementType)}
126
- onKeyDown={(event) => {
127
- if (event.key === "Enter") onAdd(tile.type as ElementType);
128
- }}
129
133
  >
130
134
  <Icon size={20} aria-hidden="true" />
131
135
  <span>{tile.name}</span>
@@ -12,12 +12,16 @@ import {
12
12
  } from "@phosphor-icons/react";
13
13
  import * as React from "react";
14
14
  import {
15
+ cleanClassName,
15
16
  duplicateClass,
17
+ MAX_CLASSES,
16
18
  findClassUsages,
17
19
  moveDesignClass,
18
20
  patchClassState,
19
21
  patchClassStyle,
20
22
  renameClass,
23
+ classNameTaken,
24
+ classNameTakenMessage,
21
25
  type DesignSystem,
22
26
  type Layout,
23
27
  type StyleProps,
@@ -65,7 +69,9 @@ function classKeys(
65
69
  return (
66
70
  state === "normal"
67
71
  ? withFit
68
- : withFit.filter((key) => key !== "transition" && key !== "entrance")
72
+ : withFit.filter(
73
+ (key) => key !== "transition" && key !== "entrance" && key !== "iconAnimation",
74
+ )
69
75
  ) as StyleKey[];
70
76
  }
71
77
 
@@ -142,6 +148,7 @@ export function ClassesSection({
142
148
  const [editing, setEditing] = React.useState<string | null>(null);
143
149
  const [renaming, setRenaming] = React.useState<string | null>(null);
144
150
  const [filter, setFilter] = React.useState("");
151
+ const [nameError, setNameError] = React.useState<string | null>(null);
145
152
  const rows = React.useRef(new Map<string, HTMLButtonElement>());
146
153
 
147
154
  const q = filter.trim().toLowerCase();
@@ -150,8 +157,13 @@ export function ClassesSection({
150
157
  : classes;
151
158
 
152
159
  const create = async () => {
153
- const trimmed = name.trim();
154
- if (!trimmed) return;
160
+ const trimmed = cleanClassName(name);
161
+ if (!trimmed || classes.length >= MAX_CLASSES) return;
162
+ if (classNameTaken(design, trimmed)) {
163
+ setNameError(classNameTakenMessage(trimmed));
164
+ return;
165
+ }
166
+ setNameError(null);
155
167
  const id = uniqueId(
156
168
  trimmed,
157
169
  "class",
@@ -167,6 +179,11 @@ export function ClassesSection({
167
179
  const rename = (id: string, next: string | null) => {
168
180
  setRenaming(null);
169
181
  requestAnimationFrame(() => rows.current.get(id)?.focus());
182
+ if (next && classNameTaken(design, next, id)) {
183
+ setNameError(classNameTakenMessage(next));
184
+ return;
185
+ }
186
+ setNameError(null);
170
187
  if (next) void onSave(renameClass(design, id, next));
171
188
  };
172
189
 
@@ -177,7 +194,17 @@ export function ClassesSection({
177
194
  element still win. Editing a class updates every element that uses it. Tablet and mobile
178
195
  overrides stay on the Style tab.
179
196
  </p>
180
- <SectionHead title="Classes" count={classes.length} onNew={() => setCreating(true)} />
197
+ <SectionHead
198
+ title="Classes"
199
+ count={classes.length}
200
+ onNew={() => setCreating(true)}
201
+ newDisabled={classes.length >= MAX_CLASSES}
202
+ />
203
+ {classes.length >= MAX_CLASSES ? (
204
+ <p className="emvb-helper" data-emvb-class-cap="">
205
+ This site has 100 classes, the most allowed. Delete one to add another.
206
+ </p>
207
+ ) : null}
181
208
  {classes.length > 3 && (
182
209
  <SiteSearch label="Search classes" value={filter} onChange={setFilter} />
183
210
  )}
@@ -298,7 +325,10 @@ export function ClassesSection({
298
325
  {
299
326
  label: "Duplicate",
300
327
  icon: CopySimpleIcon,
301
- onSelect: () => void onSave(duplicateClass(design, cls.id)),
328
+ onSelect: () => {
329
+ const next = duplicateClass(design, cls.id);
330
+ if (next !== design) void onSave(next);
331
+ },
302
332
  },
303
333
  {
304
334
  label: "Delete",
@@ -314,11 +344,19 @@ export function ClassesSection({
314
344
  );
315
345
  })}
316
346
  </ul>
347
+ {nameError && (
348
+ <p className="emvb-inline-error" role="alert" data-emvb-class-name-error="">
349
+ {nameError}
350
+ </p>
351
+ )}
317
352
  {creating && (
318
353
  <CreateRow
319
354
  rowProps={{ "data-emvb-class-create": "" }}
320
355
  submitLabel="Create class"
321
- onCancel={() => setCreating(false)}
356
+ onCancel={() => {
357
+ setCreating(false);
358
+ setNameError(null);
359
+ }}
322
360
  onCreate={() => void create()}
323
361
  >
324
362
  <Input
@@ -1,13 +1,32 @@
1
1
  import { Button, Input, Select } from "@cloudflare/kumo";
2
2
  import { PlusIcon, WarningCircleIcon } from "@phosphor-icons/react";
3
3
  import * as React from "react";
4
- import { slugify, type DesignSystem, type StyleProps } from "../../../core/index.ts";
4
+ import {
5
+ cleanClassName,
6
+ slugify,
7
+ variableListFull,
8
+ type DesignSystem,
9
+ type StyleProps,
10
+ } from "../../../core/index.ts";
5
11
 
6
12
  type ColorValue = StyleProps["color"];
7
13
  import { BUTTON, FIELD } from "../../ui.ts";
14
+ import {
15
+ formatOklch,
16
+ formatRgb,
17
+ oklchClipped,
18
+ parseHex,
19
+ parseOklch,
20
+ parseRgb,
21
+ toHex,
22
+ } from "./color-space.ts";
23
+ import { pickerHex } from "./variable-values.ts";
8
24
 
9
25
  const HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
10
26
  const NONE = "__none__";
27
+ const CUSTOM = "__custom__";
28
+ /** Swatch buttons shown under the Select; the rest stay in its list (W-136). */
29
+ const SWATCH_LIMIT = 16;
11
30
  const BAD_HEX = "Enter a hex color such as #1a2b3c.";
12
31
 
13
32
  const errorText = (error: unknown) =>
@@ -22,6 +41,10 @@ function InlineError({ children }: { children: string }) {
22
41
  );
23
42
  }
24
43
 
44
+ function Swatch({ color }: { color: string }) {
45
+ return <span className="emvb-swatch" style={{ background: color }} aria-hidden="true" />;
46
+ }
47
+
25
48
  const uniqueId = (design: DesignSystem, name: string) => {
26
49
  const base = slugify(name).slice(0, 34) || "color";
27
50
  const taken = new Set(design.variables.colors.map((color) => color.id));
@@ -31,8 +54,10 @@ const uniqueId = (design: DesignSystem, name: string) => {
31
54
  };
32
55
 
33
56
  /**
34
- * Colour bound to a design variable (R-004 partial). Creating a variable or editing a bound
35
- * variable's value saves the design at once: it applies to every page (D-013).
57
+ * Colour: Default, a design variable (R-004) or a custom hex (W-136, D-043). The Select keeps its
58
+ * visible label and shows the current swatch; the site's colour variables also show as swatch
59
+ * buttons under it, and "Custom color" opens a validated hex field with the browser's picker.
60
+ * Creating a variable or editing a bound variable's value saves the design at once (D-013).
36
61
  */
37
62
  export function ColorControl({
38
63
  label,
@@ -53,38 +78,95 @@ export function ColorControl({
53
78
  const colors = design.variables.colors;
54
79
  const bound = value && typeof value === "object" ? colors.find((c) => c.id === value.var) : null;
55
80
  const [creating, setCreating] = React.useState(false);
81
+ const [customOpen, setCustomOpen] = React.useState(false);
82
+ // The hex an out-of-gamut OKLCH entry was clipped to. It lives here because committing a custom
83
+ // colour remounts CustomColor (W-183).
84
+ const [clipped, setClipped] = React.useState<string | null>(null);
85
+ const custom = typeof value === "string" ? value : null;
86
+ const showCustom = custom !== null || customOpen;
56
87
 
57
- const selectValue = !value ? NONE : typeof value === "object" ? value.var : value;
88
+ const selectValue = showCustom ? CUSTOM : value && typeof value === "object" ? value.var : NONE;
89
+ const pick = (next: unknown) => {
90
+ if (next === CUSTOM) {
91
+ setCustomOpen(true);
92
+ return;
93
+ }
94
+ setCustomOpen(false);
95
+ onChange(next === NONE || next === null ? undefined : { var: String(next) });
96
+ };
58
97
  return (
59
98
  <div className="emvb-field-group" data-emvb-control="color">
60
99
  <Select
61
100
  label={label}
62
101
  className={FIELD}
63
102
  value={selectValue}
64
- onValueChange={(next) =>
65
- onChange(
66
- next === NONE
67
- ? undefined
68
- : HEX.test(String(next))
69
- ? String(next)
70
- : { var: String(next) },
71
- )
72
- }
73
- renderValue={(current: unknown) =>
74
- current === NONE
75
- ? (placeholder ?? "Default")
76
- : (colors.find((c) => c.id === current)?.name ?? String(current))
77
- }
103
+ onValueChange={pick}
104
+ renderValue={(current: unknown) => {
105
+ if (current === NONE) return placeholder ?? "Default";
106
+ if (current === CUSTOM)
107
+ return custom ? (
108
+ <>
109
+ <Swatch color={custom} />
110
+ {custom}
111
+ </>
112
+ ) : (
113
+ "Custom color"
114
+ );
115
+ const color = colors.find((c) => c.id === current);
116
+ return color ? (
117
+ <>
118
+ <Swatch color={color.value} />
119
+ {color.name}
120
+ </>
121
+ ) : (
122
+ String(current)
123
+ );
124
+ }}
78
125
  >
79
126
  <Select.Option value={NONE}>Default</Select.Option>
80
- {typeof value === "string" && <Select.Option value={value}>{value}</Select.Option>}
81
127
  {colors.map((color) => (
82
128
  <Select.Option key={color.id} value={color.id}>
83
- <span className="emvb-swatch" style={{ background: color.value }} aria-hidden="true" />
129
+ <Swatch color={color.value} />
84
130
  {color.name}
85
131
  </Select.Option>
86
132
  ))}
133
+ <Select.Option value={CUSTOM}>Custom color</Select.Option>
87
134
  </Select>
135
+ {colors.length > 0 && (
136
+ <div
137
+ className="emvb-swatches"
138
+ role="group"
139
+ aria-label={`${label}: colour variables`}
140
+ data-emvb-swatches=""
141
+ >
142
+ {colors.slice(0, SWATCH_LIMIT).map((color) => (
143
+ <button
144
+ key={color.id}
145
+ type="button"
146
+ className="emvb-swatch-button"
147
+ aria-label={`${color.name} (${color.value})`}
148
+ aria-pressed={bound?.id === color.id}
149
+ title={`${color.name} · ${color.value}`}
150
+ onClick={() => pick(color.id)}
151
+ >
152
+ <Swatch color={color.value} />
153
+ </button>
154
+ ))}
155
+ {colors.length > SWATCH_LIMIT && (
156
+ <span className="emvb-helper">+{colors.length - SWATCH_LIMIT} more in the list</span>
157
+ )}
158
+ </div>
159
+ )}
160
+ {showCustom && (
161
+ <CustomColor
162
+ key={custom ?? "new"}
163
+ value={custom ?? ""}
164
+ label={label}
165
+ clipped={clipped}
166
+ onClipped={setClipped}
167
+ onCommit={onChange}
168
+ />
169
+ )}
88
170
  {bound && (
89
171
  <VariableValue
90
172
  key={bound.id}
@@ -104,7 +186,10 @@ export function ColorControl({
104
186
  {creating ? (
105
187
  <NewVariable
106
188
  onCancel={() => setCreating(false)}
107
- onCreate={async (name, hex) => {
189
+ onCreate={async (raw, hex) => {
190
+ const name = cleanClassName(raw);
191
+ // Past the cap or with no name left the save would fail (W-219).
192
+ if (!name || variableListFull(design, "color")) return;
108
193
  const id = uniqueId(design, name);
109
194
  await onDesignChange({
110
195
  ...design,
@@ -120,6 +205,7 @@ export function ColorControl({
120
205
  className={BUTTON}
121
206
  icon={<PlusIcon aria-hidden="true" />}
122
207
  onClick={() => setCreating(true)}
208
+ disabled={variableListFull(design, "color")}
123
209
  >
124
210
  New variable
125
211
  </Button>
@@ -128,6 +214,167 @@ export function ColorControl({
128
214
  );
129
215
  }
130
216
 
217
+ /** The custom hex field with the browser's colour picker beside it (W-136). */
218
+ function CustomColor({
219
+ value,
220
+ label,
221
+ clipped,
222
+ onClipped,
223
+ onCommit,
224
+ }: {
225
+ value: string;
226
+ label: string;
227
+ clipped: string | null;
228
+ onClipped: (hex: string | null) => void;
229
+ onCommit: (hex: string) => void;
230
+ }) {
231
+ const [draft, setDraft] = React.useState(value);
232
+ // The field that refused its entry is the one marked, not always the hex (W-174).
233
+ const [error, setError] = React.useState<{ field: Notation; message: string } | null>(null);
234
+ const picker = React.useRef<HTMLInputElement | null>(null);
235
+ const commit = (next: string) => {
236
+ const hex = next.trim();
237
+ if (hex === value || hex === "") {
238
+ setError(null);
239
+ return;
240
+ }
241
+ if (!HEX.test(hex)) {
242
+ setError({ field: "hex", message: BAD_HEX });
243
+ return;
244
+ }
245
+ setError(null);
246
+ onCommit(hex);
247
+ };
248
+ // The picker applies on its native change event (the dialog closed), not on every drag step.
249
+ React.useEffect(() => {
250
+ const el = picker.current;
251
+ if (!el) return;
252
+ const onPicked = () => {
253
+ setDraft(el.value);
254
+ commit(el.value);
255
+ };
256
+ el.addEventListener("change", onPicked);
257
+ return () => el.removeEventListener("change", onPicked);
258
+ });
259
+ return (
260
+ <div className="emvb-custom-color" data-emvb-custom-color="">
261
+ <div className="emvb-custom-color-row">
262
+ <Input
263
+ label="Custom color"
264
+ className={`${FIELD} emvb-mono`}
265
+ placeholder="#1a2b3c"
266
+ value={draft}
267
+ variant={error?.field === "hex" ? "error" : undefined}
268
+ aria-invalid={error?.field === "hex" ? true : undefined}
269
+ onChange={(event) => setDraft(event.target.value)}
270
+ onBlur={() => commit(draft)}
271
+ onKeyDown={(event) => {
272
+ if (event.key === "Enter") commit(draft);
273
+ }}
274
+ />
275
+ <input
276
+ type="color"
277
+ className="emvb-color-picker"
278
+ aria-label={`Pick ${label.toLowerCase()}`}
279
+ ref={picker}
280
+ value={pickerHex(HEX.test(draft) ? draft : value || "#000000")}
281
+ onChange={(event) => setDraft(event.target.value)}
282
+ />
283
+ </div>
284
+ {error?.field === "hex" && <InlineError>{error.message}</InlineError>}
285
+ <NotationRow
286
+ label="RGB"
287
+ text={formatRgb(parseHex(HEX.test(draft) ? draft : value) ?? { r: 0, g: 0, b: 0, a: 1 })}
288
+ placeholder="255, 128, 0"
289
+ error={error?.field === "rgb" ? error.message : undefined}
290
+ onCommit={(text) => {
291
+ const next = parseRgb(text);
292
+ if (!next) {
293
+ setError({ field: "rgb", message: RGB_RANGE });
294
+ return;
295
+ }
296
+ setError(null);
297
+ const hex = toHex(next);
298
+ setDraft(hex);
299
+ onCommit(hex);
300
+ }}
301
+ />
302
+ <NotationRow
303
+ label="OKLCH"
304
+ text={formatOklch(parseHex(HEX.test(draft) ? draft : value) ?? { r: 0, g: 0, b: 0, a: 1 })}
305
+ placeholder="62.8, 0.150, 29"
306
+ error={error?.field === "oklch" ? error.message : undefined}
307
+ onCommit={(text) => {
308
+ const next = parseOklch(text);
309
+ if (!next) {
310
+ setError({ field: "oklch", message: OKLCH_RANGE });
311
+ return;
312
+ }
313
+ setError(null);
314
+ const hex = toHex(next);
315
+ onClipped(oklchClipped(text) ? hex : null);
316
+ setDraft(hex);
317
+ onCommit(hex);
318
+ }}
319
+ />
320
+ {clipped !== null && clipped === draft && !error && (
321
+ <p className="emvb-helper" role="status" data-emvb-clipped="">
322
+ Clipped to {clipped}: that OKLCH colour is outside sRGB, so its out-of-range channels were
323
+ cut to fit.
324
+ </p>
325
+ )}
326
+ </div>
327
+ );
328
+ }
329
+
330
+ type Notation = "hex" | "rgb" | "oklch";
331
+
332
+ // A refusal says the valid ranges, not only an example (W-181).
333
+ const RGB_RANGE =
334
+ "Enter red, green and blue from 0 to 255, such as 255, 128, 0. Alpha is optional: 0–1 or 0–100%.";
335
+ const OKLCH_RANGE =
336
+ "Enter lightness 0–100 (or 0–1), chroma 0–0.5 and hue 0–360, such as 62.8, 0.150, 29.";
337
+
338
+ /** A second view of the same colour. Commits on Enter or blur, like the hex field. */
339
+ function NotationRow({
340
+ label,
341
+ text,
342
+ placeholder,
343
+ error,
344
+ onCommit,
345
+ }: {
346
+ label: string;
347
+ text: string;
348
+ placeholder: string;
349
+ error: string | undefined;
350
+ onCommit: (text: string) => void;
351
+ }) {
352
+ const [draft, setDraft] = React.useState(text);
353
+ React.useEffect(() => {
354
+ setDraft(text);
355
+ }, [text]);
356
+ return (
357
+ <>
358
+ <Input
359
+ label={label}
360
+ className={`${FIELD} emvb-mono`}
361
+ placeholder={placeholder}
362
+ value={draft}
363
+ variant={error ? "error" : undefined}
364
+ aria-invalid={error ? true : undefined}
365
+ onChange={(event) => setDraft(event.target.value)}
366
+ onBlur={() => {
367
+ if (draft.trim() !== text) onCommit(draft);
368
+ }}
369
+ onKeyDown={(event) => {
370
+ if (event.key === "Enter") onCommit(draft);
371
+ }}
372
+ />
373
+ {error && <InlineError>{error}</InlineError>}
374
+ </>
375
+ );
376
+ }
377
+
131
378
  function VariableValue({
132
379
  name,
133
380
  value,