@perkyzz/emvb 0.0.0-stage → 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 (217) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/THIRD_PARTY_NOTICES.md +32 -0
  4. package/licenses/fontawesome-free.LICENSE +165 -0
  5. package/licenses/lucide-static.LICENSE +43 -0
  6. package/licenses/remixicon.LICENSE +201 -0
  7. package/licenses/tabler-icons.LICENSE +21 -0
  8. package/package.json +76 -4
  9. package/src/admin/access/RequireEditor.tsx +39 -0
  10. package/src/admin/access/role.ts +36 -0
  11. package/src/admin/access/sidebar-shim.ts +108 -0
  12. package/src/admin/api.ts +75 -0
  13. package/src/admin/content-api.ts +210 -0
  14. package/src/admin/editor/Editor.tsx +532 -0
  15. package/src/admin/editor/EditorOverlay.tsx +66 -0
  16. package/src/admin/editor/ShortcutsDialog.tsx +30 -0
  17. package/src/admin/editor/SmallScreenNotice.tsx +21 -0
  18. package/src/admin/editor/TopBar.tsx +243 -0
  19. package/src/admin/editor/canvas/CanvasFrame.tsx +536 -0
  20. package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
  21. package/src/admin/editor/canvas/DeviceBar.tsx +91 -0
  22. package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
  23. package/src/admin/editor/canvas/SelectionOverlay.tsx +239 -0
  24. package/src/admin/editor/canvas/canvas-events.ts +245 -0
  25. package/src/admin/editor/canvas/state-preview.ts +19 -0
  26. package/src/admin/editor/canvas/tab-reveal.ts +101 -0
  27. package/src/admin/editor/canvas/vnode-react.tsx +69 -0
  28. package/src/admin/editor/clipboard-store.ts +44 -0
  29. package/src/admin/editor/dialogs.tsx +164 -0
  30. package/src/admin/editor/dnd/drag-stash.ts +27 -0
  31. package/src/admin/editor/dnd/drop-target.ts +148 -0
  32. package/src/admin/editor/dnd/new-element.ts +107 -0
  33. package/src/admin/editor/editor-css.ts +625 -0
  34. package/src/admin/editor/exit.ts +27 -0
  35. package/src/admin/editor/host.ts +30 -0
  36. package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
  37. package/src/admin/editor/icons/library.ts +243 -0
  38. package/src/admin/editor/icons/set-format.ts +29 -0
  39. package/src/admin/editor/icons/sets/fontawesome.json +1 -0
  40. package/src/admin/editor/icons/sets/lucide.json +1 -0
  41. package/src/admin/editor/icons/sets/remix.json +1 -0
  42. package/src/admin/editor/icons/sets/tabler.json +1 -0
  43. package/src/admin/editor/panels/AddPanel.tsx +146 -0
  44. package/src/admin/editor/panels/ClassesSection.tsx +372 -0
  45. package/src/admin/editor/panels/ColorControl.tsx +477 -0
  46. package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
  47. package/src/admin/editor/panels/ElementPanel.tsx +773 -0
  48. package/src/admin/editor/panels/LayersPanel.tsx +573 -0
  49. package/src/admin/editor/panels/LeftPanel.tsx +81 -0
  50. package/src/admin/editor/panels/PageSettings.tsx +255 -0
  51. package/src/admin/editor/panels/SettingsPanel.tsx +113 -0
  52. package/src/admin/editor/panels/SiteStylesDrawer.tsx +548 -0
  53. package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
  54. package/src/admin/editor/panels/TriggersEditor.tsx +214 -0
  55. package/src/admin/editor/panels/VariableSection.tsx +530 -0
  56. package/src/admin/editor/panels/color-space.ts +195 -0
  57. package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
  58. package/src/admin/editor/panels/settings/BackgroundControls.tsx +418 -0
  59. package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
  60. package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
  61. package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
  62. package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
  63. package/src/admin/editor/panels/settings/ClassChipInput.tsx +451 -0
  64. package/src/admin/editor/panels/settings/DraftTextField.tsx +242 -0
  65. package/src/admin/editor/panels/settings/EffectsControls.tsx +354 -0
  66. package/src/admin/editor/panels/settings/FieldBindControl.tsx +119 -0
  67. package/src/admin/editor/panels/settings/FieldControl.tsx +270 -0
  68. package/src/admin/editor/panels/settings/FormBindControl.tsx +106 -0
  69. package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
  70. package/src/admin/editor/panels/settings/IconPicker.tsx +170 -0
  71. package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
  72. package/src/admin/editor/panels/settings/LengthRow.tsx +217 -0
  73. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +130 -0
  74. package/src/admin/editor/panels/settings/MediaPicker.tsx +220 -0
  75. package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
  76. package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
  77. package/src/admin/editor/panels/settings/StyleRow.tsx +563 -0
  78. package/src/admin/editor/panels/settings/VariableBinding.tsx +171 -0
  79. package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
  80. package/src/admin/editor/panels/settings/background-mode.ts +112 -0
  81. package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
  82. package/src/admin/editor/panels/settings/box-sides.ts +153 -0
  83. package/src/admin/editor/panels/settings/button-url.ts +27 -0
  84. package/src/admin/editor/panels/settings/class-suggest.ts +53 -0
  85. package/src/admin/editor/panels/settings/form-names.ts +38 -0
  86. package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
  87. package/src/admin/editor/panels/settings/image-alt.ts +25 -0
  88. package/src/admin/editor/panels/settings/length-units.ts +116 -0
  89. package/src/admin/editor/panels/settings/loop-context.ts +26 -0
  90. package/src/admin/editor/panels/settings/node-props.ts +11 -0
  91. package/src/admin/editor/panels/settings/style-sections.ts +394 -0
  92. package/src/admin/editor/panels/settings/svg-images.ts +14 -0
  93. package/src/admin/editor/panels/site-list.tsx +209 -0
  94. package/src/admin/editor/panels/variable-values.ts +73 -0
  95. package/src/admin/editor/restore-deleted.ts +80 -0
  96. package/src/admin/editor/section-templates.ts +50 -0
  97. package/src/admin/editor/shortcuts.ts +20 -0
  98. package/src/admin/editor/store.ts +240 -0
  99. package/src/admin/editor/useClipboardActions.ts +136 -0
  100. package/src/admin/editor/useEditorCommands.ts +203 -0
  101. package/src/admin/editor/useEditorData.ts +193 -0
  102. package/src/admin/editor/useEditorShortcuts.ts +180 -0
  103. package/src/admin/editor/useMediaQuery.ts +20 -0
  104. package/src/admin/editor/useNodeActions.ts +184 -0
  105. package/src/admin/editor/useSave.ts +242 -0
  106. package/src/admin/entry-status.ts +19 -0
  107. package/src/admin/forms-api.ts +73 -0
  108. package/src/admin/icon-uploads-api.ts +44 -0
  109. package/src/admin/index.tsx +20 -0
  110. package/src/admin/media-api.ts +68 -0
  111. package/src/admin/pages/CreateDialog.tsx +150 -0
  112. package/src/admin/pages/EditorPage.tsx +57 -0
  113. package/src/admin/pages/NewPageDialog.tsx +83 -0
  114. package/src/admin/pages/NewThemePartDialog.tsx +61 -0
  115. package/src/admin/pages/PageList.tsx +91 -0
  116. package/src/admin/pages/PagesPage.tsx +25 -0
  117. package/src/admin/pages/SetupShell.tsx +90 -0
  118. package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
  119. package/src/admin/pages/ThemePartList.tsx +221 -0
  120. package/src/admin/pages/list-kit.tsx +125 -0
  121. package/src/admin/setup/plan.ts +150 -0
  122. package/src/admin/setup/run.ts +61 -0
  123. package/src/admin/setup/spec.ts +107 -0
  124. package/src/admin/setup/useSetupStatus.ts +62 -0
  125. package/src/admin/theme-api.ts +233 -0
  126. package/src/admin/ui.ts +244 -0
  127. package/src/admin/widgets/LayoutField.tsx +67 -0
  128. package/src/astro/EmVBPage.astro +63 -0
  129. package/src/astro/env.d.ts +5 -0
  130. package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
  131. package/src/astro/floats/index.ts +1 -0
  132. package/src/astro/floats/runtime.ts +78 -0
  133. package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
  134. package/src/astro/forms/index.ts +1 -0
  135. package/src/astro/forms-definitions.ts +126 -0
  136. package/src/astro/index.ts +16 -0
  137. package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
  138. package/src/astro/menu/index.ts +1 -0
  139. package/src/astro/menu/runtime.ts +37 -0
  140. package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
  141. package/src/astro/popups/index.ts +1 -0
  142. package/src/astro/popups/runtime.ts +296 -0
  143. package/src/astro/render.ts +108 -0
  144. package/src/astro/resolve-theme.ts +515 -0
  145. package/src/astro/resolve.ts +65 -0
  146. package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
  147. package/src/astro/tabs/index.ts +1 -0
  148. package/src/astro/tabs/runtime.ts +116 -0
  149. package/src/astro/theme-context.ts +89 -0
  150. package/src/astro/theme-posts.ts +84 -0
  151. package/src/constants.ts +12 -0
  152. package/src/core/arrange.ts +448 -0
  153. package/src/core/clipboard.ts +504 -0
  154. package/src/core/css/generate.ts +236 -0
  155. package/src/core/css/scope.ts +88 -0
  156. package/src/core/design/cascade.ts +28 -0
  157. package/src/core/design/classes.ts +224 -0
  158. package/src/core/design/devices.ts +56 -0
  159. package/src/core/design/states.ts +49 -0
  160. package/src/core/design/transfer.ts +132 -0
  161. package/src/core/design/variables.ts +359 -0
  162. package/src/core/elements/definition.ts +12 -0
  163. package/src/core/elements/dynamic-elements.ts +225 -0
  164. package/src/core/elements/field-id.ts +6 -0
  165. package/src/core/elements/form-elements.ts +219 -0
  166. package/src/core/elements/index.ts +960 -0
  167. package/src/core/elements/menu.ts +190 -0
  168. package/src/core/elements/plain-text.ts +32 -0
  169. package/src/core/forms/binding.ts +61 -0
  170. package/src/core/forms/definition.ts +45 -0
  171. package/src/core/forms/success-layout.ts +63 -0
  172. package/src/core/icons/catalog.ts +273 -0
  173. package/src/core/index.ts +380 -0
  174. package/src/core/limits.ts +9 -0
  175. package/src/core/migrate/index.ts +126 -0
  176. package/src/core/nesting.ts +51 -0
  177. package/src/core/render/context.ts +21 -0
  178. package/src/core/render/dynamic-post.ts +175 -0
  179. package/src/core/render/extras.ts +98 -0
  180. package/src/core/render/form.ts +235 -0
  181. package/src/core/render/index.ts +372 -0
  182. package/src/core/render/loop.ts +43 -0
  183. package/src/core/render/pagination.ts +76 -0
  184. package/src/core/render/section.ts +37 -0
  185. package/src/core/render/tabs.ts +76 -0
  186. package/src/core/render/vnode.ts +87 -0
  187. package/src/core/sanitize/css.ts +771 -0
  188. package/src/core/sanitize/embed-url.ts +160 -0
  189. package/src/core/sanitize/escape.ts +10 -0
  190. package/src/core/sanitize/href.ts +6 -0
  191. package/src/core/sanitize/media-url.ts +7 -0
  192. package/src/core/sanitize/svg-upload.ts +203 -0
  193. package/src/core/sanitize/svg.ts +374 -0
  194. package/src/core/sanitize/url.ts +31 -0
  195. package/src/core/schema/descriptors.ts +37 -0
  196. package/src/core/schema/design.ts +93 -0
  197. package/src/core/schema/layout.ts +1008 -0
  198. package/src/core/schema/state-names.ts +4 -0
  199. package/src/core/schema/style.ts +336 -0
  200. package/src/core/stats.ts +18 -0
  201. package/src/core/tabs/presence.ts +25 -0
  202. package/src/core/theme/conditions.ts +324 -0
  203. package/src/core/theme/dynamic.ts +143 -0
  204. package/src/core/theme/float.ts +113 -0
  205. package/src/core/theme/pagination.ts +97 -0
  206. package/src/core/theme/parse-doc.ts +36 -0
  207. package/src/core/theme/part-types.ts +81 -0
  208. package/src/core/theme/popup-markup.ts +41 -0
  209. package/src/core/theme/popup-rules.ts +62 -0
  210. package/src/core/theme/triggers.ts +104 -0
  211. package/src/core/tree-ops.ts +139 -0
  212. package/src/core/validate.ts +185 -0
  213. package/src/index.ts +18 -0
  214. package/src/server/design-routes.ts +125 -0
  215. package/src/server/hooks.ts +118 -0
  216. package/src/server/icon-routes.ts +79 -0
  217. package/src/server/plugin.ts +43 -0
