@c9up/nebula 0.1.9 → 0.2.1

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 (132) 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/configure.d.ts +6 -0
  6. package/dist/configure.js +4 -22
  7. package/dist/lib/children.d.ts +30 -0
  8. package/dist/lib/id.d.ts +13 -29
  9. package/dist/lib/id.js +13 -39
  10. package/dist/lib/motion.d.ts +10 -0
  11. package/dist/lib/motion.js +10 -0
  12. package/dist/molecules/Accordion.d.ts +36 -15
  13. package/dist/molecules/Accordion.js +68 -45
  14. package/dist/molecules/Breadcrumb.d.ts +29 -11
  15. package/dist/molecules/Breadcrumb.js +52 -52
  16. package/dist/molecules/Collapsible.d.ts +18 -17
  17. package/dist/molecules/Collapsible.js +42 -34
  18. package/dist/molecules/Field.d.ts +66 -30
  19. package/dist/molecules/Field.js +106 -55
  20. package/dist/molecules/InputGroup.d.ts +39 -21
  21. package/dist/molecules/InputGroup.js +43 -27
  22. package/dist/molecules/InputOTP.d.ts +26 -3
  23. package/dist/molecules/InputOTP.js +109 -76
  24. package/dist/molecules/Message.js +5 -4
  25. package/dist/molecules/Pagination.d.ts +36 -11
  26. package/dist/molecules/Pagination.js +58 -74
  27. package/dist/molecules/RadioGroup.d.ts +25 -17
  28. package/dist/molecules/RadioGroup.js +47 -43
  29. package/dist/molecules/Resizable.d.ts +32 -7
  30. package/dist/molecules/Resizable.js +76 -49
  31. package/dist/molecules/Tabs.d.ts +49 -11
  32. package/dist/molecules/Tabs.js +107 -51
  33. package/dist/molecules/ToggleGroup.d.ts +16 -10
  34. package/dist/molecules/ToggleGroup.js +37 -21
  35. package/dist/molecules/index.d.ts +11 -11
  36. package/dist/molecules/index.js +11 -11
  37. package/dist/organisms/AlertDialog.d.ts +80 -28
  38. package/dist/organisms/AlertDialog.js +192 -67
  39. package/dist/organisms/Carousel.d.ts +25 -3
  40. package/dist/organisms/Carousel.js +75 -43
  41. package/dist/organisms/Combobox.d.ts +2 -1
  42. package/dist/organisms/Combobox.js +46 -16
  43. package/dist/organisms/Command.d.ts +73 -32
  44. package/dist/organisms/Command.js +209 -131
  45. package/dist/organisms/CommandDialog.js +5 -5
  46. package/dist/organisms/ContextMenu.d.ts +41 -19
  47. package/dist/organisms/ContextMenu.js +112 -71
  48. package/dist/organisms/DataTable.js +42 -8
  49. package/dist/organisms/Dialog.d.ts +97 -31
  50. package/dist/organisms/Dialog.js +193 -68
  51. package/dist/organisms/Drawer.d.ts +33 -10
  52. package/dist/organisms/Drawer.js +130 -52
  53. package/dist/organisms/DropdownMenu.d.ts +58 -11
  54. package/dist/organisms/DropdownMenu.js +137 -50
  55. package/dist/organisms/Form.js +9 -11
  56. package/dist/organisms/HoverCard.d.ts +39 -19
  57. package/dist/organisms/HoverCard.js +116 -40
  58. package/dist/organisms/Menubar.d.ts +55 -19
  59. package/dist/organisms/Menubar.js +166 -82
  60. package/dist/organisms/NavigationMenu.d.ts +77 -31
  61. package/dist/organisms/NavigationMenu.js +169 -98
  62. package/dist/organisms/Popover.d.ts +72 -12
  63. package/dist/organisms/Popover.js +132 -25
  64. package/dist/organisms/Questionnaire.js +5 -5
  65. package/dist/organisms/Select.d.ts +99 -18
  66. package/dist/organisms/Select.js +253 -111
  67. package/dist/organisms/Sheet.d.ts +57 -13
  68. package/dist/organisms/Sheet.js +141 -54
  69. package/dist/organisms/Sidebar.d.ts +146 -56
  70. package/dist/organisms/Sidebar.js +236 -128
  71. package/dist/organisms/Tooltip.d.ts +88 -14
  72. package/dist/organisms/Tooltip.js +210 -47
  73. package/dist/organisms/index.d.ts +15 -15
  74. package/dist/organisms/index.js +15 -15
  75. package/dist/primitives/floating.js +25 -0
  76. package/dist/primitives/floatingSurface.d.ts +11 -0
  77. package/dist/primitives/floatingSurface.js +8 -0
  78. package/dist/primitives/menu.d.ts +45 -1
  79. package/dist/primitives/menu.js +45 -14
  80. package/dist/primitives/menuParts.d.ts +113 -0
  81. package/dist/primitives/menuParts.js +207 -0
  82. package/dist/stubs.d.ts +13 -0
  83. package/dist/stubs.js +14 -0
  84. package/nebula.css +1 -1
  85. package/package.json +4 -3
  86. package/registry.json +18 -6
  87. package/src/atoms/Avatar.ts +141 -37
  88. package/src/atoms/index.ts +13 -1
  89. package/src/configure.ts +10 -23
  90. package/src/lib/children.ts +31 -0
  91. package/src/lib/id.ts +11 -39
  92. package/src/lib/motion.ts +12 -0
  93. package/src/molecules/Accordion.ts +120 -60
  94. package/src/molecules/Breadcrumb.ts +77 -76
  95. package/src/molecules/Collapsible.ts +77 -41
  96. package/src/molecules/Field.ts +187 -72
  97. package/src/molecules/InputGroup.ts +88 -35
  98. package/src/molecules/InputOTP.ts +150 -86
  99. package/src/molecules/Message.ts +6 -4
  100. package/src/molecules/Pagination.ts +96 -101
  101. package/src/molecules/RadioGroup.ts +70 -59
  102. package/src/molecules/Resizable.ts +138 -75
  103. package/src/molecules/Tabs.ts +187 -74
  104. package/src/molecules/ToggleGroup.ts +77 -38
  105. package/src/molecules/index.ts +85 -8
  106. package/src/organisms/AlertDialog.ts +319 -82
  107. package/src/organisms/Carousel.ts +129 -52
  108. package/src/organisms/Combobox.ts +64 -19
  109. package/src/organisms/Command.ts +342 -159
  110. package/src/organisms/CommandDialog.ts +6 -6
  111. package/src/organisms/ContextMenu.ts +185 -83
  112. package/src/organisms/DataTable.ts +56 -10
  113. package/src/organisms/Dialog.ts +312 -102
  114. package/src/organisms/Drawer.ts +219 -71
  115. package/src/organisms/DropdownMenu.ts +219 -61
  116. package/src/organisms/Form.ts +48 -34
  117. package/src/organisms/HoverCard.ts +187 -53
  118. package/src/organisms/Menubar.ts +247 -87
  119. package/src/organisms/NavigationMenu.ts +306 -128
  120. package/src/organisms/Popover.ts +215 -39
  121. package/src/organisms/Questionnaire.ts +9 -6
  122. package/src/organisms/Select.ts +397 -130
  123. package/src/organisms/Sheet.ts +252 -83
  124. package/src/organisms/Sidebar.ts +438 -185
  125. package/src/organisms/Tooltip.ts +315 -65
  126. package/src/organisms/index.ts +284 -14
  127. package/src/primitives/floating.ts +33 -0
  128. package/src/primitives/floatingSurface.ts +19 -0
  129. package/src/primitives/menu.ts +79 -18
  130. package/src/primitives/menuParts.ts +371 -0
  131. package/src/stubs.ts +16 -0
  132. package/stubs/config/nebula.stub +21 -0
