@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.
Files changed (122) hide show
  1. package/dist/atoms/Avatar.d.ts +36 -9
  2. package/dist/atoms/Avatar.js +66 -27
  3. package/dist/atoms/index.d.ts +1 -1
  4. package/dist/atoms/index.js +1 -1
  5. package/dist/lib/children.d.ts +30 -0
  6. package/dist/lib/motion.d.ts +10 -0
  7. package/dist/lib/motion.js +10 -0
  8. package/dist/molecules/Accordion.d.ts +36 -15
  9. package/dist/molecules/Accordion.js +68 -45
  10. package/dist/molecules/Breadcrumb.d.ts +29 -11
  11. package/dist/molecules/Breadcrumb.js +52 -52
  12. package/dist/molecules/Collapsible.d.ts +18 -17
  13. package/dist/molecules/Collapsible.js +42 -34
  14. package/dist/molecules/Field.d.ts +66 -30
  15. package/dist/molecules/Field.js +106 -55
  16. package/dist/molecules/InputGroup.d.ts +39 -21
  17. package/dist/molecules/InputGroup.js +43 -27
  18. package/dist/molecules/InputOTP.d.ts +26 -3
  19. package/dist/molecules/InputOTP.js +109 -76
  20. package/dist/molecules/Message.js +5 -4
  21. package/dist/molecules/Pagination.d.ts +36 -11
  22. package/dist/molecules/Pagination.js +58 -74
  23. package/dist/molecules/RadioGroup.d.ts +25 -17
  24. package/dist/molecules/RadioGroup.js +47 -43
  25. package/dist/molecules/Resizable.d.ts +32 -7
  26. package/dist/molecules/Resizable.js +76 -49
  27. package/dist/molecules/Tabs.d.ts +49 -11
  28. package/dist/molecules/Tabs.js +107 -51
  29. package/dist/molecules/ToggleGroup.d.ts +16 -10
  30. package/dist/molecules/ToggleGroup.js +37 -21
  31. package/dist/molecules/index.d.ts +11 -11
  32. package/dist/molecules/index.js +11 -11
  33. package/dist/organisms/AlertDialog.d.ts +80 -28
  34. package/dist/organisms/AlertDialog.js +192 -67
  35. package/dist/organisms/Carousel.d.ts +25 -3
  36. package/dist/organisms/Carousel.js +75 -43
  37. package/dist/organisms/Combobox.d.ts +2 -1
  38. package/dist/organisms/Combobox.js +46 -16
  39. package/dist/organisms/Command.d.ts +73 -32
  40. package/dist/organisms/Command.js +209 -131
  41. package/dist/organisms/CommandDialog.js +5 -5
  42. package/dist/organisms/ContextMenu.d.ts +41 -19
  43. package/dist/organisms/ContextMenu.js +112 -71
  44. package/dist/organisms/DataTable.js +42 -8
  45. package/dist/organisms/Dialog.d.ts +97 -31
  46. package/dist/organisms/Dialog.js +193 -68
  47. package/dist/organisms/Drawer.d.ts +33 -10
  48. package/dist/organisms/Drawer.js +130 -52
  49. package/dist/organisms/DropdownMenu.d.ts +58 -11
  50. package/dist/organisms/DropdownMenu.js +137 -50
  51. package/dist/organisms/Form.js +9 -11
  52. package/dist/organisms/HoverCard.d.ts +39 -19
  53. package/dist/organisms/HoverCard.js +116 -40
  54. package/dist/organisms/Menubar.d.ts +55 -19
  55. package/dist/organisms/Menubar.js +166 -82
  56. package/dist/organisms/NavigationMenu.d.ts +77 -31
  57. package/dist/organisms/NavigationMenu.js +169 -98
  58. package/dist/organisms/Popover.d.ts +72 -12
  59. package/dist/organisms/Popover.js +132 -25
  60. package/dist/organisms/Questionnaire.js +5 -5
  61. package/dist/organisms/Select.d.ts +99 -18
  62. package/dist/organisms/Select.js +253 -111
  63. package/dist/organisms/Sheet.d.ts +57 -13
  64. package/dist/organisms/Sheet.js +141 -54
  65. package/dist/organisms/Sidebar.d.ts +146 -56
  66. package/dist/organisms/Sidebar.js +236 -128
  67. package/dist/organisms/Tooltip.d.ts +88 -14
  68. package/dist/organisms/Tooltip.js +210 -47
  69. package/dist/organisms/index.d.ts +15 -15
  70. package/dist/organisms/index.js +15 -15
  71. package/dist/primitives/floating.js +25 -0
  72. package/dist/primitives/floatingSurface.d.ts +11 -0
  73. package/dist/primitives/floatingSurface.js +8 -0
  74. package/dist/primitives/menu.d.ts +45 -1
  75. package/dist/primitives/menu.js +45 -14
  76. package/dist/primitives/menuParts.d.ts +113 -0
  77. package/dist/primitives/menuParts.js +207 -0
  78. package/nebula.css +1 -1
  79. package/package.json +5 -4
  80. package/registry.json +18 -6
  81. package/src/atoms/Avatar.ts +141 -37
  82. package/src/atoms/index.ts +13 -1
  83. package/src/lib/children.ts +31 -0
  84. package/src/lib/motion.ts +12 -0
  85. package/src/molecules/Accordion.ts +120 -60
  86. package/src/molecules/Breadcrumb.ts +77 -76
  87. package/src/molecules/Collapsible.ts +77 -41
  88. package/src/molecules/Field.ts +187 -72
  89. package/src/molecules/InputGroup.ts +88 -35
  90. package/src/molecules/InputOTP.ts +150 -86
  91. package/src/molecules/Message.ts +6 -4
  92. package/src/molecules/Pagination.ts +96 -101
  93. package/src/molecules/RadioGroup.ts +70 -59
  94. package/src/molecules/Resizable.ts +138 -75
  95. package/src/molecules/Tabs.ts +187 -74
  96. package/src/molecules/ToggleGroup.ts +77 -38
  97. package/src/molecules/index.ts +85 -8
  98. package/src/organisms/AlertDialog.ts +319 -82
  99. package/src/organisms/Carousel.ts +129 -52
  100. package/src/organisms/Combobox.ts +64 -19
  101. package/src/organisms/Command.ts +342 -159
  102. package/src/organisms/CommandDialog.ts +6 -6
  103. package/src/organisms/ContextMenu.ts +185 -83
  104. package/src/organisms/DataTable.ts +56 -10
  105. package/src/organisms/Dialog.ts +312 -102
  106. package/src/organisms/Drawer.ts +219 -71
  107. package/src/organisms/DropdownMenu.ts +219 -61
  108. package/src/organisms/Form.ts +48 -34
  109. package/src/organisms/HoverCard.ts +187 -53
  110. package/src/organisms/Menubar.ts +247 -87
  111. package/src/organisms/NavigationMenu.ts +306 -128
  112. package/src/organisms/Popover.ts +215 -39
  113. package/src/organisms/Questionnaire.ts +9 -6
  114. package/src/organisms/Select.ts +397 -130
  115. package/src/organisms/Sheet.ts +252 -83
  116. package/src/organisms/Sidebar.ts +438 -185
  117. package/src/organisms/Tooltip.ts +315 -65
  118. package/src/organisms/index.ts +284 -14
  119. package/src/primitives/floating.ts +33 -0
  120. package/src/primitives/floatingSurface.ts +19 -0
  121. package/src/primitives/menu.ts +79 -18
  122. package/src/primitives/menuParts.ts +371 -0
