@c9up/nebula 0.2.2 → 0.2.4

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 (116) hide show
  1. package/README.md +8 -7
  2. package/dist/atoms/Avatar.d.ts +14 -0
  3. package/dist/atoms/Avatar.js +46 -8
  4. package/dist/atoms/Checkbox.d.ts +1 -1
  5. package/dist/atoms/Checkbox.js +1 -1
  6. package/dist/atoms/Input.d.ts +5 -0
  7. package/dist/atoms/Input.js +1 -0
  8. package/dist/cli/add.d.ts +2 -0
  9. package/dist/cli/add.js +4 -1
  10. package/dist/cli/diff.d.ts +2 -0
  11. package/dist/cli/diff.js +4 -1
  12. package/dist/cli/projectConfig.d.ts +16 -0
  13. package/dist/cli/projectConfig.js +61 -0
  14. package/dist/console/AddCommand.js +6 -1
  15. package/dist/console/DiffCommand.js +6 -1
  16. package/dist/lib/children.d.ts +13 -8
  17. package/dist/lib/children.js +5 -0
  18. package/dist/lib/icons.d.ts +8 -0
  19. package/dist/lib/icons.js +8 -0
  20. package/dist/lib/image.js +12 -1
  21. package/dist/molecules/Breadcrumb.d.ts +11 -2
  22. package/dist/molecules/Breadcrumb.js +2 -2
  23. package/dist/molecules/InputOTP.d.ts +14 -0
  24. package/dist/molecules/InputOTP.js +71 -28
  25. package/dist/molecules/Pagination.d.ts +34 -2
  26. package/dist/molecules/Pagination.js +15 -6
  27. package/dist/molecules/PieceMap.d.ts +72 -0
  28. package/dist/molecules/PieceMap.js +78 -0
  29. package/dist/molecules/Resizable.d.ts +2 -0
  30. package/dist/molecules/Resizable.js +1 -1
  31. package/dist/molecules/index.d.ts +4 -3
  32. package/dist/molecules/index.js +2 -1
  33. package/dist/organisms/Calendar.d.ts +13 -0
  34. package/dist/organisms/Calendar.js +2 -2
  35. package/dist/organisms/Carousel.d.ts +8 -0
  36. package/dist/organisms/Carousel.js +8 -5
  37. package/dist/organisms/Combobox.d.ts +13 -0
  38. package/dist/organisms/Combobox.js +5 -2
  39. package/dist/organisms/Command.js +56 -8
  40. package/dist/organisms/DataTable.d.ts +23 -1
  41. package/dist/organisms/DataTable.js +57 -27
  42. package/dist/organisms/DatePicker.d.ts +7 -0
  43. package/dist/organisms/DatePicker.js +2 -1
  44. package/dist/organisms/DateRangePicker.d.ts +2 -0
  45. package/dist/organisms/DateRangePicker.js +2 -1
  46. package/dist/organisms/Dialog.d.ts +13 -3
  47. package/dist/organisms/Dialog.js +12 -5
  48. package/dist/organisms/Form.d.ts +15 -0
  49. package/dist/organisms/Form.js +10 -0
  50. package/dist/organisms/Questionnaire.d.ts +4 -0
  51. package/dist/organisms/Questionnaire.js +5 -2
  52. package/dist/organisms/Select.d.ts +25 -4
  53. package/dist/organisms/Select.js +116 -15
  54. package/dist/organisms/Sheet.d.ts +2 -0
  55. package/dist/organisms/Sheet.js +2 -1
  56. package/dist/organisms/Sidebar.d.ts +10 -2
  57. package/dist/organisms/Sidebar.js +62 -29
  58. package/dist/organisms/Sonner.d.ts +243 -0
  59. package/dist/organisms/Sonner.js +909 -0
  60. package/dist/organisms/Toaster.d.ts +11 -0
  61. package/dist/organisms/Toaster.js +34 -9
  62. package/dist/organisms/Tooltip.d.ts +4 -0
  63. package/dist/organisms/index.d.ts +8 -2
  64. package/dist/organisms/index.js +7 -1
  65. package/dist/primitives/dismissable.d.ts +11 -4
  66. package/dist/primitives/dismissable.js +8 -6
  67. package/dist/primitives/floating.d.ts +5 -1
  68. package/dist/primitives/floating.js +32 -2
  69. package/dist/primitives/menu.js +2 -1
  70. package/dist/primitives/modalSurface.js +70 -36
  71. package/dist/primitives/presence.js +8 -5
  72. package/dist/templates/SettingsLayout.d.ts +2 -0
  73. package/dist/templates/SettingsLayout.js +1 -1
  74. package/nebula.css +2 -2
  75. package/package.json +3 -3
  76. package/registry.json +26 -0
  77. package/src/atoms/Avatar.ts +54 -7
  78. package/src/atoms/Checkbox.ts +1 -1
  79. package/src/atoms/Input.ts +6 -0
  80. package/src/cli/add.ts +7 -1
  81. package/src/cli/diff.ts +7 -1
  82. package/src/cli/projectConfig.ts +71 -0
  83. package/src/console/AddCommand.ts +6 -1
  84. package/src/console/DiffCommand.ts +6 -1
  85. package/src/lib/children.ts +15 -7
  86. package/src/lib/icons.ts +28 -0
  87. package/src/lib/image.ts +13 -1
  88. package/src/molecules/Breadcrumb.ts +15 -4
  89. package/src/molecules/InputOTP.ts +88 -28
  90. package/src/molecules/Pagination.ts +50 -8
  91. package/src/molecules/PieceMap.ts +136 -0
  92. package/src/molecules/Resizable.ts +3 -1
  93. package/src/molecules/index.ts +17 -0
  94. package/src/organisms/Calendar.ts +16 -2
  95. package/src/organisms/Carousel.ts +17 -5
  96. package/src/organisms/Combobox.ts +18 -2
  97. package/src/organisms/Command.ts +66 -7
  98. package/src/organisms/DataTable.ts +95 -30
  99. package/src/organisms/DatePicker.ts +10 -2
  100. package/src/organisms/DateRangePicker.ts +4 -2
  101. package/src/organisms/Dialog.ts +17 -5
  102. package/src/organisms/Form.ts +21 -0
  103. package/src/organisms/Questionnaire.ts +10 -2
  104. package/src/organisms/Select.ts +140 -16
  105. package/src/organisms/Sheet.ts +5 -1
  106. package/src/organisms/Sidebar.ts +62 -23
  107. package/src/organisms/Sonner.ts +1366 -0
  108. package/src/organisms/Toaster.ts +38 -10
  109. package/src/organisms/Tooltip.ts +4 -0
  110. package/src/organisms/index.ts +30 -1
  111. package/src/primitives/dismissable.ts +16 -8
  112. package/src/primitives/floating.ts +40 -3
  113. package/src/primitives/menu.ts +2 -1
  114. package/src/primitives/modalSurface.ts +77 -40
  115. package/src/primitives/presence.ts +12 -9
  116. package/src/templates/SettingsLayout.ts +3 -1