@@ -1,51 +1,87 @@
1
1
  /**
2
- * AlertDialog — a modal that interrupts to ask something.
2
+ * AlertDialog — a modal that interrupts to ask a question.
3
3
  *
4
- * The differences from Dialog are all about refusing to be dismissed by
5
- * accident, because the user is being asked to confirm something they cannot
6
- * undo:
4
+ * AlertDialog({ children: () => html`
5
+ * ${AlertDialogTrigger({ children: "Delete" })}
6
+ * ${AlertDialogContent({ children: () => html`
7
+ * ${AlertDialogHeader({ children: html`
8
+ * ${AlertDialogTitle({ children: "Delete this?" })}
9
+ * ${AlertDialogDescription({ children: "It cannot be undone." })}
10
+ * ` })}
11
+ * ${AlertDialogFooter({ children: html`
12
+ * ${AlertDialogCancel({ children: "Cancel" })}
13
+ * ${AlertDialogAction({ children: "Delete", variant: "destructive" })}
14
+ * ` })}
15
+ * ` })}
16
+ * ` })
7
17
  *
8
- * - `role="alertdialog"`, which tells assistive technology this is not merely
9
- * a window but a message requiring a response.
10
- * - No close button and no backdrop dismissal. Both are the "click somewhere
11
- * else and it goes away" gesture, and an accidental dismissal of "delete
12
- * this?" reads as a cancel — the safe answer — but leaves the user unsure
13
- * which one they gave.
14
- * - Escape still closes. It is unambiguous and deliberate, and taking it away
15
- * would trap a keyboard user in the dialog.
16
- * - Focus starts on **Cancel**, not the action. The destructive button is the
17
- * one the user must reach for on purpose.
18
+ * Everything that distinguishes it from a Dialog is about refusing to let the
19
+ * question be dismissed by accident:
20
+ *
21
+ * - `role="alertdialog"`, which tells a screen reader this interrupts.
22
+ * - No outside-click dismissal. A decision has to be made, and clicking past
23
+ * it is not one.
24
+ * - Focus lands on the CANCEL button, not the confirming action. The safe
25
+ * choice is the one a hurried Enter should hit.
26
+ *
27
+ * Escape still closes, and it cancels rather than confirms — it is the
28
+ * keyboard spelling of the same safe way out.
18
29
  */