@@ -1,42 +1,82 @@
1
1
  /**
2
- * Menubar — an application menu bar.
2
+ * Menubar — the application menu bar.
3
3
  *
4
- * A row of menu buttons where at most one menu is open. The behaviour that
5
- * makes it a menubar rather than a row of independent dropdowns is the
6
- * handover: once a menu is open, moving the pointer onto a neighbouring button
7
- * switches to it without a click, and ArrowLeft/ArrowRight do the same from
8
- * the keyboard. That is how every desktop menu bar behaves, and a row of
9
- * dropdowns that each need their own click is the thing users notice.
4
+ * Menubar({ children: () => html`
5
+ * ${MenubarMenu({ children: () => html`
6
+ * ${MenubarTrigger({ children: "File" })}
7
+ * ${MenubarContent({ children: () => html`
8
+ * ${MenubarItem({ children: "New", onSelect: create })}
9
+ * ` })}
10
+ * ` })}
11
+ * ` })
10
12
  *
11
- * One `floatingSurface` for the bar, not one per menu. The open menu is an
12
- * index, so switching moves the same surface to a new anchor instead of
13
- * closing one and opening another — which would flash, and would lose the
14
- * exit animation into the entry animation.
13
+ * One surface for the whole bar, not one per menu: only one menu is ever open,
14
+ * and moving between them is a change of anchor and contents rather than a
15
+ * close and an open. That is what makes left/right feel like walking a bar
16
+ * instead of dismissing and summoning.
17
+ *
18
+ * Each `MenubarMenu` claims an index as it is built, which is the order the
19
+ * arrows walk. The rows themselves come from `createMenuParts`, so a menu in
20
+ * the bar and a dropdown menu cannot drift apart.
15
21
  */
