@c9up/nebula 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/dist/atoms/Avatar.d.ts +36 -9
  2. package/dist/atoms/Avatar.js +66 -27
  3. package/dist/atoms/index.d.ts +1 -1
  4. package/dist/atoms/index.js +1 -1
  5. package/dist/lib/children.d.ts +30 -0
  6. package/dist/lib/motion.d.ts +10 -0
  7. package/dist/lib/motion.js +10 -0
  8. package/dist/molecules/Accordion.d.ts +36 -15
  9. package/dist/molecules/Accordion.js +68 -45
  10. package/dist/molecules/Breadcrumb.d.ts +29 -11
  11. package/dist/molecules/Breadcrumb.js +52 -52
  12. package/dist/molecules/Collapsible.d.ts +18 -17
  13. package/dist/molecules/Collapsible.js +42 -34
  14. package/dist/molecules/Field.d.ts +66 -30
  15. package/dist/molecules/Field.js +106 -55
  16. package/dist/molecules/InputGroup.d.ts +39 -21
  17. package/dist/molecules/InputGroup.js +43 -27
  18. package/dist/molecules/InputOTP.d.ts +26 -3
  19. package/dist/molecules/InputOTP.js +109 -76
  20. package/dist/molecules/Message.js +5 -4
  21. package/dist/molecules/Pagination.d.ts +36 -11
  22. package/dist/molecules/Pagination.js +58 -74
  23. package/dist/molecules/RadioGroup.d.ts +25 -17
  24. package/dist/molecules/RadioGroup.js +47 -43
  25. package/dist/molecules/Resizable.d.ts +32 -7
  26. package/dist/molecules/Resizable.js +76 -49
  27. package/dist/molecules/Tabs.d.ts +49 -11
  28. package/dist/molecules/Tabs.js +107 -51
  29. package/dist/molecules/ToggleGroup.d.ts +16 -10
  30. package/dist/molecules/ToggleGroup.js +37 -21
  31. package/dist/molecules/index.d.ts +11 -11
  32. package/dist/molecules/index.js +11 -11
  33. package/dist/organisms/AlertDialog.d.ts +80 -28
  34. package/dist/organisms/AlertDialog.js +192 -67
  35. package/dist/organisms/Carousel.d.ts +25 -3
  36. package/dist/organisms/Carousel.js +75 -43
  37. package/dist/organisms/Combobox.d.ts +2 -1
  38. package/dist/organisms/Combobox.js +46 -16
  39. package/dist/organisms/Command.d.ts +73 -32
  40. package/dist/organisms/Command.js +209 -131
  41. package/dist/organisms/CommandDialog.js +5 -5
  42. package/dist/organisms/ContextMenu.d.ts +41 -19
  43. package/dist/organisms/ContextMenu.js +112 -71
  44. package/dist/organisms/DataTable.js +42 -8
  45. package/dist/organisms/Dialog.d.ts +97 -31
  46. package/dist/organisms/Dialog.js +193 -68
  47. package/dist/organisms/Drawer.d.ts +33 -10
  48. package/dist/organisms/Drawer.js +130 -52
  49. package/dist/organisms/DropdownMenu.d.ts +58 -11
  50. package/dist/organisms/DropdownMenu.js +137 -50
  51. package/dist/organisms/Form.js +9 -11
  52. package/dist/organisms/HoverCard.d.ts +39 -19
  53. package/dist/organisms/HoverCard.js +116 -40
  54. package/dist/organisms/Menubar.d.ts +55 -19
  55. package/dist/organisms/Menubar.js +166 -82
  56. package/dist/organisms/NavigationMenu.d.ts +77 -31
  57. package/dist/organisms/NavigationMenu.js +169 -98
  58. package/dist/organisms/Popover.d.ts +72 -12
  59. package/dist/organisms/Popover.js +132 -25
  60. package/dist/organisms/Questionnaire.js +5 -5
  61. package/dist/organisms/Select.d.ts +99 -18
  62. package/dist/organisms/Select.js +253 -111
  63. package/dist/organisms/Sheet.d.ts +57 -13
  64. package/dist/organisms/Sheet.js +141 -54
  65. package/dist/organisms/Sidebar.d.ts +146 -56
  66. package/dist/organisms/Sidebar.js +236 -128
  67. package/dist/organisms/Tooltip.d.ts +88 -14
  68. package/dist/organisms/Tooltip.js +210 -47
  69. package/dist/organisms/index.d.ts +15 -15
  70. package/dist/organisms/index.js +15 -15
  71. package/dist/primitives/floating.js +25 -0
  72. package/dist/primitives/floatingSurface.d.ts +11 -0
  73. package/dist/primitives/floatingSurface.js +8 -0
  74. package/dist/primitives/menu.d.ts +45 -1
  75. package/dist/primitives/menu.js +45 -14
  76. package/dist/primitives/menuParts.d.ts +113 -0
  77. package/dist/primitives/menuParts.js +207 -0
  78. package/nebula.css +1 -1
  79. package/package.json +3 -3
  80. package/registry.json +18 -6
  81. package/src/atoms/Avatar.ts +141 -37
  82. package/src/atoms/index.ts +13 -1
  83. package/src/lib/children.ts +31 -0
  84. package/src/lib/motion.ts +12 -0
  85. package/src/molecules/Accordion.ts +120 -60
  86. package/src/molecules/Breadcrumb.ts +77 -76
  87. package/src/molecules/Collapsible.ts +77 -41
  88. package/src/molecules/Field.ts +187 -72
  89. package/src/molecules/InputGroup.ts +88 -35
  90. package/src/molecules/InputOTP.ts +150 -86
  91. package/src/molecules/Message.ts +6 -4
  92. package/src/molecules/Pagination.ts +96 -101
  93. package/src/molecules/RadioGroup.ts +70 -59
  94. package/src/molecules/Resizable.ts +138 -75
  95. package/src/molecules/Tabs.ts +187 -74
  96. package/src/molecules/ToggleGroup.ts +77 -38
  97. package/src/molecules/index.ts +85 -8
  98. package/src/organisms/AlertDialog.ts +319 -82
  99. package/src/organisms/Carousel.ts +129 -52
  100. package/src/organisms/Combobox.ts +64 -19
  101. package/src/organisms/Command.ts +342 -159
  102. package/src/organisms/CommandDialog.ts +6 -6
  103. package/src/organisms/ContextMenu.ts +185 -83
  104. package/src/organisms/DataTable.ts +56 -10
  105. package/src/organisms/Dialog.ts +312 -102
  106. package/src/organisms/Drawer.ts +219 -71
  107. package/src/organisms/DropdownMenu.ts +219 -61
  108. package/src/organisms/Form.ts +48 -34
  109. package/src/organisms/HoverCard.ts +187 -53
  110. package/src/organisms/Menubar.ts +247 -87
  111. package/src/organisms/NavigationMenu.ts +306 -128
  112. package/src/organisms/Popover.ts +215 -39
  113. package/src/organisms/Questionnaire.ts +9 -6
  114. package/src/organisms/Select.ts +397 -130
  115. package/src/organisms/Sheet.ts +252 -83
  116. package/src/organisms/Sidebar.ts +438 -185
  117. package/src/organisms/Tooltip.ts +315 -65
  118. package/src/organisms/index.ts +284 -14
  119. package/src/primitives/floating.ts +33 -0
  120. package/src/primitives/floatingSurface.ts +19 -0
  121. package/src/primitives/menu.ts +79 -18
  122. package/src/primitives/menuParts.ts +371 -0
