@c9up/nebula 0.2.3 → 0.2.5

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 (99) hide show
  1. package/README.md +9 -8
  2. package/dist/atoms/AspectRatio.d.ts +10 -4
  3. package/dist/atoms/AspectRatio.js +21 -6
  4. package/dist/atoms/Avatar.d.ts +14 -0
  5. package/dist/atoms/Avatar.js +46 -8
  6. package/dist/atoms/NativeSelect.d.ts +10 -0
  7. package/dist/atoms/NativeSelect.js +4 -1
  8. package/dist/atoms/Progress.d.ts +9 -1
  9. package/dist/atoms/Progress.js +21 -5
  10. package/dist/atoms/Switch.d.ts +7 -1
  11. package/dist/atoms/Switch.js +7 -1
  12. package/dist/cli/add.d.ts +2 -0
  13. package/dist/cli/add.js +4 -1
  14. package/dist/cli/diff.d.ts +48 -0
  15. package/dist/cli/diff.js +77 -3
  16. package/dist/cli/projectConfig.d.ts +16 -0
  17. package/dist/cli/projectConfig.js +61 -0
  18. package/dist/console/AddCommand.js +6 -1
  19. package/dist/console/DiffCommand.js +12 -12
  20. package/dist/lib/icons.d.ts +8 -0
  21. package/dist/lib/icons.js +8 -0
  22. package/dist/lib/image.js +12 -1
  23. package/dist/molecules/Accordion.js +1 -2
  24. package/dist/molecules/Collapsible.js +1 -2
  25. package/dist/molecules/InputOTP.d.ts +12 -0
  26. package/dist/molecules/InputOTP.js +68 -27
  27. package/dist/molecules/Resizable.d.ts +6 -0
  28. package/dist/molecules/Resizable.js +8 -2
  29. package/dist/molecules/index.d.ts +1 -1
  30. package/dist/molecules/index.js +1 -1
  31. package/dist/organisms/AlertDialog.js +4 -5
  32. package/dist/organisms/Chart.d.ts +103 -15
  33. package/dist/organisms/Chart.js +518 -99
  34. package/dist/organisms/Combobox.d.ts +13 -0
  35. package/dist/organisms/Combobox.js +5 -2
  36. package/dist/organisms/Command.js +56 -8
  37. package/dist/organisms/Dialog.js +7 -7
  38. package/dist/organisms/Form.d.ts +15 -0
  39. package/dist/organisms/Form.js +10 -0
  40. package/dist/organisms/Select.d.ts +6 -4
  41. package/dist/organisms/Select.js +1 -0
  42. package/dist/organisms/Sidebar.d.ts +9 -9
  43. package/dist/organisms/Sidebar.js +77 -32
  44. package/dist/organisms/Sonner.d.ts +243 -0
  45. package/dist/organisms/Sonner.js +916 -0
  46. package/dist/organisms/Toaster.d.ts +9 -0
  47. package/dist/organisms/Toaster.js +28 -5
  48. package/dist/organisms/Tooltip.d.ts +4 -0
  49. package/dist/organisms/index.d.ts +7 -1
  50. package/dist/organisms/index.js +7 -1
  51. package/dist/primitives/dismissable.d.ts +11 -4
  52. package/dist/primitives/dismissable.js +8 -6
  53. package/dist/primitives/floating.d.ts +5 -1
  54. package/dist/primitives/floating.js +8 -1
  55. package/dist/primitives/menu.js +2 -1
  56. package/dist/primitives/menuParts.js +3 -3
  57. package/dist/primitives/modalSurface.js +70 -36
  58. package/dist/primitives/portal.d.ts +15 -0
  59. package/dist/primitives/portal.js +18 -1
  60. package/dist/primitives/presence.js +8 -5
  61. package/nebula.css +2 -2
  62. package/package.json +1 -1
  63. package/registry.json +13 -0
  64. package/src/atoms/AspectRatio.ts +21 -6
  65. package/src/atoms/Avatar.ts +54 -7
  66. package/src/atoms/NativeSelect.ts +14 -1
  67. package/src/atoms/Progress.ts +21 -5
  68. package/src/atoms/Switch.ts +7 -1
  69. package/src/cli/add.ts +7 -1
  70. package/src/cli/diff.ts +130 -3
  71. package/src/cli/projectConfig.ts +71 -0
  72. package/src/console/AddCommand.ts +6 -1
  73. package/src/console/DiffCommand.ts +18 -16
  74. package/src/lib/icons.ts +28 -0
  75. package/src/lib/image.ts +13 -1
  76. package/src/molecules/Accordion.ts +1 -2
  77. package/src/molecules/Collapsible.ts +1 -3
  78. package/src/molecules/InputOTP.ts +81 -27
  79. package/src/molecules/Resizable.ts +8 -2
  80. package/src/molecules/index.ts +3 -0
  81. package/src/organisms/AlertDialog.ts +3 -5
  82. package/src/organisms/Chart.ts +633 -125
  83. package/src/organisms/Combobox.ts +18 -2
  84. package/src/organisms/Command.ts +66 -7
  85. package/src/organisms/Dialog.ts +6 -7
  86. package/src/organisms/Form.ts +21 -0
  87. package/src/organisms/Select.ts +7 -4
  88. package/src/organisms/Sidebar.ts +75 -25
  89. package/src/organisms/Sonner.ts +1373 -0
  90. package/src/organisms/Toaster.ts +30 -6
  91. package/src/organisms/Tooltip.ts +4 -0
  92. package/src/organisms/index.ts +29 -0
  93. package/src/primitives/dismissable.ts +16 -8
  94. package/src/primitives/floating.ts +13 -2
  95. package/src/primitives/menu.ts +2 -1
  96. package/src/primitives/menuParts.ts +3 -3
  97. package/src/primitives/modalSurface.ts +77 -40
  98. package/src/primitives/portal.ts +27 -1
  99. package/src/primitives/presence.ts +12 -9