16
22
 
17
- import { component, html, signal } from "@c9up/aurora";
18
- import type { Child } from "../lib/children.js";
23
+ import {
24
+ component,
25
+ createContext,
26
+ html,
27
+ inject,
28
+ provide,
29
+ signal,
30
+ type TemplateResult,
31
+ } from "@c9up/aurora";
32
+ import type { Child, Parts, Slot } from "../lib/children.js";
33
+ import { slot } from "../lib/children.js";
19
34
  import { cn } from "../lib/cn.js";
20
35
  import { uid } from "../lib/id.js";
36
+ import { slideInFromSide, zoomInOut } from "../lib/motion.js";
21
37
  import { type Reactive, read } from "../lib/props.js";
22
38
  import { floatingSurface } from "../primitives/floatingSurface.js";
23
- import { type MenuEntry, menuPanel, wireMenu } from "../primitives/menu.js";
39
+ import { menuPanelClasses, wireMenu } from "../primitives/menu.js";
40
+ import { createMenuParts, type MenuOwner } from "../primitives/menuParts.js";
24
41
 
25
- export interface MenubarMenu {
26
- label: Child;
27
- entries: readonly MenuEntry[];
28
- disabled?: boolean;
42
+ const triggerClasses =
43
+ "focus:bg-accent focus:text-accent-foreground data-[state=open]:bg-accent data-[state=open]:text-accent-foreground flex items-center rounded-sm px-2 py-1 text-sm font-medium outline-hidden select-none disabled:pointer-events-none disabled:opacity-50";
44
+
45
+ interface MenuRegistration {
46
+ readonly triggerId: string;
47
+ disabled: boolean;
48
+ body: Child;
49
+ panelClass: Reactive<string> | undefined;
50
+ }
51
+
52
+ interface MenubarApi extends MenuOwner {
53
+ readonly contentId: string;
54
+ /** Claim the next index. Registration order is the order arrows walk. */
55
+ claim(triggerId: string): number;
56
+ describe(index: number, patch: Partial<MenuRegistration>): void;
57
+ readonly openIndex: () => number;
58
+ openAt(index: number, withKeyboard: boolean): void;
59
+ close(): void;
60
+ focusTrigger(index: number): void;
29
61
  }
30
62
 
63
+ const MenubarContext = createContext<MenubarApi>("Menubar");
64
+
65
+ interface MenubarMenuApi {
66
+ readonly index: number;
67
+ readonly triggerId: string;
68
+ }
69
+
70
+ const MenubarMenuContext = createContext<MenubarMenuApi>("MenubarMenu");
71
+
31
72
  export interface MenubarProps {
32
- menus: readonly MenubarMenu[];
33
73
  class?: Reactive<string>;
74
+ children?: Parts;
34
75
  }
35
76
 
36
77
  export const Menubar = component<MenubarProps>((props) => {
37
78
  const contentId = uid("menubar-content");
38
- const triggerIds = props.menus.map(() => uid("menubar-trigger"));
39
- /** Index of the open menu, or `-1`. */
79
+ const menus: MenuRegistration[] = [];
40
80
  const openIndex = signal(-1);
41
81
 
42
82
  let unwire: (() => void) | null = null;
@@ -47,35 +87,74 @@ export const Menubar = component<MenubarProps>((props) => {
47
87
  }
48
88
 
49
89
  function openAt(index: number, withKeyboard: boolean): void {
50
- if (props.menus[index]?.disabled === true) return;
90
+ if (menus[index]?.disabled === true) return;
51
91
  focusFirstOnOpen = withKeyboard;
52
92
  openIndex(index);
53
93
  }
54
94
 
95
+ function focusTrigger(index: number): void {
96
+ const count = menus.length;
97
+ if (count === 0) return;
98
+ const entry = menus[(index + count) % count];
99
+ if (entry === undefined) return;
100
+ document.getElementById(entry.triggerId)?.focus({ preventScroll: true });
101
+ }
102
+
55
103
  function move(delta: number): void {
56
- const count = props.menus.length;
104
+ const count = menus.length;
57
105
  if (count === 0) return;
58
106
  const from = openIndex();
59
107
  if (from === -1) return;
60
108
  openAt((from + delta + count) % count, true);
61
109
  }
62
110
 
111
+ function onPanelKeyDown(event: KeyboardEvent): void {
112
+ if (event.key === "ArrowRight") {
113
+ event.preventDefault();
114
+ move(1);
115
+ } else if (event.key === "ArrowLeft") {
116
+ event.preventDefault();
117
+ move(-1);
118
+ }
119
+ }
120
+
121
+ provide<MenubarApi>(MenubarContext, {
122
+ contentId,
123
+ claim(triggerId) {
124
+ menus.push({
125
+ triggerId,
126
+ disabled: false,
127
+ body: null,
128
+ panelClass: undefined,
129
+ });
130
+ return menus.length - 1;
131
+ },
132
+ describe(index, patch) {
133
+ const entry = menus[index];
134
+ if (entry === undefined) return;
135
+ Object.assign(entry, patch);
136
+ },
137
+ openIndex: () => openIndex(),
138
+ openAt,
139
+ close,
140
+ focusTrigger,
141
+ closeAll: close,
142
+ });
143
+
144
+ const parts = props.children?.();
145
+
63
146
  floatingSurface({
64
147
  anchor: () => {
65
- const id = triggerIds[openIndex()];
66
- return id === undefined ? null : document.getElementById(id);
148
+ const entry = menus[openIndex()];
149
+ return entry === undefined
150
+ ? null
151
+ : document.getElementById(entry.triggerId);
67
152
  },
68
153
  open: () => openIndex() !== -1,
69
154
  onClose: close,
70
155
  placement: "bottom-start",
71
156
  offset: 4,
72
- content: () =>
73
- menuPanel({
74
- id: contentId,
75
- entries: props.menus[openIndex()]?.entries ?? [],
76
- onCloseAll: close,
77
- labelledBy: triggerIds[openIndex()],
78
- }),
157
+ content: () => renderPanel(menus[openIndex()], contentId),
79
158
  onOpened: (panel) => {
80
159
  const enterItems = focusFirstOnOpen;
81
160
  focusFirstOnOpen = false;
@@ -98,16 +177,6 @@ export const Menubar = component<MenubarProps>((props) => {
98
177
  },
99
178
  });
100
179
 
101
- function onPanelKeyDown(event: KeyboardEvent): void {
102
- if (event.key === "ArrowRight") {
103
- event.preventDefault();
104
- move(1);
105
- } else if (event.key === "ArrowLeft") {
106
- event.preventDefault();
107
- move(-1);
108
- }
109
- }
110
-
111
180
  return html`<div
112
181
  data-slot="menubar"
113
182
  role="menubar"
@@ -116,52 +185,143 @@ export const Menubar = component<MenubarProps>((props) => {
116
185
  "bg-background flex h-9 items-center gap-1 rounded-md border p-1 shadow-xs",
117
186
  read(props.class),
118
187
  )}"
119
- >
120
- ${props.menus.map((menu, index) => renderTrigger(menu, index))}
121
- </div>`;
122
-
123
- function renderTrigger(menu: MenubarMenu, index: number): Child {
124
- return html`<button
125
- type="button"
126
- role="menuitem"
127
- data-slot="menubar-trigger"
128
- id="${triggerIds[index]}"
129
- aria-haspopup="menu"
130
- aria-expanded="${() => (openIndex() === index ? "true" : "false")}"
131
- aria-controls="${() => (openIndex() === index ? contentId : undefined)}"
132
- data-state="${() => (openIndex() === index ? "open" : "closed")}"
133
- ?disabled="${menu.disabled === true}"
134
- class="focus:bg-accent focus:text-accent-foreground data-[state=open]:bg-accent data-[state=open]:text-accent-foreground flex select-none items-center rounded-sm px-2 py-1 text-sm font-medium outline-none disabled:pointer-events-none disabled:opacity-50"
135
- @click="${() => (openIndex() === index ? close() : openAt(index, false))}"
136
- @pointerenter="${() => {
137
- // Hover switches menus only while one is already open — otherwise
138
- // the bar would spring menus at a pointer merely passing over it.
139
- if (openIndex() !== -1) openAt(index, false);
140
- }}"
141
- @keydown="${(event: KeyboardEvent) => {
142
- if (
143
- event.key === "ArrowDown" ||
144
- event.key === "Enter" ||
145
- event.key === " "
146
- ) {
147
- event.preventDefault();
148
- openAt(index, true);
149
- } else if (event.key === "ArrowRight") {
150
- event.preventDefault();
151
- focusTrigger(index + 1);
152
- } else if (event.key === "ArrowLeft") {
153
- event.preventDefault();
154
- focusTrigger(index - 1);
155
- }
156
- }}"
157
- >${menu.label}</button>`;
188
+ >${parts}</div>`;
189
+ });
190
+
191
+ export interface MenubarMenuProps {
192
+ children?: Parts;
193
+ }
194
+
195
+ /**
196
+ * One menu in the bar.
197
+ *
198
+ * Claims its index as it is built, and provides it to the trigger and the
199
+ * content inside — neither of which then has to be told which menu it belongs
200
+ * to.
201
+ */
202
+ export const MenubarMenu = component<MenubarMenuProps>((props) => {
203
+ const bar = inject(MenubarContext);
204
+ const triggerId = uid("menubar-trigger");
205
+ const index = bar.claim(triggerId);
206
+
207
+ provide<MenubarMenuApi>(MenubarMenuContext, { index, triggerId });
208
+
209
+ return html`${props.children?.()}`;
210
+ });
211
+
212
+ export interface MenubarTriggerProps {
213
+ children?: Slot;
214
+ disabled?: boolean;
215
+ class?: Reactive<string>;
216
+ }
217
+
218
+ export const MenubarTrigger = component<MenubarTriggerProps>((props) => {
219
+ const bar = inject(MenubarContext);
220
+ const menu = inject(MenubarMenuContext);
221
+ if (props.disabled === true) {
222
+ bar.describe(menu.index, { disabled: true });
158
223
  }
224
+ const open = (): boolean => bar.openIndex() === menu.index;
159
225
 
160
- function focusTrigger(index: number): void {
161
- const count = props.menus.length;
162
- if (count === 0) return;
163
- const id = triggerIds[(index + count) % count];
164
- if (id === undefined) return;
165
- document.getElementById(id)?.focus({ preventScroll: true });
226
+ function onKeyDown(event: KeyboardEvent): void {
227
+ if (
228
+ event.key === "ArrowDown" ||
229
+ event.key === "Enter" ||
230
+ event.key === " "
231
+ ) {
232
+ event.preventDefault();
233
+ bar.openAt(menu.index, true);
234
+ } else if (event.key === "ArrowRight") {
235
+ event.preventDefault();
236
+ bar.focusTrigger(menu.index + 1);
237
+ } else if (event.key === "ArrowLeft") {
238
+ event.preventDefault();
239
+ bar.focusTrigger(menu.index - 1);
240
+ }
166
241
  }
242
+
243
+ return html`<button
244
+ type="button"
245
+ data-slot="menubar-trigger"
246
+ id="${menu.triggerId}"
247
+ role="menuitem"
248
+ aria-haspopup="menu"
249
+ aria-expanded="${() => (open() ? "true" : "false")}"
250
+ aria-controls="${() => (open() ? bar.contentId : undefined)}"
251
+ data-state="${() => (open() ? "open" : "closed")}"
252
+ ?disabled="${props.disabled === true}"
253
+ class="${() => cn(triggerClasses, read(props.class))}"
254
+ @click="${() => (open() ? bar.close() : bar.openAt(menu.index, false))}"
255
+ @pointerenter="${() => {
256
+ // Hover switches menus only while one is already open — otherwise the
257
+ // bar would spring menus at a pointer merely passing over it.
258
+ if (bar.openIndex() !== -1) bar.openAt(menu.index, false);
259
+ }}"
260
+ @keydown="${onKeyDown}"
261
+ >${slot(props.children)}</button>`;
262
+ });
263
+
264
+ export interface MenubarContentProps {
265
+ children?: Parts;
266
+ class?: Reactive<string>;
267
+ }
268
+
269
+ export const MenubarContent = component<MenubarContentProps>((props) => {
270
+ const bar = inject(MenubarContext);
271
+ const menu = inject(MenubarMenuContext);
272
+ bar.describe(menu.index, {
273
+ body: props.children?.(),
274
+ panelClass: props.class,
275
+ });
276
+ return html``;
167
277
  });
278
+
279
+ const parts = createMenuParts("menubar", MenubarContext);
280
+
281
+ export const MenubarGroup = parts.Group;
282
+ export const MenubarItem = parts.Item;
283
+ export const MenubarCheckboxItem = parts.CheckboxItem;
284
+ export const MenubarRadioGroup = parts.RadioGroup;
285
+ export const MenubarRadioItem = parts.RadioItem;
286
+ export const MenubarLabel = parts.Label;
287
+ export const MenubarSeparator = parts.Separator;
288
+ export const MenubarShortcut = parts.Shortcut;
289
+ export const MenubarSub = parts.Sub;
290
+ export const MenubarSubTrigger = parts.SubTrigger;
291
+ export const MenubarSubContent = parts.SubContent;
292
+ export const MenubarPortal = parts.Portal;
293
+
294
+ export type {
295
+ MenuCheckboxItemProps as MenubarCheckboxItemProps,
296
+ MenuGroupProps as MenubarGroupProps,
297
+ MenuItemProps as MenubarItemProps,
298
+ MenuLabelProps as MenubarLabelProps,
299
+ MenuPortalProps as MenubarPortalProps,
300
+ MenuRadioGroupProps as MenubarRadioGroupProps,
301
+ MenuRadioItemProps as MenubarRadioItemProps,
302
+ MenuSeparatorProps as MenubarSeparatorProps,
303
+ MenuShortcutProps as MenubarShortcutProps,
304
+ MenuSubContentProps as MenubarSubContentProps,
305
+ MenuSubProps as MenubarSubProps,
306
+ MenuSubTriggerProps as MenubarSubTriggerProps,
307
+ } from "../primitives/menuParts.js";
308
+
309
+ function renderPanel(
310
+ entry: MenuRegistration | undefined,
311
+ contentId: string,
312
+ ): TemplateResult {
313
+ return html`<div
314
+ data-slot="menubar-content"
315
+ id="${contentId}"
316
+ role="menu"
317
+ aria-labelledby="${entry?.triggerId}"
318
+ aria-orientation="vertical"
319
+ tabindex="-1"
320
+ class="${cn(
321
+ menuPanelClasses,
322
+ zoomInOut,
323
+ slideInFromSide,
324
+ read(entry?.panelClass),
325
+ )}"
326
+ >${entry?.body}</div>`;
327
+ }