19
30
 
20
- import { component, html } from "@c9up/aurora";
21
- import { buttonVariants } from "../atoms/Button.js";
22
- import type { Child } from "../lib/children.js";
23
- import { type Slot, slot } from "../lib/children.js";
31
+ import {
32
+ component,
33
+ createContext,
34
+ html,
35
+ inject,
36
+ onUnmount,
37
+ provide,
38
+ type TemplateResult,
39
+ } from "@c9up/aurora";
40
+ import { type ButtonVariants, buttonVariants } from "../atoms/Button.js";
41
+ import type { Child, Parts, Slot } from "../lib/children.js";
42
+ import { slot } from "../lib/children.js";
24
43
  import { cn } from "../lib/cn.js";
25
44
  import { uid } from "../lib/id.js";
26
45
  import { fadeInOut, zoomInOut } from "../lib/motion.js";
27
46
  import { type Reactive, read } from "../lib/props.js";
28
47
  import { controllable } from "../primitives/controllable.js";
29
48
  import { modalSurface } from "../primitives/modalSurface.js";
30
- import { dialogBackdropClasses, dialogPanelClasses } from "./Dialog.js";
49
+ import { portal } from "../primitives/portal.js";
50
+ import { dialogBackdropClasses } from "./Dialog.js";
51
+
52
+ const panelClasses =
53
+ "group/alert-dialog-content 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 data-[size=sm]:max-w-xs sm:max-w-lg";
54
+
55
+ interface RegisteredContent {
56
+ readonly body: Child;
57
+ readonly size: "default" | "sm";
58
+ readonly class?: Reactive<string>;
59
+ }
60
+
61
+ interface AlertDialogApi {
62
+ readonly triggerId: string;
63
+ readonly panelId: string;
64
+ readonly titleId: string;
65
+ readonly descriptionId: string;
66
+ readonly cancelId: string;
67
+ readonly open: () => boolean;
68
+ setOpen(next: boolean): void;
69
+ confirm(): void;
70
+ cancel(): void;
71
+ declareTitle(): void;
72
+ declareDescription(): void;
73
+ register(content: RegisteredContent): void;
74
+ }
75
+
76
+ const AlertDialogContext = createContext<AlertDialogApi>("AlertDialog");
31
77
 
32
78
  export interface AlertDialogProps {
33
- trigger?: Slot;
34
- title: Child;
35
- description: Child;
36
- /** Label for the confirming action. Default `"Continue"`. */
37
- actionLabel?: Child;
38
- /** Label for the way out. Default `"Cancel"`. */
39
- cancelLabel?: Child;
40
- /** Styles the action as destructive. */
41
- destructive?: boolean;
42
79
  open?: Reactive<boolean>;
43
80
  defaultOpen?: boolean;
44
81
  onOpenChange?: (open: boolean) => void;
45
82
  onConfirm?: () => void;
46
83
  onCancel?: () => void;
47
- triggerClass?: Reactive<string>;
48
- contentClass?: Reactive<string>;
84
+ children?: Parts;
49
85
  }
50
86
 
