@c9up/nebula 0.1.8 → 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.
- package/dist/atoms/Avatar.d.ts +36 -9
- package/dist/atoms/Avatar.js +66 -27
- package/dist/atoms/index.d.ts +1 -1
- package/dist/atoms/index.js +1 -1
- package/dist/lib/children.d.ts +30 -0
- package/dist/lib/motion.d.ts +10 -0
- package/dist/lib/motion.js +10 -0
- package/dist/molecules/Accordion.d.ts +36 -15
- package/dist/molecules/Accordion.js +68 -45
- package/dist/molecules/Breadcrumb.d.ts +29 -11
- package/dist/molecules/Breadcrumb.js +52 -52
- package/dist/molecules/Collapsible.d.ts +18 -17
- package/dist/molecules/Collapsible.js +42 -34
- package/dist/molecules/Field.d.ts +66 -30
- package/dist/molecules/Field.js +106 -55
- package/dist/molecules/InputGroup.d.ts +39 -21
- package/dist/molecules/InputGroup.js +43 -27
- package/dist/molecules/InputOTP.d.ts +26 -3
- package/dist/molecules/InputOTP.js +109 -76
- package/dist/molecules/Message.js +5 -4
- package/dist/molecules/Pagination.d.ts +36 -11
- package/dist/molecules/Pagination.js +58 -74
- package/dist/molecules/RadioGroup.d.ts +25 -17
- package/dist/molecules/RadioGroup.js +47 -43
- package/dist/molecules/Resizable.d.ts +32 -7
- package/dist/molecules/Resizable.js +76 -49
- package/dist/molecules/Tabs.d.ts +49 -11
- package/dist/molecules/Tabs.js +107 -51
- package/dist/molecules/ToggleGroup.d.ts +16 -10
- package/dist/molecules/ToggleGroup.js +37 -21
- package/dist/molecules/index.d.ts +11 -11
- package/dist/molecules/index.js +11 -11
- package/dist/organisms/AlertDialog.d.ts +80 -28
- package/dist/organisms/AlertDialog.js +192 -67
- package/dist/organisms/Carousel.d.ts +25 -3
- package/dist/organisms/Carousel.js +75 -43
- package/dist/organisms/Combobox.d.ts +2 -1
- package/dist/organisms/Combobox.js +46 -16
- package/dist/organisms/Command.d.ts +73 -32
- package/dist/organisms/Command.js +209 -131
- package/dist/organisms/CommandDialog.js +5 -5
- package/dist/organisms/ContextMenu.d.ts +41 -19
- package/dist/organisms/ContextMenu.js +112 -71
- package/dist/organisms/DataTable.js +42 -8
- package/dist/organisms/Dialog.d.ts +97 -31
- package/dist/organisms/Dialog.js +193 -68
- package/dist/organisms/Drawer.d.ts +33 -10
- package/dist/organisms/Drawer.js +130 -52
- package/dist/organisms/DropdownMenu.d.ts +58 -11
- package/dist/organisms/DropdownMenu.js +137 -50
- package/dist/organisms/Form.js +9 -11
- package/dist/organisms/HoverCard.d.ts +39 -19
- package/dist/organisms/HoverCard.js +116 -40
- package/dist/organisms/Menubar.d.ts +55 -19
- package/dist/organisms/Menubar.js +166 -82
- package/dist/organisms/NavigationMenu.d.ts +77 -31
- package/dist/organisms/NavigationMenu.js +169 -98
- package/dist/organisms/Popover.d.ts +72 -12
- package/dist/organisms/Popover.js +132 -25
- package/dist/organisms/Questionnaire.js +5 -5
- package/dist/organisms/Select.d.ts +99 -18
- package/dist/organisms/Select.js +253 -111
- package/dist/organisms/Sheet.d.ts +57 -13
- package/dist/organisms/Sheet.js +141 -54
- package/dist/organisms/Sidebar.d.ts +146 -56
- package/dist/organisms/Sidebar.js +236 -128
- package/dist/organisms/Tooltip.d.ts +88 -14
- package/dist/organisms/Tooltip.js +210 -47
- package/dist/organisms/index.d.ts +15 -15
- package/dist/organisms/index.js +15 -15
- package/dist/primitives/floating.js +25 -0
- package/dist/primitives/floatingSurface.d.ts +11 -0
- package/dist/primitives/floatingSurface.js +8 -0
- package/dist/primitives/menu.d.ts +45 -1
- package/dist/primitives/menu.js +45 -14
- package/dist/primitives/menuParts.d.ts +113 -0
- package/dist/primitives/menuParts.js +207 -0
- package/nebula.css +1 -1
- package/package.json +5 -4
- package/registry.json +18 -6
- package/src/atoms/Avatar.ts +141 -37
- package/src/atoms/index.ts +13 -1
- package/src/lib/children.ts +31 -0
- package/src/lib/motion.ts +12 -0
- package/src/molecules/Accordion.ts +120 -60
- package/src/molecules/Breadcrumb.ts +77 -76
- package/src/molecules/Collapsible.ts +77 -41
- package/src/molecules/Field.ts +187 -72
- package/src/molecules/InputGroup.ts +88 -35
- package/src/molecules/InputOTP.ts +150 -86
- package/src/molecules/Message.ts +6 -4
- package/src/molecules/Pagination.ts +96 -101
- package/src/molecules/RadioGroup.ts +70 -59
- package/src/molecules/Resizable.ts +138 -75
- package/src/molecules/Tabs.ts +187 -74
- package/src/molecules/ToggleGroup.ts +77 -38
- package/src/molecules/index.ts +85 -8
- package/src/organisms/AlertDialog.ts +319 -82
- package/src/organisms/Carousel.ts +129 -52
- package/src/organisms/Combobox.ts +64 -19
- package/src/organisms/Command.ts +342 -159
- package/src/organisms/CommandDialog.ts +6 -6
- package/src/organisms/ContextMenu.ts +185 -83
- package/src/organisms/DataTable.ts +56 -10
- package/src/organisms/Dialog.ts +312 -102
- package/src/organisms/Drawer.ts +219 -71
- package/src/organisms/DropdownMenu.ts +219 -61
- package/src/organisms/Form.ts +48 -34
- package/src/organisms/HoverCard.ts +187 -53
- package/src/organisms/Menubar.ts +247 -87
- package/src/organisms/NavigationMenu.ts +306 -128
- package/src/organisms/Popover.ts +215 -39
- package/src/organisms/Questionnaire.ts +9 -6
- package/src/organisms/Select.ts +397 -130
- package/src/organisms/Sheet.ts +252 -83
- package/src/organisms/Sidebar.ts +438 -185
- package/src/organisms/Tooltip.ts +315 -65
- package/src/organisms/index.ts +284 -14
- package/src/primitives/floating.ts +33 -0
- package/src/primitives/floatingSurface.ts +19 -0
- package/src/primitives/menu.ts +79 -18
- package/src/primitives/menuParts.ts +371 -0
package/dist/primitives/menu.js
CHANGED
|
@@ -33,10 +33,31 @@ import { portal } from "./portal.js";
|
|
|
33
33
|
import { rovingFocus } from "./rovingFocus.js";
|
|
34
34
|
import { typeahead } from "./typeahead.js";
|
|
35
35
|
// ─── classes ─────────────────────────────────────────────────────────
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Upstream's class strings, shared by the three menus built on this module.
|
|
38
|
+
*
|
|
39
|
+
* shadcn repeats them in `dropdown-menu.tsx`, `context-menu.tsx` and
|
|
40
|
+
* `menubar.tsx` because each file is copied on its own. Here all three already
|
|
41
|
+
* depend on this module, and the registry copies it with them, so repeating
|
|
42
|
+
* the strings would only be three places to update when one of them changes.
|
|
43
|
+
*
|
|
44
|
+
* `--nebula-available-height` and `--nebula-transform-origin` stand in for
|
|
45
|
+
* Radix's per-component custom properties; `autoPosition` writes both.
|
|
46
|
+
*/
|
|
47
|
+
export const menuPanelClasses = "bg-popover text-popover-foreground z-50 max-h-(--nebula-available-height) min-w-[8rem] origin-(--nebula-transform-origin) overflow-x-hidden overflow-y-auto rounded-md border p-1 shadow-md outline-none";
|
|
48
|
+
/** A submenu panel: no height cap, a deeper shadow. */
|
|
49
|
+
export const menuSubPanelClasses = "bg-popover text-popover-foreground z-50 min-w-[8rem] origin-(--nebula-transform-origin) overflow-hidden rounded-md border p-1 shadow-lg outline-none";
|
|
50
|
+
export const menuItemClasses = "relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[inset]:pl-8 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground data-[variant=destructive]:*:[svg]:text-destructive!";
|
|
51
|
+
/** Checkbox and radio rows: the same, indented to leave room for the mark. */
|
|
52
|
+
export const menuCheckItemClasses = "relative flex cursor-default items-center gap-2 rounded-sm py-1.5 pr-2 pl-8 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4";
|
|
53
|
+
export const menuIndicatorClasses = "pointer-events-none absolute left-2 flex size-3.5 items-center justify-center";
|
|
54
|
+
export const menuLabelClasses = "px-2 py-1.5 text-sm font-medium data-[inset]:pl-8";
|
|
55
|
+
export const menuSeparatorClasses = "bg-border -mx-1 my-1 h-px";
|
|
56
|
+
export const menuShortcutClasses = "text-muted-foreground ml-auto text-xs tracking-widest";
|
|
57
|
+
export const menuSubTriggerClasses = "flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground data-[inset]:pl-8 data-[state=open]:bg-accent data-[state=open]:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground";
|
|
58
|
+
const itemClasses = menuItemClasses;
|
|
38
59
|
const destructiveClasses = "text-destructive focus:bg-destructive/10 focus:text-destructive [&_svg]:text-destructive";
|
|
39
|
-
const indentedItemClasses =
|
|
60
|
+
const indentedItemClasses = menuCheckItemClasses;
|
|
40
61
|
/** The panel's markup. Behaviour is attached separately by `wireMenu`. */
|
|
41
62
|
export function menuPanel(options) {
|
|
42
63
|
return html `<div
|
|
@@ -64,7 +85,7 @@ function renderEntry(entry, closeAll) {
|
|
|
64
85
|
if (isRadioGroup(entry))
|
|
65
86
|
return renderRadioGroup(entry, closeAll);
|
|
66
87
|
if (isSubmenu(entry))
|
|
67
|
-
return renderSubmenuTrigger(entry);
|
|
88
|
+
return renderSubmenuTrigger(entry, closeAll);
|
|
68
89
|
return renderAction(entry, closeAll);
|
|
69
90
|
}
|
|
70
91
|
function renderAction(entry, closeAll) {
|
|
@@ -128,8 +149,8 @@ function renderRadioGroup(entry, closeAll) {
|
|
|
128
149
|
* those into the DOM is not possible. `wireMenu` reads them back when the row
|
|
129
150
|
* is opened.
|
|
130
151
|
*/
|
|
131
|
-
const
|
|
132
|
-
function renderSubmenuTrigger(entry) {
|
|
152
|
+
const submenuPanels = new WeakMap();
|
|
153
|
+
function renderSubmenuTrigger(entry, closeAll) {
|
|
133
154
|
const id = uid("menu-sub-trigger");
|
|
134
155
|
const row = html `<div
|
|
135
156
|
role="menuitem"
|
|
@@ -145,7 +166,8 @@ function renderSubmenuTrigger(entry) {
|
|
|
145
166
|
<span class="flex-1 truncate">${entry.label}</span>
|
|
146
167
|
${ChevronRightIcon({ class: "ml-auto size-4" })}
|
|
147
168
|
</div>`;
|
|
148
|
-
|
|
169
|
+
const entries = entry.entries;
|
|
170
|
+
pendingSubmenus.set(id, ({ id: panelId }) => menuPanel({ id: panelId, entries, onCloseAll: closeAll }));
|
|
149
171
|
return row;
|
|
150
172
|
}
|
|
151
173
|
/**
|
|
@@ -199,12 +221,12 @@ export function wireMenu(panel, options) {
|
|
|
199
221
|
onMatch: (item) => focusSilently(item),
|
|
200
222
|
});
|
|
201
223
|
function openSubmenu(trigger) {
|
|
202
|
-
const
|
|
203
|
-
if (
|
|
224
|
+
const build = submenuPanels.get(trigger);
|
|
225
|
+
if (build === undefined)
|
|
204
226
|
return;
|
|
205
227
|
closeSubmenusFrom(0);
|
|
206
228
|
const panelId = uid("menu-sub");
|
|
207
|
-
const mount = portal(
|
|
229
|
+
const mount = portal(build({ id: panelId }));
|
|
208
230
|
const element = mount.host.firstElementChild;
|
|
209
231
|
if (!(element instanceof HTMLElement)) {
|
|
210
232
|
mount.close();
|
|
@@ -321,19 +343,28 @@ export function wireMenu(panel, options) {
|
|
|
321
343
|
group.destroy();
|
|
322
344
|
};
|
|
323
345
|
}
|
|
324
|
-
/** Move each submenu's
|
|
346
|
+
/** Move each submenu's panel factory from the build-time map onto its element. */
|
|
325
347
|
function claimSubmenuEntries(panel) {
|
|
326
348
|
for (const node of panel.querySelectorAll("[data-submenu]")) {
|
|
327
349
|
const id = node.getAttribute("data-submenu");
|
|
328
350
|
if (id === null)
|
|
329
351
|
continue;
|
|
330
|
-
const
|
|
331
|
-
if (
|
|
352
|
+
const build = pendingSubmenus.get(id);
|
|
353
|
+
if (build === undefined)
|
|
332
354
|
continue;
|
|
333
|
-
|
|
355
|
+
submenuPanels.set(node, build);
|
|
334
356
|
pendingSubmenus.delete(id);
|
|
335
357
|
}
|
|
336
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Register a composed submenu panel against a trigger id.
|
|
361
|
+
*
|
|
362
|
+
* The parts in `DropdownMenu` and friends build their own markup, so they hand
|
|
363
|
+
* the factory over directly instead of going through `MenuEntry`.
|
|
364
|
+
*/
|
|
365
|
+
export function registerSubmenu(id, build) {
|
|
366
|
+
pendingSubmenus.set(id, build);
|
|
367
|
+
}
|
|
337
368
|
// ─── entry guards ────────────────────────────────────────────────────
|
|
338
369
|
//
|
|
339
370
|
// Discriminating on an optional `type` needs real guards: `entry.type ===
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows every menu is made of.
|
|
3
|
+
*
|
|
4
|
+
* DropdownMenu, ContextMenu and Menubar are the same menu with three different
|
|
5
|
+
* opening gestures: a button, a right-click, a bar. Upstream repeats all
|
|
6
|
+
* fifteen row components in each of the three files, because each file is
|
|
7
|
+
* copied on its own and has to stand alone. Here the three already share
|
|
8
|
+
* `menu.ts` — its class strings, its keyboard model, its submenu registry —
|
|
9
|
+
* and the registry copies it with them, so repeating the rows would only be
|
|
10
|
+
* three places to fix when one of them is wrong.
|
|
11
|
+
*
|
|
12
|
+
* const parts = createMenuParts("context-menu", ContextMenuContext);
|
|
13
|
+
* export const ContextMenuItem = parts.Item;
|
|
14
|
+
*
|
|
15
|
+
* What differs between the three is exactly two things: the `data-slot`
|
|
16
|
+
* prefix, and which context tells a row how to close the menu it is in. Both
|
|
17
|
+
* are arguments.
|
|
18
|
+
*
|
|
19
|
+
* The rows carry `data-nebula-item` and a `role`, which is the whole of the
|
|
20
|
+
* contract `wireMenu` reads — roving focus, type-ahead, arrows and submenu
|
|
21
|
+
* traversal all work on the rendered DOM, so a row built here behaves the same
|
|
22
|
+
* as one written by hand.
|
|
23
|
+
*/
|
|
24
|
+
import { type Context, type TemplateResult } from "@c9up/aurora";
|
|
25
|
+
import type { Parts, Slot } from "../lib/children.js";
|
|
26
|
+
import { type Reactive } from "../lib/props.js";
|
|
27
|
+
/** What a row needs from the menu it is in. */
|
|
28
|
+
export interface MenuOwner {
|
|
29
|
+
/** A row was chosen: the whole stack closes. */
|
|
30
|
+
closeAll(): void;
|
|
31
|
+
}
|
|
32
|
+
export interface MenuGroupProps {
|
|
33
|
+
children?: Slot;
|
|
34
|
+
class?: Reactive<string>;
|
|
35
|
+
}
|
|
36
|
+
export interface MenuItemProps {
|
|
37
|
+
children?: Slot;
|
|
38
|
+
/** Indent to line up with the checkbox and radio rows. */
|
|
39
|
+
inset?: boolean;
|
|
40
|
+
variant?: "default" | "destructive";
|
|
41
|
+
disabled?: boolean;
|
|
42
|
+
onSelect?: () => void;
|
|
43
|
+
class?: Reactive<string>;
|
|
44
|
+
}
|
|
45
|
+
export interface MenuCheckboxItemProps {
|
|
46
|
+
children?: Slot;
|
|
47
|
+
checked?: Reactive<boolean>;
|
|
48
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
49
|
+
disabled?: boolean;
|
|
50
|
+
class?: Reactive<string>;
|
|
51
|
+
}
|
|
52
|
+
export interface MenuRadioGroupProps {
|
|
53
|
+
children?: Parts;
|
|
54
|
+
value?: Reactive<string | undefined>;
|
|
55
|
+
onValueChange?: (value: string) => void;
|
|
56
|
+
class?: Reactive<string>;
|
|
57
|
+
}
|
|
58
|
+
export interface MenuRadioItemProps {
|
|
59
|
+
children?: Slot;
|
|
60
|
+
value: string;
|
|
61
|
+
disabled?: boolean;
|
|
62
|
+
class?: Reactive<string>;
|
|
63
|
+
}
|
|
64
|
+
export interface MenuLabelProps {
|
|
65
|
+
children?: Slot;
|
|
66
|
+
inset?: boolean;
|
|
67
|
+
class?: Reactive<string>;
|
|
68
|
+
}
|
|
69
|
+
export interface MenuSeparatorProps {
|
|
70
|
+
class?: Reactive<string>;
|
|
71
|
+
}
|
|
72
|
+
export interface MenuShortcutProps {
|
|
73
|
+
children?: Slot;
|
|
74
|
+
class?: Reactive<string>;
|
|
75
|
+
}
|
|
76
|
+
export interface MenuSubProps {
|
|
77
|
+
children?: Parts;
|
|
78
|
+
}
|
|
79
|
+
export interface MenuSubTriggerProps {
|
|
80
|
+
children?: Slot;
|
|
81
|
+
inset?: boolean;
|
|
82
|
+
disabled?: boolean;
|
|
83
|
+
class?: Reactive<string>;
|
|
84
|
+
}
|
|
85
|
+
export interface MenuSubContentProps {
|
|
86
|
+
children?: Parts;
|
|
87
|
+
class?: Reactive<string>;
|
|
88
|
+
}
|
|
89
|
+
export interface MenuPortalProps {
|
|
90
|
+
children?: Slot;
|
|
91
|
+
container?: () => Element | null;
|
|
92
|
+
}
|
|
93
|
+
export interface MenuParts {
|
|
94
|
+
Group: (props?: MenuGroupProps) => TemplateResult;
|
|
95
|
+
Item: (props?: MenuItemProps) => TemplateResult;
|
|
96
|
+
CheckboxItem: (props?: MenuCheckboxItemProps) => TemplateResult;
|
|
97
|
+
RadioGroup: (props?: MenuRadioGroupProps) => TemplateResult;
|
|
98
|
+
RadioItem: (props: MenuRadioItemProps) => TemplateResult;
|
|
99
|
+
Label: (props?: MenuLabelProps) => TemplateResult;
|
|
100
|
+
Separator: (props?: MenuSeparatorProps) => TemplateResult;
|
|
101
|
+
Shortcut: (props?: MenuShortcutProps) => TemplateResult;
|
|
102
|
+
Sub: (props?: MenuSubProps) => TemplateResult;
|
|
103
|
+
SubTrigger: (props?: MenuSubTriggerProps) => TemplateResult;
|
|
104
|
+
SubContent: (props?: MenuSubContentProps) => TemplateResult;
|
|
105
|
+
Portal: (props?: MenuPortalProps) => TemplateResult;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Build one menu's fifteen rows.
|
|
109
|
+
*
|
|
110
|
+
* @param prefix The `data-slot` prefix, e.g. `"dropdown-menu"`.
|
|
111
|
+
* @param owner The context carrying that menu's `closeAll`.
|
|
112
|
+
*/
|
|
113
|
+
export declare function createMenuParts(prefix: string, owner: Context<MenuOwner>): MenuParts;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows every menu is made of.
|
|
3
|
+
*
|
|
4
|
+
* DropdownMenu, ContextMenu and Menubar are the same menu with three different
|
|
5
|
+
* opening gestures: a button, a right-click, a bar. Upstream repeats all
|
|
6
|
+
* fifteen row components in each of the three files, because each file is
|
|
7
|
+
* copied on its own and has to stand alone. Here the three already share
|
|
8
|
+
* `menu.ts` — its class strings, its keyboard model, its submenu registry —
|
|
9
|
+
* and the registry copies it with them, so repeating the rows would only be
|
|
10
|
+
* three places to fix when one of them is wrong.
|
|
11
|
+
*
|
|
12
|
+
* const parts = createMenuParts("context-menu", ContextMenuContext);
|
|
13
|
+
* export const ContextMenuItem = parts.Item;
|
|
14
|
+
*
|
|
15
|
+
* What differs between the three is exactly two things: the `data-slot`
|
|
16
|
+
* prefix, and which context tells a row how to close the menu it is in. Both
|
|
17
|
+
* are arguments.
|
|
18
|
+
*
|
|
19
|
+
* The rows carry `data-nebula-item` and a `role`, which is the whole of the
|
|
20
|
+
* contract `wireMenu` reads — roving focus, type-ahead, arrows and submenu
|
|
21
|
+
* traversal all work on the rendered DOM, so a row built here behaves the same
|
|
22
|
+
* as one written by hand.
|
|
23
|
+
*/
|
|
24
|
+
import { component, createContext, html, inject, provide, } from "@c9up/aurora";
|
|
25
|
+
import { slot } from "../lib/children.js";
|
|
26
|
+
import { cn } from "../lib/cn.js";
|
|
27
|
+
import { CheckIcon, ChevronRightIcon, CircleIcon } from "../lib/icons.js";
|
|
28
|
+
import { uid } from "../lib/id.js";
|
|
29
|
+
import { slideInFromSide, zoomInOut } from "../lib/motion.js";
|
|
30
|
+
import { read } from "../lib/props.js";
|
|
31
|
+
import { menuCheckItemClasses, menuIndicatorClasses, menuItemClasses, menuLabelClasses, menuSeparatorClasses, menuShortcutClasses, menuSubPanelClasses, menuSubTriggerClasses, registerSubmenu, } from "./menu.js";
|
|
32
|
+
import { portal } from "./portal.js";
|
|
33
|
+
/**
|
|
34
|
+
* Build one menu's fifteen rows.
|
|
35
|
+
*
|
|
36
|
+
* @param prefix The `data-slot` prefix, e.g. `"dropdown-menu"`.
|
|
37
|
+
* @param owner The context carrying that menu's `closeAll`.
|
|
38
|
+
*/
|
|
39
|
+
export function createMenuParts(prefix, owner) {
|
|
40
|
+
const RadioGroupContext = createContext(`${prefix}-radio-group`);
|
|
41
|
+
const SubContext = createContext(`${prefix}-sub`);
|
|
42
|
+
const Group = component((props) => html `<div
|
|
43
|
+
data-slot="${`${prefix}-group`}"
|
|
44
|
+
role="group"
|
|
45
|
+
class="${() => cn(read(props.class))}"
|
|
46
|
+
>${slot(props.children)}</div>`);
|
|
47
|
+
const Item = component((props) => {
|
|
48
|
+
const menu = inject(owner);
|
|
49
|
+
function activate() {
|
|
50
|
+
if (props.disabled === true)
|
|
51
|
+
return;
|
|
52
|
+
props.onSelect?.();
|
|
53
|
+
menu.closeAll();
|
|
54
|
+
}
|
|
55
|
+
return html `<div
|
|
56
|
+
data-slot="${`${prefix}-item`}"
|
|
57
|
+
data-nebula-item
|
|
58
|
+
role="menuitem"
|
|
59
|
+
data-inset="${props.inset === true ? "" : undefined}"
|
|
60
|
+
data-variant="${props.variant ?? "default"}"
|
|
61
|
+
data-disabled="${props.disabled === true ? "" : undefined}"
|
|
62
|
+
aria-disabled="${props.disabled === true ? "true" : undefined}"
|
|
63
|
+
tabindex="-1"
|
|
64
|
+
class="${() => cn(menuItemClasses, read(props.class))}"
|
|
65
|
+
@click="${activate}"
|
|
66
|
+
>${slot(props.children)}</div>`;
|
|
67
|
+
});
|
|
68
|
+
const CheckboxItem = component((props) => {
|
|
69
|
+
const menu = inject(owner);
|
|
70
|
+
const checked = () => read(props.checked) === true;
|
|
71
|
+
function activate() {
|
|
72
|
+
if (props.disabled === true)
|
|
73
|
+
return;
|
|
74
|
+
props.onCheckedChange?.(!checked());
|
|
75
|
+
menu.closeAll();
|
|
76
|
+
}
|
|
77
|
+
return html `<div
|
|
78
|
+
data-slot="${`${prefix}-checkbox-item`}"
|
|
79
|
+
data-nebula-item
|
|
80
|
+
role="menuitemcheckbox"
|
|
81
|
+
aria-checked="${() => (checked() ? "true" : "false")}"
|
|
82
|
+
data-disabled="${props.disabled === true ? "" : undefined}"
|
|
83
|
+
aria-disabled="${props.disabled === true ? "true" : undefined}"
|
|
84
|
+
tabindex="-1"
|
|
85
|
+
class="${() => cn(menuCheckItemClasses, read(props.class))}"
|
|
86
|
+
@click="${activate}"
|
|
87
|
+
><span class="${menuIndicatorClasses}">${() => checked() ? CheckIcon({ class: "size-4" }) : null}</span>${slot(props.children)}</div>`;
|
|
88
|
+
});
|
|
89
|
+
const RadioGroup = component((props) => {
|
|
90
|
+
const menu = inject(owner);
|
|
91
|
+
provide(RadioGroupContext, {
|
|
92
|
+
value: () => read(props.value),
|
|
93
|
+
select(next) {
|
|
94
|
+
props.onValueChange?.(next);
|
|
95
|
+
menu.closeAll();
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
const rows = props.children?.();
|
|
99
|
+
return html `<div
|
|
100
|
+
data-slot="${`${prefix}-radio-group`}"
|
|
101
|
+
role="group"
|
|
102
|
+
class="${() => cn(read(props.class))}"
|
|
103
|
+
>${rows}</div>`;
|
|
104
|
+
});
|
|
105
|
+
const RadioItem = component((props) => {
|
|
106
|
+
const group = inject(RadioGroupContext);
|
|
107
|
+
const selected = () => group.value() === props.value;
|
|
108
|
+
function activate() {
|
|
109
|
+
if (props.disabled === true)
|
|
110
|
+
return;
|
|
111
|
+
group.select(props.value);
|
|
112
|
+
}
|
|
113
|
+
return html `<div
|
|
114
|
+
data-slot="${`${prefix}-radio-item`}"
|
|
115
|
+
data-nebula-item
|
|
116
|
+
role="menuitemradio"
|
|
117
|
+
aria-checked="${() => (selected() ? "true" : "false")}"
|
|
118
|
+
data-disabled="${props.disabled === true ? "" : undefined}"
|
|
119
|
+
aria-disabled="${props.disabled === true ? "true" : undefined}"
|
|
120
|
+
tabindex="-1"
|
|
121
|
+
class="${() => cn(menuCheckItemClasses, read(props.class))}"
|
|
122
|
+
@click="${activate}"
|
|
123
|
+
><span class="${menuIndicatorClasses}">${() => selected()
|
|
124
|
+
? CircleIcon({ class: "size-2 fill-current" })
|
|
125
|
+
: null}</span>${slot(props.children)}</div>`;
|
|
126
|
+
});
|
|
127
|
+
const Label = component((props) => html `<div
|
|
128
|
+
data-slot="${`${prefix}-label`}"
|
|
129
|
+
data-inset="${props.inset === true ? "" : undefined}"
|
|
130
|
+
class="${() => cn(menuLabelClasses, read(props.class))}"
|
|
131
|
+
>${slot(props.children)}</div>`);
|
|
132
|
+
const Separator = component((props) => html `<div
|
|
133
|
+
data-slot="${`${prefix}-separator`}"
|
|
134
|
+
role="separator"
|
|
135
|
+
class="${() => cn(menuSeparatorClasses, read(props.class))}"
|
|
136
|
+
></div>`);
|
|
137
|
+
const Shortcut = component((props) => html `<span
|
|
138
|
+
data-slot="${`${prefix}-shortcut`}"
|
|
139
|
+
class="${() => cn(menuShortcutClasses, read(props.class))}"
|
|
140
|
+
>${slot(props.children)}</span>`);
|
|
141
|
+
const Sub = component((props) => {
|
|
142
|
+
const triggerId = uid(`${prefix}-sub-trigger`);
|
|
143
|
+
let body;
|
|
144
|
+
let panelClass;
|
|
145
|
+
provide(SubContext, {
|
|
146
|
+
triggerId,
|
|
147
|
+
register(registered, className) {
|
|
148
|
+
body = registered;
|
|
149
|
+
panelClass = className;
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
const parts = props.children?.();
|
|
153
|
+
// Registered AFTER the parts are built, so `body` is the panel the
|
|
154
|
+
// `SubContent` among them just handed over. A submenu is built fresh on
|
|
155
|
+
// each open, which is why this is a factory.
|
|
156
|
+
registerSubmenu(triggerId, ({ id }) => html `<div
|
|
157
|
+
data-slot="${`${prefix}-sub-content`}"
|
|
158
|
+
id="${id}"
|
|
159
|
+
role="menu"
|
|
160
|
+
aria-orientation="vertical"
|
|
161
|
+
tabindex="-1"
|
|
162
|
+
class="${cn(menuSubPanelClasses, zoomInOut, slideInFromSide, read(panelClass))}"
|
|
163
|
+
>${body}</div>`);
|
|
164
|
+
return html `${parts}`;
|
|
165
|
+
});
|
|
166
|
+
const SubTrigger = component((props) => {
|
|
167
|
+
const sub = inject(SubContext);
|
|
168
|
+
return html `<div
|
|
169
|
+
data-slot="${`${prefix}-sub-trigger`}"
|
|
170
|
+
data-nebula-item
|
|
171
|
+
data-submenu="${sub.triggerId}"
|
|
172
|
+
role="menuitem"
|
|
173
|
+
aria-haspopup="menu"
|
|
174
|
+
aria-expanded="false"
|
|
175
|
+
data-inset="${props.inset === true ? "" : undefined}"
|
|
176
|
+
data-disabled="${props.disabled === true ? "" : undefined}"
|
|
177
|
+
tabindex="-1"
|
|
178
|
+
class="${() => cn(menuSubTriggerClasses, read(props.class))}"
|
|
179
|
+
>${slot(props.children)}${ChevronRightIcon({ class: "ml-auto size-4" })}</div>`;
|
|
180
|
+
});
|
|
181
|
+
const SubContent = component((props) => {
|
|
182
|
+
const sub = inject(SubContext);
|
|
183
|
+
sub.register(props.children?.(), props.class);
|
|
184
|
+
return html ``;
|
|
185
|
+
});
|
|
186
|
+
const Portal = component((props) => {
|
|
187
|
+
const mounted = portal(html `${slot(props.children)}`, {
|
|
188
|
+
container: props.container,
|
|
189
|
+
});
|
|
190
|
+
mounted.host.setAttribute("data-slot", `${prefix}-portal`);
|
|
191
|
+
return html ``;
|
|
192
|
+
});
|
|
193
|
+
return {
|
|
194
|
+
Group,
|
|
195
|
+
Item,
|
|
196
|
+
CheckboxItem,
|
|
197
|
+
RadioGroup,
|
|
198
|
+
RadioItem,
|
|
199
|
+
Label,
|
|
200
|
+
Separator,
|
|
201
|
+
Shortcut,
|
|
202
|
+
Sub,
|
|
203
|
+
SubTrigger,
|
|
204
|
+
SubContent,
|
|
205
|
+
Portal,
|
|
206
|
+
};
|
|
207
|
+
}
|