@@ -1,22 +1,33 @@
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
- import { component, html } from "@c9up/aurora";
30
+ import { component, createContext, html, inject, onUnmount, provide, } from "@c9up/aurora";
20
31
  import { buttonVariants } from "../atoms/Button.js";
21
32
  import { slot } from "../lib/children.js";
22
33
  import { cn } from "../lib/cn.js";
@@ -25,13 +36,19 @@ import { fadeInOut, zoomInOut } from "../lib/motion.js";
25
36
  import { read } from "../lib/props.js";
26
37
  import { controllable } from "../primitives/controllable.js";
27
38
  import { modalSurface } from "../primitives/modalSurface.js";
28
- import { dialogBackdropClasses, dialogPanelClasses } from "./Dialog.js";
39
+ import { portal } from "../primitives/portal.js";
40
+ import { dialogBackdropClasses } from "./Dialog.js";
41
+ const panelClasses = "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";
42
+ const AlertDialogContext = createContext("AlertDialog");
29
43
  export const AlertDialog = component((props) => {
30
44
  const triggerId = uid("alert-dialog-trigger");
31
45
  const panelId = uid("alert-dialog-panel");
32
46
  const titleId = uid("alert-dialog-title");
33
47
  const descriptionId = uid("alert-dialog-description");
34
48
  const cancelId = uid("alert-dialog-cancel");
49
+ let hasTitle = false;
50
+ let hasDescription = false;
51
+ let content;
35
52
  const state = controllable({
36
53
  value: props.open,
37
54
  initial: props.defaultOpen ?? false,
@@ -45,60 +62,168 @@ export const AlertDialog = component((props) => {
45
62
  props.onConfirm?.();
46
63
  state.set(false);
47
64
  }
48
- modalSurface({
65
+ provide(AlertDialogContext, {
66
+ triggerId,
67
+ panelId,
68
+ titleId,
69
+ descriptionId,
70
+ cancelId,
49
71
  open: () => state.current(),
50
- onClose: cancel,
51
- panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
52
- initialFocus: (panel) => panel.querySelector(`#${CSS.escape(cancelId)}`),
53
- returnFocus: () => document.getElementById(triggerId),
54
- dismissOnOutside: false,
55
- content: () => html `<div data-slot="alert-dialog-overlay" class="${cn("fixed inset-0 z-50", fadeInOut)}">
56
- <div class="${dialogBackdropClasses}"></div>
57
- <div
58
- data-slot="alert-dialog-content"
59
- id="${panelId}"
60
- role="alertdialog"
61
- aria-modal="true"
62
- aria-labelledby="${titleId}"
63
- aria-describedby="${descriptionId}"
64
- tabindex="-1"
65
- class="${cn(dialogPanelClasses, zoomInOut, read(props.contentClass))}"
66
- >
67
- <div class="flex flex-col gap-2 text-center sm:text-left">
68
- <h2 id="${titleId}" class="text-lg font-semibold">${props.title}</h2>
69
- <p id="${descriptionId}" class="text-muted-foreground text-sm">
70
- ${props.description}
71
- </p>
72
- </div>
73
- <div class="flex flex-col-reverse gap-2 sm:flex-row sm:justify-end">
74
- <button
75
- type="button"
76
- id="${cancelId}"
77
- data-slot="alert-dialog-cancel"
78
- class="${buttonVariants({ variant: "outline" })}"
79
- @click="${cancel}"
80
- >${props.cancelLabel ?? "Cancel"}</button>
81
- <button
82
- type="button"
83
- data-slot="alert-dialog-action"
84
- class="${buttonVariants({
85
- variant: props.destructive === true ? "destructive" : "default",
86
- })}"
87
- @click="${confirm}"
88
- >${props.actionLabel ?? "Continue"}</button>
89
- </div>
90
- </div>
91
- </div>`,
72
+ setOpen: (next) => state.set(next),
73
+ confirm,
74
+ cancel,
75
+ declareTitle() {
76
+ hasTitle = true;
77
+ },
78
+ declareDescription() {
79
+ hasDescription = true;
80
+ },
81
+ register(registered) {
82
+ content = registered;
83
+ },
92
84
  });
93
- if (props.trigger === undefined) {
94
- return html `<span data-slot="alert-dialog" hidden></span>`;
85
+ const parts = props.children?.();
86
+ if (content !== undefined) {
87
+ const registered = content;
88
+ if (!hasTitle) {
89
+ console.warn("nebula: an AlertDialog has no AlertDialogTitle. It will be announced only as “alertdialog”.");
90
+ }
91
+ modalSurface({
92
+ open: () => state.current(),
93
+ // Escape cancels rather than confirms: the keyboard spelling of the
94
+ // safe way out.
95
+ onClose: cancel,
96
+ panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
97
+ initialFocus: (panel) => panel.querySelector(`#${CSS.escape(cancelId)}`),
98
+ returnFocus: () => document.getElementById(triggerId),
99
+ dismissOnOutside: false,
100
+ content: () => renderSurface(registered, {
101
+ panelId,
102
+ titleId: hasTitle ? titleId : undefined,
103
+ descriptionId: hasDescription ? descriptionId : undefined,
104
+ }),
105
+ });
95
106
  }
107
+ return html `${parts}`;
108
+ });
109
+ export const AlertDialogTrigger = component((props) => {
110
+ const dialog = inject(AlertDialogContext);
111
+ return html `<button
112
+ type="button"
113
+ data-slot="alert-dialog-trigger"
114
+ id="${dialog.triggerId}"
115
+ aria-haspopup="dialog"
116
+ aria-expanded="${() => (dialog.open() ? "true" : "false")}"
117
+ data-state="${() => (dialog.open() ? "open" : "closed")}"
118
+ class="${() => cn(read(props.class))}"
119
+ @click="${() => dialog.setOpen(true)}"
120
+ >${slot(props.children)}</button>`;
121
+ });
122
+ export const AlertDialogContent = component((props) => {
123
+ const dialog = inject(AlertDialogContext);
124
+ dialog.register({
125
+ body: props.children?.(),
126
+ size: props.size ?? "default",
127
+ class: props.class,
128
+ });
129
+ return html ``;
130
+ });
131
+ export const AlertDialogOverlay = component((props) => html `<div
132
+ data-slot="alert-dialog-overlay"
133
+ class="${() => cn(dialogBackdropClasses, read(props.class))}"
134
+ ></div>`);
135
+ /**
136
+ * Mount children outside the component's own DOM position.
137
+ *
138
+ * `AlertDialogContent` does not need it — the dialog portals its whole overlay
139
+ * through `modalSurface`, which also owns the trap and the scroll lock. It is
140
+ * exported because upstream exports it, doing here what its name says rather
141
+ * than standing in as a passthrough.
142
+ */
143
+ export const AlertDialogPortal = component((props) => {
144
+ const mounted = portal(html `${slot(props.children)}`, {
145
+ container: props.container,
146
+ });
147
+ mounted.host.setAttribute("data-slot", "alert-dialog-portal");
148
+ onUnmount(() => mounted.close());
149
+ return html ``;
150
+ });
151
+ export const AlertDialogHeader = component((props) => html `<div
152
+ data-slot="alert-dialog-header"
153
+ class="${() => cn("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", read(props.class))}"
154
+ >${slot(props.children)}</div>`);
155
+ export const AlertDialogFooter = component((props) => html `<div
156
+ data-slot="alert-dialog-footer"
157
+ class="${() => cn("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", read(props.class))}"
158
+ >${slot(props.children)}</div>`);
159
+ /** An icon block above the title — a warning glyph, a product mark. */
160
+ export const AlertDialogMedia = component((props) => html `<div
161
+ data-slot="alert-dialog-media"
162
+ class="${() => cn("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", read(props.class))}"
163
+ >${slot(props.children)}</div>`);
164
+ export const AlertDialogTitle = component((props) => {
165
+ const dialog = inject(AlertDialogContext);
166
+ dialog.declareTitle();
167
+ return html `<h2
168
+ id="${dialog.titleId}"
169
+ data-slot="alert-dialog-title"
170
+ class="${() => cn("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", read(props.class))}"
171
+ >${slot(props.children)}</h2>`;
172
+ });
173
+ export const AlertDialogDescription = component((props) => {
174
+ const dialog = inject(AlertDialogContext);
175
+ dialog.declareDescription();
176
+ return html `<p
177
+ id="${dialog.descriptionId}"
178
+ data-slot="alert-dialog-description"
179
+ class="${() => cn("text-muted-foreground text-sm", read(props.class))}"
180
+ >${slot(props.children)}</p>`;
181
+ });
182
+ /** The confirming action. */
183
+ export const AlertDialogAction = component((props) => {
184
+ const dialog = inject(AlertDialogContext);
185
+ return html `<button
186
+ type="button"
187
+ data-slot="alert-dialog-action"
188
+ class="${() => cn(buttonVariants({
189
+ variant: props.variant ?? "default",
190
+ size: props.size ?? "default",
191
+ }), read(props.class))}"
192
+ @click="${() => dialog.confirm()}"
193
+ >${slot(props.children)}</button>`;
194
+ });
195
+ /**
196
+ * The way out, and where focus lands.
197
+ *
198
+ * The safe choice is the one a hurried Enter should hit, which is why the
199
+ * surface focuses this rather than the action beside it.
200
+ */
201
+ export const AlertDialogCancel = component((props) => {
202
+ const dialog = inject(AlertDialogContext);
96
203
  return html `<button
97
204
  type="button"
98
- data-slot="alert-dialog-trigger"
99
- id="${triggerId}"
100
- aria-haspopup="dialog"
101
- class="${() => cn(read(props.triggerClass))}"
102
- @click="${() => state.set(true)}"
103
- >${slot(props.trigger)}</button>`;
205
+ data-slot="alert-dialog-cancel"
206
+ id="${dialog.cancelId}"
207
+ class="${() => cn(buttonVariants({
208
+ variant: props.variant ?? "outline",
209
+ size: props.size ?? "default",
210
+ }), read(props.class))}"
211
+ @click="${() => dialog.cancel()}"
212
+ >${slot(props.children)}</button>`;
104
213
  });
214
+ function renderSurface(registered, ids) {
215
+ return html `<div data-slot="alert-dialog-portal" class="${cn("fixed inset-0 z-50", fadeInOut)}">
216
+ <div data-slot="alert-dialog-overlay" class="${dialogBackdropClasses}"></div>
217
+ <div
218
+ data-slot="alert-dialog-content"
219
+ data-size="${registered.size}"
220
+ id="${ids.panelId}"
221
+ role="alertdialog"
222
+ aria-modal="true"
223
+ aria-labelledby="${ids.titleId}"
224
+ aria-describedby="${ids.descriptionId}"
225
+ tabindex="-1"
226
+ class="${cn(panelClasses, zoomInOut, read(registered.class))}"
227
+ >${registered.body}</div>
228
+ </div>`;
229
+ }
@@ -16,14 +16,36 @@
16
16
  * tell how much is off-screen — the visual affordance of a strip running off
17
17
  * the edge carries none of that.
18
18
  */
19
- import type { Child } from "../lib/children.js";
19
+ import type { Parts, Slot } from "../lib/children.js";
20
20
  import { type Reactive } from "../lib/props.js";
21
21
  export interface CarouselProps {
22
- slides: readonly Child[];
23
22
  /** Announced before the slides. */
24
23
  label?: string;
25
24
  orientation?: "horizontal" | "vertical";
26
25
  class?: Reactive<string>;
27
- slideClass?: Reactive<string>;
26
+ children?: Parts;
28
27
  }
29
28
  export declare const Carousel: (props?: CarouselProps | undefined) => import("@c9up/aurora").TemplateResult;
29
+ export interface CarouselContentProps {
30
+ children?: Slot;
31
+ class?: Reactive<string>;
32
+ }
33
+ /** The scroll container. */
34
+ export declare const CarouselContent: (props?: CarouselContentProps | undefined) => import("@c9up/aurora").TemplateResult;
35
+ export interface CarouselItemProps {
36
+ children?: Slot;
37
+ class?: Reactive<string>;
38
+ }
39
+ /**
40
+ * One slide.
41
+ *
42
+ * Claims its position as it is built, which is what `"3 of 7"` needs — and the
43
+ * count is only final once every slide has been built, so the label reads it
44
+ * lazily rather than capturing it.
45
+ */
46
+ export declare const CarouselItem: (props?: CarouselItemProps | undefined) => import("@c9up/aurora").TemplateResult;
47
+ export interface CarouselButtonProps {
48
+ class?: Reactive<string>;
49
+ }
50
+ export declare const CarouselPrevious: (props?: CarouselButtonProps | undefined) => import("@c9up/aurora").TemplateResult;
51
+ export declare const CarouselNext: (props?: CarouselButtonProps | undefined) => import("@c9up/aurora").TemplateResult;
@@ -16,27 +16,29 @@
16
16
  * tell how much is off-screen — the visual affordance of a strip running off
17
17
  * the edge carries none of that.
18
18
  */
19
- import { component, html, onMount, onUnmount, signal } from "@c9up/aurora";
19
+ import { component, createContext, html, inject, onMount, onUnmount, provide, signal, } from "@c9up/aurora";
20
20
  import { buttonVariants } from "../atoms/Button.js";
21
+ import { slot } from "../lib/children.js";
21
22
  import { cn } from "../lib/cn.js";
22
23
  import { ArrowLeftIcon, ArrowRightIcon } from "../lib/icons.js";
23
24
  import { uid } from "../lib/id.js";
24
25
  import { read } from "../lib/props.js";
26
+ const CarouselContext = createContext("Carousel");
25
27
  export const Carousel = component((props) => {
26
28
  const trackId = uid("carousel-track");
27
29
  const horizontal = props.orientation !== "vertical";
28
30
  const atStart = signal(true);
29
31
  const atEnd = signal(false);
32
+ let slides = 0;
30
33
  function track() {
31
34
  return document.getElementById(trackId);
32
35
  }
33
36
  /**
34
- * Recompute whether either end is reached.
37
+ * Which arrows are usable, read from the scroll position.
35
38
  *
36
- * The one-pixel tolerance is load-bearing: with fractional device pixel
37
- * ratios, `scrollLeft` at the far end lands a fraction short of
38
- * `scrollWidth - clientWidth`, and an exact comparison leaves the "next"
39
- * button enabled on a carousel that cannot scroll any further.
39
+ * Derived rather than tracked: the track is a scroll container, so a drag,
40
+ * a wheel, a trackpad swipe and the buttons all move it, and only the
41
+ * element knows where it ended up.
40
42
  */
41
43
  function syncEdges() {
42
44
  const element = track();
@@ -79,7 +81,19 @@ export const Carousel = component((props) => {
79
81
  track()?.removeEventListener("scroll", syncEdges);
80
82
  window.removeEventListener("resize", syncEdges);
81
83
  });
82
- const count = props.slides.length;
84
+ provide(CarouselContext, {
85
+ trackId,
86
+ horizontal,
87
+ atStart: () => atStart(),
88
+ atEnd: () => atEnd(),
89
+ claimSlide() {
90
+ slides += 1;
91
+ return slides;
92
+ },
93
+ slideCount: () => slides,
94
+ scrollByPage,
95
+ syncEdges,
96
+ });
83
97
  return html `<div
84
98
  data-slot="carousel"
85
99
  role="region"
@@ -87,42 +101,60 @@ export const Carousel = component((props) => {
87
101
  aria-label="${props.label ?? "Carousel"}"
88
102
  class="${() => cn("relative", read(props.class))}"
89
103
  @keydown="${onKeyDown}"
90
- >
91
- <div
92
- data-slot="carousel-track"
93
- id="${trackId}"
94
- tabindex="0"
95
- class="${cn("flex snap-mandatory gap-4 overflow-auto scroll-smooth outline-none motion-reduce:scroll-auto", horizontal
96
- ? "snap-x flex-row [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
97
- : "snap-y max-h-96 flex-col [scrollbar-width:none] [&::-webkit-scrollbar]:hidden")}"
98
- >
99
- ${props.slides.map((child, index) => html `<div
100
- data-slot="carousel-slide"
101
- role="group"
102
- aria-roledescription="slide"
103
- aria-label="${`${index + 1} of ${count}`}"
104
- class="${() => cn("min-w-0 shrink-0 grow-0 basis-full snap-start", read(props.slideClass))}"
105
- >${child}</div>`)}
106
- </div>
107
- <button
108
- type="button"
109
- data-slot="carousel-previous"
110
- aria-label="Previous slide"
111
- ?disabled="${() => atStart()}"
112
- class="${cn(buttonVariants({ variant: "outline", size: "icon" }), "absolute size-8 rounded-full", horizontal
104
+ >${props.children?.()}</div>`;
105
+ });
106
+ /** The scroll container. */
107
+ export const CarouselContent = component((props) => {
108
+ const carousel = inject(CarouselContext);
109
+ return html `<div
110
+ data-slot="carousel-content"
111
+ id="${carousel.trackId}"
112
+ tabindex="0"
113
+ class="${() => cn("flex snap-mandatory gap-4 overflow-auto scroll-smooth outline-none motion-reduce:scroll-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden", carousel.horizontal ? "snap-x flex-row" : "snap-y max-h-96 flex-col", read(props.class))}"
114
+ >${slot(props.children)}</div>`;
115
+ });
116
+ /**
117
+ * One slide.
118
+ *
119
+ * Claims its position as it is built, which is what `"3 of 7"` needs — and the
120
+ * count is only final once every slide has been built, so the label reads it
121
+ * lazily rather than capturing it.
122
+ */
123
+ export const CarouselItem = component((props) => {
124
+ const carousel = inject(CarouselContext);
125
+ const position = carousel.claimSlide();
126
+ return html `<div
127
+ data-slot="carousel-item"
128
+ role="group"
129
+ aria-roledescription="slide"
130
+ aria-label="${() => `${position} of ${carousel.slideCount()}`}"
131
+ class="${() => cn("min-w-0 shrink-0 grow-0 basis-full snap-start", read(props.class))}"
132
+ >${slot(props.children)}</div>`;
133
+ });
134
+ const arrowClasses = "absolute size-8 rounded-full";
135
+ export const CarouselPrevious = component((props) => {
136
+ const carousel = inject(CarouselContext);
137
+ return html `<button
138
+ type="button"
139
+ data-slot="carousel-previous"
140
+ aria-label="Previous slide"
141
+ ?disabled="${() => carousel.atStart()}"
142
+ class="${() => cn(buttonVariants({ variant: "outline", size: "icon" }), arrowClasses, carousel.horizontal
113
143
  ? "top-1/2 -left-12 -translate-y-1/2"
114
- : "-top-12 left-1/2 -translate-x-1/2 rotate-90")}"
115
- @click="${() => scrollByPage(-1)}"
116
- >${ArrowLeftIcon({ class: "size-4" })}</button>
117
- <button
118
- type="button"
119
- data-slot="carousel-next"
120
- aria-label="Next slide"
121
- ?disabled="${() => atEnd()}"
122
- class="${cn(buttonVariants({ variant: "outline", size: "icon" }), "absolute size-8 rounded-full", horizontal
144
+ : "-top-12 left-1/2 -translate-x-1/2 rotate-90", read(props.class))}"
145
+ @click="${() => carousel.scrollByPage(-1)}"
146
+ >${ArrowLeftIcon({ class: "size-4" })}</button>`;
147
+ });
148
+ export const CarouselNext = component((props) => {
149
+ const carousel = inject(CarouselContext);
150
+ return html `<button
151
+ type="button"
152
+ data-slot="carousel-next"
153
+ aria-label="Next slide"
154
+ ?disabled="${() => carousel.atEnd()}"
155
+ class="${() => cn(buttonVariants({ variant: "outline", size: "icon" }), arrowClasses, carousel.horizontal
123
156
  ? "top-1/2 -right-12 -translate-y-1/2"
