@perkyzz/emvb 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/README.md +4 -2
  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 +8 -4
  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 +23 -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 +164 -20
  106. package/src/astro/tabs/runtime.ts +6 -1
  107. package/src/astro/theme-context.ts +11 -3
  108. package/src/constants.ts +1 -1
  109. package/src/core/arrange.ts +98 -10
  110. package/src/core/clipboard.ts +91 -9
  111. package/src/core/css/generate.ts +21 -4
  112. package/src/core/design/classes.ts +95 -9
  113. package/src/core/design/transfer.ts +101 -2
  114. package/src/core/design/variables.ts +51 -11
  115. package/src/core/elements/dynamic-elements.ts +57 -5
  116. package/src/core/elements/field-id.ts +6 -0
  117. package/src/core/elements/form-elements.ts +23 -8
  118. package/src/core/elements/index.ts +267 -49
  119. package/src/core/elements/menu.ts +190 -0
  120. package/src/core/forms/definition.ts +11 -0
  121. package/src/core/index.ts +67 -3
  122. package/src/core/limits.ts +2 -0
  123. package/src/core/migrate/index.ts +51 -1
  124. package/src/core/nesting.ts +51 -0
  125. package/src/core/render/context.ts +7 -0
  126. package/src/core/render/dynamic-post.ts +39 -11
  127. package/src/core/render/extras.ts +22 -3
  128. package/src/core/render/form.ts +134 -36
  129. package/src/core/render/index.ts +123 -25
  130. package/src/core/render/loop.ts +10 -3
  131. package/src/core/render/pagination.ts +76 -0
  132. package/src/core/render/section.ts +5 -1
  133. package/src/core/render/tabs.ts +8 -4
  134. package/src/core/render/vnode.ts +1 -1
  135. package/src/core/sanitize/css.ts +197 -17
  136. package/src/core/sanitize/embed-url.ts +64 -7
  137. package/src/core/sanitize/svg-upload.ts +203 -0
  138. package/src/core/sanitize/svg.ts +102 -21
  139. package/src/core/schema/descriptors.ts +4 -0
  140. package/src/core/schema/design.ts +7 -2
  141. package/src/core/schema/layout.ts +210 -19
  142. package/src/core/schema/style.ts +87 -8
  143. package/src/core/tabs/presence.ts +9 -0
  144. package/src/core/theme/conditions.ts +4 -1
  145. package/src/core/theme/dynamic.ts +10 -0
  146. package/src/core/theme/float.ts +113 -0
  147. package/src/core/theme/pagination.ts +97 -0
  148. package/src/core/theme/part-types.ts +2 -0
  149. package/src/core/theme/popup-markup.ts +3 -1
  150. package/src/core/theme/popup-rules.ts +4 -3
  151. package/src/server/hooks.ts +20 -0
  152. package/src/server/icon-routes.ts +79 -0
  153. package/src/server/plugin.ts +5 -1