51
87
  export const AlertDialog = component<AlertDialogProps>((props) => {
@@ -54,6 +90,9 @@ export const AlertDialog = component<AlertDialogProps>((props) => {
54
90
  const titleId = uid("alert-dialog-title");
55
91
  const descriptionId = uid("alert-dialog-description");
56
92
  const cancelId = uid("alert-dialog-cancel");
93
+ let hasTitle = false;
94
+ let hasDescription = false;
95
+ let content: RegisteredContent | undefined;
57
96
 
58
97
  const state = controllable<boolean>({
59
98
  value: props.open,
@@ -71,63 +110,261 @@ export const AlertDialog = component<AlertDialogProps>((props) => {
71
110
  state.set(false);
72
111
  }
73
112
 
74
- modalSurface({
113
+ provide<AlertDialogApi>(AlertDialogContext, {
114
+ triggerId,
115
+ panelId,
116
+ titleId,
117
+ descriptionId,
118
+ cancelId,
75
119
  open: () => state.current(),
76
- onClose: cancel,
77
- panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
78
- initialFocus: (panel) => panel.querySelector(`#${CSS.escape(cancelId)}`),
79
- returnFocus: () => document.getElementById(triggerId),
80
- dismissOnOutside: false,
81
- content: () =>
82
- html`<div data-slot="alert-dialog-overlay" class="${cn("fixed inset-0 z-50", fadeInOut)}">
83
- <div class="${dialogBackdropClasses}"></div>
84
- <div
85
- data-slot="alert-dialog-content"
86
- id="${panelId}"
87
- role="alertdialog"
88
- aria-modal="true"
89
- aria-labelledby="${titleId}"
90
- aria-describedby="${descriptionId}"
91
- tabindex="-1"
92
- class="${cn(dialogPanelClasses, zoomInOut, read(props.contentClass))}"
93
- >
94
- <div class="flex flex-col gap-2 text-center sm:text-left">
95
- <h2 id="${titleId}" class="text-lg font-semibold">${props.title}</h2>
96
- <p id="${descriptionId}" class="text-muted-foreground text-sm">
97
- ${props.description}
98
- </p>
99
- </div>
100
- <div class="flex flex-col-reverse gap-2 sm:flex-row sm:justify-end">
101
- <button
102
- type="button"
103
- id="${cancelId}"
104
- data-slot="alert-dialog-cancel"
105
- class="${buttonVariants({ variant: "outline" })}"
106
- @click="${cancel}"
107
- >${props.cancelLabel ?? "Cancel"}</button>
108
- <button
109
- type="button"
110
- data-slot="alert-dialog-action"
111
- class="${buttonVariants({
112
- variant: props.destructive === true ? "destructive" : "default",
113
- })}"
114
- @click="${confirm}"
115
- >${props.actionLabel ?? "Continue"}</button>
116
- </div>
117
- </div>
118
- </div>`,
120
+ setOpen: (next) => state.set(next),
121
+ confirm,
122
+ cancel,
123
+ declareTitle() {
124
+ hasTitle = true;
125
+ },
126
+ declareDescription() {
127
+ hasDescription = true;
128
+ },
129
+ register(registered) {
130
+ content = registered;
131
+ },
119
132
  });
120
133
 
121
- if (props.trigger === undefined) {
122
- return html`<span data-slot="alert-dialog" hidden></span>`;
134
+ const parts = props.children?.();
135
+
136
+ if (content !== undefined) {
137
+ const registered = content;
138
+ if (!hasTitle) {
139
+ console.warn(
140
+ "nebula: an AlertDialog has no AlertDialogTitle. It will be announced only as “alertdialog”.",
141
+ );
142
+ }
143
+ modalSurface({
144
+ open: () => state.current(),
145
+ // Escape cancels rather than confirms: the keyboard spelling of the
146
+ // safe way out.
147
+ onClose: cancel,
148
+ panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
149
+ initialFocus: (panel) => panel.querySelector(`#${CSS.escape(cancelId)}`),
150
+ returnFocus: () => document.getElementById(triggerId),
151
+ dismissOnOutside: false,
152
+ content: () =>
153
+ renderSurface(registered, {
154
+ panelId,
155
+ titleId: hasTitle ? titleId : undefined,
156
+ descriptionId: hasDescription ? descriptionId : undefined,
157
+ }),
158
+ });
123
159
  }
124
160
 
161
+ return html`${parts}`;
162
+ });
163
+
164
+ export interface AlertDialogTriggerProps {
165
+ children?: Slot;
166
+ class?: Reactive<string>;
167
+ }
168
+
169
+ export const AlertDialogTrigger = component<AlertDialogTriggerProps>(
170
+ (props) => {
171
+ const dialog = inject(AlertDialogContext);
172
+ return html`<button
173
+ type="button"
174
+ data-slot="alert-dialog-trigger"
175
+ id="${dialog.triggerId}"
176
+ aria-haspopup="dialog"
177
+ aria-expanded="${() => (dialog.open() ? "true" : "false")}"
178
+ data-state="${() => (dialog.open() ? "open" : "closed")}"
179
+ class="${() => cn(read(props.class))}"
180
+ @click="${() => dialog.setOpen(true)}"
181
+ >${slot(props.children)}</button>`;
182
+ },
183
+ );
184
+
185
+ export interface AlertDialogContentProps {
186
+ children?: Parts;
187
+ size?: "default" | "sm";
188
+ class?: Reactive<string>;
189
+ }
190
+
191
+ export const AlertDialogContent = component<AlertDialogContentProps>(
192
+ (props) => {
193
+ const dialog = inject(AlertDialogContext);
194
+ dialog.register({
195
+ body: props.children?.(),
196
+ size: props.size ?? "default",
197
+ class: props.class,
198
+ });
199
+ return html``;
200
+ },
201
+ );
202
+
203
+ export interface AlertDialogSectionProps {
204
+ children?: Slot;
205
+ class?: Reactive<string>;
206
+ }
207
+
208
+ export const AlertDialogOverlay = component<AlertDialogSectionProps>(
209
+ (props) => html`<div
210
+ data-slot="alert-dialog-overlay"
211
+ class="${() => cn(dialogBackdropClasses, read(props.class))}"
212
+ ></div>`,
213
+ );
214
+
215
+ export interface AlertDialogPortalProps {
216
+ children?: Slot;
217
+ container?: () => Element | null;
218
+ }
219
+
220
+ /**
221
+ * Mount children outside the component's own DOM position.
222
+ *
223
+ * `AlertDialogContent` does not need it — the dialog portals its whole overlay
224
+ * through `modalSurface`, which also owns the trap and the scroll lock. It is
225
+ * exported because upstream exports it, doing here what its name says rather
226
+ * than standing in as a passthrough.
227
+ */
228
+ export const AlertDialogPortal = component<AlertDialogPortalProps>((props) => {
229
+ const mounted = portal(html`${slot(props.children)}`, {
230
+ container: props.container,
231
+ });
232
+ mounted.host.setAttribute("data-slot", "alert-dialog-portal");
233
+ onUnmount(() => mounted.close());
234
+ return html``;
235
+ });
236
+
237
+ export const AlertDialogHeader = component<AlertDialogSectionProps>(
238
+ (props) => html`<div
239
+ data-slot="alert-dialog-header"
240
+ class="${() =>
241
+ cn(
242
+ "grid grid-rows-[auto_1fr] place-items-center gap-1.5 text-center has-data-[slot=alert-dialog-media]:grid-rows-[auto_auto_1fr] has-data-[slot=alert-dialog-media]:gap-x-6 sm:group-data-[size=default]/alert-dialog-content:place-items-start sm:group-data-[size=default]/alert-dialog-content:text-left",
243
+ read(props.class),
244
+ )}"
245
+ >${slot(props.children)}</div>`,
246
+ );
247
+
248
+ export const AlertDialogFooter = component<AlertDialogSectionProps>(
249
+ (props) => html`<div
250
+ data-slot="alert-dialog-footer"
251
+ class="${() =>
252
+ cn(
253
+ "flex flex-col-reverse gap-2 group-data-[size=sm]/alert-dialog-content:grid group-data-[size=sm]/alert-dialog-content:grid-cols-2 sm:flex-row sm:justify-end",
254
+ read(props.class),
255
+ )}"
256
+ >${slot(props.children)}</div>`,
257
+ );
258
+
259
+ /** An icon block above the title — a warning glyph, a product mark. */
260
+ export const AlertDialogMedia = component<AlertDialogSectionProps>(
261
+ (props) => html`<div
262
+ data-slot="alert-dialog-media"
263
+ class="${() =>
264
+ cn(
265
+ "bg-muted mb-2 inline-flex size-16 items-center justify-center rounded-md sm:group-data-[size=default]/alert-dialog-content:row-span-2 *:[svg:not([class*='size-'])]:size-8",
266
+ read(props.class),
267
+ )}"
268
+ >${slot(props.children)}</div>`,
269
+ );
270
+
271
+ export const AlertDialogTitle = component<AlertDialogSectionProps>((props) => {
272
+ const dialog = inject(AlertDialogContext);
273
+ dialog.declareTitle();
274
+ return html`<h2
275
+ id="${dialog.titleId}"
276
+ data-slot="alert-dialog-title"
277
+ class="${() =>
278
+ cn(
279
+ "text-lg font-semibold sm:group-data-[size=default]/alert-dialog-content:group-has-data-[slot=alert-dialog-media]/alert-dialog-content:col-start-2",
280
+ read(props.class),
281
+ )}"
282
+ >${slot(props.children)}</h2>`;
283
+ });
284
+
285
+ export const AlertDialogDescription = component<AlertDialogSectionProps>(
286
+ (props) => {
287
+ const dialog = inject(AlertDialogContext);
288
+ dialog.declareDescription();
289
+ return html`<p
290
+ id="${dialog.descriptionId}"
291
+ data-slot="alert-dialog-description"
292
+ class="${() => cn("text-muted-foreground text-sm", read(props.class))}"
293
+ >${slot(props.children)}</p>`;
294
+ },
295
+ );
296
+
297
+ export interface AlertDialogButtonProps {
298
+ children?: Slot;
299
+ variant?: ButtonVariants["variant"];
300
+ size?: ButtonVariants["size"];
301
+ class?: Reactive<string>;
302
+ }
303
+
304
+ /** The confirming action. */
305
+ export const AlertDialogAction = component<AlertDialogButtonProps>((props) => {
306
+ const dialog = inject(AlertDialogContext);
125
307
  return html`<button
126
308
  type="button"
127
- data-slot="alert-dialog-trigger"
128
- id="${triggerId}"
129
- aria-haspopup="dialog"
130
- class="${() => cn(read(props.triggerClass))}"
131
- @click="${() => state.set(true)}"
132
- >${slot(props.trigger)}</button>`;
309
+ data-slot="alert-dialog-action"
310
+ class="${() =>
311
+ cn(
312
+ buttonVariants({
313
+ variant: props.variant ?? "default",
314
+ size: props.size ?? "default",
315
+ }),
316
+ read(props.class),
317
+ )}"
318
+ @click="${() => dialog.confirm()}"
319
+ >${slot(props.children)}</button>`;
133
320
  });
321
+
322
+ /**
323
+ * The way out, and where focus lands.
324
+ *
325
+ * The safe choice is the one a hurried Enter should hit, which is why the
326
+ * surface focuses this rather than the action beside it.
327
+ */
328
+ export const AlertDialogCancel = component<AlertDialogButtonProps>((props) => {
329
+ const dialog = inject(AlertDialogContext);
330
+ return html`<button
331
+ type="button"
332
+ data-slot="alert-dialog-cancel"
333
+ id="${dialog.cancelId}"
334
+ class="${() =>
335
+ cn(
336
+ buttonVariants({
337
+ variant: props.variant ?? "outline",
338
+ size: props.size ?? "default",
339
+ }),
340
+ read(props.class),
341
+ )}"
342
+ @click="${() => dialog.cancel()}"
343
+ >${slot(props.children)}</button>`;
344
+ });
345
+
346
+ interface SurfaceIds {
347
+ readonly panelId: string;
348
+ readonly titleId: string | undefined;
349
+ readonly descriptionId: string | undefined;
350
+ }
351
+
352
+ function renderSurface(
353
+ registered: RegisteredContent,
354
+ ids: SurfaceIds,
355
+ ): TemplateResult {
356
+ return html`<div data-slot="alert-dialog-portal" class="${cn("fixed inset-0 z-50", fadeInOut)}">
357
+ <div data-slot="alert-dialog-overlay" class="${dialogBackdropClasses}"></div>
358
+ <div
359
+ data-slot="alert-dialog-content"
360
+ data-size="${registered.size}"
361
+ id="${ids.panelId}"
362
+ role="alertdialog"
363
+ aria-modal="true"
364
+ aria-labelledby="${ids.titleId}"
365
+ aria-describedby="${ids.descriptionId}"
366
+ tabindex="-1"
367
+ class="${cn(panelClasses, zoomInOut, read(registered.class))}"
368
+ >${registered.body}</div>
369
+ </div>`;
370
+ }
@@ -17,21 +17,44 @@
17
17
  * the edge carries none of that.
18
18
  */
19
19
 
20
- import { component, html, onMount, onUnmount, signal } from "@c9up/aurora";
20
+ import {
21
+ component,
22
+ createContext,
23
+ html,
24
+ inject,
25
+ onMount,
26
+ onUnmount,
27
+ provide,
28
+ signal,
29
+ } from "@c9up/aurora";
21
30
  import { buttonVariants } from "../atoms/Button.js";
22
- import type { Child } from "../lib/children.js";
31
+ import type { Parts, Slot } from "../lib/children.js";
32
+ import { slot } from "../lib/children.js";
23
33
  import { cn } from "../lib/cn.js";
24
34
  import { ArrowLeftIcon, ArrowRightIcon } from "../lib/icons.js";
25
35
  import { uid } from "../lib/id.js";
26
36
  import { type Reactive, read } from "../lib/props.js";
27
37
 
38
+ interface CarouselApi {
39
+ readonly trackId: string;
40
+ readonly horizontal: boolean;
41
+ readonly atStart: () => boolean;
42
+ readonly atEnd: () => boolean;
43
+ /** Claim the next slide's position, for its "n of m" label. */
44
+ claimSlide(): number;
45
+ readonly slideCount: () => number;
46
+ scrollByPage(direction: 1 | -1): void;
47
+ syncEdges(): void;
48
+ }
49
+
50
+ const CarouselContext = createContext<CarouselApi>("Carousel");
51
+
28
52
  export interface CarouselProps {
29
- slides: readonly Child[];
30
53
  /** Announced before the slides. */
31
54
  label?: string;
32
55
  orientation?: "horizontal" | "vertical";
33
56
  class?: Reactive<string>;
34
- slideClass?: Reactive<string>;
57
+ children?: Parts;
35
58
  }
36
59
 
37
60
  export const Carousel = component<CarouselProps>((props) => {
@@ -39,18 +62,18 @@ export const Carousel = component<CarouselProps>((props) => {
39
62
  const horizontal = props.orientation !== "vertical";
40
63
  const atStart = signal(true);
41
64
  const atEnd = signal(false);
65
+ let slides = 0;
42
66
 
43
67
  function track(): HTMLElement | null {
44
68
  return document.getElementById(trackId);
45
69
  }
46
70
 
47
71
  /**
48
- * Recompute whether either end is reached.
72
+ * Which arrows are usable, read from the scroll position.
49
73
  *
50
- * The one-pixel tolerance is load-bearing: with fractional device pixel
51
- * ratios, `scrollLeft` at the far end lands a fraction short of
52
- * `scrollWidth - clientWidth`, and an exact comparison leaves the "next"
53
- * button enabled on a carousel that cannot scroll any further.
74
+ * Derived rather than tracked: the track is a scroll container, so a drag,
75
+ * a wheel, a trackpad swipe and the buttons all move it, and only the
76
+ * element knows where it ended up.
54
77
  */
55
78
  function syncEdges(): void {
56
79
  const element = track();
@@ -98,7 +121,19 @@ export const Carousel = component<CarouselProps>((props) => {
98
121
  window.removeEventListener("resize", syncEdges);
99
122
  });
100
123
 
101
- const count = props.slides.length;
124
+ provide<CarouselApi>(CarouselContext, {
125
+ trackId,
126
+ horizontal,
127
+ atStart: () => atStart(),
128
+ atEnd: () => atEnd(),
129
+ claimSlide() {
130
+ slides += 1;
131
+ return slides;
132
+ },
133
+ slideCount: () => slides,
134
+ scrollByPage,
135
+ syncEdges,
136
+ });
102
137
 
103
138
  return html`<div
104
139
  data-slot="carousel"
@@ -107,55 +142,97 @@ export const Carousel = component<CarouselProps>((props) => {
107
142
  aria-label="${props.label ?? "Carousel"}"
108
143
  class="${() => cn("relative", read(props.class))}"
109
144
  @keydown="${onKeyDown}"
110
- >
111
- <div
112
- data-slot="carousel-track"
113
- id="${trackId}"
114
- tabindex="0"
115
- class="${cn(
116
- "flex snap-mandatory gap-4 overflow-auto scroll-smooth outline-none motion-reduce:scroll-auto",
117
- horizontal
118
- ? "snap-x flex-row [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
119
- : "snap-y max-h-96 flex-col [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
145
+ >${props.children?.()}</div>`;
146
+ });
147
+
148
+ export interface CarouselContentProps {
149
+ children?: Slot;
150
+ class?: Reactive<string>;
151
+ }
152
+
153
+ /** The scroll container. */
154
+ export const CarouselContent = component<CarouselContentProps>((props) => {
155
+ const carousel = inject(CarouselContext);
156
+ return html`<div
157
+ data-slot="carousel-content"
158
+ id="${carousel.trackId}"
159
+ tabindex="0"
160
+ class="${() =>
161
+ cn(
162
+ "flex snap-mandatory gap-4 overflow-auto scroll-smooth outline-none motion-reduce:scroll-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
163
+ carousel.horizontal ? "snap-x flex-row" : "snap-y max-h-96 flex-col",
164
+ read(props.class),
120
165
  )}"
121
- >
122
- ${props.slides.map(
123
- (child, index) => html`<div
124
- data-slot="carousel-slide"
125
- role="group"
126
- aria-roledescription="slide"
127
- aria-label="${`${index + 1} of ${count}`}"
128
- class="${() => cn("min-w-0 shrink-0 grow-0 basis-full snap-start", read(props.slideClass))}"
129
- >${child}</div>`,
130
- )}
131
- </div>
132
- <button
133
- type="button"
134
- data-slot="carousel-previous"
135
- aria-label="Previous slide"
136
- ?disabled="${() => atStart()}"
137
- class="${cn(
166
+ >${slot(props.children)}</div>`;
167
+ });
168
+
169
+ export interface CarouselItemProps {
170
+ children?: Slot;
171
+ class?: Reactive<string>;
172
+ }
173
+
174
+ /**
175
+ * One slide.
176
+ *
177
+ * Claims its position as it is built, which is what `"3 of 7"` needs — and the
178
+ * count is only final once every slide has been built, so the label reads it
179
+ * lazily rather than capturing it.
180
+ */
181
+ export const CarouselItem = component<CarouselItemProps>((props) => {
182
+ const carousel = inject(CarouselContext);
183
+ const position = carousel.claimSlide();
184
+ return html`<div
185
+ data-slot="carousel-item"
186
+ role="group"
187
+ aria-roledescription="slide"
188
+ aria-label="${() => `${position} of ${carousel.slideCount()}`}"
189
+ class="${() =>
190
+ cn("min-w-0 shrink-0 grow-0 basis-full snap-start", read(props.class))}"
191
+ >${slot(props.children)}</div>`;
192
+ });
193
+
194
+ export interface CarouselButtonProps {
195
+ class?: Reactive<string>;
196
+ }
197
+
198
+ const arrowClasses = "absolute size-8 rounded-full";
199
+
200
+ export const CarouselPrevious = component<CarouselButtonProps>((props) => {
201
+ const carousel = inject(CarouselContext);
202
+ return html`<button
203
+ type="button"
204
+ data-slot="carousel-previous"
205
+ aria-label="Previous slide"
206
+ ?disabled="${() => carousel.atStart()}"
207
+ class="${() =>
208
+ cn(
138
209
  buttonVariants({ variant: "outline", size: "icon" }),
139
- "absolute size-8 rounded-full",
140
- horizontal
210
+ arrowClasses,
211
+ carousel.horizontal
141
212
  ? "top-1/2 -left-12 -translate-y-1/2"
142
213
  : "-top-12 left-1/2 -translate-x-1/2 rotate-90",
214
+ read(props.class),
143
215
  )}"
144
- @click="${() => scrollByPage(-1)}"
145
- >${ArrowLeftIcon({ class: "size-4" })}</button>
146
- <button
147
- type="button"
148
- data-slot="carousel-next"
149
- aria-label="Next slide"
150
- ?disabled="${() => atEnd()}"
151
- class="${cn(
216
+ @click="${() => carousel.scrollByPage(-1)}"
217
+ >${ArrowLeftIcon({ class: "size-4" })}</button>`;
218
+ });
219
+
220
+ export const CarouselNext = component<CarouselButtonProps>((props) => {
221
+ const carousel = inject(CarouselContext);
222
+ return html`<button
223
+ type="button"
224
+ data-slot="carousel-next"
225
+ aria-label="Next slide"
226
+ ?disabled="${() => carousel.atEnd()}"
227
+ class="${() =>
228
+ cn(
152
229
  buttonVariants({ variant: "outline", size: "icon" }),
153
- "absolute size-8 rounded-full",
154
- horizontal
230
+ arrowClasses,
231
+ carousel.horizontal
155
232
  ? "top-1/2 -right-12 -translate-y-1/2"
156
233
  : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90",
234
+ read(props.class),
157
235
  )}"
158
- @click="${() => scrollByPage(1)}"
159
- >${ArrowRightIcon({ class: "size-4" })}</button>
160
- </div>`;
236
+ @click="${() => carousel.scrollByPage(1)}"
237
+ >${ArrowRightIcon({ class: "size-4" })}</button>`;
161
238
  });