124
- : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90")}"
125
- @click="${() => scrollByPage(1)}"
126
- >${ArrowRightIcon({ class: "size-4" })}</button>
127
- </div>`;
157
+ : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90", read(props.class))}"
158
+ @click="${() => carousel.scrollByPage(1)}"
159
+ >${ArrowRightIcon({ class: "size-4" })}</button>`;
128
160
  });
@@ -14,6 +14,7 @@
14
14
  * control — and returns to the trigger on close, so the tab order does not
15
15
  * jump to the top of the page after choosing.
16
16
  */
17
+ import { type TemplateResult } from "@c9up/aurora";
17
18
  import { type Reactive } from "../lib/props.js";
18
19
  export interface ComboboxOption {
19
20
  value: string;
@@ -38,4 +39,4 @@ export interface ComboboxProps {
38
39
  contentClass?: Reactive<string>;
39
40
  onValueChange?: (value: string) => void;
40
41
  }
41
- export declare const Combobox: (props?: ComboboxProps | undefined) => import("@c9up/aurora").TemplateResult;
42
+ export declare const Combobox: (props?: ComboboxProps | undefined) => TemplateResult;
@@ -23,7 +23,7 @@ import { 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
- import { Command } from "./Command.js";
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
29
  const triggerId = uid("combobox-trigger");
@@ -40,18 +40,53 @@ export const Combobox = component((props) => {
40
40
  function labelFor(value) {
41
41
  return props.options.find((option) => option.value === value)?.label;
42
42
  }
43
- const items = () => props.options.map((option) => ({
44
- value: option.value,
45
- label: option.label,
46
- keywords: option.keywords,
47
- disabled: option.disabled,
48
- group: option.group,
49
- }));
50
- function choose(item) {
51
- selection.set(item.value);
43
+ function choose(value) {
44
+ selection.set(value);
52
45
  open(false);
53
46
  focusSilently(document.getElementById(triggerId));
54
47
  }
48
+ /**
49
+ * The palette inside the popover.
50
+ *
51
+ * Built fresh on each open, so the options prop is read at the moment the
52
+ * surface appears rather than captured once — a combobox whose options load
53
+ * asynchronously is the common case.
54
+ *
55
+ * Consecutive options sharing a `group` are gathered under one heading, in
56
+ * the order they were given. A group that repeats after an interruption
57
+ * gets a second heading, because reordering the caller's list to merge them
58
+ * would change which option the arrows reach next.
59
+ */
60
+ function renderPalette() {
61
+ const groups = [];
62
+ for (const option of props.options) {
63
+ const last = groups[groups.length - 1];
64
+ if (last !== undefined && last.heading === option.group) {
65
+ last.options.push(option);
66
+ }
67
+ else {
68
+ groups.push({ heading: option.group, options: [option] });
69
+ }
70
+ }
71
+ return Command({
72
+ onSelect: choose,
73
+ children: () => html `${CommandInput({
74
+ placeholder: props.searchPlaceholder ?? "Search…",
75
+ })}${CommandList({
76
+ children: () => html `${CommandEmpty({
77
+ children: props.emptyMessage ?? "No results found.",
78
+ })}${groups.map((group) => CommandGroup({
79
+ heading: group.heading,
80
+ children: () => group.options.map((option) => CommandItem({
81
+ value: option.value,
82
+ children: option.label,
83
+ keywords: option.keywords,
84
+ disabled: option.disabled,
85
+ })),
86
+ }))}`,
87
+ })}`,
88
+ });
89
+ }
55
90
  floatingSurface({
56
91
  anchor: () => document.getElementById(triggerId),
57
92
  open: () => open(),
@@ -69,12 +104,7 @@ export const Combobox = component((props) => {
69
104
  id="${contentId}"
70
105
  class="${cn("bg-popover text-popover-foreground z-50 min-w-(--nebula-anchor-width) overflow-hidden rounded-md border shadow-md", zoomInOut, read(props.contentClass))}"
71
106
  >
72
- ${Command({
73
- items: items(),
74
- placeholder: props.searchPlaceholder ?? "Search…",
75
- emptyMessage: props.emptyMessage ?? "No results found.",
76
- onSelect: choose,
77
- })}
107
+ ${renderPalette()}
78
108
  </div>`,
79
109
  });
80
110
  return html `<div data-slot="combobox" class="${() => cn("inline-flex", read(props.class))}">