@@ -0,0 +1,195 @@
1
+ /** Views of a stored hex colour. The layout keeps hex; these convert for the picker (W-160). */
2
+
3
+ export type Rgba = { r: number; g: number; b: number; a: number };
4
+
5
+ const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
6
+
7
+ function channel(n: number): number {
8
+ return Math.min(255, Math.max(0, Math.round(n)));
9
+ }
10
+
11
+ function srgbToLinear(c: number): number {
12
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
13
+ }
14
+
15
+ function linearToSrgb(c: number): number {
16
+ const v = Math.min(1, Math.max(0, c));
17
+ return v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055;
18
+ }
19
+
20
+ /** `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa` as 0–255 channels. Alpha is 0–1. */
21
+ export function parseHex(hex: string): Rgba | null {
22
+ const h = hex.trim();
23
+ if (!HEX.test(h)) return null;
24
+ let body = h.slice(1).toLowerCase();
25
+ if (body.length === 3 || body.length === 4) body = [...body].map((ch) => ch + ch).join("");
26
+ const r = Number.parseInt(body.slice(0, 2), 16);
27
+ const g = Number.parseInt(body.slice(2, 4), 16);
28
+ const b = Number.parseInt(body.slice(4, 6), 16);
29
+ const a = body.length === 8 ? Number.parseInt(body.slice(6, 8), 16) / 255 : 1;
30
+ return { r, g, b, a };
31
+ }
32
+
33
+ /** 6 digits when opaque, 8 when not. */
34
+ export function toHex({ r, g, b, a }: Rgba): string {
35
+ const hex = [r, g, b].map((n) => channel(n).toString(16).padStart(2, "0")).join("");
36
+ if (a >= 0.999) return `#${hex}`;
37
+ const alpha = channel(a * 255)
38
+ .toString(16)
39
+ .padStart(2, "0");
40
+ return `#${hex}${alpha}`;
41
+ }
42
+
43
+ export function formatRgb({ r, g, b, a }: Rgba): string {
44
+ const rgb = `${channel(r)}, ${channel(g)}, ${channel(b)}`;
45
+ if (a >= 0.999) return rgb;
46
+ return `${rgb}, ${Math.round(a * 100)}%`;
47
+ }
48
+
49
+ /** "255, 128, 0", "255 128 0", or with a fourth alpha as 0–1 or a percent. */
50
+ export function parseRgb(text: string): Rgba | null {
51
+ const parts = text
52
+ .trim()
53
+ .replace(/^rgba?\(/i, "")
54
+ .replace(/\)$/, "")
55
+ .split(/[\s,]+/)
56
+ .filter(Boolean);
57
+ if (parts.length !== 3 && parts.length !== 4) return null;
58
+ const nums = parts.slice(0, 3).map((part) => Number(part));
59
+ if (nums.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return null;
60
+ let a = 1;
61
+ const alpha = parts[3];
62
+ if (alpha !== undefined) {
63
+ if (alpha.endsWith("%")) {
64
+ const pct = Number(alpha.slice(0, -1));
65
+ if (!Number.isFinite(pct) || pct < 0 || pct > 100) return null;
66
+ a = pct / 100;
67
+ } else {
68
+ const n = Number(alpha);
69
+ if (!Number.isFinite(n) || n < 0 || n > 1) return null;
70
+ a = n;
71
+ }
72
+ }
73
+ return { r: nums[0] ?? 0, g: nums[1] ?? 0, b: nums[2] ?? 0, a };
74
+ }
75
+
76
+ function linearSrgbToOklab(r: number, g: number, b: number): [number, number, number] {
77
+ const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
78
+ const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
79
+ const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
80
+ return [
81
+ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
82
+ 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
83
+ 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
84
+ ];
85
+ }
86
+
87
+ function oklabToLinearSrgb(L: number, a: number, b: number): [number, number, number] {
88
+ const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
89
+ const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
90
+ const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
91
+ return [
92
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
93
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
94
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
95
+ ];
96
+ }
97
+
98
+ type Oklch = { l: number; c: number; h: number; a: number };
99
+
100
+ function rgbaToOklch({ r, g, b, a }: Rgba): Oklch {
101
+ const [L, A, B] = linearSrgbToOklab(
102
+ srgbToLinear(r / 255),
103
+ srgbToLinear(g / 255),
104
+ srgbToLinear(b / 255),
105
+ );
106
+ const c = Math.hypot(A, B);
107
+ let h = (Math.atan2(B, A) * 180) / Math.PI;
108
+ if (h < 0) h += 360;
109
+ return { l: L * 100, c, h: c < 0.0001 ? 0 : h, a };
110
+ }
111
+
112
+ /** Out-of-gamut channels are clipped into sRGB. */
113
+ function oklchToRgba({ l, c, h, a }: Oklch): Rgba {
114
+ const rad = (h * Math.PI) / 180;
115
+ const [lr, lg, lb] = oklabToLinearSrgb(l / 100, c * Math.cos(rad), c * Math.sin(rad));
116
+ return {
117
+ r: channel(linearToSrgb(lr) * 255),
118
+ g: channel(linearToSrgb(lg) * 255),
119
+ b: channel(linearToSrgb(lb) * 255),
120
+ a,
121
+ };
122
+ }
123
+
124
+ export function formatOklch(rgba: Rgba): string {
125
+ const { l, c, h, a } = rgbaToOklch(rgba);
126
+ // 3 / 5 / 2 digits is the least that round-trips every sRGB hex.
127
+ const base = `${l.toFixed(3)}, ${c.toFixed(5)}, ${h.toFixed(2)}`;
128
+ if (a >= 0.999) return base;
129
+ return `${base}, ${Math.round(a * 100)}%`;
130
+ }
131
+
132
+ /**
133
+ * Lightness as 0–100: "62.8%" is a percent; a bare number up to 1 is CSS's 0–1 form ("0.628", as
134
+ * pasted from `oklch(0.628 0.2577 29.23)`), and a bigger one is EmVB's own 0–100 form (W-173).
135
+ * Every sRGB colour but black has L above 3, so the two bare forms don't overlap.
136
+ */
137
+ function oklchLightness(raw: string): number {
138
+ if (raw.endsWith("%")) return Number(raw.slice(0, -1));
139
+ const n = Number(raw);
140
+ return n <= 1 ? n * 100 : n;
141
+ }
142
+
143
+ /**
144
+ * "70, 0.15, 40", "70% 0.15 40 / 50%" or CSS `oklch(0.7 0.15 40deg)`. L is 0–100 (or 0–1),
145
+ * C is 0–0.5 (or a percent of 0.4, as in CSS), H is 0–360.
146
+ */
147
+ export function parseOklch(text: string): Rgba | null {
148
+ const oklch = readOklch(text);
149
+ return oklch ? oklchToRgba(oklch) : null;
150
+ }
151
+
152
+ /**
153
+ * Whether a typed OKLCH colour lies outside sRGB, so saving it as hex clipped a channel (W-183).
154
+ * The slack is half an 8-bit step, so a value EmVB formatted itself never counts as clipped.
155
+ */
156
+ export function oklchClipped(text: string): boolean {
157
+ const oklch = readOklch(text);
158
+ if (!oklch) return false;
159
+ const rad = (oklch.h * Math.PI) / 180;
160
+ const linear = oklabToLinearSrgb(oklch.l / 100, oklch.c * Math.cos(rad), oklch.c * Math.sin(rad));
161
+ return linear.some((v) => v < -0.00015 || v > 1.0045);
162
+ }
163
+
164
+ function readOklch(text: string): Oklch | null {
165
+ const parts = text
166
+ .trim()
167
+ .replace(/^oklch\(/i, "")
168
+ .replace(/\)$/, "")
169
+ .replace("/", " ")
170
+ .split(/[\s,]+/)
171
+ .filter(Boolean);
172
+ if (parts.length !== 3 && parts.length !== 4) return null;
173
+ const l = oklchLightness(parts[0] ?? "");
174
+ const cRaw = parts[1] ?? "";
175
+ const c = cRaw.endsWith("%") ? (Number(cRaw.slice(0, -1)) / 100) * 0.4 : Number(cRaw);
176
+ const hRaw = parts[2] ?? "";
177
+ const h = Number(hRaw.endsWith("deg") ? hRaw.slice(0, -3) : hRaw);
178
+ if (!Number.isFinite(l) || l < 0 || l > 100) return null;
179
+ if (!Number.isFinite(c) || c < 0 || c > 0.5) return null;
180
+ if (!Number.isFinite(h) || h < 0 || h > 360) return null;
181
+ let a = 1;
182
+ const alpha = parts[3];
183
+ if (alpha !== undefined) {
184
+ if (alpha.endsWith("%")) {
185
+ const pct = Number(alpha.slice(0, -1));
186
+ if (!Number.isFinite(pct) || pct < 0 || pct > 100) return null;
187
+ a = pct / 100;
188
+ } else {
189
+ const n = Number(alpha);
190
+ if (!Number.isFinite(n) || n < 0 || n > 1) return null;
191
+ a = n;
192
+ }
193
+ }
194
+ return { l, c, h, a };
195
+ }
@@ -0,0 +1,119 @@
1
+ import { Button, Input } from "@cloudflare/kumo";
2
+ import * as React from "react";
3
+ import type { Layout, LayoutNode } from "../../../../core/index.ts";
4
+ import { BUTTON, FIELD } from "../../../ui.ts";
5
+ import {
6
+ attributeErrors,
7
+ htmlIdError,
8
+ MAX_ATTR_VALUE,
9
+ savableAttributes,
10
+ type AttributeRow,
11
+ } from "./advanced-rules.ts";
12
+
13
+ /**
14
+ * CSS id: a value the page can't save (bad characters, or another element's id) stays in the
15
+ * field with an inline error and is not stored (W-189).
16
+ */
17
+ export function HtmlIdField({
18
+ node,
19
+ layout,
20
+ onChange,
21
+ }: {
22
+ node: LayoutNode;
23
+ layout: Layout | null;
24
+ onChange: (node: LayoutNode) => void;
25
+ }) {
26
+ const stored = node.htmlId ?? "";
27
+ const [draft, setDraft] = React.useState(stored);
28
+ React.useEffect(() => {
29
+ setDraft((current) => (htmlIdError(current, layout, node.id) ? current : stored));
30
+ // Follow the stored id when another element is picked, or undo changes it.
31
+ }, [stored, node.id]);
32
+ const error = htmlIdError(draft, layout, node.id);
33
+ return (
34
+ <Input
35
+ label="CSS id"
36
+ className={`${FIELD} emvb-mono`}
37
+ value={draft}
38
+ error={error ?? undefined}
39
+ aria-invalid={error ? true : undefined}
40
+ onChange={(event) => {
41
+ const next = event.target.value.trim();
42
+ setDraft(next);
43
+ if (htmlIdError(next, layout, node.id)) return;
44
+ onChange({ ...node, htmlId: next === "" ? undefined : next });
45
+ }}
46
+ />
47
+ );
48
+ }
49
+
50
+ const sameRows = (a: readonly AttributeRow[] | undefined, b: readonly AttributeRow[] | undefined) =>
51
+ JSON.stringify(a ?? []) === JSON.stringify(b ?? []);
52
+
53
+ /**
54
+ * Attributes: rows keep what you type; only rows the page can save are stored, and the others
55
+ * say why inline, so a half-typed or refused name never blocks Save (W-189).
56
+ */
57
+ export function AttributesEditor({
58
+ attributes,
59
+ onChange,
60
+ }: {
61
+ attributes: LayoutNode["attributes"];
62
+ onChange: (next: LayoutNode["attributes"]) => void;
63
+ }) {
64
+ const [rows, setRows] = React.useState<AttributeRow[]>(() => [...(attributes ?? [])]);
65
+ React.useEffect(() => {
66
+ // Undo, redo or another element: show the stored rows unless they are what these rows save.
67
+ setRows((current) =>
68
+ sameRows(savableAttributes(current), attributes) ? current : [...(attributes ?? [])],
69
+ );
70
+ }, [attributes]);
71
+ const errors = attributeErrors(rows);
72
+ const change = (next: AttributeRow[]) => {
73
+ setRows(next);
74
+ const keep = savableAttributes(next);
75
+ if (!sameRows(keep, attributes)) onChange(keep);
76
+ };
77
+ const update = (index: number, patch: Partial<AttributeRow>) =>
78
+ change(rows.map((item, i) => (i === index ? { ...item, ...patch } : item)));
79
+ return (
80
+ <div className="emvb-field-group" data-emvb-attributes="">
81
+ <span className="emvb-var-field-label">Attributes</span>
82
+ {rows.map((item, index) => (
83
+ <div key={index} className="emvb-style-row">
84
+ <Input
85
+ label="Name"
86
+ className={`${FIELD} emvb-mono`}
87
+ value={item.name}
88
+ error={errors[index] ?? undefined}
89
+ aria-invalid={errors[index] ? true : undefined}
90
+ onChange={(event) => update(index, { name: event.target.value.trim().toLowerCase() })}
91
+ />
92
+ <Input
93
+ label="Value"
94
+ className={FIELD}
95
+ value={item.value}
96
+ maxLength={MAX_ATTR_VALUE}
97
+ onChange={(event) => update(index, { value: event.target.value })}
98
+ />
99
+ <Button
100
+ type="button"
101
+ variant="ghost"
102
+ onClick={() => change(rows.filter((_, i) => i !== index))}
103
+ >
104
+ Remove
105
+ </Button>
106
+ </div>
107
+ ))}
108
+ <Button
109
+ type="button"
110
+ variant="secondary"
111
+ className={BUTTON}
112
+ onClick={() => setRows([...rows, { name: "data-", value: "" }])}
113
+ >
114
+ Add attribute
115
+ </Button>
116
+ <p className="emvb-helper">data-* and aria-* names only. Event handlers are refused.</p>
117
+ </div>
118
+ );
119
+ }
@@ -1,4 +1,4 @@
1
- import { Button, Input } from "@cloudflare/kumo";
1
+ import { Button, Input, Select } from "@cloudflare/kumo";
2
2
  import { PlusIcon } from "@phosphor-icons/react";
3
3
  import * as React from "react";
4
4
  import { sanitizeMediaUrl, type DesignSystem, type StyleProps } from "../../../../core/index.ts";
@@ -13,8 +13,23 @@ type Overlay = NonNullable<StyleProps["overlay"]>;
13
13
 
14
14
  const ANGLE: NumberSpec = { min: 0, max: 360, integer: true, example: "180", suffix: "°" };
15
15
  const OVERLAY_OPACITY: NumberSpec = { min: 0, max: 100, scale: 100, example: "40", suffix: "%" };
16
+ const STOP_AT: NumberSpec = { min: 0, max: 100, integer: true, example: "50", suffix: "%" };
16
17
 
17
- const NEW_GRADIENT: Gradient = { angle: 180, from: "#ffffff", to: "#000000" };
18
+ /**
19
+ * Where a stop goes when its location is cleared: spaced evenly by its place in the list, the way
20
+ * CSS places stops that have no location (W-180).
21
+ */
22
+ const autoStopAt = (index: number, count: number) =>
23
+ Math.round((index / Math.max(1, count - 1)) * 100);
24
+
25
+ export const NEW_GRADIENT: Gradient = {
26
+ type: "linear",
27
+ angle: 180,
28
+ stops: [
29
+ { color: "#ffffff", at: 0 },
30
+ { color: "#000000", at: 100 },
31
+ ],
32
+ };
18
33
  const NEW_OVERLAY: Overlay = { color: "#000000", opacity: 0.4 };
19
34
 
20
35
  /** Background image: library, upload, or a URL. Without `fetcher`, only the URL is offered. */
@@ -100,7 +115,7 @@ export function BackgroundImageControl({
100
115
  <span className="emvb-helper emvb-mono">{src}</span>
101
116
  </div>
102
117
  ) : (
103
- <p className="emvb-helper">No image yet. The background colour shows through.</p>
118
+ <p className="emvb-helper">No image yet, so this element has no background.</p>
104
119
  )}
105
120
  {fetcher && (
106
121
  <div className="emvb-media-actions">
@@ -196,69 +211,152 @@ export function BackgroundImageControl({
196
211
  );
197
212
  }
198
213
 
214
+ const GRADIENT_TYPES: { value: Gradient["type"]; label: string }[] = [
215
+ { value: "linear", label: "Linear" },
216
+ { value: "radial", label: "Radial" },
217
+ { value: "conic", label: "Conic" },
218
+ ];
219
+
220
+ const RADIAL_AT = [
221
+ "center",
222
+ "top",
223
+ "bottom",
224
+ "left",
225
+ "right",
226
+ "top left",
227
+ "top right",
228
+ "bottom left",
229
+ "bottom right",
230
+ ] as const;
231
+
232
+ /** Linear, radial or conic, with 2–10 stops. Angle is a slider and a number. */
199
233
  export function GradientControl({
200
234
  value,
201
235
  design,
202
236
  onChange,
203
237
  onDesignChange,
204
- reset,
205
238
  }: {
206
- value: Gradient | undefined;
239
+ value: Gradient;
207
240
  design: DesignSystem;
208
- onChange: (next: Gradient | undefined) => void;
241
+ onChange: (next: Gradient) => void;
209
242
  onDesignChange: (design: DesignSystem) => Promise<void>;
210
- reset: React.ReactNode;
211
243
  }) {
212
- const patch = (part: Partial<Gradient>) => {
213
- if (!value) return;
214
- onChange({ ...value, ...part });
244
+ const stops = value.stops;
245
+ const defaultAngle = value.type === "conic" ? 0 : 180;
246
+ // An emptied angle goes back to the type's default instead of 0 (W-180).
247
+ const setAngle = (angle: number | undefined) => {
248
+ const next = { ...value, angle };
249
+ if (angle === undefined) delete next.angle;
250
+ onChange(next);
251
+ };
252
+ const setStop = (index: number, part: Partial<Gradient["stops"][number]>) => {
253
+ onChange({
254
+ ...value,
255
+ stops: stops.map((stop, i) => (i === index ? { ...stop, ...part } : stop)),
256
+ });
257
+ };
258
+ const addStop = () => {
259
+ if (stops.length >= 10) return;
260
+ const used = new Set(stops.map((stop) => stop.at));
261
+ let at = 50;
262
+ while (used.has(at) && at < 100) at += 1;
263
+ onChange({ ...value, stops: [...stops, { color: "#808080", at }] });
215
264
  };
216
265
  return (
217
- <div
218
- className="emvb-style-row"
219
- data-emvb-style="gradient"
220
- data-set={value ? "true" : undefined}
221
- >
222
- <div className="emvb-field-group">
223
- <span className="emvb-var-field-label">Gradient</span>
224
- {value ? (
225
- <>
226
- <NumberField
227
- fieldKey="gradient.angle"
228
- label="Angle"
229
- value={value.angle}
230
- spec={ANGLE}
231
- onCommit={(n) => patch({ angle: n ?? 0 })}
232
- />
233
- <ColorControl
234
- label="From"
235
- value={value.from}
236
- design={design}
237
- onChange={(from) => from && patch({ from })}
238
- onDesignChange={onDesignChange}
239
- />
240
- <ColorControl
241
- label="To"
242
- value={value.to}
243
- design={design}
244
- onChange={(to) => to && patch({ to })}
245
- onDesignChange={onDesignChange}
246
- />
247
- </>
248
- ) : (
249
- <Button
250
- type="button"
251
- className={BUTTON}
252
- variant="secondary"
253
- icon={PlusIcon}
254
- data-emvb-add-gradient=""
255
- onClick={() => onChange(NEW_GRADIENT)}
256
- >
257
- Add gradient
258
- </Button>
259
- )}
260
- </div>
261
- {reset}
266
+ <div className="emvb-field-group" data-emvb-gradient="">
267
+ <Select
268
+ label="Gradient type"
269
+ className={FIELD}
270
+ value={value.type}
271
+ onValueChange={(next) => {
272
+ const type = GRADIENT_TYPES.find((option) => option.value === next)?.value;
273
+ if (type) onChange({ ...value, type });
274
+ }}
275
+ >
276
+ {GRADIENT_TYPES.map((option) => (
277
+ <Select.Option key={option.value} value={option.value}>
278
+ {option.label}
279
+ </Select.Option>
280
+ ))}
281
+ </Select>
282
+ {value.type === "radial" ? (
283
+ <Select
284
+ label="Position"
285
+ className={FIELD}
286
+ value={value.position ?? "center"}
287
+ onValueChange={(next) => {
288
+ const position = RADIAL_AT.find((option) => option === next);
289
+ if (position) onChange({ ...value, position });
290
+ }}
291
+ >
292
+ {RADIAL_AT.map((option) => (
293
+ <Select.Option key={option} value={option}>
294
+ {option}
295
+ </Select.Option>
296
+ ))}
297
+ </Select>
298
+ ) : (
299
+ <div className="emvb-angle">
300
+ <NumberField
301
+ fieldKey="gradient.angle"
302
+ label="Angle"
303
+ value={value.angle}
304
+ inherited={defaultAngle}
305
+ spec={ANGLE}
306
+ onCommit={setAngle}
307
+ />
308
+ <input
309
+ type="range"
310
+ className="emvb-angle-slider"
311
+ min={0}
312
+ max={360}
313
+ aria-label="Angle slider"
314
+ value={value.angle ?? defaultAngle}
315
+ onChange={(event) => onChange({ ...value, angle: Number(event.target.value) })}
316
+ />
317
+ </div>
318
+ )}
319
+ {stops.map((stop, index) => (
320
+ <div className="emvb-gradient-stop" key={index} data-emvb-gradient-stop={index}>
321
+ <ColorControl
322
+ label={`Color ${index + 1}`}
323
+ value={stop.color}
324
+ design={design}
325
+ onChange={(color) => color && setStop(index, { color })}
326
+ onDesignChange={onDesignChange}
327
+ />
328
+ <NumberField
329
+ fieldKey={`gradient.stop.${index}`}
330
+ label={`Location ${index + 1}`}
331
+ value={stop.at}
332
+ inherited={autoStopAt(index, stops.length)}
333
+ spec={STOP_AT}
334
+ onCommit={(n) => setStop(index, { at: n ?? autoStopAt(index, stops.length) })}
335
+ />
336
+ {stops.length > 2 && (
337
+ <Button
338
+ type="button"
339
+ variant="secondary"
340
+ className={BUTTON}
341
+ onClick={() => onChange({ ...value, stops: stops.filter((_, i) => i !== index) })}
342
+ >
343
+ Remove color {index + 1}
344
+ </Button>
345
+ )}
346
+ </div>
347
+ ))}
348
+ {stops.length < 10 && (
349
+ <Button
350
+ type="button"
351
+ variant="secondary"
352
+ className={BUTTON}
353
+ data-emvb-add-stop=""
354
+ icon={PlusIcon}
355
+ onClick={addStop}
356
+ >
357
+ Add location
358
+ </Button>
359
+ )}
262
360
  </div>
263
361
  );
264
362
  }