@@ -0,0 +1,199 @@
1
+ import { Button } from "@cloudflare/kumo";
2
+ import * as React from "react";
3
+ import {
4
+ ArrowDownIcon,
5
+ ArrowUpIcon,
6
+ PencilSimpleIcon,
7
+ PlusIcon,
8
+ TrashIcon,
9
+ } from "@phosphor-icons/react";
10
+ import { MAX_TABS, type FieldDescriptor, type LayoutNode } from "../../../../core/index.ts";
11
+ import { BUTTON } from "../../../ui.ts";
12
+ import { newAccordionItem, newTabPanel } from "../../dnd/new-element.ts";
13
+ import { DraftTextField } from "./DraftTextField.tsx";
14
+
15
+ /** Layout operations the list runs through the editor, so limits, history and Restore apply. */
16
+ export type ItemActions = {
17
+ add: (parentId: string, node: LayoutNode) => void;
18
+ /** Deletes with the editor's usual confirm and Restore toast. */
19
+ remove: (id: string) => void;
20
+ move: (id: string, direction: "up" | "down") => void;
21
+ };
22
+
23
+ type Kind = {
24
+ noun: string;
25
+ field: (n: number) => FieldDescriptor;
26
+ make: (n: number) => LayoutNode;
27
+ max?: number;
28
+ };
29
+
30
+ const KINDS: Record<string, Kind> = {
31
+ accordion: {
32
+ noun: "item",
33
+ field: (n) => ({ key: "summary", kind: "text", label: `Item ${n} title` }),
34
+ make: (n) => newAccordionItem(n),
35
+ },
36
+ tabs: {
37
+ noun: "tab",
38
+ field: (n) => ({ key: "label", kind: "text", label: `Tab ${n} label` }),
39
+ make: (n) => newTabPanel(n),
40
+ max: MAX_TABS,
41
+ },
42
+ };
43
+
44
+ /** True for the elements whose Content tab is a list of their items (W-130). */
45
+ export const hasItemList = (type: string) => type in KINDS;
46
+
47
+ const childrenOf = (node: LayoutNode): LayoutNode[] =>
48
+ "children" in node && Array.isArray(node.children) ? (node.children as LayoutNode[]) : [];
49
+
50
+ /** Fallback when no editor actions are wired (isolated panels): plain edits of the parent. */
51
+ function localActions(node: LayoutNode, onChange: (node: LayoutNode) => void): ItemActions {
52
+ const kids = childrenOf(node);
53
+ const set = (children: LayoutNode[]) => onChange({ ...node, children } as LayoutNode);
54
+ return {
55
+ add: (_parentId, child) => set([...kids, child]),
56
+ remove: (id) => set(kids.filter((child) => child.id !== id)),
57
+ move: (id, direction) => {
58
+ const from = kids.findIndex((child) => child.id === id);
59
+ const to = direction === "up" ? from - 1 : from + 1;
60
+ if (from < 0 || to < 0 || to >= kids.length) return;
61
+ const next = [...kids];
62
+ const [moved] = next.splice(from, 1);
63
+ if (moved) next.splice(to, 0, moved);
64
+ set(next);
65
+ },
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Accordion items or Tabs panels as a list: title, edit (select it), move, delete, and Add
71
+ * (W-130). Each item's content is its child elements, edited on the canvas.
72
+ */
73
+ export function ItemList({
74
+ node,
75
+ onChange,
76
+ onSelect,
77
+ actions,
78
+ }: {
79
+ node: LayoutNode;
80
+ onChange: (node: LayoutNode) => void;
81
+ onSelect: (id: string) => void;
82
+ actions?: ItemActions;
83
+ }) {
84
+ const kind = KINDS[node.type];
85
+ const kids = childrenOf(node);
86
+ // An added item's title field takes focus with its text selected, so typing names it (W-154).
87
+ const listRef = React.useRef<HTMLDivElement>(null);
88
+ const focusNew = React.useRef<string | null>(null);
89
+ const ids = kids.map((child) => child.id).join(" ");
90
+ React.useEffect(() => {
91
+ const id = focusNew.current;
92
+ if (!id || !ids.split(" ").includes(id)) return;
93
+ focusNew.current = null;
94
+ const field = listRef.current?.querySelector<HTMLInputElement>(
95
+ `[data-emvb-item="${id}"] input`,
96
+ );
97
+ field?.focus();
98
+ field?.select();
99
+ }, [ids]);
100
+ if (!kind) return null;
101
+ const run = actions ?? localActions(node, onChange);
102
+ const Noun = kind.noun === "tab" ? "Tab" : "Item";
103
+ const full = kind.max !== undefined && kids.length >= kind.max;
104
+ return (
105
+ <div className="emvb-item-list" data-emvb-item-list={node.type} ref={listRef}>
106
+ <h3 className="emvb-section-label">{kind.noun === "tab" ? "Tabs" : "Items"}</h3>
107
+ {kids.length === 0 ? (
108
+ <p className="emvb-helper" data-emvb-item-empty="">
109
+ {kind.noun === "tab"
110
+ ? "No tabs yet. Add a tab, then put elements in its panel on the canvas."
111
+ : "No items yet. Add an item, then put elements in its body on the canvas."}
112
+ </p>
113
+ ) : (
114
+ <ol className="emvb-trigger-list">
115
+ {kids.map((child, index) => {
116
+ const n = index + 1;
117
+ return (
118
+ <li key={child.id} className="emvb-item-row" data-emvb-item={child.id}>
119
+ <DraftTextField
120
+ field={kind.field(n)}
121
+ node={child}
122
+ onChange={(next) =>
123
+ onChange({
124
+ ...node,
125
+ children: kids.map((k) => (k.id === next.id ? next : k)),
126
+ } as LayoutNode)
127
+ }
128
+ />
129
+ <div className="emvb-item-actions">
130
+ <Button
131
+ type="button"
132
+ variant="ghost"
133
+ shape="square"
134
+ className={BUTTON}
135
+ aria-label={`Edit ${kind.noun} ${n}`}
136
+ title={`Select ${kind.noun} ${n} to edit its settings and content`}
137
+ icon={<PencilSimpleIcon aria-hidden="true" />}
138
+ onClick={() => onSelect(child.id)}
139
+ />
140
+ <Button
141
+ type="button"
142
+ variant="ghost"
143
+ shape="square"
144
+ className={BUTTON}
145
+ aria-label={`Move ${kind.noun} ${n} up`}
146
+ title={`Move ${kind.noun} ${n} up`}
147
+ disabled={index === 0}
148
+ icon={<ArrowUpIcon aria-hidden="true" />}
149
+ onClick={() => run.move(child.id, "up")}
150
+ />
151
+ <Button
152
+ type="button"
153
+ variant="ghost"
154
+ shape="square"
155
+ className={BUTTON}
156
+ aria-label={`Move ${kind.noun} ${n} down`}
157
+ title={`Move ${kind.noun} ${n} down`}
158
+ disabled={index === kids.length - 1}
159
+ icon={<ArrowDownIcon aria-hidden="true" />}
160
+ onClick={() => run.move(child.id, "down")}
161
+ />
162
+ <Button
163
+ type="button"
164
+ variant="ghost"
165
+ shape="square"
166
+ className={BUTTON}
167
+ aria-label={`Delete ${kind.noun} ${n}`}
168
+ title={`Delete ${kind.noun} ${n}`}
169
+ icon={<TrashIcon aria-hidden="true" />}
170
+ onClick={() => run.remove(child.id)}
171
+ />
172
+ </div>
173
+ </li>
174
+ );
175
+ })}
176
+ </ol>
177
+ )}
178
+ <Button
179
+ type="button"
180
+ variant="secondary"
181
+ className={BUTTON}
182
+ icon={<PlusIcon aria-hidden="true" />}
183
+ disabled={full}
184
+ onClick={() => {
185
+ const fresh = kind.make(kids.length + 1);
186
+ focusNew.current = fresh.id;
187
+ run.add(node.id, fresh);
188
+ }}
189
+ >
190
+ {`Add ${kind.noun}`}
191
+ </Button>
192
+ {full && (
193
+ <p className="emvb-helper">
194
+ {Noun}s can show up to {kind.max} {kind.noun}s.
195
+ </p>
196
+ )}
197
+ </div>
198
+ );
199
+ }
@@ -0,0 +1,217 @@
1
+ import { DropdownMenu, InputGroup } from "@cloudflare/kumo";
2
+ import { CaretDownIcon } from "@phosphor-icons/react";
3
+ import * as React from "react";
4
+ import type { DesignSystem, StyleProps } from "../../../../core/index.ts";
5
+ import { FIELD } from "../../../ui.ts";
6
+ import {
7
+ parseLengthDraft,
8
+ unitsFor,
9
+ type LengthLiteral,
10
+ type LengthUnit,
11
+ type UnitChoice,
12
+ } from "./length-units.ts";
13
+ import type { StyleKey } from "./style-sections.ts";
14
+ import { bindKind, boundRef, lengthRef, VariableButton, VariableChip } from "./VariableBinding.tsx";
15
+
16
+ const isLiteral = (value: unknown): value is LengthLiteral =>
17
+ typeof value === "object" &&
18
+ value !== null &&
19
+ typeof (value as { value?: unknown }).value === "number" &&
20
+ typeof (value as { unit?: unknown }).unit === "string";
21
+
22
+ const same = (a: unknown, b: unknown) =>
23
+ a === b || (isLiteral(a) && isLiteral(b) && a.value === b.value && a.unit === b.unit);
24
+
25
+ type NumericUnit = LengthUnit | "x";
26
+
27
+ const firstNumeric = (units: readonly UnitChoice[]): NumericUnit =>
28
+ (units.find((u) => u !== "auto") as NumericUnit | undefined) ?? "px";
29
+
30
+ /** A length property: number input, unit menu inside its right edge, variable button (W-088). */
31
+ export function LengthRow({
32
+ styleKey,
33
+ label,
34
+ value,
35
+ design,
36
+ set,
37
+ placeholder,
38
+ slider = false,
39
+ onPatch,
40
+ reset,
41
+ }: {
42
+ styleKey: StyleKey;
43
+ label: string;
44
+ value: unknown;
45
+ design: DesignSystem;
46
+ set: boolean;
47
+ /** The Normal value, shown while a state is edited (W-089). */
48
+ placeholder?: string;
49
+ /** Gap: a slider for the common range. The number field still accepts the full length. */
50
+ slider?: boolean;
51
+ onPatch: (patch: Partial<StyleProps>) => void;
52
+ reset: React.ReactNode;
53
+ }) {
54
+ const units = unitsFor(styleKey);
55
+ // W-213: a plain number is a unitless line height, shown with the `x` unit.
56
+ const literal: { value: number; unit: NumericUnit } | undefined = isLiteral(value)
57
+ ? value
58
+ : typeof value === "number"
59
+ ? { value, unit: "x" }
60
+ : undefined;
61
+ const numericUnit = literal?.unit ?? firstNumeric(units);
62
+ const [draft, setDraft] = React.useState(literal ? String(literal.value) : "");
63
+ const [unit, setUnit] = React.useState<UnitChoice>(value === "auto" ? "auto" : numericUnit);
64
+ const lastNumeric = React.useRef<NumericUnit>(numericUnit);
65
+ const [error, setError] = React.useState<string | null>(null);
66
+ React.useEffect(() => {
67
+ setDraft(literal ? String(literal.value) : "");
68
+ setUnit(value === "auto" ? "auto" : (literal?.unit ?? lastNumeric.current));
69
+ if (literal) lastNumeric.current = literal.unit;
70
+ setError(null);
71
+ }, [value, literal]);
72
+
73
+ const send = (next: LengthLiteral | "auto" | number | undefined) => {
74
+ if (!same(next, value)) onPatch({ [styleKey]: next });
75
+ };
76
+
77
+ const commit = () => {
78
+ const parsed = parseLengthDraft(draft, styleKey, unit === "auto" ? lastNumeric.current : unit);
79
+ if (!parsed.ok) {
80
+ setError(parsed.message);
81
+ return;
82
+ }
83
+ setError(null);
84
+ if (parsed.value === undefined && value === "auto") return;
85
+ send(parsed.value);
86
+ };
87
+
88
+ const pickUnit = (next: UnitChoice) => {
89
+ setError(null);
90
+ setUnit(next);
91
+ if (next === "auto") {
92
+ setDraft("");
93
+ send("auto");
94
+ return;
95
+ }
96
+ lastNumeric.current = next;
97
+ const parsed = parseLengthDraft(draft, styleKey, next);
98
+ if (parsed.ok && parsed.value !== undefined && parsed.value !== "auto") {
99
+ const n = typeof parsed.value === "number" ? parsed.value : parsed.value.value;
100
+ if (next === "x") {
101
+ if (n <= 100) send(n);
102
+ else setError("Line height without a unit can be from 0 to 100.");
103
+ } else send({ value: n, unit: next });
104
+ }
105
+ };
106
+
107
+ const kind = bindKind(styleKey);
108
+ const chip = lengthRef(value);
109
+ const current = kind ? boundRef(value, kind) : null;
110
+ return (
111
+ <div
112
+ className="emvb-style-row"
113
+ data-emvb-style={styleKey}
114
+ data-set={set ? "true" : undefined}
115
+ data-inherited={!set && placeholder !== undefined ? "true" : undefined}
116
+ data-unit={chip ? undefined : unit}
117
+ >
118
+ {chip ? (
119
+ <VariableChip
120
+ label={label}
121
+ kind={chip.kind}
122
+ design={design}
123
+ id={chip.var}
124
+ onDetach={(kept) => onPatch({ [styleKey]: typeof kept === "object" ? kept : undefined })}
125
+ />
126
+ ) : (
127
+ <div className={slider ? "emvb-length-stack" : undefined}>
128
+ <InputGroup
129
+ label={label}
130
+ className={`${FIELD} emvb-length`}
131
+ error={error ? { message: error, match: true } : undefined}
132
+ >
133
+ <InputGroup.Input
134
+ className="emvb-mono"
135
+ aria-label={label}
136
+ inputMode="decimal"
137
+ value={draft}
138
+ placeholder={unit === "auto" ? "auto" : placeholder}
139
+ title={`A number in ${unit === "auto" ? lastNumeric.current : unit}, or with its own unit (2rem, 50%)${
140
+ units.includes("auto") ? ", or auto" : ""
141
+ }`}
142
+ aria-invalid={error ? true : undefined}
143
+ onChange={(event) => {
144
+ if (unit === "auto") setUnit(lastNumeric.current);
145
+ setDraft(event.target.value);
146
+ }}
147
+ onBlur={commit}
148
+ onKeyDown={(event) => {
149
+ if (event.key === "Enter") commit();
150
+ }}
151
+ />
152
+ <InputGroup.Addon align="end" className="emvb-unit-addon">
153
+ <DropdownMenu>
154
+ <DropdownMenu.Trigger>
155
+ <button
156
+ type="button"
157
+ className="emvb-unit-btn"
158
+ aria-label={`${label} unit (${unit})`}
159
+ data-emvb-unit={unit}
160
+ >
161
+ {unit === "x" ? "×" : unit}
162
+ <CaretDownIcon size={12} aria-hidden="true" />
163
+ </button>
164
+ </DropdownMenu.Trigger>
165
+ <DropdownMenu.Content data-emvb-unit-menu={styleKey}>
166
+ <DropdownMenu.RadioGroup
167
+ aria-label={`${label} unit`}
168
+ value={unit}
169
+ onValueChange={(next: UnitChoice) => pickUnit(next)}
170
+ >
171
+ {units.map((choice) => (
172
+ <DropdownMenu.RadioItem
173
+ key={choice}
174
+ value={choice}
175
+ closeOnClick
176
+ data-emvb-unit-option={choice}
177
+ >
178
+ <span className="emvb-mono">{choice === "x" ? "× (no unit)" : choice}</span>
179
+ <DropdownMenu.RadioItemIndicator />
180
+ </DropdownMenu.RadioItem>
181
+ ))}
182
+ </DropdownMenu.RadioGroup>
183
+ </DropdownMenu.Content>
184
+ </DropdownMenu>
185
+ </InputGroup.Addon>
186
+ </InputGroup>
187
+ {slider && unit !== "auto" && (
188
+ <input
189
+ className="emvb-gap-slider"
190
+ type="range"
191
+ min={0}
192
+ max={200}
193
+ step={1}
194
+ aria-label={`${label} slider`}
195
+ value={Math.min(200, literal?.value ?? 0)}
196
+ onChange={(event) => {
197
+ const next = Number(event.target.value);
198
+ setDraft(String(next));
199
+ send(unit === "x" ? next : { value: next, unit });
200
+ }}
201
+ />
202
+ )}
203
+ </div>
204
+ )}
205
+ {kind && (
206
+ <VariableButton
207
+ label={label}
208
+ kind={kind}
209
+ design={design}
210
+ current={current?.var ?? null}
211
+ onBind={(next) => onPatch({ [styleKey]: next })}
212
+ />
213
+ )}
214
+ {reset}
215
+ </div>
216
+ );
217
+ }
@@ -0,0 +1,130 @@
1
+ import { Input, Select } from "@cloudflare/kumo";
2
+ import * as React from "react";
3
+ import type { ThemePartType } from "../../../../core/index.ts";
4
+ import type { Fetcher } from "../../../api.ts";
5
+ import { listThemeParts, type ThemePartSummary } from "../../../theme-api.ts";
6
+ import { FIELD } from "../../../ui.ts";
7
+ import { useAdminLinks } from "../../host.ts";
8
+
9
+ const MANUAL = "__manual__";
10
+ const NONE = "__none__";
11
+
12
+ /**
13
+ * Loop Item theme-part binder (W-077): pick a published/draft Loop Item by title,
14
+ * keep storing the part id string for render compatibility.
15
+ */
16
+ export function LoopItemBindControl({
17
+ value,
18
+ fetcher,
19
+ onChange,
20
+ partType = "loop_item",
21
+ label = "Loop Item",
22
+ emptyLabel = "Use nested elements",
23
+ marker = "loop-item",
24
+ }: {
25
+ value: string;
26
+ fetcher: Fetcher;
27
+ onChange: (itemPartId: string) => void;
28
+ partType?: ThemePartType;
29
+ label?: string;
30
+ emptyLabel?: string;
31
+ /** `data-emvb-${marker}-bind` keeps the loop-item tests stable. */
32
+ marker?: string;
33
+ }) {
34
+ const [parts, setParts] = React.useState<ThemePartSummary[] | null>(null);
35
+ const [failed, setFailed] = React.useState(false);
36
+ const adminLinks = useAdminLinks();
37
+
38
+ React.useEffect(() => {
39
+ let cancelled = false;
40
+ void listThemeParts(fetcher)
41
+ .then((all) => {
42
+ if (cancelled) return;
43
+ setParts(all.filter((part) => part.partType === partType));
44
+ setFailed(false);
45
+ })
46
+ .catch(() => {
47
+ if (!cancelled) {
48
+ setParts(null);
49
+ setFailed(true);
50
+ }
51
+ });
52
+ return () => {
53
+ cancelled = true;
54
+ };
55
+ }, [fetcher, partType]);
56
+
57
+ if (parts && parts.length > 0) {
58
+ const known = parts.some((part) => part.id === value);
59
+ const selectValue = value === "" ? NONE : known ? value : MANUAL;
60
+ return (
61
+ <div {...{ [`data-emvb-${marker}-bind`]: "list" }}>
62
+ <Select
63
+ label={label}
64
+ className={FIELD}
65
+ value={selectValue}
66
+ onValueChange={(next) => {
67
+ if (!next || next === MANUAL) return;
68
+ if (next === NONE) {
69
+ onChange("");
70
+ return;
71
+ }
72
+ onChange(next);
73
+ }}
74
+ renderValue={(current: unknown) => {
75
+ if (current === NONE || current === "") return emptyLabel;
76
+ if (current === MANUAL) return "Current id (not in list)";
77
+ const match = parts.find((part) => part.id === String(current));
78
+ return match ? labelFor(match) : String(current);
79
+ }}
80
+ >
81
+ <Select.Option value={NONE}>{emptyLabel}</Select.Option>
82
+ {!known && value ? (
83
+ <Select.Option value={MANUAL}>Current id (not in list)</Select.Option>
84
+ ) : null}
85
+ {parts.map((part) => (
86
+ <Select.Option key={part.id} value={part.id}>
87
+ {labelFor(part)}
88
+ </Select.Option>
89
+ ))}
90
+ </Select>
91
+ <Input
92
+ label={`${label} part id`}
93
+ className={`${FIELD} emvb-mono`}
94
+ value={value}
95
+ onChange={(event) => onChange(event.target.value.trim())}
96
+ />
97
+ <p className="emvb-helper">
98
+ Optional published {label} theme part. Leave blank to use nested elements.
99
+ {adminLinks && <a href="/_emdash/admin/plugins/emvb/theme"> Open Theme Builder</a>}
100
+ </p>
101
+ </div>
102
+ );
103
+ }
104
+
105
+ return (
106
+ <div {...{ [`data-emvb-${marker}-bind`]: "manual" }}>
107
+ <Input
108
+ label={`${label} part id`}
109
+ className={`${FIELD} emvb-mono`}
110
+ value={value}
111
+ onChange={(event) => onChange(event.target.value.trim())}
112
+ />
113
+ <p className="emvb-helper">
114
+ {failed
115
+ ? `Could not load ${label} parts. Paste a theme-part id${adminLinks ? ", or " : "."}`
116
+ : `No ${label} theme parts yet. Paste an id${adminLinks ? ", or " : "."}`}
117
+ {adminLinks && (
118
+ <>
119
+ <a href="/_emdash/admin/plugins/emvb/theme">create one in Theme Builder</a>.
120
+ </>
121
+ )}
122
+ </p>
123
+ </div>
124
+ );
125
+ }
126
+
127
+ function labelFor(part: ThemePartSummary): string {
128
+ const status = part.status === "published" ? "" : ` (${part.status})`;
129
+ return `${part.title}${status}`;
130
+ }