@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.
- package/README.md +9 -8
- package/dist/atoms/AspectRatio.d.ts +10 -4
- package/dist/atoms/AspectRatio.js +21 -6
- package/dist/atoms/Avatar.d.ts +14 -0
- package/dist/atoms/Avatar.js +46 -8
- package/dist/atoms/NativeSelect.d.ts +10 -0
- package/dist/atoms/NativeSelect.js +4 -1
- package/dist/atoms/Progress.d.ts +9 -1
- package/dist/atoms/Progress.js +21 -5
- package/dist/atoms/Switch.d.ts +7 -1
- package/dist/atoms/Switch.js +7 -1
- package/dist/cli/add.d.ts +2 -0
- package/dist/cli/add.js +4 -1
- package/dist/cli/diff.d.ts +48 -0
- package/dist/cli/diff.js +77 -3
- package/dist/cli/projectConfig.d.ts +16 -0
- package/dist/cli/projectConfig.js +61 -0
- package/dist/console/AddCommand.js +6 -1
- package/dist/console/DiffCommand.js +12 -12
- package/dist/lib/icons.d.ts +8 -0
- package/dist/lib/icons.js +8 -0
- package/dist/lib/image.js +12 -1
- package/dist/molecules/Accordion.js +1 -2
- package/dist/molecules/Collapsible.js +1 -2
- package/dist/molecules/InputOTP.d.ts +12 -0
- package/dist/molecules/InputOTP.js +68 -27
- package/dist/molecules/Resizable.d.ts +6 -0
- package/dist/molecules/Resizable.js +8 -2
- package/dist/molecules/index.d.ts +1 -1
- package/dist/molecules/index.js +1 -1
- package/dist/organisms/AlertDialog.js +4 -5
- package/dist/organisms/Chart.d.ts +103 -15
- package/dist/organisms/Chart.js +518 -99
- package/dist/organisms/Combobox.d.ts +13 -0
- package/dist/organisms/Combobox.js +5 -2
- package/dist/organisms/Command.js +56 -8
- package/dist/organisms/Dialog.js +7 -7
- package/dist/organisms/Form.d.ts +15 -0
- package/dist/organisms/Form.js +10 -0
- package/dist/organisms/Select.d.ts +6 -4
- package/dist/organisms/Select.js +1 -0
- package/dist/organisms/Sidebar.d.ts +9 -9
- package/dist/organisms/Sidebar.js +77 -32
- package/dist/organisms/Sonner.d.ts +243 -0
- package/dist/organisms/Sonner.js +916 -0
- package/dist/organisms/Toaster.d.ts +9 -0
- package/dist/organisms/Toaster.js +28 -5
- package/dist/organisms/Tooltip.d.ts +4 -0
- package/dist/organisms/index.d.ts +7 -1
- package/dist/organisms/index.js +7 -1
- package/dist/primitives/dismissable.d.ts +11 -4
- package/dist/primitives/dismissable.js +8 -6
- package/dist/primitives/floating.d.ts +5 -1
- package/dist/primitives/floating.js +8 -1
- package/dist/primitives/menu.js +2 -1
- package/dist/primitives/menuParts.js +3 -3
- package/dist/primitives/modalSurface.js +70 -36
- package/dist/primitives/portal.d.ts +15 -0
- package/dist/primitives/portal.js +18 -1
- package/dist/primitives/presence.js +8 -5
- package/nebula.css +2 -2
- package/package.json +1 -1
- package/registry.json +13 -0
- package/src/atoms/AspectRatio.ts +21 -6
- package/src/atoms/Avatar.ts +54 -7
- package/src/atoms/NativeSelect.ts +14 -1
- package/src/atoms/Progress.ts +21 -5
- package/src/atoms/Switch.ts +7 -1
- package/src/cli/add.ts +7 -1
- package/src/cli/diff.ts +130 -3
- package/src/cli/projectConfig.ts +71 -0
- package/src/console/AddCommand.ts +6 -1
- package/src/console/DiffCommand.ts +18 -16
- package/src/lib/icons.ts +28 -0
- package/src/lib/image.ts +13 -1
- package/src/molecules/Accordion.ts +1 -2
- package/src/molecules/Collapsible.ts +1 -3
- package/src/molecules/InputOTP.ts +81 -27
- package/src/molecules/Resizable.ts +8 -2
- package/src/molecules/index.ts +3 -0
- package/src/organisms/AlertDialog.ts +3 -5
- package/src/organisms/Chart.ts +633 -125
- package/src/organisms/Combobox.ts +18 -2
- package/src/organisms/Command.ts +66 -7
- package/src/organisms/Dialog.ts +6 -7
- package/src/organisms/Form.ts +21 -0
- package/src/organisms/Select.ts +7 -4
- package/src/organisms/Sidebar.ts +75 -25
- package/src/organisms/Sonner.ts +1373 -0
- package/src/organisms/Toaster.ts +30 -6
- package/src/organisms/Tooltip.ts +4 -0
- package/src/organisms/index.ts +29 -0
- package/src/primitives/dismissable.ts +16 -8
- package/src/primitives/floating.ts +13 -2
- package/src/primitives/menu.ts +2 -1
- package/src/primitives/menuParts.ts +3 -3
- package/src/primitives/modalSurface.ts +77 -40
- package/src/primitives/portal.ts +27 -1
- package/src/primitives/presence.ts +12 -9
|
@@ -20,7 +20,7 @@ import { cn } from "../lib/cn.js";
|
|
|
20
20
|
import { ChevronsUpDownIcon } from "../lib/icons.js";
|
|
21
21
|
import { uid } from "../lib/id.js";
|
|
22
22
|
import { zoomInOut } from "../lib/motion.js";
|
|
23
|
-
import { type Reactive, read, readOr } from "../lib/props.js";
|
|
23
|
+
import { accessor, type Reactive, read, readOr } from "../lib/props.js";
|
|
24
24
|
import { controllable } from "../primitives/controllable.js";
|
|
25
25
|
import { floatingSurface } from "../primitives/floatingSurface.js";
|
|
26
26
|
import { focusSilently } from "../primitives/focusable.js";
|
|
@@ -42,8 +42,21 @@ export interface ComboboxOption {
|
|
|
42
42
|
group?: string;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* `id`, `label`, `labelledBy` and `describedBy` name the trigger. Upstream's
|
|
47
|
+
* trigger is a Button that takes every button prop; these are the ones that
|
|
48
|
+
* name it — a `<Label for>` or a `FieldLabel` points at the `id`. Without them
|
|
49
|
+
* the combobox's only name was the value it showed.
|
|
50
|
+
*/
|
|
45
51
|
export interface ComboboxProps {
|
|
46
52
|
options: readonly ComboboxOption[];
|
|
53
|
+
/** The trigger's id; a unique one is minted when left out. */
|
|
54
|
+
id?: string;
|
|
55
|
+
/** Accessible name (`aria-label`), for a combobox no `<label>` names. */
|
|
56
|
+
label?: Reactive<string | undefined>;
|
|
57
|
+
/** Id(s) of the element(s) that name it (`aria-labelledby`). */
|
|
58
|
+
labelledBy?: Reactive<string | undefined>;
|
|
59
|
+
describedBy?: Reactive<string | undefined>;
|
|
47
60
|
name?: string;
|
|
48
61
|
value?: Reactive<string | undefined>;
|
|
49
62
|
defaultValue?: string;
|
|
@@ -60,7 +73,7 @@ export interface ComboboxProps {
|
|
|
60
73
|
}
|
|
61
74
|
|
|
62
75
|
export const Combobox = component<ComboboxProps>((props) => {
|
|
63
|
-
const triggerId = uid("combobox-trigger");
|
|
76
|
+
const triggerId = props.id ?? uid("combobox-trigger");
|
|
64
77
|
const contentId = uid("combobox-content");
|
|
65
78
|
const open = signal(false);
|
|
66
79
|
|
|
@@ -165,6 +178,9 @@ export const Combobox = component<ComboboxProps>((props) => {
|
|
|
165
178
|
type="button"
|
|
166
179
|
data-slot="combobox-trigger"
|
|
167
180
|
id="${triggerId}"
|
|
181
|
+
aria-label="${accessor(props.label, undefined)}"
|
|
182
|
+
aria-labelledby="${accessor(props.labelledBy, undefined)}"
|
|
183
|
+
aria-describedby="${accessor(props.describedBy, undefined)}"
|
|
168
184
|
role="combobox"
|
|
169
185
|
aria-haspopup="listbox"
|
|
170
186
|
aria-expanded="${() => (open() ? "true" : "false")}"
|
package/src/organisms/Command.ts
CHANGED
|
@@ -29,6 +29,8 @@ import {
|
|
|
29
29
|
createContext,
|
|
30
30
|
html,
|
|
31
31
|
inject,
|
|
32
|
+
onMount,
|
|
33
|
+
onUnmount,
|
|
32
34
|
provide,
|
|
33
35
|
signal,
|
|
34
36
|
} from "@c9up/aurora";
|
|
@@ -67,13 +69,23 @@ interface RegisteredItem extends CommandCandidate {
|
|
|
67
69
|
run(): void;
|
|
68
70
|
}
|
|
69
71
|
|
|
72
|
+
/** One mounted item's registration. */
|
|
73
|
+
interface Registration {
|
|
74
|
+
/** Run this item — the mounted one, whatever else shares its value. */
|
|
75
|
+
select(): void;
|
|
76
|
+
/** Mounted: the palette's bindings learn it is there. */
|
|
77
|
+
mounted(): void;
|
|
78
|
+
/** Unmounted: it leaves the palette. */
|
|
79
|
+
unregister(): void;
|
|
80
|
+
}
|
|
81
|
+
|
|
70
82
|
interface CommandApi {
|
|
71
83
|
readonly inputId: string;
|
|
72
84
|
readonly listId: string;
|
|
73
85
|
readonly query: () => string;
|
|
74
86
|
readonly active: () => string | undefined;
|
|
75
87
|
setActive(value: string | undefined): void;
|
|
76
|
-
register(item: Omit<RegisteredItem, "id">):
|
|
88
|
+
register(item: Omit<RegisteredItem, "id">): Registration;
|
|
77
89
|
/** This palette's element id for a value. Per instance: ids are page-wide. */
|
|
78
90
|
itemId(value: string): string;
|
|
79
91
|
/** Does this item survive the current query? */
|
|
@@ -139,6 +151,33 @@ export const Command = component<CommandProps>((props) => {
|
|
|
139
151
|
*/
|
|
140
152
|
const explicitActive = signal<string | undefined>(undefined);
|
|
141
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Bumped when items mount or unmount, so the bindings that read the list
|
|
156
|
+
* — the empty state, the groups, the highlight — follow it. `items` itself
|
|
157
|
+
* stays a plain array, filled during setup: a signal written while the
|
|
158
|
+
* items are being built re-entered the effect building them.
|
|
159
|
+
*
|
|
160
|
+
* Coalesced to one bump per task, as cmdk schedules its store updates: a
|
|
161
|
+
* list of a hundred items mounting is one pass over the bindings, not a
|
|
162
|
+
* hundred.
|
|
163
|
+
*/
|
|
164
|
+
const revision = signal(0);
|
|
165
|
+
let scheduled = false;
|
|
166
|
+
function changed(): void {
|
|
167
|
+
if (scheduled) return;
|
|
168
|
+
scheduled = true;
|
|
169
|
+
queueMicrotask(() => {
|
|
170
|
+
scheduled = false;
|
|
171
|
+
// The highlighted item left: back to "the first match", as cmdk
|
|
172
|
+
// re-selects the first item when the selected one unmounts.
|
|
173
|
+
const chosen = explicitActive();
|
|
174
|
+
if (chosen !== undefined && !items.some((i) => i.value === chosen)) {
|
|
175
|
+
explicitActive(undefined);
|
|
176
|
+
}
|
|
177
|
+
revision(revision() + 1);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
142
181
|
function active(): string | undefined {
|
|
143
182
|
const chosen = explicitActive();
|
|
144
183
|
if (chosen !== undefined) return chosen;
|
|
@@ -146,15 +185,20 @@ export const Command = component<CommandProps>((props) => {
|
|
|
146
185
|
}
|
|
147
186
|
|
|
148
187
|
function matches(): readonly RegisteredItem[] {
|
|
188
|
+
revision();
|
|
149
189
|
const filter = props.filter ?? defaultFilter;
|
|
150
190
|
return items.filter((item) => !item.disabled && filter(item, query()));
|
|
151
191
|
}
|
|
152
192
|
|
|
193
|
+
function runItem(item: RegisteredItem): void {
|
|
194
|
+
if (item.disabled || !items.includes(item)) return;
|
|
195
|
+
item.run();
|
|
196
|
+
props.onSelect?.(item.value);
|
|
197
|
+
}
|
|
198
|
+
|
|
153
199
|
function run(value: string): void {
|
|
154
200
|
const item = items.find((entry) => entry.value === value);
|
|
155
|
-
if (item
|
|
156
|
-
item.run();
|
|
157
|
-
props.onSelect?.(value);
|
|
201
|
+
if (item !== undefined) runItem(item);
|
|
158
202
|
}
|
|
159
203
|
|
|
160
204
|
function move(delta: number): void {
|
|
@@ -178,7 +222,18 @@ export const Command = component<CommandProps>((props) => {
|
|
|
178
222
|
setActive: (value) => explicitActive(value),
|
|
179
223
|
itemId,
|
|
180
224
|
register(item) {
|
|
181
|
-
|
|
225
|
+
const entry: RegisteredItem = { ...item, id: itemId(item.value) };
|
|
226
|
+
items.push(entry);
|
|
227
|
+
return {
|
|
228
|
+
select: () => runItem(entry),
|
|
229
|
+
mounted: changed,
|
|
230
|
+
unregister(): void {
|
|
231
|
+
const index = items.indexOf(entry);
|
|
232
|
+
if (index === -1) return;
|
|
233
|
+
items.splice(index, 1);
|
|
234
|
+
changed();
|
|
235
|
+
},
|
|
236
|
+
};
|
|
182
237
|
},
|
|
183
238
|
isVisible(value) {
|
|
184
239
|
return matches().some((item) => item.value === value);
|
|
@@ -334,7 +389,9 @@ export const CommandItem = component<CommandItemProps>((props) => {
|
|
|
334
389
|
const children = props.children;
|
|
335
390
|
|
|
336
391
|
const id = command.itemId(props.value);
|
|
337
|
-
|
|
392
|
+
// Registered during setup, so the first pass over the bindings sees it;
|
|
393
|
+
// gone again when it unmounts, as cmdk's item cleanup.
|
|
394
|
+
const registration = command.register({
|
|
338
395
|
value: props.value,
|
|
339
396
|
textValue:
|
|
340
397
|
props.textValue ??
|
|
@@ -344,6 +401,8 @@ export const CommandItem = component<CommandItemProps>((props) => {
|
|
|
344
401
|
groupId: group?.groupId,
|
|
345
402
|
run: () => props.onSelect?.(),
|
|
346
403
|
});
|
|
404
|
+
onMount(() => registration.mounted());
|
|
405
|
+
onUnmount(() => registration.unregister());
|
|
347
406
|
|
|
348
407
|
const chosen = (): boolean => command.active() === props.value;
|
|
349
408
|
return html`<div
|
|
@@ -360,7 +419,7 @@ export const CommandItem = component<CommandItemProps>((props) => {
|
|
|
360
419
|
"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",
|
|
361
420
|
read(props.class),
|
|
362
421
|
)}"
|
|
363
|
-
@click="${() =>
|
|
422
|
+
@click="${() => registration.select()}"
|
|
364
423
|
@pointerenter="${() => command.setActive(props.value)}"
|
|
365
424
|
>${slot(children)}</div>`;
|
|
366
425
|
});
|
package/src/organisms/Dialog.ts
CHANGED
|
@@ -43,12 +43,11 @@ import {
|
|
|
43
43
|
createContext,
|
|
44
44
|
html,
|
|
45
45
|
inject,
|
|
46
|
-
onUnmount,
|
|
47
46
|
provide,
|
|
48
47
|
signal,
|
|
49
48
|
type TemplateResult,
|
|
50
49
|
} from "@c9up/aurora";
|
|
51
|
-
import {
|
|
50
|
+
import { buttonVariants } from "../atoms/Button.js";
|
|
52
51
|
import type { Child, Parts, Slot } from "../lib/children.js";
|
|
53
52
|
import { slot } from "../lib/children.js";
|
|
54
53
|
import { cn } from "../lib/cn.js";
|
|
@@ -58,7 +57,7 @@ import { fadeInOut, zoomInOut } from "../lib/motion.js";
|
|
|
58
57
|
import { type Reactive, read } from "../lib/props.js";
|
|
59
58
|
import { controllable } from "../primitives/controllable.js";
|
|
60
59
|
import { modalSurface } from "../primitives/modalSurface.js";
|
|
61
|
-
import {
|
|
60
|
+
import { portalOnMount } from "../primitives/portal.js";
|
|
62
61
|
|
|
63
62
|
export const dialogBackdropClasses =
|
|
64
63
|
"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";
|
|
@@ -233,11 +232,10 @@ export interface DialogPortalProps {
|
|
|
233
232
|
* rather than standing in as a passthrough.
|
|
234
233
|
*/
|
|
235
234
|
export const DialogPortal = component<DialogPortalProps>((props) => {
|
|
236
|
-
|
|
235
|
+
portalOnMount(html`${slot(props.children)}`, {
|
|
237
236
|
container: props.container,
|
|
237
|
+
slot: "dialog-portal",
|
|
238
238
|
});
|
|
239
|
-
mounted.host.setAttribute("data-slot", "dialog-portal");
|
|
240
|
-
onUnmount(() => mounted.close());
|
|
241
239
|
return html``;
|
|
242
240
|
});
|
|
243
241
|
|
|
@@ -301,8 +299,9 @@ export const DialogFooter = component<DialogFooterProps>((props) => {
|
|
|
301
299
|
? html`<button
|
|
302
300
|
type="button"
|
|
303
301
|
data-slot="dialog-close"
|
|
302
|
+
class="${buttonVariants({ variant: "outline" })}"
|
|
304
303
|
@click="${() => dialog.setOpen(false)}"
|
|
305
|
-
>${
|
|
304
|
+
>${props.closeLabel ?? "Close"}</button>`
|
|
306
305
|
: null;
|
|
307
306
|
return html`<div
|
|
308
307
|
data-slot="dialog-footer"
|
package/src/organisms/Form.ts
CHANGED
|
@@ -77,6 +77,17 @@ export function bind<T, K extends keyof T & string>(
|
|
|
77
77
|
|
|
78
78
|
export interface FormProps<T> {
|
|
79
79
|
form: AuroraForm<T>;
|
|
80
|
+
/**
|
|
81
|
+
* How the browser submits it before the page is hydrated, or when a
|
|
82
|
+
* script fails to load. Default `"post"`.
|
|
83
|
+
*/
|
|
84
|
+
method?: "post" | "get" | "dialog";
|
|
85
|
+
/** Where that submission goes. Left out, it goes to the current page. */
|
|
86
|
+
action?: string;
|
|
87
|
+
enctype?:
|
|
88
|
+
| "application/x-www-form-urlencoded"
|
|
89
|
+
| "multipart/form-data"
|
|
90
|
+
| "text/plain";
|
|
80
91
|
children?: Slot;
|
|
81
92
|
class?: Reactive<string>;
|
|
82
93
|
}
|
|
@@ -88,11 +99,21 @@ export interface FormProps<T> {
|
|
|
88
99
|
* styled, appear one at a time, and vanish on scroll — and they would fire
|
|
89
100
|
* before the controller's validation ever runs, so the user would see two
|
|
90
101
|
* different error presentations for the same field.
|
|
102
|
+
*
|
|
103
|
+
* NAMED DEVIATION: `method="post"` unless told otherwise. shadcn's `Form`
|
|
104
|
+
* renders no `<form>`, so the app writes its own method; this one owns the
|
|
105
|
+
* element, and the server renders it. Pressed before hydration, a form with
|
|
106
|
+
* no method is a GET, and every named field — a password included — lands in
|
|
107
|
+
* the URL, the history and the access logs. An app with CSRF protection puts
|
|
108
|
+
* its token field among the children, so that early POST is accepted.
|
|
91
109
|
*/
|
|
92
110
|
export function Form<T>(props: FormProps<T>): ReturnType<typeof html> {
|
|
93
111
|
return html`<form
|
|
94
112
|
data-slot="form"
|
|
95
113
|
novalidate
|
|
114
|
+
method="${props.method ?? "post"}"
|
|
115
|
+
action="${props.action}"
|
|
116
|
+
enctype="${props.enctype}"
|
|
96
117
|
class="${() => cn("flex flex-col gap-6", read(props.class))}"
|
|
97
118
|
@submit="${(event: Event) => {
|
|
98
119
|
// A DOM listener returns nothing, so this promise is nobody's to
|
package/src/organisms/Select.ts
CHANGED
|
@@ -424,10 +424,10 @@ export const Select = component<SelectProps>((props) => {
|
|
|
424
424
|
});
|
|
425
425
|
|
|
426
426
|
/**
|
|
427
|
-
* `id`, `label` and `describedBy
|
|
428
|
-
* button prop, and these are the ones that name it — a
|
|
429
|
-
* `for` at the `id`, and a `<label for>` names a
|
|
430
|
-
* combobox's only name was the value it showed.
|
|
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
431
|
*/
|
|
432
432
|
export interface SelectTriggerProps {
|
|
433
433
|
children?: Slot;
|
|
@@ -435,6 +435,8 @@ export interface SelectTriggerProps {
|
|
|
435
435
|
id?: string;
|
|
436
436
|
/** Accessible name (`aria-label`), for a select no `<label>` names. */
|
|
437
437
|
label?: Reactive<string | undefined>;
|
|
438
|
+
/** Id(s) of the element(s) that name it (`aria-labelledby`). */
|
|
439
|
+
labelledBy?: Reactive<string | undefined>;
|
|
438
440
|
describedBy?: Reactive<string | undefined>;
|
|
439
441
|
class?: Reactive<string>;
|
|
440
442
|
}
|
|
@@ -446,6 +448,7 @@ export const SelectTrigger = component<SelectTriggerProps>((props) => {
|
|
|
446
448
|
data-slot="select-trigger"
|
|
447
449
|
id="${select.claimTriggerId(props.id)}"
|
|
448
450
|
aria-label="${accessor(props.label, undefined)}"
|
|
451
|
+
aria-labelledby="${accessor(props.labelledBy, undefined)}"
|
|
449
452
|
aria-describedby="${accessor(props.describedBy, undefined)}"
|
|
450
453
|
role="combobox"
|
|
451
454
|
aria-haspopup="listbox"
|
package/src/organisms/Sidebar.ts
CHANGED
|
@@ -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
|
-
*
|
|
69
|
-
* the trigger may sit in a header that is not inside the
|
|
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
|
-
|
|
73
|
-
|
|
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
|
|
|
@@ -82,14 +89,24 @@ export function sidebarCollapsed(): boolean {
|
|
|
82
89
|
}
|
|
83
90
|
|
|
84
91
|
export interface SidebarApi {
|
|
92
|
+
/** The column's state: persisted, and what the wide layout shows. */
|
|
85
93
|
readonly open: () => boolean;
|
|
94
|
+
/** The drawer's state on a phone: closed on every load, never persisted. */
|
|
95
|
+
readonly openMobile: () => boolean;
|
|
86
96
|
readonly isMobile: () => boolean;
|
|
87
97
|
readonly state: () => "expanded" | "collapsed";
|
|
88
98
|
setOpen(next: boolean): void;
|
|
99
|
+
setOpenMobile(next: boolean): void;
|
|
100
|
+
/** The drawer on a phone, the column otherwise. */
|
|
89
101
|
toggleSidebar(): void;
|
|
90
102
|
}
|
|
91
103
|
|
|
92
104
|
const SidebarContext = createContext<SidebarApi>("SidebarProvider");
|
|
105
|
+
/** The same provider, for the parts that also work outside one. */
|
|
106
|
+
const NearestSidebar = createContext<SidebarApi | null>(
|
|
107
|
+
"SidebarProvider",
|
|
108
|
+
null,
|
|
109
|
+
);
|
|
93
110
|
|
|
94
111
|
/**
|
|
95
112
|
* The sidebar's state, from inside it.
|
|
@@ -112,6 +129,10 @@ export interface SidebarProviderProps {
|
|
|
112
129
|
export const SidebarProvider = component<SidebarProviderProps>((props) => {
|
|
113
130
|
const open = sidebarState(props.defaultOpen ?? true);
|
|
114
131
|
const mobile = signal(false);
|
|
132
|
+
// Apart from the column's, as upstream: sharing it opened the drawer over
|
|
133
|
+
// the page on every load, since the column is open by default and its
|
|
134
|
+
// state outlives the page in the cookie.
|
|
135
|
+
const openMobile = signal(false);
|
|
115
136
|
|
|
116
137
|
function setOpen(next: boolean): void {
|
|
117
138
|
open(next);
|
|
@@ -119,7 +140,8 @@ export const SidebarProvider = component<SidebarProviderProps>((props) => {
|
|
|
119
140
|
}
|
|
120
141
|
|
|
121
142
|
function toggleSidebar(): void {
|
|
122
|
-
|
|
143
|
+
if (mobile()) openMobile(!openMobile());
|
|
144
|
+
else setOpen(!open());
|
|
123
145
|
}
|
|
124
146
|
|
|
125
147
|
function onKeyDown(event: KeyboardEvent): void {
|
|
@@ -153,13 +175,16 @@ export const SidebarProvider = component<SidebarProviderProps>((props) => {
|
|
|
153
175
|
return controlled === undefined ? open() : controlled;
|
|
154
176
|
}
|
|
155
177
|
|
|
156
|
-
provide<SidebarApi>(SidebarContext, {
|
|
178
|
+
const api = provide<SidebarApi>(SidebarContext, {
|
|
157
179
|
open: current,
|
|
180
|
+
openMobile: () => openMobile(),
|
|
158
181
|
isMobile: () => mobile(),
|
|
159
182
|
state: () => (current() ? "expanded" : "collapsed"),
|
|
160
183
|
setOpen,
|
|
184
|
+
setOpenMobile: (next) => openMobile(next),
|
|
161
185
|
toggleSidebar,
|
|
162
186
|
});
|
|
187
|
+
provide(NearestSidebar, api);
|
|
163
188
|
|
|
164
189
|
return html`<div
|
|
165
190
|
data-slot="sidebar-wrapper"
|
|
@@ -189,8 +214,8 @@ export const Sidebar = component<SidebarProps>((props) => {
|
|
|
189
214
|
return html`<div data-slot="sidebar-root">${() =>
|
|
190
215
|
sidebar.isMobile()
|
|
191
216
|
? Sheet({
|
|
192
|
-
open: () => sidebar.
|
|
193
|
-
onOpenChange: (next) => sidebar.
|
|
217
|
+
open: () => sidebar.openMobile(),
|
|
218
|
+
onOpenChange: (next) => sidebar.setOpenMobile(next),
|
|
194
219
|
children: () =>
|
|
195
220
|
SheetContent({
|
|
196
221
|
side,
|
|
@@ -246,8 +271,19 @@ export interface SidebarTriggerProps {
|
|
|
246
271
|
class?: Reactive<string>;
|
|
247
272
|
}
|
|
248
273
|
|
|
249
|
-
|
|
250
|
-
|
|
274
|
+
/** The provider's toggle, or the page-wide state's outside a provider. */
|
|
275
|
+
function toggleNearest(): () => void {
|
|
276
|
+
const sidebar = inject(NearestSidebar);
|
|
277
|
+
if (sidebar !== null) return () => sidebar.toggleSidebar();
|
|
278
|
+
return () => {
|
|
279
|
+
const open = sidebarState();
|
|
280
|
+
open(!open());
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export const SidebarTrigger = component<SidebarTriggerProps>((props) => {
|
|
285
|
+
const toggle = toggleNearest();
|
|
286
|
+
return Button({
|
|
251
287
|
variant: "ghost",
|
|
252
288
|
size: "icon",
|
|
253
289
|
label: props.label ?? "Toggle sidebar",
|
|
@@ -257,11 +293,11 @@ export const SidebarTrigger = component<SidebarTriggerProps>((props) =>
|
|
|
257
293
|
props.onToggle();
|
|
258
294
|
return;
|
|
259
295
|
}
|
|
260
|
-
|
|
296
|
+
toggle();
|
|
261
297
|
},
|
|
262
298
|
children: PanelLeftIcon({ class: "size-4" }),
|
|
263
|
-
})
|
|
264
|
-
);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
265
301
|
|
|
266
302
|
export interface SidebarRailProps {
|
|
267
303
|
/** Accessible name and tooltip. Default `"Toggle sidebar"`. */
|
|
@@ -277,8 +313,9 @@ export interface SidebarRailProps {
|
|
|
277
313
|
* keyboard has `cmd/ctrl + B` and the trigger, so a third stop on every page
|
|
278
314
|
* would be noise.
|
|
279
315
|
*/
|
|
280
|
-
export const SidebarRail = component<SidebarRailProps>(
|
|
281
|
-
(
|
|
316
|
+
export const SidebarRail = component<SidebarRailProps>((props) => {
|
|
317
|
+
const toggle = toggleNearest();
|
|
318
|
+
return html`<button
|
|
282
319
|
type="button"
|
|
283
320
|
data-slot="sidebar-rail"
|
|
284
321
|
aria-label="${props.label ?? "Toggle sidebar"}"
|
|
@@ -289,9 +326,9 @@ export const SidebarRail = component<SidebarRailProps>(
|
|
|
289
326
|
"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",
|
|
290
327
|
read(props.class),
|
|
291
328
|
)}"
|
|
292
|
-
@click="${
|
|
293
|
-
></button
|
|
294
|
-
);
|
|
329
|
+
@click="${toggle}"
|
|
330
|
+
></button>`;
|
|
331
|
+
});
|
|
295
332
|
|
|
296
333
|
export interface SidebarInsetProps {
|
|
297
334
|
children?: Slot;
|
|
@@ -450,6 +487,9 @@ export interface SidebarMenuButtonProps extends SidebarMenuButtonVariants {
|
|
|
450
487
|
}
|
|
451
488
|
|
|
452
489
|
export const SidebarMenuButton = component<SidebarMenuButtonProps>((props) => {
|
|
490
|
+
const sidebar = inject(NearestSidebar);
|
|
491
|
+
const collapsed = (): boolean =>
|
|
492
|
+
sidebar === null ? sidebarCollapsed() : sidebar.state() === "collapsed";
|
|
453
493
|
const merged = (): string =>
|
|
454
494
|
cn(
|
|
455
495
|
sidebarMenuButtonVariants({
|
|
@@ -483,7 +523,7 @@ export const SidebarMenuButton = component<SidebarMenuButtonProps>((props) => {
|
|
|
483
523
|
// the entry and lose focus mid-keyboard-navigation. The tooltip decides for
|
|
484
524
|
// itself whether to open, which is the cheaper half to make conditional.
|
|
485
525
|
return html`${() =>
|
|
486
|
-
|
|
526
|
+
collapsed()
|
|
487
527
|
? Tooltip({
|
|
488
528
|
children: () =>
|
|
489
529
|
html`${TooltipTrigger({
|
|
@@ -534,18 +574,28 @@ export interface SidebarMenuSkeletonProps {
|
|
|
534
574
|
*
|
|
535
575
|
* The widths vary per row, which is what stops a loading column from reading
|
|
536
576
|
* as a bar chart. Seeded from a random draw the moment the row is built, so it
|
|
537
|
-
* does not shift on every re-render.
|
|
577
|
+
* does not shift on every re-render. The draw picks a class, not a `style`
|
|
578
|
+
* attribute, which a Content-Security-Policy without `'unsafe-inline'` blocks.
|
|
538
579
|
*/
|
|
580
|
+
const SKELETON_WIDTHS: readonly string[] = [
|
|
581
|
+
"max-w-[50%]",
|
|
582
|
+
"max-w-[60%]",
|
|
583
|
+
"max-w-[70%]",
|
|
584
|
+
"max-w-[80%]",
|
|
585
|
+
"max-w-[90%]",
|
|
586
|
+
];
|
|
587
|
+
|
|
539
588
|
export const SidebarMenuSkeleton = component<SidebarMenuSkeletonProps>(
|
|
540
589
|
(props) => {
|
|
541
|
-
const width =
|
|
590
|
+
const width =
|
|
591
|
+
SKELETON_WIDTHS[Math.floor(Math.random() * SKELETON_WIDTHS.length)];
|
|
542
592
|
return html`<div
|
|
543
593
|
data-slot="sidebar-menu-skeleton"
|
|
544
594
|
class="${() =>
|
|
545
595
|
cn("flex h-8 items-center gap-2 rounded-md px-2", read(props.class))}"
|
|
546
596
|
>${
|
|
547
597
|
props.showIcon === true ? Skeleton({ class: "size-4 rounded-md" }) : null
|
|
548
|
-
}<div class="flex-1"
|
|
598
|
+
}<div class="${cn("flex-1", width)}">${Skeleton({
|
|
549
599
|
class: "h-4 w-full",
|
|
550
600
|
})}</div></div>`;
|
|
551
601
|
},
|