@@ -23,8 +23,21 @@ export interface ComboboxOption {
23
23
  disabled?: boolean;
24
24
  group?: string;
25
25
  }
26
+ /**
27
+ * `id`, `label`, `labelledBy` and `describedBy` name the trigger. Upstream's
28
+ * trigger is a Button that takes every button prop; these are the ones that
29
+ * name it — a `<Label for>` or a `FieldLabel` points at the `id`. Without them
30
+ * the combobox's only name was the value it showed.
31
+ */
26
32
  export interface ComboboxProps {
27
33
  options: readonly ComboboxOption[];
34
+ /** The trigger's id; a unique one is minted when left out. */
35
+ id?: string;
36
+ /** Accessible name (`aria-label`), for a combobox no `<label>` names. */
37
+ label?: Reactive<string | undefined>;
38
+ /** Id(s) of the element(s) that name it (`aria-labelledby`). */
39
+ labelledBy?: Reactive<string | undefined>;
40
+ describedBy?: Reactive<string | undefined>;
28
41
  name?: string;
29
42
  value?: Reactive<string | undefined>;
30
43
  defaultValue?: string;
@@ -19,14 +19,14 @@ import { cn } from "../lib/cn.js";
19
19
  import { ChevronsUpDownIcon } from "../lib/icons.js";
20
20
  import { uid } from "../lib/id.js";
21
21
  import { zoomInOut } from "../lib/motion.js";
22
- import { read, readOr } from "../lib/props.js";
22
+ import { accessor, read, readOr } from "../lib/props.js";
23
23
  import { controllable } from "../primitives/controllable.js";
24
24
  import { floatingSurface } from "../primitives/floatingSurface.js";
25
25
  import { focusSilently } from "../primitives/focusable.js";
26
26
  import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "./Command.js";
27
27
  import { selectTriggerClasses } from "./Select.js";
28
28
  export const Combobox = component((props) => {
29
- const triggerId = uid("combobox-trigger");
29
+ const triggerId = props.id ?? uid("combobox-trigger");
30
30
  const contentId = uid("combobox-content");
31
31
  const open = signal(false);
32
32
  const selection = controllable({
@@ -112,6 +112,9 @@ export const Combobox = component((props) => {
112
112
  type="button"
113
113
  data-slot="combobox-trigger"
114
114
  id="${triggerId}"
115
+ aria-label="${accessor(props.label, undefined)}"
116
+ aria-labelledby="${accessor(props.labelledBy, undefined)}"
117
+ aria-describedby="${accessor(props.describedBy, undefined)}"
115
118
  role="combobox"
116
119
  aria-haspopup="listbox"
117
120
  aria-expanded="${() => (open() ? "true" : "false")}"
@@ -23,7 +23,7 @@
23
23
  * focused, which is what lets a screen reader read each result as the arrows
24
24
  * pass over it while typing continues to work.
25
25
  */
26
- import { component, createContext, html, inject, provide, signal, } from "@c9up/aurora";
26
+ import { component, createContext, html, inject, onMount, onUnmount, provide, signal, } from "@c9up/aurora";
27
27
  import { slot } from "../lib/children.js";
28
28
  import { cn } from "../lib/cn.js";
29
29
  import { SearchIcon } from "../lib/icons.js";
@@ -70,6 +70,33 @@ export const Command = component((props) => {
70
70
  * Nothing needs the value stored before the user moves.
71
71
  */
72
72
  const explicitActive = signal(undefined);
73
+ /**
74
+ * Bumped when items mount or unmount, so the bindings that read the list
75
+ * — the empty state, the groups, the highlight — follow it. `items` itself
76
+ * stays a plain array, filled during setup: a signal written while the
77
+ * items are being built re-entered the effect building them.
78
+ *
79
+ * Coalesced to one bump per task, as cmdk schedules its store updates: a
80
+ * list of a hundred items mounting is one pass over the bindings, not a
81
+ * hundred.
82
+ */
83
+ const revision = signal(0);
84
+ let scheduled = false;
85
+ function changed() {
86
+ if (scheduled)
87
+ return;
88
+ scheduled = true;
89
+ queueMicrotask(() => {
90
+ scheduled = false;
91
+ // The highlighted item left: back to "the first match", as cmdk
92
+ // re-selects the first item when the selected one unmounts.
93
+ const chosen = explicitActive();
94
+ if (chosen !== undefined && !items.some((i) => i.value === chosen)) {
95
+ explicitActive(undefined);
96
+ }
97
+ revision(revision() + 1);
98
+ });
99
+ }
73
100
  function active() {
74
101
  const chosen = explicitActive();
75
102
  if (chosen !== undefined)
@@ -77,15 +104,20 @@ export const Command = component((props) => {
77
104
  return matches()[0]?.value;
78
105
  }
79
106
  function matches() {
107
+ revision();
80
108
  const filter = props.filter ?? defaultFilter;
81
109
  return items.filter((item) => !item.disabled && filter(item, query()));
82
110
  }
83
- function run(value) {
84
- const item = items.find((entry) => entry.value === value);
85
- if (item === undefined || item.disabled)
111
+ function runItem(item) {
112
+ if (item.disabled || !items.includes(item))
86
113
  return;
87
114
  item.run();
88
- props.onSelect?.(value);
115
+ props.onSelect?.(item.value);
116
+ }
117
+ function run(value) {
118
+ const item = items.find((entry) => entry.value === value);
119
+ if (item !== undefined)
120
+ runItem(item);
89
121
  }
90
122
  function move(delta) {
91
123
  const list = matches();
@@ -109,7 +141,19 @@ export const Command = component((props) => {
109
141
  setActive: (value) => explicitActive(value),
110
142
  itemId,
111
143
  register(item) {
112
- items.push({ ...item, id: itemId(item.value) });
144
+ const entry = { ...item, id: itemId(item.value) };
145
+ items.push(entry);
146
+ return {
147
+ select: () => runItem(entry),
148
+ mounted: changed,
149
+ unregister() {
150
+ const index = items.indexOf(entry);
151
+ if (index === -1)
152
+ return;
153
+ items.splice(index, 1);
154
+ changed();
155
+ },
156
+ };
113
157
  },
114
158
  isVisible(value) {
115
159
  return matches().some((item) => item.value === value);
@@ -214,7 +258,9 @@ export const CommandItem = component((props) => {
214
258
  const group = inject(CommandGroupContext);
215
259
  const children = props.children;
216
260
  const id = command.itemId(props.value);
217
- command.register({
261
+ // Registered during setup, so the first pass over the bindings sees it;
262
+ // gone again when it unmounts, as cmdk's item cleanup.
263
+ const registration = command.register({
218
264
  value: props.value,
219
265
  textValue: props.textValue ??
220
266
  (typeof children === "string" ? children : props.value),
@@ -223,6 +269,8 @@ export const CommandItem = component((props) => {
223
269
  groupId: group?.groupId,
224
270
  run: () => props.onSelect?.(),
225
271
  });
272
+ onMount(() => registration.mounted());
273
+ onUnmount(() => registration.unregister());
226
274
  const chosen = () => command.active() === props.value;
227
275
  return html `<div
228
276
  data-slot="command-item"
@@ -234,7 +282,7 @@ export const CommandItem = component((props) => {
234
282
  data-active="${() => (chosen() ? "" : undefined)}"
235
283
  data-disabled="${props.disabled === true ? "" : undefined}"
236
284
  class="${() => cn("relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-[active]:bg-accent data-[active]:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4", read(props.class))}"
237
- @click="${() => command.run(props.value)}"
285
+ @click="${() => registration.select()}"
238
286
  @pointerenter="${() => command.setActive(props.value)}"
239
287
  >${slot(children)}</div>`;
240
288
  });
@@ -37,8 +37,8 @@
37
37
  * `DialogTitle` is warned about rather than tolerated — as Radix did through
38
38
  * 1.1; 1.3 counts its titles and no longer warns.
39
39
  */
40
- import { component, createContext, html, inject, onUnmount, provide, signal, } from "@c9up/aurora";
41
- import { Button } from "../atoms/Button.js";
40
+ import { component, createContext, html, inject, provide, signal, } from "@c9up/aurora";
41
+ import { buttonVariants } from "../atoms/Button.js";
42
42
  import { slot } from "../lib/children.js";
43
43
  import { cn } from "../lib/cn.js";
44
44
  import { XIcon } from "../lib/icons.js";
@@ -47,7 +47,7 @@ import { fadeInOut, zoomInOut } from "../lib/motion.js";
47
47
  import { read } from "../lib/props.js";
48
48
  import { controllable } from "../primitives/controllable.js";
49
49
  import { modalSurface } from "../primitives/modalSurface.js";
50
- import { portal } from "../primitives/portal.js";
50
+ import { portalOnMount } from "../primitives/portal.js";
51
51
  export const dialogBackdropClasses = "fixed inset-0 z-50 bg-black/50 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0";
52
52
  export const dialogPanelClasses = "bg-background fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-lg border p-6 shadow-lg duration-200 outline-none sm:max-w-lg";
53
53
  const closeButtonClasses = "ring-offset-background focus-visible:ring-ring absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0";
@@ -151,11 +151,10 @@ export const DialogOverlay = component((props) => html `<div
151
151
  * rather than standing in as a passthrough.
152
152
  */
153
153
  export const DialogPortal = component((props) => {
154
- const mounted = portal(html `${slot(props.children)}`, {
154
+ portalOnMount(html `${slot(props.children)}`, {
155
155
  container: props.container,
156
+ slot: "dialog-portal",
156
157
  });
157
- mounted.host.setAttribute("data-slot", "dialog-portal");
158
- onUnmount(() => mounted.close());
159
158
  return html ``;
160
159
  });
161
160
  /**
@@ -188,8 +187,9 @@ export const DialogFooter = component((props) => {
188
187
  ? html `<button
189
188
  type="button"
190
189
  data-slot="dialog-close"
190
+ class="${buttonVariants({ variant: "outline" })}"
191
191
  @click="${() => dialog.setOpen(false)}"
192
- >${Button({ variant: "outline", children: props.closeLabel ?? "Close" })}</button>`
192
+ >${props.closeLabel ?? "Close"}</button>`
193
193
  : null;
194
194
  return html `<div
195
195
  data-slot="dialog-footer"
@@ -51,6 +51,14 @@ export interface FieldBinding<V> {
51
51
  export declare function bind<T, K extends keyof T & string>(form: AuroraForm<T>, name: K): FieldBinding<T[K]>;
52
52
  export interface FormProps<T> {
53
53
  form: AuroraForm<T>;
54
+ /**
55
+ * How the browser submits it before the page is hydrated, or when a
56
+ * script fails to load. Default `"post"`.
57
+ */
58
+ method?: "post" | "get" | "dialog";
59
+ /** Where that submission goes. Left out, it goes to the current page. */
60
+ action?: string;
61
+ enctype?: "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain";
54
62
  children?: Slot;
55
63
  class?: Reactive<string>;
56
64
  }
@@ -61,6 +69,13 @@ export interface FormProps<T> {
61
69
  * styled, appear one at a time, and vanish on scroll — and they would fire
62
70
  * before the controller's validation ever runs, so the user would see two
63
71
  * different error presentations for the same field.
72
+ *
73
+ * NAMED DEVIATION: `method="post"` unless told otherwise. shadcn's `Form`
74
+ * renders no `<form>`, so the app writes its own method; this one owns the
75
+ * element, and the server renders it. Pressed before hydration, a form with
76
+ * no method is a GET, and every named field — a password included — lands in
77
+ * the URL, the history and the access logs. An app with CSRF protection puts
78
+ * its token field among the children, so that early POST is accepted.
64
79
  */
65
80
  export declare function Form<T>(props: FormProps<T>): ReturnType<typeof html>;
66
81
  interface FieldShellProps {
@@ -52,11 +52,21 @@ export function bind(form, name) {
52
52
  * styled, appear one at a time, and vanish on scroll — and they would fire
53
53
  * before the controller's validation ever runs, so the user would see two
54
54
  * different error presentations for the same field.
55
+ *
56
+ * NAMED DEVIATION: `method="post"` unless told otherwise. shadcn's `Form`
57
+ * renders no `<form>`, so the app writes its own method; this one owns the
58
+ * element, and the server renders it. Pressed before hydration, a form with
59
+ * no method is a GET, and every named field — a password included — lands in
60
+ * the URL, the history and the access logs. An app with CSRF protection puts
61
+ * its token field among the children, so that early POST is accepted.
55
62
  */
56
63
  export function Form(props) {
57
64
  return html `<form
58
65
  data-slot="form"
59
66
  novalidate
67
+ method="${props.method ?? "post"}"
68
+ action="${props.action}"
69
+ enctype="${props.enctype}"
60
70
  class="${() => cn("flex flex-col gap-6", read(props.class))}"
61
71
  @submit="${(event) => {
62
72
  // A DOM listener returns nothing, so this promise is nobody's to
@@ -66,10 +66,10 @@ export interface SelectProps {
66
66
  }
67
67
  export declare const Select: (props?: SelectProps | undefined) => TemplateResult;
68
68
  /**
69
- * `id`, `label` and `describedBy` as on `Input`: upstream's trigger takes every
70
- * button prop, and these are the ones that name it — a `FieldLabel` points its
71
- * `for` at the `id`, and a `<label for>` names a `<button>`. Without them the
72
- * combobox's only name was the value it showed.
69
+ * `id`, `label`, `labelledBy` and `describedBy`, as on `Combobox`: upstream's
70
+ * trigger takes every button prop, and these are the ones that name it — a
71
+ * `FieldLabel` points its `for` at the `id`, and a `<label for>` names a
72
+ * `<button>`. Without them the combobox's only name was the value it showed.
73
73
  */
74
74
  export interface SelectTriggerProps {
75
75
  children?: Slot;
@@ -77,6 +77,8 @@ export interface SelectTriggerProps {
77
77
  id?: string;
78
78
  /** Accessible name (`aria-label`), for a select no `<label>` names. */
79
79
  label?: Reactive<string | undefined>;
80
+ /** Id(s) of the element(s) that name it (`aria-labelledby`). */
81
+ labelledBy?: Reactive<string | undefined>;
80
82
  describedBy?: Reactive<string | undefined>;
81
83
  class?: Reactive<string>;
82
84
  }
@@ -341,6 +341,7 @@ export const SelectTrigger = component((props) => {
341
341
  data-slot="select-trigger"
342
342
  id="${select.claimTriggerId(props.id)}"
343
343
  aria-label="${accessor(props.label, undefined)}"
344
+ aria-labelledby="${accessor(props.labelledBy, undefined)}"
344
345
  aria-describedby="${accessor(props.describedBy, undefined)}"
345
346
  role="combobox"
346
347
  aria-haspopup="listbox"
@@ -35,16 +35,23 @@ import { type Reactive } from "../lib/props.js";
35
35
  /**
36
36
  * The cookie-backed open state, shared by every sidebar on the page.
37
37
  *
38
- * A module singleton and not a per-provider signal: the shell renders once and
39
- * the trigger may sit in a header that is not inside the provider's subtree.
38
+ * In the browser, a module singleton and not a per-provider signal: the shell
39
+ * renders once and the trigger may sit in a header that is not inside the
40
+ * provider's subtree. On the server a module is shared by every request, so
41
+ * each call reads the cookie of the request being rendered instead.
40
42
  */
41
43
  export declare function sidebarState(defaultOpen?: boolean): Signal<boolean>;
42
44
  export declare function sidebarCollapsed(): boolean;
43
45
  export interface SidebarApi {
46
+ /** The column's state: persisted, and what the wide layout shows. */
44
47
  readonly open: () => boolean;
48
+ /** The drawer's state on a phone: closed on every load, never persisted. */
49
+ readonly openMobile: () => boolean;
45
50
  readonly isMobile: () => boolean;
46
51
  readonly state: () => "expanded" | "collapsed";
47
52
  setOpen(next: boolean): void;
53
+ setOpenMobile(next: boolean): void;
54
+ /** The drawer on a phone, the column otherwise. */
48
55
  toggleSidebar(): void;
49
56
  }
50
57
  /**
@@ -178,13 +185,6 @@ export interface SidebarMenuSkeletonProps {
178
185
  showIcon?: boolean;
179
186
  class?: Reactive<string>;
180
187
  }
181
- /**
182
- * A placeholder row while the navigation loads.
183
- *
184
- * The widths vary per row, which is what stops a loading column from reading
185
- * as a bar chart. Seeded from a random draw the moment the row is built, so it
186
- * does not shift on every re-render.
187
- */
188
188
  export declare const SidebarMenuSkeleton: (props?: SidebarMenuSkeletonProps | undefined) => import("@c9up/aurora").TemplateResult;
189
189
  export declare const SidebarMenuSub: (props?: {
190
190
  children?: Slot;
@@ -45,24 +45,33 @@ const COOKIE_NAME = "nebula:sidebar";
45
45
  const COOKIE_MAX_AGE_DAYS = 365;
46
46
  const MOBILE_BREAKPOINT = 768;
47
47
  let sharedOpen;
48
+ function openFromCookie(defaultOpen) {
49
+ return cookieState(COOKIE_NAME, defaultOpen, booleanCookie, {
50
+ path: "/",
51
+ maxAge: COOKIE_MAX_AGE_DAYS * 24 * 60 * 60,
52
+ sameSite: "lax",
53
+ });
54
+ }
48
55
  /**
49
56
  * The cookie-backed open state, shared by every sidebar on the page.
50
57
  *
51
- * A module singleton and not a per-provider signal: the shell renders once and
52
- * the trigger may sit in a header that is not inside the provider's subtree.
58
+ * In the browser, a module singleton and not a per-provider signal: the shell
59
+ * renders once and the trigger may sit in a header that is not inside the
60
+ * provider's subtree. On the server a module is shared by every request, so
61
+ * each call reads the cookie of the request being rendered instead.
53
62
  */
54
63
  export function sidebarState(defaultOpen = true) {
55
- sharedOpen ??= cookieState(COOKIE_NAME, defaultOpen, booleanCookie, {
56
- path: "/",
57
- maxAge: COOKIE_MAX_AGE_DAYS * 24 * 60 * 60,
58
- sameSite: "lax",
59
- });
64
+ if (typeof document === "undefined")
65
+ return openFromCookie(defaultOpen);
66
+ sharedOpen ??= openFromCookie(defaultOpen);
60
67
  return sharedOpen;
61
68
  }
62
69
  export function sidebarCollapsed() {
63
70
  return !sidebarState()();
64
71
  }
65
72
  const SidebarContext = createContext("SidebarProvider");
73
+ /** The same provider, for the parts that also work outside one. */
74
+ const NearestSidebar = createContext("SidebarProvider", null);
66
75
  /**
67
76
  * The sidebar's state, from inside it.
68
77
  *
@@ -74,12 +83,19 @@ export function useSidebar() {
74
83
  export const SidebarProvider = component((props) => {
75
84
  const open = sidebarState(props.defaultOpen ?? true);
76
85
  const mobile = signal(false);
86
+ // Apart from the column's, as upstream: sharing it opened the drawer over
87
+ // the page on every load, since the column is open by default and its
88
+ // state outlives the page in the cookie.
89
+ const openMobile = signal(false);
77
90
  function setOpen(next) {
78
91
  open(next);
79
92
  props.onOpenChange?.(next);
80
93
  }
81
94
  function toggleSidebar() {
82
- setOpen(!open());
95
+ if (mobile())
96
+ openMobile(!openMobile());
97
+ else
98
+ setOpen(!open());
83
99
  }
84
100
  function onKeyDown(event) {
85
101
  if (event.key !== "b" && event.key !== "B")
@@ -109,13 +125,16 @@ export const SidebarProvider = component((props) => {
109
125
  const controlled = read(props.open);
110
126
  return controlled === undefined ? open() : controlled;
111
127
  }
112
- provide(SidebarContext, {
128
+ const api = provide(SidebarContext, {
113
129
  open: current,
130
+ openMobile: () => openMobile(),
114
131
  isMobile: () => mobile(),
115
132
  state: () => (current() ? "expanded" : "collapsed"),
116
133
  setOpen,
134
+ setOpenMobile: (next) => openMobile(next),
117
135
  toggleSidebar,
118
136
  });
137
+ provide(NearestSidebar, api);
119
138
  return html `<div
120
139
  data-slot="sidebar-wrapper"
121
140
  class="${() => cn("flex min-h-svh w-full", read(props.class))}"
@@ -128,8 +147,8 @@ export const Sidebar = component((props) => {
128
147
  const body = props.children?.();
129
148
  return html `<div data-slot="sidebar-root">${() => sidebar.isMobile()
130
149
  ? Sheet({
131
- open: () => sidebar.open(),
132
- onOpenChange: (next) => sidebar.setOpen(next),
150
+ open: () => sidebar.openMobile(),
151
+ onOpenChange: (next) => sidebar.setOpenMobile(next),
133
152
  children: () => SheetContent({
134
153
  side,
135
154
  closeLabel: props.closeLabel,
@@ -168,20 +187,33 @@ export const Sidebar = component((props) => {
168
187
  class="flex h-full w-full flex-col"
169
188
  >${body}</div></nav>`}</div>`;
170
189
  });
171
- export const SidebarTrigger = component((props) => Button({
172
- variant: "ghost",
173
- size: "icon",
174
- label: props.label ?? "Toggle sidebar",
175
- class: () => cn("size-7", read(props.class)),
176
- onClick: () => {
177
- if (props.onToggle !== undefined) {
178
- props.onToggle();
179
- return;
180
- }
181
- sidebarState()(!sidebarState()());
182
- },
183
- children: PanelLeftIcon({ class: "size-4" }),
184
- }));
190
+ /** The provider's toggle, or the page-wide state's outside a provider. */
191
+ function toggleNearest() {
192
+ const sidebar = inject(NearestSidebar);
193
+ if (sidebar !== null)
194
+ return () => sidebar.toggleSidebar();
195
+ return () => {
196
+ const open = sidebarState();
197
+ open(!open());
198
+ };
199
+ }
200
+ export const SidebarTrigger = component((props) => {
201
+ const toggle = toggleNearest();
202
+ return Button({
203
+ variant: "ghost",
204
+ size: "icon",
205
+ label: props.label ?? "Toggle sidebar",
206
+ class: () => cn("size-7", read(props.class)),
207
+ onClick: () => {
208
+ if (props.onToggle !== undefined) {
209
+ props.onToggle();
210
+ return;
211
+ }
212
+ toggle();
213
+ },
214
+ children: PanelLeftIcon({ class: "size-4" }),
215
+ });
216
+ });
185
217
  /**
186
218
  * The hit area along the sidebar's edge that toggles it.
187
219
  *
@@ -190,15 +222,18 @@ export const SidebarTrigger = component((props) => Button({
190
222
  * keyboard has `cmd/ctrl + B` and the trigger, so a third stop on every page
191
223
  * would be noise.
192
224
  */
193
- export const SidebarRail = component((props) => html `<button
225
+ export const SidebarRail = component((props) => {
226
+ const toggle = toggleNearest();
227
+ return html `<button
194
228
  type="button"
195
229
  data-slot="sidebar-rail"
196
230
  aria-label="${props.label ?? "Toggle sidebar"}"
197
231
  tabindex="-1"
198
232
  title="${props.label ?? "Toggle sidebar"}"
199
233
  class="${() => cn("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", read(props.class))}"
200
- @click="${() => sidebarState()(!sidebarState()())}"
201
- ></button>`);
234
+ @click="${toggle}"
235
+ ></button>`;
236
+ });
202
237
  /** The page beside the sidebar. */
203
238
  export const SidebarInset = component((props) => html `<main
204
239
  data-slot="sidebar-inset"
@@ -252,6 +287,8 @@ export const sidebarMenuButtonVariants = cva("peer/menu-button ring-sidebar-ring
252
287
  defaultVariants: { variant: "default", size: "default" },
253
288
  });
254
289
  export const SidebarMenuButton = component((props) => {
290
+ const sidebar = inject(NearestSidebar);
291
+ const collapsed = () => sidebar === null ? sidebarCollapsed() : sidebar.state() === "collapsed";
255
292
  const merged = () => cn(sidebarMenuButtonVariants({
256
293
  variant: props.variant,
257
294
  size: props.size,
@@ -277,7 +314,7 @@ export const SidebarMenuButton = component((props) => {
277
314
  // Wrapped once, not toggled: swapping the wrapper on collapse would remount
278
315
  // the entry and lose focus mid-keyboard-navigation. The tooltip decides for
279
316
  // itself whether to open, which is the cheaper half to make conditional.
280
- return html `${() => sidebarCollapsed()
317
+ return html `${() => collapsed()
281
318
  ? Tooltip({
282
319
  children: () => html `${TooltipTrigger({
283
320
  children: () => entry,
@@ -303,14 +340,22 @@ export const SidebarMenuBadge = component((props) => html `<div
303
340
  *
304
341
  * The widths vary per row, which is what stops a loading column from reading
305
342
  * as a bar chart. Seeded from a random draw the moment the row is built, so it
306
- * does not shift on every re-render.
343
+ * does not shift on every re-render. The draw picks a class, not a `style`
344
+ * attribute, which a Content-Security-Policy without `'unsafe-inline'` blocks.
307
345
  */
346
+ const SKELETON_WIDTHS = [
347
+ "max-w-[50%]",
348
+ "max-w-[60%]",
349
+ "max-w-[70%]",
350
+ "max-w-[80%]",
351
+ "max-w-[90%]",
352
+ ];
308
353
  export const SidebarMenuSkeleton = component((props) => {
309
- const width = `${Math.floor(Math.random() * 40) + 50}%`;
354
+ const width = SKELETON_WIDTHS[Math.floor(Math.random() * SKELETON_WIDTHS.length)];
310
355
  return html `<div
311
356
  data-slot="sidebar-menu-skeleton"
312
357
  class="${() => cn("flex h-8 items-center gap-2 rounded-md px-2", read(props.class))}"
313
- >${props.showIcon === true ? Skeleton({ class: "size-4 rounded-md" }) : null}<div class="flex-1" style="${`max-width: ${width}`}">${Skeleton({
358
+ >${props.showIcon === true ? Skeleton({ class: "size-4 rounded-md" }) : null}<div class="${cn("flex-1", width)}">${Skeleton({
314
359
  class: "h-4 w-full",
315
360
  })}</div></div>`;
316
361
  });