@@ -19,10 +19,19 @@
19
19
  * ` })}
20
20
  * ` })
21
21
  *
22
- * The items REGISTER themselves as they are built, in document order, and the
23
- * keyboard model then walks that list rather than the DOM. That is deliberate:
24
- * the listbox pattern keeps focus on the trigger and only moves
25
- * `aria-activedescendant`, so there is no focused element to walk from.
22
+ * The items REGISTER themselves as they are built: what each one is called and
23
+ * whether it is disabled, by value. WHICH options exist and in what order is
24
+ * read from the mounted list, as Radix's collection reads it — so options built
25
+ * in an accessor follow a signal:
26
+ *
27
+ * SelectContent({ children: () => html`${() => roles().map((role) =>
28
+ * SelectItem({ value: role.id, children: role.name }))}` })
29
+ *
30
+ * Closed, the list is still mounted, outside the document, as Radix renders a
31
+ * closed select's content into a DocumentFragment: the options exist, so the
32
+ * trigger shows the chosen one's label and the keyboard can walk them. Focus
33
+ * stays on the trigger and only `aria-activedescendant` moves, so the walk is
34
+ * over those options, not over focusable elements.
26
35
  *
27
36
  * What a native `<select>` was doing for free, and all of which has to be put
28
37
  * back by hand:
@@ -42,12 +51,16 @@
42
51
  import {
43
52
  component,
44
53
  createContext,
54
+ effect,
45
55
  html,
46
56
  inject,
57
+ onMount,
47
58
  onUnmount,
48
59
  provide,
60
+ render,
49
61
  signal,
50
62
  type TemplateResult,
63
+ untrack,
51
64
  } from "@c9up/aurora";
52
65
  import type { Child, Parts, Slot } from "../lib/children.js";
53
66
  import { slot } from "../lib/children.js";
@@ -55,7 +68,7 @@ import { cn } from "../lib/cn.js";
55
68
  import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "../lib/icons.js";
56
69
  import { uid } from "../lib/id.js";
57
70
  import { slideInFromSide, zoomInOut } from "../lib/motion.js";
58
- import { type Reactive, read, readOr } from "../lib/props.js";
71
+ import { accessor, type Reactive, read, readOr } from "../lib/props.js";
59
72
  import { controllable } from "../primitives/controllable.js";
60
73
  import type { Align, Placement, Side } from "../primitives/floating.js";
61
74
  import { floatingSurface } from "../primitives/floatingSurface.js";
@@ -91,7 +104,12 @@ interface RegisteredContent {
91
104
  }
92
105
 
93
106
  interface SelectApi {
94
- readonly triggerId: string;
107
+ /**
108
+ * The trigger's id: the one it was given, or one minted here. Claimed as the
109
+ * trigger is built, which is before anything that reads it — the anchor,
110
+ * the list's `aria-labelledby`, the focus return — first runs.
111
+ */
112
+ claimTriggerId(id: string | undefined): string;
95
113
  readonly listId: string;
96
114
  readonly open: () => boolean;
97
115
  readonly value: () => string | undefined;
@@ -108,6 +126,12 @@ interface SelectApi {
108
126
  setActive(value: string | undefined): void;
109
127
  onTriggerKeyDown(event: KeyboardEvent): void;
110
128
  registerItem(item: RegisteredItem): void;
129
+ /**
130
+ * An option's element is in a mounted list; the returned function says it
131
+ * left. Counted, because the list can be mounted twice for a moment — the
132
+ * closing panel still fading out, the closed copy already in place.
133
+ */
134
+ attachItem(value: string): () => void;
111
135
  registerContent(content: RegisteredContent): void;
112
136
  }
113
137
 
@@ -126,9 +150,25 @@ export interface SelectProps {
126
150
  }
127
151
 
128
152
  export const Select = component<SelectProps>((props) => {
129
- const triggerId = uid("select-trigger");
153
+ let triggerId = uid("select-trigger");
130
154
  const listId = uid("select-list");
131
- const items: RegisteredItem[] = [];
155
+ /** What each value's option is called, latest registration first. */
156
+ const registry = new Map<string, RegisteredItem>();
157
+ /** Moves whenever an option registers, so the chosen label follows. */
158
+ const revision = signal(0);
159
+ /** How many mounted lists hold each value's option. */
160
+ const attached = new Map<string, number>();
161
+ /** Whether a list has been mounted at all — never, on the server. */
162
+ let mountedOnce = false;
163
+ // Untracked read: called while the accessor building an option runs, and
164
+ // reading the counter there would make that accessor re-run on every change.
165
+ const bump = (): void => {
166
+ revision(untrack(() => revision()) + 1);
167
+ };
168
+ /** The open panel, once it has rendered. */
169
+ let panel: HTMLElement | null = null;
170
+ /** Where the list is mounted while closed — outside the document. */
171
+ let closedHost: HTMLElement | null = null;
132
172
  /** Per instance, not per module: two selects may offer the same value. */
133
173
  const optionIds = new Map<string, string>();
134
174
  let content: RegisteredContent | undefined;
@@ -144,8 +184,26 @@ export const Select = component<SelectProps>((props) => {
144
184
  /** The option the keyboard is on. Distinct from the selected one. */
145
185
  const active = signal<string | undefined>(undefined);
146
186
 
187
+ /**
188
+ * The options the list holds now, in its order. Read from the mounted list,
189
+ * which is what drops an option a signal removed and places one it added;
190
+ * before anything is mounted (on the server), in registration order.
191
+ */
192
+ function items(): readonly RegisteredItem[] {
193
+ const root = open() && panel?.isConnected === true ? panel : closedHost;
194
+ if (root === null) return [...registry.values()];
195
+ const out: RegisteredItem[] = [];
196
+ for (const element of root.querySelectorAll<HTMLElement>(
197
+ "[data-slot=select-item]",
198
+ )) {
199
+ const item = registry.get(element.dataset.value ?? "");
200
+ if (item !== undefined && item.id === element.id) out.push(item);
201
+ }
202
+ return out;
203
+ }
204
+
147
205
  const enabled = (): readonly RegisteredItem[] =>
148
- items.filter((item) => !item.disabled);
206
+ items().filter((item) => !item.disabled);
149
207
 
150
208
  function choose(value: string): void {
151
209
  selection.set(value);
@@ -235,7 +293,10 @@ export const Select = component<SelectProps>((props) => {
235
293
  }
236
294
 
237
295
  provide<SelectApi>(SelectContext, {
238
- triggerId,
296
+ claimTriggerId(id) {
297
+ if (id !== undefined) triggerId = id;
298
+ return triggerId;
299
+ },
239
300
  listId,
240
301
  open: () => open(),
241
302
  value: () => selection.current(),
@@ -243,8 +304,15 @@ export const Select = component<SelectProps>((props) => {
243
304
  disabled: () => readOr(props.disabled, false),
244
305
  invalid: () => read(props.invalid) === true,
245
306
  required: props.required === true,
246
- chosenLabel: () =>
247
- items.find((item) => item.value === selection.current())?.textValue,
307
+ chosenLabel: () => {
308
+ revision();
309
+ const current = selection.current();
310
+ if (current === undefined) return undefined;
311
+ // An option a signal removed keeps its registration, but not its
312
+ // place in a list: its label must not stay on the trigger.
313
+ if (mountedOnce && !attached.has(current)) return undefined;
314
+ return registry.get(current)?.textValue;
315
+ },
248
316
  itemId(value) {
249
317
  let id = optionIds.get(value);
250
318
  if (id === undefined) {
@@ -261,7 +329,19 @@ export const Select = component<SelectProps>((props) => {
261
329
  setActive: (value) => active(value),
262
330
  onTriggerKeyDown,
263
331
  registerItem(item) {
264
- items.push(item);
332
+ registry.set(item.value, item);
333
+ bump();
334
+ },
335
+ attachItem(value) {
336
+ mountedOnce = true;
337
+ attached.set(value, (attached.get(value) ?? 0) + 1);
338
+ bump();
339
+ return () => {
340
+ const left = (attached.get(value) ?? 1) - 1;
341
+ if (left > 0) attached.set(value, left);
342
+ else attached.delete(value);
343
+ bump();
344
+ };
265
345
  },
266
346
  registerContent(registered) {
267
347
  content = registered;
@@ -288,7 +368,32 @@ export const Select = component<SelectProps>((props) => {
288
368
  // Focus never enters the panel — the listbox pattern keeps it on the
289
369
  // trigger — so nothing here traps or moves it.
290
370
  content: () => renderList(registered, { listId, triggerId }),
291
- onOpened: scrollActiveIntoView,
371
+ onOpened: (element) => {
372
+ panel = element;
373
+ scrollActiveIntoView(element);
374
+ },
375
+ });
376
+
377
+ onMount(() => {
378
+ let unmountClosed: (() => void) | null = null;
379
+ const stop = effect(() => {
380
+ if (open()) {
381
+ unmountClosed?.();
382
+ unmountClosed = null;
383
+ closedHost = null;
384
+ return;
385
+ }
386
+ if (unmountClosed !== null) return;
387
+ const host = document.createElement("div");
388
+ // Untracked: what the options read belongs to their own bindings,
389
+ // not to this effect, which only follows `open`.
390
+ unmountClosed = untrack(() => render(html`${registered.body}`, host));
391
+ closedHost = host;
392
+ });
393
+ return () => {
394
+ stop();
395
+ unmountClosed?.();
396
+ };
292
397
  });
293
398
  }
294
399
 
@@ -301,7 +406,7 @@ export const Select = component<SelectProps>((props) => {
301
406
  function scrollActiveIntoView(panel: HTMLElement): void {
302
407
  const current = active() ?? selection.current();
303
408
  if (current === undefined) return;
304
- const id = items.find((item) => item.value === current)?.id;
409
+ const id = registry.get(current)?.id;
305
410
  if (id === undefined) return;
306
411
  panel
307
412
  .querySelector(`#${CSS.escape(id)}`)
@@ -318,9 +423,21 @@ export const Select = component<SelectProps>((props) => {
318
423
  /></div>`;
319
424
  });
320
425
 
426
+ /**
427
+ * `id`, `label`, `labelledBy` and `describedBy`, as on `Combobox`: upstream's
428
+ * trigger takes every button prop, and these are the ones that name it — a
429
+ * `FieldLabel` points its `for` at the `id`, and a `<label for>` names a
430
+ * `<button>`. Without them the combobox's only name was the value it showed.
431
+ */
321
432
  export interface SelectTriggerProps {
322
433
  children?: Slot;
323
434
  size?: "default" | "sm";
435
+ id?: string;
436
+ /** Accessible name (`aria-label`), for a select no `<label>` names. */
437
+ label?: Reactive<string | undefined>;
438
+ /** Id(s) of the element(s) that name it (`aria-labelledby`). */
439
+ labelledBy?: Reactive<string | undefined>;
440
+ describedBy?: Reactive<string | undefined>;
324
441
  class?: Reactive<string>;
325
442
  }
326
443
 
@@ -329,7 +446,10 @@ export const SelectTrigger = component<SelectTriggerProps>((props) => {
329
446
  return html`<button
330
447
  type="button"
331
448
  data-slot="select-trigger"
332
- id="${select.triggerId}"
449
+ id="${select.claimTriggerId(props.id)}"
450
+ aria-label="${accessor(props.label, undefined)}"
451
+ aria-labelledby="${accessor(props.labelledBy, undefined)}"
452
+ aria-describedby="${accessor(props.describedBy, undefined)}"
333
453
  role="combobox"
334
454
  aria-haspopup="listbox"
335
455
  aria-expanded="${() => (select.open() ? "true" : "false")}"
@@ -449,6 +569,10 @@ export const SelectItem = component<SelectItemProps>((props) => {
449
569
  disabled: props.disabled === true,
450
570
  });
451
571
 
572
+ // On each mount, not once: the same option is mounted again every time the
573
+ // list opens or closes, and unmounted when a signal drops it.
574
+ onMount(() => select.attachItem(props.value));
575
+
452
576
  const chosen = (): boolean => select.value() === props.value;
453
577
  return html`<div
454
578
  data-slot="select-item"
@@ -51,6 +51,7 @@ interface RegisteredContent {
51
51
  readonly body: Child;
52
52
  readonly side: Side;
53
53
  readonly showCloseButton: boolean;
54
+ readonly closeLabel: string;
54
55
  readonly class?: Reactive<string>;
55
56
  }
56
57
 
@@ -197,6 +198,8 @@ export interface SheetContentProps {
197
198
  /** Which edge it comes from. Default `"right"`. */
198
199
  side?: Side;
199
200
  showCloseButton?: boolean;
201
+ /** The corner cross's accessible name. Default `"Close"`. */
202
+ closeLabel?: string;
200
203
  class?: Reactive<string>;
201
204
  }
202
205
 
@@ -216,6 +219,7 @@ export const SheetContent = component<SheetContentProps>((props) => {
216
219
  body,
217
220
  side: props.side ?? "right",
218
221
  showCloseButton: props.showCloseButton ?? true,
222
+ closeLabel: props.closeLabel ?? "Close",
219
223
  class: props.class,
220
224
  });
221
225
  return html``;
@@ -289,7 +293,7 @@ function renderSurface(
289
293
  data-slot="sheet-close"
290
294
  class="${closeButtonClasses}"
291
295
  @click="${() => ids.close()}"
292
- >${XIcon({ class: "size-4" })}<span class="sr-only">Close</span></button>`
296
+ >${XIcon({ class: "size-4" })}<span class="sr-only">${registered.closeLabel}</span></button>`
293
297
  : null;
294
298
  return html`<div data-slot="sheet-portal" class="${cn("fixed inset-0 z-50", fadeInOut)}">
295
299
  <div data-slot="sheet-overlay" class="${dialogBackdropClasses}"></div>
@@ -62,18 +62,25 @@ const MOBILE_BREAKPOINT = 768;
62
62
 
63
63
  let sharedOpen: Signal<boolean> | undefined;
64
64
 
65
+ function openFromCookie(defaultOpen: boolean): Signal<boolean> {
66
+ return cookieState(COOKIE_NAME, defaultOpen, booleanCookie, {
67
+ path: "/",
68
+ maxAge: COOKIE_MAX_AGE_DAYS * 24 * 60 * 60,
69
+ sameSite: "lax",
70
+ });
71
+ }
72
+
65
73
  /**
66
74
  * The cookie-backed open state, shared by every sidebar on the page.
67
75
  *
68
- * A module singleton and not a per-provider signal: the shell renders once and
69
- * the trigger may sit in a header that is not inside the provider's subtree.
76
+ * In the browser, a module singleton and not a per-provider signal: the shell
77
+ * renders once and the trigger may sit in a header that is not inside the
78
+ * provider's subtree. On the server a module is shared by every request, so
79
+ * each call reads the cookie of the request being rendered instead.
70
80
  */
71
81
  export function sidebarState(defaultOpen = true): Signal<boolean> {
72
- sharedOpen ??= cookieState(COOKIE_NAME, defaultOpen, booleanCookie, {
73
- path: "/",
74
- maxAge: COOKIE_MAX_AGE_DAYS * 24 * 60 * 60,
75
- sameSite: "lax",
76
- });
82
+ if (typeof document === "undefined") return openFromCookie(defaultOpen);
83
+ sharedOpen ??= openFromCookie(defaultOpen);
77
84
  return sharedOpen;
78
85
  }
79
86
 
@@ -90,6 +97,11 @@ export interface SidebarApi {
90
97
  }
91
98
 
92
99
  const SidebarContext = createContext<SidebarApi>("SidebarProvider");
100
+ /** The same provider, for the parts that also work outside one. */
101
+ const NearestSidebar = createContext<SidebarApi | null>(
102
+ "SidebarProvider",
103
+ null,
104
+ );
93
105
 
94
106
  /**
95
107
  * The sidebar's state, from inside it.
@@ -153,13 +165,14 @@ export const SidebarProvider = component<SidebarProviderProps>((props) => {
153
165
  return controlled === undefined ? open() : controlled;
154
166
  }
155
167
 
156
- provide<SidebarApi>(SidebarContext, {
168
+ const api = provide<SidebarApi>(SidebarContext, {
157
169
  open: current,
158
170
  isMobile: () => mobile(),
159
171
  state: () => (current() ? "expanded" : "collapsed"),
160
172
  setOpen,
161
173
  toggleSidebar,
162
174
  });
175
+ provide(NearestSidebar, api);
163
176
 
164
177
  return html`<div
165
178
  data-slot="sidebar-wrapper"
@@ -175,6 +188,8 @@ export interface SidebarProps {
175
188
  collapsible?: "icon" | "offcanvas" | "none";
176
189
  /** Announced as the navigation landmark's name. */
177
190
  label?: string;
191
+ /** The close cross of the sheet it turns into on mobile. Default `"Close"`. */
192
+ closeLabel?: string;
178
193
  class?: Reactive<string>;
179
194
  }
180
195
 
@@ -192,6 +207,7 @@ export const Sidebar = component<SidebarProps>((props) => {
192
207
  children: () =>
193
208
  SheetContent({
194
209
  side,
210
+ closeLabel: props.closeLabel,
195
211
  class: "bg-sidebar text-sidebar-foreground w-[18rem] p-0",
196
212
  children: () =>
197
213
  html`${SheetHeader({
@@ -210,7 +226,11 @@ export const Sidebar = component<SidebarProps>((props) => {
210
226
  data-slot="sidebar"
211
227
  data-state="${() => sidebar.state()}"
212
228
  data-side="${side}"
213
- data-collapsible="${collapsible}"
229
+ data-collapsible="${() =>
230
+ // The mode only once collapsed, as upstream: every
231
+ // `group-data-[collapsible=icon]:` class keys off it, and set
232
+ // while expanded it squeezed the full-width bar to icons.
233
+ sidebar.state() === "collapsed" ? collapsible : ""}"
214
234
  aria-label="${props.label ?? "Sidebar"}"
215
235
  class="${() =>
216
236
  cn(
@@ -234,27 +254,42 @@ export const Sidebar = component<SidebarProps>((props) => {
234
254
  export interface SidebarTriggerProps {
235
255
  /** Overrides the provider's toggle — for a trigger outside the shell. */
236
256
  onToggle?: () => void;
257
+ /** The button's accessible name. Default `"Toggle sidebar"`. */
258
+ label?: string;
237
259
  class?: Reactive<string>;
238
260
  }
239
261
 
240
- export const SidebarTrigger = component<SidebarTriggerProps>((props) =>
241
- Button({
262
+ /** The provider's toggle, or the page-wide state's outside a provider. */
263
+ function toggleNearest(): () => void {
264
+ const sidebar = inject(NearestSidebar);
265
+ if (sidebar !== null) return () => sidebar.toggleSidebar();
266
+ return () => {
267
+ const open = sidebarState();
268
+ open(!open());
269
+ };
270
+ }
271
+
272
+ export const SidebarTrigger = component<SidebarTriggerProps>((props) => {
273
+ const toggle = toggleNearest();
274
+ return Button({
242
275
  variant: "ghost",
243
276
  size: "icon",
244
- label: "Toggle sidebar",
277
+ label: props.label ?? "Toggle sidebar",
245
278
  class: () => cn("size-7", read(props.class)),
246
279
  onClick: () => {
247
280
  if (props.onToggle !== undefined) {
248
281
  props.onToggle();
249
282
  return;
250
283
  }
251
- sidebarState()(!sidebarState()());
284
+ toggle();
252
285
  },
253
286
  children: PanelLeftIcon({ class: "size-4" }),
254
- }),
255
- );
287
+ });
288
+ });
256
289
 
257
290
  export interface SidebarRailProps {
291
+ /** Accessible name and tooltip. Default `"Toggle sidebar"`. */
292
+ label?: string;
258
293
  class?: Reactive<string>;
259
294
  }
260
295
 
@@ -266,21 +301,22 @@ export interface SidebarRailProps {
266
301
  * keyboard has `cmd/ctrl + B` and the trigger, so a third stop on every page
267
302
  * would be noise.
268
303
  */
269
- export const SidebarRail = component<SidebarRailProps>(
270
- (props) => html`<button
304
+ export const SidebarRail = component<SidebarRailProps>((props) => {
305
+ const toggle = toggleNearest();
306
+ return html`<button
271
307
  type="button"
272
308
  data-slot="sidebar-rail"
273
- aria-label="Toggle sidebar"
309
+ aria-label="${props.label ?? "Toggle sidebar"}"
274
310
  tabindex="-1"
275
- title="Toggle sidebar"
311
+ title="${props.label ?? "Toggle sidebar"}"
276
312
  class="${() =>
277
313
  cn(
278
314
  "hover:after:bg-sidebar-border absolute inset-y-0 z-20 hidden w-4 -translate-x-1/2 transition-all ease-linear group-data-[side=left]:-right-4 group-data-[side=right]:left-0 after:absolute after:inset-y-0 after:left-1/2 after:w-[2px] sm:flex",
279
315
  read(props.class),
280
316
  )}"
281
- @click="${() => sidebarState()(!sidebarState()())}"
282
- ></button>`,
283
- );
317
+ @click="${toggle}"
318
+ ></button>`;
319
+ });
284
320
 
285
321
  export interface SidebarInsetProps {
286
322
  children?: Slot;
@@ -439,6 +475,9 @@ export interface SidebarMenuButtonProps extends SidebarMenuButtonVariants {
439
475
  }
440
476
 
441
477
  export const SidebarMenuButton = component<SidebarMenuButtonProps>((props) => {
478
+ const sidebar = inject(NearestSidebar);
479
+ const collapsed = (): boolean =>
480
+ sidebar === null ? sidebarCollapsed() : sidebar.state() === "collapsed";
442
481
  const merged = (): string =>
443
482
  cn(
444
483
  sidebarMenuButtonVariants({
@@ -472,7 +511,7 @@ export const SidebarMenuButton = component<SidebarMenuButtonProps>((props) => {
472
511
  // the entry and lose focus mid-keyboard-navigation. The tooltip decides for
473
512
  // itself whether to open, which is the cheaper half to make conditional.
474
513
  return html`${() =>
475
- sidebarCollapsed()
514
+ collapsed()
476
515
  ? Tooltip({
477
516
  children: () =>
478
517
  html`${TooltipTrigger({