@enablo/swirlcn-ui 0.0.0-stage → 0.5.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 (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -2
  3. package/dist/chunk-3DJWDNQL.js +77 -0
  4. package/dist/chunk-7XUCETA6.js +447 -0
  5. package/dist/chunk-KZESIYUN.js +18 -0
  6. package/dist/flip.d.ts +98 -0
  7. package/dist/flip.js +158 -0
  8. package/dist/fonts/Inter-italic.var.woff2 +0 -0
  9. package/dist/fonts/Inter.var.woff2 +0 -0
  10. package/dist/fonts/OFL.txt +96 -0
  11. package/dist/index.d.ts +2459 -0
  12. package/dist/index.js +8439 -0
  13. package/dist/server.d.ts +144 -0
  14. package/dist/server.js +88 -0
  15. package/dist/spacing-BQaEPbUv.d.ts +59 -0
  16. package/dist/theme.css +146 -0
  17. package/package.json +84 -3
  18. package/src/components/accordion.tsx +102 -0
  19. package/src/components/action-list.tsx +294 -0
  20. package/src/components/alert-dialog.tsx +155 -0
  21. package/src/components/autocomplete.tsx +313 -0
  22. package/src/components/avatar.tsx +258 -0
  23. package/src/components/badge.tsx +100 -0
  24. package/src/components/banner.tsx +112 -0
  25. package/src/components/button-group.tsx +87 -0
  26. package/src/components/button.tsx +362 -0
  27. package/src/components/card.tsx +186 -0
  28. package/src/components/carousel.tsx +245 -0
  29. package/src/components/checkbox.tsx +125 -0
  30. package/src/components/chip.tsx +153 -0
  31. package/src/components/color-input.tsx +98 -0
  32. package/src/components/data-cell.tsx +230 -0
  33. package/src/components/date-input.tsx +223 -0
  34. package/src/components/date-picker.tsx +276 -0
  35. package/src/components/description-list.tsx +97 -0
  36. package/src/components/dialog.tsx +405 -0
  37. package/src/components/empty-state.tsx +63 -0
  38. package/src/components/file-chip.tsx +206 -0
  39. package/src/components/file-uploader.tsx +148 -0
  40. package/src/components/flip-app-header.tsx +104 -0
  41. package/src/components/flip-brand.tsx +77 -0
  42. package/src/components/flip-theme.tsx +95 -0
  43. package/src/components/form-control.tsx +397 -0
  44. package/src/components/form-group.tsx +26 -0
  45. package/src/components/heading.tsx +70 -0
  46. package/src/components/icon.tsx +119 -0
  47. package/src/components/image-grid.tsx +285 -0
  48. package/src/components/inline-notification.tsx +167 -0
  49. package/src/components/inline-status.tsx +100 -0
  50. package/src/components/input.tsx +390 -0
  51. package/src/components/label.tsx +28 -0
  52. package/src/components/lightbox.tsx +314 -0
  53. package/src/components/link.tsx +46 -0
  54. package/src/components/list.tsx +40 -0
  55. package/src/components/menu.tsx +530 -0
  56. package/src/components/option-list.tsx +582 -0
  57. package/src/components/pagination.tsx +336 -0
  58. package/src/components/popover.tsx +194 -0
  59. package/src/components/progress.tsx +158 -0
  60. package/src/components/radio-group.tsx +136 -0
  61. package/src/components/resource-list.tsx +686 -0
  62. package/src/components/search.tsx +127 -0
  63. package/src/components/select.tsx +456 -0
  64. package/src/components/separator.tsx +93 -0
  65. package/src/components/skeleton-box.tsx +63 -0
  66. package/src/components/skeleton-text.tsx +66 -0
  67. package/src/components/spinner.tsx +83 -0
  68. package/src/components/stack.tsx +86 -0
  69. package/src/components/status-indicator.tsx +31 -0
  70. package/src/components/switch.tsx +90 -0
  71. package/src/components/table.tsx +242 -0
  72. package/src/components/tabs.tsx +147 -0
  73. package/src/components/tag.tsx +169 -0
  74. package/src/components/text.tsx +126 -0
  75. package/src/components/textarea.tsx +130 -0
  76. package/src/components/theme-provider.tsx +115 -0
  77. package/src/components/thumbnail.tsx +250 -0
  78. package/src/components/time-input.tsx +123 -0
  79. package/src/components/toast.tsx +217 -0
  80. package/src/components/toggle-group.tsx +124 -0
  81. package/src/components/toolbar.tsx +105 -0
  82. package/src/components/tooltip.tsx +97 -0
  83. package/src/components/tree-navigation.tsx +194 -0
  84. package/src/components/tree-view.tsx +292 -0
  85. package/src/flip.ts +12 -0
  86. package/src/index.ts +317 -0
  87. package/src/lib/date-mask.ts +176 -0
  88. package/src/lib/field-context.ts +86 -0
  89. package/src/lib/flip-app-script.ts +38 -0
  90. package/src/lib/flip-host.ts +84 -0
  91. package/src/lib/flip-theme-css.ts +90 -0
  92. package/src/lib/focus.ts +18 -0
  93. package/src/lib/glyphs.ts +137 -0
  94. package/src/lib/spacing.ts +93 -0
  95. package/src/lib/theme-context.ts +27 -0
  96. package/src/lib/theme-script.ts +49 -0
  97. package/src/lib/use-mobile.ts +17 -0
  98. package/src/lib/utils.ts +28 -0
  99. package/src/server.ts +20 -0
  100. package/src/styles/fonts/Inter-italic.var.woff2 +0 -0
  101. package/src/styles/fonts/Inter.var.woff2 +0 -0
  102. package/src/styles/fonts/OFL.txt +96 -0
  103. package/src/styles/theme.css +146 -0
@@ -0,0 +1,530 @@
1
+ import * as React from "react";
2
+ import { Dialog as DialogPrimitive, DropdownMenu as MenuPrimitive } from "radix-ui";
3
+ import { cn } from "../lib/utils";
4
+ import { useMobile } from "../lib/use-mobile";
5
+ import { ActionList, ActionListItem } from "./action-list";
6
+ import { Button } from "./button";
7
+ import { Heading } from "./heading";
8
+
9
+ /* ---------------------------------------------------------------- icons */
10
+
11
+ const glyph = (d: string) => (
12
+ <svg viewBox="0 0 24 24" aria-hidden focusable={false}>
13
+ <path d={d} fill="currentColor" />
14
+ </svg>
15
+ );
16
+ const CheckIcon = () => glyph("M18.6213 7.37879C19.2071 7.96458 19.2071 8.91433 18.6213 9.50011L10.6213 17.5C10.0355 18.0858 9.08577 18.0858 8.49998 17.5L5.56068 14.5605C4.97489 13.9748 4.97489 13.025 5.56068 12.4392C6.14646 11.8534 7.09621 11.8534 7.682 12.4392L9.56064 14.318L16.5 7.37879C17.0858 6.79301 18.0355 6.79301 18.6213 7.37879Z");
17
+ const ChevronRightIcon = () => glyph("M8.59999 17.4C8.38333 17.1834 8.27499 16.9167 8.27499 16.6C8.27499 16.2834 8.38333 16.0167 8.59999 15.8L12.4 12L8.59999 8.20002C8.38333 7.98336 8.27499 7.71669 8.27499 7.40002C8.27499 7.08336 8.38333 6.81669 8.59999 6.60002C8.81666 6.38336 9.08333 6.27502 9.39999 6.27502C9.71666 6.27502 9.98333 6.38336 10.2 6.60002L14.8 11.2C14.9167 11.3167 15 11.4417 15.05 11.575C15.1 11.7084 15.125 11.85 15.125 12C15.125 12.15 15.1 12.2917 15.05 12.425C15 12.5584 14.9167 12.6834 14.8 12.8L10.2 17.4C9.98333 17.6167 9.71666 17.725 9.39999 17.725C9.08333 17.725 8.81666 17.6167 8.59999 17.4Z");
18
+ const ChevronLeftIcon = () => glyph("M13.2 17.4L8.59999 12.8C8.48333 12.6834 8.39999 12.5584 8.34999 12.425C8.29999 12.2917 8.27499 12.15 8.27499 12C8.27499 11.85 8.29999 11.7084 8.34999 11.575C8.39999 11.4417 8.48333 11.3167 8.59999 11.2L13.2 6.60002C13.4167 6.38336 13.6833 6.27502 14 6.27502C14.3167 6.27502 14.5833 6.38336 14.8 6.60002C15.0167 6.81669 15.125 7.08336 15.125 7.40002C15.125 7.71669 15.0167 7.98336 14.8 8.20002L11 12L14.8 15.8C15.0167 16.0167 15.125 16.2834 15.125 16.6C15.125 16.9167 15.0167 17.1834 14.8 17.4C14.5833 17.6167 14.3167 17.725 14 17.725C13.6833 17.725 13.4167 17.6167 13.2 17.4Z");
19
+ const CloseIcon = () => glyph("M12 13.6L7.19999 18.4C6.98333 18.6167 6.71666 18.725 6.39999 18.725C6.08333 18.725 5.81666 18.6167 5.59999 18.4C5.38333 18.1834 5.27499 17.9167 5.27499 17.6C5.27499 17.2834 5.38333 17.0167 5.59999 16.8L10.4 12L5.59999 7.20002C5.38333 6.98336 5.27499 6.71669 5.27499 6.40002C5.27499 6.08336 5.38333 5.81669 5.59999 5.60002C5.81666 5.38336 6.08333 5.27502 6.39999 5.27502C6.71666 5.27502 6.98333 5.38336 7.19999 5.60002L12 10.4L16.8 5.60002C17.0167 5.38336 17.2833 5.27502 17.6 5.27502C17.9167 5.27502 18.1833 5.38336 18.4 5.60002C18.6167 5.81669 18.725 6.08336 18.725 6.40002C18.725 6.71669 18.6167 6.98336 18.4 7.20002L13.6 12L18.4 16.8C18.6167 17.0167 18.725 17.2834 18.725 17.6C18.725 17.9167 18.6167 18.1834 18.4 18.4C18.1833 18.6167 17.9167 18.725 17.6 18.725C17.2833 18.725 17.0167 18.6167 16.8 18.4L12 13.6Z");
20
+
21
+ /* ---------------------------------------------------------------- types */
22
+
23
+ type Side = "top" | "right" | "bottom" | "left";
24
+ export type MenuPlacement = Side | `${Side}-start` | `${Side}-end`;
25
+
26
+ export interface MenuProps {
27
+ /** Names the menu for assistive tech, and titles it on mobile. */
28
+ label: string;
29
+ /**
30
+ * The element that opens the menu, such as a `Button`. Only the top-level
31
+ * menu takes one; a `Menu` nested in a `MenuItem` is that item's submenu.
32
+ */
33
+ trigger?: React.ReactElement;
34
+ /**
35
+ * `action` items run something. `selection` items pick one value, shown
36
+ * with a check, and the parent item shows the chosen label.
37
+ */
38
+ variant?: "action" | "selection";
39
+ /** The chosen value, for a `selection` menu. */
40
+ value?: string;
41
+ defaultValue?: string;
42
+ onValueChange?: (value: string) => void;
43
+ /** Where the menu opens relative to its trigger, on desktop. */
44
+ placement?: MenuPlacement;
45
+ open?: boolean;
46
+ defaultOpen?: boolean;
47
+ onOpenChange?: (open: boolean) => void;
48
+ /** Called when the mobile sheet's Done button is pressed. */
49
+ onDone?: () => void;
50
+ mobileBackButtonLabel?: string;
51
+ mobileCloseMenuButtonLabel?: string;
52
+ mobileDoneButtonLabel?: string;
53
+ children?: React.ReactNode;
54
+ }
55
+
56
+ export interface MenuItemProps {
57
+ label: string;
58
+ description?: string;
59
+ icon?: React.ReactNode;
60
+ /** Small marker over the icon's corner, such as a dot `Badge`. */
61
+ iconBadge?: React.ReactNode;
62
+ avatar?: React.ReactNode;
63
+ /** Trailing icon or `Tag`. */
64
+ suffix?: React.ReactNode;
65
+ intent?: "default" | "critical";
66
+ disabled?: boolean;
67
+ truncateLabel?: boolean;
68
+ /** This item's value, in a `selection` menu. */
69
+ value?: string;
70
+ /** Runs when the item is chosen, in an `action` menu. The menu then closes. */
71
+ onSelect?: () => void;
72
+ /** A nested `Menu`, making this item open a submenu. */
73
+ children?: React.ReactNode;
74
+ }
75
+
76
+ /* ----------------------------------------------------------- the tree */
77
+
78
+ /*
79
+ * Items are read from the children up front, so desktop and mobile render the
80
+ * same tree in their own way. Put MenuItems directly inside a Menu (fragments
81
+ * are fine); a MenuItem wrapped in another component will not be found.
82
+ */
83
+ interface MenuNode {
84
+ id: string;
85
+ props: MenuProps;
86
+ items: ItemNode[];
87
+ }
88
+ interface ItemNode {
89
+ id: string;
90
+ props: MenuItemProps;
91
+ submenu?: MenuNode;
92
+ }
93
+
94
+ function collect(children: React.ReactNode, type: unknown): React.ReactElement[] {
95
+ const found: React.ReactElement[] = [];
96
+ React.Children.forEach(children, (child) => {
97
+ if (!React.isValidElement(child)) return;
98
+ if (child.type === React.Fragment) {
99
+ found.push(...collect((child.props as { children?: React.ReactNode }).children, type));
100
+ } else if (child.type === type) {
101
+ found.push(child);
102
+ }
103
+ });
104
+ return found;
105
+ }
106
+
107
+ function parseMenu(props: MenuProps, id: string): MenuNode {
108
+ return {
109
+ id,
110
+ props,
111
+ items: collect(props.children, MenuItem).map((element, index) => {
112
+ const itemProps = element.props as MenuItemProps;
113
+ const itemId = `${id}.${index}`;
114
+ const [sub] = collect(itemProps.children, Menu);
115
+ return {
116
+ id: itemId,
117
+ props: itemProps,
118
+ submenu: sub ? parseMenu(sub.props as MenuProps, `${itemId}.menu`) : undefined,
119
+ };
120
+ }),
121
+ };
122
+ }
123
+
124
+ /* ------------------------------------------------------------- state */
125
+
126
+ /** Values of uncontrolled selection menus, kept at the root so a parent item can show them. */
127
+ function useSelections(root: MenuNode) {
128
+ const [values, setValues] = React.useState<Record<string, string | undefined>>({});
129
+ const valueOf = (menu: MenuNode) =>
130
+ menu.props.value ?? (menu.id in values ? values[menu.id] : menu.props.defaultValue);
131
+ const select = (menu: MenuNode, value: string) => {
132
+ menu.props.onValueChange?.(value);
133
+ if (menu.props.value === undefined) setValues((v) => ({ ...v, [menu.id]: value }));
134
+ };
135
+ /** The chosen option's label, shown on the item that opens a selection menu. */
136
+ const chosenLabel = (menu?: MenuNode) => {
137
+ if (!menu || menu.props.variant !== "selection") return undefined;
138
+ const value = valueOf(menu);
139
+ return menu.items.find((item) => item.props.value === value)?.props.label;
140
+ };
141
+ return { valueOf, select, chosenLabel, root };
142
+ }
143
+
144
+ type Selections = ReturnType<typeof useSelections>;
145
+
146
+ function useOpen(props: MenuProps) {
147
+ const [uncontrolled, setUncontrolled] = React.useState(props.defaultOpen ?? false);
148
+ const open = props.open ?? uncontrolled;
149
+ const setOpen = (next: boolean) => {
150
+ props.onOpenChange?.(next);
151
+ if (props.open === undefined) setUncontrolled(next);
152
+ };
153
+ return [open, setOpen] as const;
154
+ }
155
+
156
+ /* ------------------------------------------------------------ desktop */
157
+
158
+ const subSurface = [
159
+ "relative z-50 max-w-[22.5rem] rounded-base p-1 shadow-level-2",
160
+ "outline-1 outline-border-translucent",
161
+ "before:absolute before:inset-0 before:-z-10 before:rounded-[inherit] before:bg-translucent-low before:backdrop-blur-l",
162
+ ];
163
+
164
+ function DesktopItems({ menu, selections }: { menu: MenuNode; selections: Selections }) {
165
+ // One submenu open per level, as in Swirl: opening another closes the last.
166
+ const [openSub, setOpenSub] = React.useState<string | null>(null);
167
+ const subContents = React.useRef(new Map<string, HTMLElement>());
168
+
169
+ // Swirl moves focus into a submenu however it was opened, so ArrowLeft can
170
+ // close it straight away. Radix only does this when opened by keyboard, and
171
+ // its SubContent does not let a caller's onOpenAutoFocus run.
172
+ React.useEffect(() => {
173
+ if (!openSub) return;
174
+ const frame = requestAnimationFrame(() => {
175
+ subContents.current
176
+ .get(openSub)
177
+ ?.querySelector<HTMLElement>('[role^="menuitem"]:not([data-disabled])')
178
+ ?.focus();
179
+ });
180
+ return () => cancelAnimationFrame(frame);
181
+ }, [openSub]);
182
+
183
+ const items = menu.items.map(({ id, props, submenu }) => {
184
+ const shared = {
185
+ label: props.label,
186
+ description: props.description,
187
+ icon: props.icon,
188
+ iconBadge: props.iconBadge,
189
+ avatar: props.avatar,
190
+ intent: props.intent,
191
+ disabled: props.disabled,
192
+ truncateLabel: props.truncateLabel,
193
+ };
194
+
195
+ if (submenu) {
196
+ return (
197
+ <MenuPrimitive.Sub
198
+ key={id}
199
+ open={openSub === id}
200
+ onOpenChange={(open) => setOpenSub(open ? id : null)}
201
+ >
202
+ <MenuPrimitive.SubTrigger
203
+ asChild
204
+ disabled={props.disabled}
205
+ // Swirl opens submenus on click or ArrowRight, never on hover.
206
+ onPointerMove={(event) => event.preventDefault()}
207
+ onPointerLeave={(event) => event.preventDefault()}
208
+ >
209
+ <ActionListItem
210
+ {...shared}
211
+ badge={selections.chosenLabel(submenu)}
212
+ suffix={props.suffix ?? <ChevronRightIcon />}
213
+ className="data-[state=open]:bg-state-hovered"
214
+ />
215
+ </MenuPrimitive.SubTrigger>
216
+ <MenuPrimitive.Portal>
217
+ <MenuPrimitive.SubContent
218
+ aria-label={submenu.props.label}
219
+ ref={(el) => {
220
+ if (el) subContents.current.set(id, el);
221
+ else subContents.current.delete(id);
222
+ }}
223
+ sideOffset={-10}
224
+ collisionPadding={8}
225
+ className={cn(subSurface)}
226
+ >
227
+ <DesktopItems menu={submenu} selections={selections} />
228
+ </MenuPrimitive.SubContent>
229
+ </MenuPrimitive.Portal>
230
+ </MenuPrimitive.Sub>
231
+ );
232
+ }
233
+
234
+ if (menu.props.variant === "selection") {
235
+ const selected = selections.valueOf(menu) === props.value;
236
+ return (
237
+ <MenuPrimitive.RadioItem
238
+ key={id}
239
+ asChild
240
+ value={props.value ?? id}
241
+ disabled={props.disabled}
242
+ // Choosing keeps the menu open, so the choice can be seen and changed.
243
+ onSelect={(event) => event.preventDefault()}
244
+ >
245
+ <ActionListItem {...shared} selected={selected} suffix={selected ? <CheckIcon /> : props.suffix} />
246
+ </MenuPrimitive.RadioItem>
247
+ );
248
+ }
249
+
250
+ return (
251
+ <MenuPrimitive.Item key={id} asChild disabled={props.disabled} onSelect={() => props.onSelect?.()}>
252
+ <ActionListItem {...shared} suffix={props.suffix} />
253
+ </MenuPrimitive.Item>
254
+ );
255
+ });
256
+
257
+ // A column, as ActionList is: the items are inline-flex, and in a plain
258
+ // block the panel would size itself to all of them laid end to end.
259
+ if (menu.props.variant !== "selection") return <div className="flex flex-col">{items}</div>;
260
+ return (
261
+ <MenuPrimitive.RadioGroup
262
+ className="flex flex-col"
263
+ value={selections.valueOf(menu) ?? ""}
264
+ onValueChange={(value) => selections.select(menu, value)}
265
+ >
266
+ {items}
267
+ </MenuPrimitive.RadioGroup>
268
+ );
269
+ }
270
+
271
+ function DesktopMenu({ root, selections }: { root: MenuNode; selections: Selections }) {
272
+ const [open, setOpen] = useOpen(root.props);
273
+ const [side, align = "start"] = (root.props.placement ?? "bottom-start").split("-") as [
274
+ Side,
275
+ "start" | "end" | undefined,
276
+ ];
277
+
278
+ return (
279
+ <MenuPrimitive.Root open={open} onOpenChange={setOpen}>
280
+ {root.props.trigger && <MenuPrimitive.Trigger asChild>{root.props.trigger}</MenuPrimitive.Trigger>}
281
+ <MenuPrimitive.Portal>
282
+ <MenuPrimitive.Content
283
+ aria-label={root.props.label}
284
+ side={side}
285
+ align={align}
286
+ sideOffset={8}
287
+ collisionPadding={8}
288
+ className={cn(
289
+ "z-50 max-w-[22.5rem] overflow-hidden rounded-base bg-surface-overlay shadow-level-2",
290
+ "origin-(--radix-dropdown-menu-content-transform-origin) duration-150",
291
+ "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-0",
292
+ "data-[state=closed]:animate-out data-[state=closed]:fade-out-0",
293
+ )}
294
+ >
295
+ <div className="max-h-[22rem] overflow-x-hidden overflow-y-auto overscroll-contain p-1">
296
+ <DesktopItems menu={root} selections={selections} />
297
+ </div>
298
+ </MenuPrimitive.Content>
299
+ </MenuPrimitive.Portal>
300
+ </MenuPrimitive.Root>
301
+ );
302
+ }
303
+
304
+ /* ------------------------------------------------------------- mobile */
305
+
306
+ const SLIDE_MS = 200;
307
+
308
+ function MobileMenu({ root, selections }: { root: MenuNode; selections: Selections }) {
309
+ const [open, setOpen] = useOpen(root.props);
310
+ // The submenus drilled into, and how deep the view currently is. Panels
311
+ // stay in the stack until the slide back has finished.
312
+ const [stack, setStack] = React.useState<MenuNode[]>([root]);
313
+ const [depth, setDepth] = React.useState(0);
314
+ const panels = React.useRef(new Map<string, HTMLElement>());
315
+ const returnTo = React.useRef<string[]>([]);
316
+ const titleId = React.useId();
317
+
318
+ const focusIn = (menuId: string, itemId?: string) => {
319
+ const panel = panels.current.get(menuId);
320
+ const target = itemId
321
+ ? panel?.querySelector<HTMLElement>(`[data-item="${itemId}"]`)
322
+ : panel?.querySelector<HTMLElement>('[role^="menuitem"]:not([disabled])');
323
+ target?.focus();
324
+ };
325
+
326
+ const reset = () => {
327
+ window.setTimeout(() => {
328
+ setStack([root]);
329
+ setDepth(0);
330
+ returnTo.current = [];
331
+ }, SLIDE_MS);
332
+ };
333
+
334
+ const close = () => {
335
+ setOpen(false);
336
+ reset();
337
+ };
338
+
339
+ const drillInto = (item: ItemNode) => {
340
+ if (!item.submenu) return;
341
+ returnTo.current.push(item.id);
342
+ setStack((s) => [...s.slice(0, depth + 1), item.submenu!]);
343
+ // Mount off-screen first, then slide.
344
+ requestAnimationFrame(() => setDepth((d) => d + 1));
345
+ window.setTimeout(() => focusIn(item.submenu!.id), SLIDE_MS);
346
+ };
347
+
348
+ const goBack = () => {
349
+ if (depth === 0) return;
350
+ const parent = stack[depth - 1]!;
351
+ const itemId = returnTo.current.pop();
352
+ setDepth(depth - 1);
353
+ window.setTimeout(() => {
354
+ setStack((s) => s.slice(0, depth));
355
+ focusIn(parent.id, itemId);
356
+ }, SLIDE_MS);
357
+ };
358
+
359
+ return (
360
+ <DialogPrimitive.Root
361
+ open={open}
362
+ onOpenChange={(next) => {
363
+ setOpen(next);
364
+ if (!next) reset();
365
+ }}
366
+ >
367
+ {root.props.trigger && <DialogPrimitive.Trigger asChild>{root.props.trigger}</DialogPrimitive.Trigger>}
368
+ <DialogPrimitive.Portal>
369
+ <DialogPrimitive.Content
370
+ aria-labelledby={titleId}
371
+ aria-describedby={undefined}
372
+ onOpenAutoFocus={(event) => {
373
+ event.preventDefault();
374
+ focusIn(root.id);
375
+ }}
376
+ className={cn(
377
+ "fixed inset-0 z-50 flex flex-col bg-surface-overlay pt-[env(safe-area-inset-top)] text-foreground outline-none",
378
+ "duration-150 data-[state=open]:animate-in data-[state=open]:fade-in-0",
379
+ "data-[state=closed]:animate-out data-[state=closed]:fade-out-0",
380
+ )}
381
+ >
382
+ <div className="flex h-14 shrink-0 items-center justify-between gap-2 border-b border-border px-4">
383
+ {depth === 0 ? (
384
+ <Button
385
+ variant="plain"
386
+ hideLabel
387
+ icon={<CloseIcon />}
388
+ label={root.props.mobileCloseMenuButtonLabel ?? "Close menu"}
389
+ onClick={close}
390
+ />
391
+ ) : (
392
+ <Button
393
+ variant="plain"
394
+ hideLabel
395
+ icon={<ChevronLeftIcon />}
396
+ label={root.props.mobileBackButtonLabel ?? "Back"}
397
+ onClick={goBack}
398
+ />
399
+ )}
400
+ <DialogPrimitive.Title asChild>
401
+ <span id={titleId} className="min-w-0 shrink grow">
402
+ <Heading level={4} as="span" align="center" truncate className="block">
403
+ {root.props.label}
404
+ </Heading>
405
+ </span>
406
+ </DialogPrimitive.Title>
407
+ <Button
408
+ intent="primary"
409
+ label={root.props.mobileDoneButtonLabel ?? "Done"}
410
+ className="-mr-2"
411
+ onClick={() => {
412
+ close();
413
+ root.props.onDone?.();
414
+ }}
415
+ />
416
+ </div>
417
+
418
+ <div className="relative min-h-0 grow overflow-hidden">
419
+ {stack.map((menu, index) => (
420
+ <div
421
+ key={menu.id}
422
+ ref={(el) => {
423
+ if (el) panels.current.set(menu.id, el);
424
+ else panels.current.delete(menu.id);
425
+ }}
426
+ aria-hidden={index !== depth || undefined}
427
+ inert={index !== depth || undefined}
428
+ className="absolute inset-0 overflow-y-auto overscroll-contain transition-transform duration-200 ease-out motion-reduce:transition-none"
429
+ style={{ transform: `translateX(${(index - depth) * 100}%)` }}
430
+ >
431
+ <ActionList aria-label={menu.props.label}>
432
+ {menu.items.map((item) => {
433
+ const { props } = item;
434
+ const shared = {
435
+ label: props.label,
436
+ description: props.description,
437
+ icon: props.icon,
438
+ iconBadge: props.iconBadge,
439
+ avatar: props.avatar,
440
+ intent: props.intent,
441
+ disabled: props.disabled,
442
+ truncateLabel: props.truncateLabel,
443
+ "data-item": item.id,
444
+ };
445
+ if (item.submenu) {
446
+ return (
447
+ <ActionListItem
448
+ key={item.id}
449
+ {...shared}
450
+ aria-haspopup="menu"
451
+ badge={selections.chosenLabel(item.submenu)}
452
+ suffix={props.suffix ?? <ChevronRightIcon />}
453
+ onClick={() => drillInto(item)}
454
+ />
455
+ );
456
+ }
457
+ if (menu.props.variant === "selection") {
458
+ const selected = selections.valueOf(menu) === props.value;
459
+ return (
460
+ <ActionListItem
461
+ key={item.id}
462
+ {...shared}
463
+ role="menuitemradio"
464
+ aria-checked={selected}
465
+ selected={selected}
466
+ suffix={selected ? <CheckIcon /> : props.suffix}
467
+ onClick={() => props.value !== undefined && selections.select(menu, props.value)}
468
+ />
469
+ );
470
+ }
471
+ return (
472
+ <ActionListItem
473
+ key={item.id}
474
+ {...shared}
475
+ suffix={props.suffix}
476
+ onClick={() => {
477
+ props.onSelect?.();
478
+ close();
479
+ }}
480
+ />
481
+ );
482
+ })}
483
+ </ActionList>
484
+ </div>
485
+ ))}
486
+ </div>
487
+ </DialogPrimitive.Content>
488
+ </DialogPrimitive.Portal>
489
+ </DialogPrimitive.Root>
490
+ );
491
+ }
492
+
493
+ /* --------------------------------------------------------------- public */
494
+
495
+ /**
496
+ * Swirl's menu (`swirl-menu` in a `swirl-popover`): actions and choices that
497
+ * open from a trigger, with nested submenus.
498
+ *
499
+ * On desktop it is a popover; a submenu opens beside its item on click or
500
+ * ArrowRight and closes with ArrowLeft. Below 768px it fills the screen and
501
+ * submenus slide in, with Back, the menu's title and Done in a header.
502
+ *
503
+ * <Menu label="Actions" trigger={<Button label="Open menu" />}>
504
+ * <MenuItem icon={<Copy />} label="Duplicate" onSelect={duplicate} />
505
+ * <MenuItem label="Text color">
506
+ * <Menu label="Text color" variant="selection" value={color} onValueChange={setColor}>
507
+ * <MenuItem label="Red" value="red" />
508
+ * <MenuItem label="Blue" value="blue" />
509
+ * </Menu>
510
+ * </MenuItem>
511
+ * </Menu>
512
+ */
513
+ export function Menu(props: MenuProps) {
514
+ const root = parseMenu(props, "menu");
515
+ const selections = useSelections(root);
516
+ const mobile = useMobile();
517
+ return mobile ? (
518
+ <MobileMenu root={root} selections={selections} />
519
+ ) : (
520
+ <DesktopMenu root={root} selections={selections} />
521
+ );
522
+ }
523
+
524
+ /**
525
+ * One entry in a `Menu`. Nest a `Menu` inside to give it a submenu. It renders
526
+ * nothing on its own: the enclosing `Menu` reads its props.
527
+ */
528
+ export function MenuItem(_props: MenuItemProps): React.ReactNode {
529
+ return null;
530
+ }