@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
@@ -8,14 +8,30 @@
8
8
  * design system that can style every control except this one is not a design
9
9
  * system.
10
10
  *
11
- * What that costs is everything the native control was doing for free, and it
12
- * all has to be put back:
11
+ * Select({ name: "plan", children: () => html`
12
+ * ${SelectTrigger({ children: SelectValue({ placeholder: "Pick one" }) })}
13
+ * ${SelectContent({ children: () => html`
14
+ * ${SelectGroup({ children: html`
15
+ * ${SelectLabel({ children: "Plans" })}
16
+ * ${SelectItem({ value: "free", children: "Free" })}
17
+ * ${SelectItem({ value: "pro", children: "Pro" })}
18
+ * ` })}
19
+ * ` })}
20
+ * ` })
21
+ *
22
+ * The items REGISTER themselves as they are built, in document order, and the
23
+ * keyboard model then walks that list rather than the DOM. That is deliberate:
24
+ * the listbox pattern keeps focus on the trigger and only moves
25
+ * `aria-activedescendant`, so there is no focused element to walk from.
26
+ *
27
+ * What a native `<select>` was doing for free, and all of which has to be put
28
+ * back by hand:
13
29
  *
14
30
  * - `role="combobox"` on the trigger, `role="listbox"` on the panel,
15
31
  * `role="option"` with `aria-selected` on each entry.
16
- * - `aria-activedescendant` rather than moving DOM focus — the listbox pattern
17
- * keeps focus on the trigger and *names* the highlighted option, which is
18
- * what lets a screen reader announce it without the focus ring jumping.
32
+ * - `aria-activedescendant` rather than moving DOM focus — it is what lets a
33
+ * screen reader announce the highlighted option without the focus ring
34
+ * jumping.
19
35
  * - Type-ahead, arrows, Home/End, Enter and Escape.
20
36
  * - A hidden input, so the control still posts with a plain HTML form.
21
37
  *
@@ -23,48 +39,99 @@
23
39
  * it, so a hundred-option list scrolls rather than running off the screen.
24
40
  */
25
41
 
26
- import { component, html, signal } from "@c9up/aurora";
27
- import type { Child } from "../lib/children.js";
42
+ import {
43
+ component,
44
+ createContext,
45
+ html,
46
+ inject,
47
+ onUnmount,
48
+ provide,
49
+ signal,
50
+ type TemplateResult,
51
+ } from "@c9up/aurora";
52
+ import type { Child, Parts, Slot } from "../lib/children.js";
53
+ import { slot } from "../lib/children.js";
28
54
  import { cn } from "../lib/cn.js";
29
- import { CheckIcon, ChevronDownIcon } from "../lib/icons.js";
55
+ import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "../lib/icons.js";
30
56
  import { uid } from "../lib/id.js";
31
- import { zoomInOut } from "../lib/motion.js";
57
+ import { slideInFromSide, zoomInOut } from "../lib/motion.js";
32
58
  import { type Reactive, read, readOr } from "../lib/props.js";
33
59
  import { controllable } from "../primitives/controllable.js";
60
+ import type { Align, Placement, Side } from "../primitives/floating.js";
34
61
  import { floatingSurface } from "../primitives/floatingSurface.js";
35
62
  import { focusSilently } from "../primitives/focusable.js";
36
63
 
37
- export interface SelectOption {
38
- value: string;
39
- label: string;
40
- disabled?: boolean;
41
- /** Groups consecutive options under a heading. */
42
- group?: string;
64
+ export const selectTriggerClasses =
65
+ "border-input dark:bg-input/30 dark:hover:bg-input/50 flex w-fit min-w-0 items-center justify-between gap-2 rounded-md border bg-transparent px-3 py-2 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-[placeholder]:text-muted-foreground data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground";
66
+
67
+ export const selectContentClasses =
68
+ "bg-popover text-popover-foreground relative z-50 max-h-(--nebula-available-height) min-w-(--nebula-anchor-width) origin-(--nebula-transform-origin) overflow-x-hidden overflow-y-auto rounded-md border shadow-md";
69
+
70
+ const itemClasses =
71
+ "relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 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]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground";
72
+
73
+ const scrollButtonClasses =
74
+ "flex cursor-default items-center justify-center py-1";
75
+
76
+ /** One registered option. Built by `SelectItem` as the parts are assembled. */
77
+ interface RegisteredItem {
78
+ readonly value: string;
79
+ readonly id: string;
80
+ /** What type-ahead matches against. */
81
+ readonly textValue: string;
82
+ readonly disabled: boolean;
83
+ }
84
+
85
+ interface RegisteredContent {
86
+ readonly body: Child;
87
+ readonly side: Side;
88
+ readonly align: Align;
89
+ readonly sideOffset: number;
90
+ readonly class?: Reactive<string>;
91
+ }
92
+
93
+ interface SelectApi {
94
+ readonly triggerId: string;
95
+ readonly listId: string;
96
+ readonly open: () => boolean;
97
+ readonly value: () => string | undefined;
98
+ readonly active: () => string | undefined;
99
+ readonly disabled: () => boolean;
100
+ readonly invalid: () => boolean;
101
+ readonly required: boolean;
102
+ /** The label of the chosen option, for `SelectValue`. */
103
+ readonly chosenLabel: () => string | undefined;
104
+ /** This select's id for a value. Per instance: ids must be unique on a page. */
105
+ itemId(value: string): string;
106
+ toggle(): void;
107
+ choose(value: string): void;
108
+ setActive(value: string | undefined): void;
109
+ onTriggerKeyDown(event: KeyboardEvent): void;
110
+ registerItem(item: RegisteredItem): void;
111
+ registerContent(content: RegisteredContent): void;
43
112
  }
44
113
 
114
+ const SelectContext = createContext<SelectApi>("Select");
115
+
45
116
  export interface SelectProps {
46
- options: readonly SelectOption[];
47
117
  name?: string;
48
118
  value?: Reactive<string | undefined>;
49
119
  defaultValue?: string;
50
- placeholder?: string;
51
120
  disabled?: Reactive<boolean>;
52
121
  invalid?: Reactive<boolean>;
53
122
  required?: boolean;
54
- class?: Reactive<string>;
55
- contentClass?: Reactive<string>;
56
123
  onValueChange?: (value: string) => void;
124
+ class?: Reactive<string>;
125
+ children?: Parts;
57
126
  }
58
127
 
59
- export const selectTriggerClasses =
60
- "border-input dark:bg-input/30 dark:hover:bg-input/50 flex h-9 w-fit min-w-0 items-center justify-between gap-2 rounded-md border bg-transparent px-3 py-2 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-[placeholder]:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4";
61
-
62
128
  export const Select = component<SelectProps>((props) => {
63
129
  const triggerId = uid("select-trigger");
64
130
  const listId = uid("select-list");
131
+ const items: RegisteredItem[] = [];
132
+ /** Per instance, not per module: two selects may offer the same value. */
65
133
  const optionIds = new Map<string, string>();
66
- for (const option of props.options)
67
- optionIds.set(option.value, uid("select-option"));
134
+ let content: RegisteredContent | undefined;
68
135
 
69
136
  const open = signal(false);
70
137
  const selection = controllable<string | undefined>({
@@ -77,12 +144,8 @@ export const Select = component<SelectProps>((props) => {
77
144
  /** The option the keyboard is on. Distinct from the selected one. */
78
145
  const active = signal<string | undefined>(undefined);
79
146
 
80
- const enabled = (): readonly SelectOption[] =>
81
- props.options.filter((option) => option.disabled !== true);
82
-
83
- function labelFor(value: string | undefined): string | undefined {
84
- return props.options.find((option) => option.value === value)?.label;
85
- }
147
+ const enabled = (): readonly RegisteredItem[] =>
148
+ items.filter((item) => !item.disabled);
86
149
 
87
150
  function choose(value: string): void {
88
151
  selection.set(value);
@@ -94,7 +157,7 @@ export const Select = component<SelectProps>((props) => {
94
157
  function moveActive(delta: number): void {
95
158
  const list = enabled();
96
159
  if (list.length === 0) return;
97
- const from = list.findIndex((option) => option.value === active());
160
+ const from = list.findIndex((item) => item.value === active());
98
161
  const next = from === -1 ? (delta > 0 ? 0 : list.length - 1) : from + delta;
99
162
  const clamped = Math.min(Math.max(next, 0), list.length - 1);
100
163
  const target = list[clamped];
@@ -102,11 +165,11 @@ export const Select = component<SelectProps>((props) => {
102
165
  }
103
166
 
104
167
  /**
105
- * Type-ahead over the option *data*, not over elements.
168
+ * Type-ahead over the registered options, not over elements.
106
169
  *
107
170
  * The shared primitive walks DOM nodes, which works for menus where focus
108
171
  * moves. Here focus stays on the trigger and only `aria-activedescendant`
109
- * moves, so the search runs over the options array instead.
172
+ * moves, so the search runs over the registration list instead.
110
173
  */
111
174
  let buffer = "";
112
175
  let bufferTimer: ReturnType<typeof setTimeout> | undefined;
@@ -119,17 +182,21 @@ export const Select = component<SelectProps>((props) => {
119
182
  }, 1000);
120
183
 
121
184
  const list = enabled();
122
- const from = list.findIndex((option) => option.value === active());
185
+ const from = list.findIndex((item) => item.value === active());
123
186
  for (let i = 1; i <= list.length; i += 1) {
124
- const option = list[(Math.max(from, 0) + i) % list.length];
125
- if (option === undefined) continue;
126
- if (option.label.toLowerCase().startsWith(buffer)) {
127
- active(option.value);
187
+ const item = list[(Math.max(from, 0) + i) % list.length];
188
+ if (item === undefined) continue;
189
+ if (item.textValue.toLowerCase().startsWith(buffer)) {
190
+ active(item.value);
128
191
  return;
129
192
  }
130
193
  }
131
194
  }
132
195
 
196
+ onUnmount(() => {
197
+ if (bufferTimer !== undefined) clearTimeout(bufferTimer);
198
+ });
199
+
133
200
  function onTriggerKeyDown(event: KeyboardEvent): void {
134
201
  if (readOr(props.disabled, false)) return;
135
202
 
@@ -167,67 +234,62 @@ export const Select = component<SelectProps>((props) => {
167
234
  }
168
235
  }
169
236
 
170
- floatingSurface({
171
- anchor: () => document.getElementById(triggerId),
237
+ provide<SelectApi>(SelectContext, {
238
+ triggerId,
239
+ listId,
172
240
  open: () => open(),
173
- onClose: () => open(false),
174
- placement: "bottom-start",
175
- offset: 4,
176
- matchWidth: true,
177
- // Focus never enters the panel — the listbox pattern keeps it on the
178
- // trigger — so nothing here traps or moves it.
179
- content: renderList,
180
- onOpened: scrollActiveIntoView,
241
+ value: () => selection.current(),
242
+ active: () => active(),
243
+ disabled: () => readOr(props.disabled, false),
244
+ invalid: () => read(props.invalid) === true,
245
+ required: props.required === true,
246
+ chosenLabel: () =>
247
+ items.find((item) => item.value === selection.current())?.textValue,
248
+ itemId(value) {
249
+ let id = optionIds.get(value);
250
+ if (id === undefined) {
251
+ id = uid("select-item");
252
+ optionIds.set(value, id);
253
+ }
254
+ return id;
255
+ },
256
+ toggle() {
257
+ active(selection.current() ?? enabled()[0]?.value);
258
+ open(!open());
259
+ },
260
+ choose,
261
+ setActive: (value) => active(value),
262
+ onTriggerKeyDown,
263
+ registerItem(item) {
264
+ items.push(item);
265
+ },
266
+ registerContent(registered) {
267
+ content = registered;
268
+ },
181
269
  });
182
270
 
183
- function renderList(): ReturnType<typeof html> {
184
- let lastGroup: string | undefined;
185
-
186
- return html`<div
187
- data-slot="select-content"
188
- id="${listId}"
189
- role="listbox"
190
- aria-labelledby="${triggerId}"
191
- class="${cn(
192
- "bg-popover text-popover-foreground z-50 max-h-(--nebula-available-height) min-w-(--nebula-anchor-width) overflow-x-hidden overflow-y-auto rounded-md border p-1 shadow-md",
193
- zoomInOut,
194
- read(props.contentClass),
195
- )}"
196
- >
197
- ${props.options.map((option) => {
198
- const heading =
199
- option.group !== undefined && option.group !== lastGroup
200
- ? html`<div class="text-muted-foreground px-2 py-1.5 text-xs">${option.group}</div>`
201
- : null;
202
- lastGroup = option.group;
203
- return [heading, renderOption(option)];
204
- })}
205
- </div>`;
206
- }
271
+ // Built HERE: every item registers during this call, in document order,
272
+ // which is the order the arrows and type-ahead walk.
273
+ const parts = props.children?.();
207
274
 
208
- function renderOption(option: SelectOption): Child {
209
- const chosen = (): boolean => selection.current() === option.value;
210
- return html`<div
211
- role="option"
212
- id="${optionIds.get(option.value)}"
213
- data-slot="select-item"
214
- data-value="${option.value}"
215
- aria-selected="${() => (chosen() ? "true" : "false")}"
216
- data-active="${() => (active() === option.value ? "" : undefined)}"
217
- data-disabled="${option.disabled === true ? "" : undefined}"
218
- class="relative flex w-full cursor-default select-none items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-none data-[active]:bg-accent data-[active]:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50"
219
- @click="${() => {
220
- if (option.disabled !== true) choose(option.value);
221
- }}"
222
- @pointerenter="${() => {
223
- if (option.disabled !== true) active(option.value);
224
- }}"
225
- >
226
- <span class="flex-1 truncate">${option.label}</span>
227
- <span class="absolute right-2 flex size-3.5 items-center justify-center">
228
- ${() => (chosen() ? CheckIcon({ class: "size-4" }) : null)}
229
- </span>
230
- </div>`;
275
+ if (content !== undefined) {
276
+ const registered = content;
277
+ const placement: Placement =
278
+ registered.align === "center"
279
+ ? registered.side
280
+ : `${registered.side}-${registered.align}`;
281
+ floatingSurface({
282
+ anchor: () => document.getElementById(triggerId),
283
+ open: () => open(),
284
+ onClose: () => open(false),
285
+ placement,
286
+ offset: registered.sideOffset,
287
+ matchWidth: true,
288
+ // Focus never enters the panel — the listbox pattern keeps it on the
289
+ // trigger — so nothing here traps or moves it.
290
+ content: () => renderList(registered, { listId, triggerId }),
291
+ onOpened: scrollActiveIntoView,
292
+ });
231
293
  }
232
294
 
233
295
  /**
@@ -239,48 +301,253 @@ export const Select = component<SelectProps>((props) => {
239
301
  function scrollActiveIntoView(panel: HTMLElement): void {
240
302
  const current = active() ?? selection.current();
241
303
  if (current === undefined) return;
242
- const id = optionIds.get(current);
304
+ const id = items.find((item) => item.value === current)?.id;
243
305
  if (id === undefined) return;
244
306
  panel
245
307
  .querySelector(`#${CSS.escape(id)}`)
246
308
  ?.scrollIntoView({ block: "nearest" });
247
309
  }
248
310
 
249
- return html`<div data-slot="select" class="${() => cn("inline-flex", read(props.class))}">
250
- <button
251
- type="button"
252
- data-slot="select-trigger"
253
- id="${triggerId}"
254
- role="combobox"
255
- aria-haspopup="listbox"
256
- aria-expanded="${() => (open() ? "true" : "false")}"
257
- aria-controls="${() => (open() ? listId : undefined)}"
258
- aria-activedescendant="${() => {
259
- const current = active();
260
- return open() && current !== undefined
261
- ? optionIds.get(current)
262
- : undefined;
263
- }}"
264
- aria-invalid="${() => (read(props.invalid) === true ? "true" : undefined)}"
265
- aria-required="${props.required === true ? "true" : undefined}"
266
- data-placeholder="${() => (selection.current() === undefined ? "" : undefined)}"
267
- ?disabled="${() => readOr(props.disabled, false)}"
268
- class="${() => cn(selectTriggerClasses, read(props.class))}"
269
- @click="${() => {
270
- active(selection.current() ?? enabled()[0]?.value);
271
- open(!open());
272
- }}"
273
- @keydown="${onTriggerKeyDown}"
274
- >
275
- <span class="truncate"
276
- >${() => labelFor(selection.current()) ?? props.placeholder ?? "Select…"}</span
277
- >
278
- ${ChevronDownIcon({ class: "size-4 opacity-50" })}
279
- </button>
280
- <input
311
+ return html`<div
312
+ data-slot="select"
313
+ class="${() => cn("inline-flex", read(props.class))}"
314
+ >${parts}<input
281
315
  type="hidden"
282
316
  name="${props.name}"
283
317
  .value="${() => selection.current() ?? ""}"
284
- />
285
- </div>`;
318
+ /></div>`;
319
+ });
320
+
321
+ export interface SelectTriggerProps {
322
+ children?: Slot;
323
+ size?: "default" | "sm";
324
+ class?: Reactive<string>;
325
+ }
326
+
327
+ export const SelectTrigger = component<SelectTriggerProps>((props) => {
328
+ const select = inject(SelectContext);
329
+ return html`<button
330
+ type="button"
331
+ data-slot="select-trigger"
332
+ id="${select.triggerId}"
333
+ role="combobox"
334
+ aria-haspopup="listbox"
335
+ aria-expanded="${() => (select.open() ? "true" : "false")}"
336
+ aria-controls="${() => (select.open() ? select.listId : undefined)}"
337
+ aria-activedescendant="${() => activeDescendant(select)}"
338
+ aria-invalid="${() => (select.invalid() ? "true" : undefined)}"
339
+ aria-required="${select.required ? "true" : undefined}"
340
+ data-size="${props.size ?? "default"}"
341
+ data-state="${() => (select.open() ? "open" : "closed")}"
342
+ data-placeholder="${() => (select.value() === undefined ? "" : undefined)}"
343
+ ?disabled="${() => select.disabled()}"
344
+ class="${() => cn(selectTriggerClasses, read(props.class))}"
345
+ @click="${() => select.toggle()}"
346
+ @keydown="${(event: KeyboardEvent) => select.onTriggerKeyDown(event)}"
347
+ >${slot(props.children)}${ChevronDownIcon({ class: "size-4 opacity-50" })}</button>`;
286
348
  });
349
+
350
+ function activeDescendant(select: SelectApi): string | undefined {
351
+ if (!select.open()) return undefined;
352
+ const current = select.active();
353
+ return current === undefined ? undefined : select.itemId(current);
354
+ }
355
+
356
+ export interface SelectValueProps {
357
+ /** Shown until something is chosen. */
358
+ placeholder?: string;
359
+ class?: Reactive<string>;
360
+ }
361
+
362
+ /** The chosen option's text, or the placeholder. */
363
+ export const SelectValue = component<SelectValueProps>((props) => {
364
+ const select = inject(SelectContext);
365
+ return html`<span
366
+ data-slot="select-value"
367
+ class="${() => cn("truncate", read(props.class))}"
368
+ >${() => select.chosenLabel() ?? props.placeholder ?? "Select…"}</span>`;
369
+ });
370
+
371
+ export interface SelectContentProps {
372
+ children?: Parts;
373
+ side?: Side;
374
+ align?: Align;
375
+ sideOffset?: number;
376
+ class?: Reactive<string>;
377
+ }
378
+
379
+ /**
380
+ * The listbox.
381
+ *
382
+ * Renders nothing where it is written — the select portals it on open — and
383
+ * builds its items HERE, during setup, which is both what puts them within
384
+ * reach of the context and what fixes their order.
385
+ */
386
+ export const SelectContent = component<SelectContentProps>((props) => {
387
+ const select = inject(SelectContext);
388
+ const body = props.children?.();
389
+ select.registerContent({
390
+ body,
391
+ side: props.side ?? "bottom",
392
+ align: props.align ?? "start",
393
+ sideOffset: props.sideOffset ?? 4,
394
+ class: props.class,
395
+ });
396
+ return html``;
397
+ });
398
+
399
+ export interface SelectGroupProps {
400
+ children?: Slot;
401
+ class?: Reactive<string>;
402
+ }
403
+
404
+ export const SelectGroup = component<SelectGroupProps>(
405
+ (props) => html`<div
406
+ data-slot="select-group"
407
+ role="group"
408
+ class="${() => cn("p-1", read(props.class))}"
409
+ >${slot(props.children)}</div>`,
410
+ );
411
+
412
+ export interface SelectLabelProps {
413
+ children?: Slot;
414
+ class?: Reactive<string>;
415
+ }
416
+
417
+ export const SelectLabel = component<SelectLabelProps>(
418
+ (props) => html`<div
419
+ data-slot="select-label"
420
+ class="${() => cn("text-muted-foreground px-2 py-1.5 text-xs", read(props.class))}"
421
+ >${slot(props.children)}</div>`,
422
+ );
423
+
424
+ export interface SelectItemProps {
425
+ value: string;
426
+ children?: Slot;
427
+ /**
428
+ * What type-ahead matches against.
429
+ *
430
+ * Defaults to the children when they are a plain string. An item built from
431
+ * markup has no text a component can read before it is in the DOM, so one
432
+ * that wants type-ahead says so.
433
+ */
434
+ textValue?: string;
435
+ disabled?: boolean;
436
+ class?: Reactive<string>;
437
+ }
438
+
439
+ export const SelectItem = component<SelectItemProps>((props) => {
440
+ const select = inject(SelectContext);
441
+ const id = select.itemId(props.value);
442
+ const children = props.children;
443
+ select.registerItem({
444
+ value: props.value,
445
+ id,
446
+ textValue:
447
+ props.textValue ??
448
+ (typeof children === "string" ? children : props.value),
449
+ disabled: props.disabled === true,
450
+ });
451
+
452
+ const chosen = (): boolean => select.value() === props.value;
453
+ return html`<div
454
+ data-slot="select-item"
455
+ id="${id}"
456
+ role="option"
457
+ data-value="${props.value}"
458
+ aria-selected="${() => (chosen() ? "true" : "false")}"
459
+ data-active="${() => (select.active() === props.value ? "" : undefined)}"
460
+ data-disabled="${props.disabled === true ? "" : undefined}"
461
+ class="${() => cn(itemClasses, read(props.class))}"
462
+ @click="${() => {
463
+ if (props.disabled !== true) select.choose(props.value);
464
+ }}"
465
+ @pointerenter="${() => {
466
+ if (props.disabled !== true) select.setActive(props.value);
467
+ }}"
468
+ ><span class="flex-1 truncate">${slot(children)}</span><span
469
+ data-slot="select-item-indicator"
470
+ class="absolute right-2 flex size-3.5 items-center justify-center"
471
+ >${() => (chosen() ? CheckIcon({ class: "size-4" }) : null)}</span></div>`;
472
+ });
473
+
474
+ export interface SelectSeparatorProps {
475
+ class?: Reactive<string>;
476
+ }
477
+
478
+ export const SelectSeparator = component<SelectSeparatorProps>(
479
+ (props) => html`<div
480
+ data-slot="select-separator"
481
+ role="separator"
482
+ class="${() => cn("bg-border pointer-events-none -mx-1 my-1 h-px", read(props.class))}"
483
+ ></div>`,
484
+ );
485
+
486
+ export interface SelectScrollButtonProps {
487
+ class?: Reactive<string>;
488
+ }
489
+
490
+ /**
491
+ * Scroll affordances for a list taller than its panel.
492
+ *
493
+ * Upstream shows them because Radix's item-aligned position hides the native
494
+ * scrollbar. The panel here scrolls natively, so these are an affordance
495
+ * rather than the only way down — which is why they scroll while hovered and
496
+ * need no keyboard equivalent.
497
+ */
498
+ export const SelectScrollUpButton = component<SelectScrollButtonProps>(
499
+ (props) => scrollButton("up", props),
500
+ );
501
+
502
+ export const SelectScrollDownButton = component<SelectScrollButtonProps>(
503
+ (props) => scrollButton("down", props),
504
+ );
505
+
506
+ const SCROLL_STEP_PX = 24;
507
+
508
+ function scrollButton(
509
+ direction: "up" | "down",
510
+ props: SelectScrollButtonProps,
511
+ ): TemplateResult {
512
+ let timer: ReturnType<typeof setInterval> | undefined;
513
+ function stop(): void {
514
+ if (timer !== undefined) clearInterval(timer);
515
+ timer = undefined;
516
+ }
517
+ function start(event: Event): void {
518
+ stop();
519
+ const target = event.currentTarget;
520
+ if (!(target instanceof Element)) return;
521
+ const panel = target.closest("[data-slot='select-content']");
522
+ if (panel === null) return;
523
+ timer = setInterval(() => {
524
+ panel.scrollTop += direction === "up" ? -SCROLL_STEP_PX : SCROLL_STEP_PX;
525
+ }, 50);
526
+ }
527
+ onUnmount(stop);
528
+ return html`<div
529
+ data-slot="${`select-scroll-${direction}-button`}"
530
+ aria-hidden="true"
531
+ class="${() => cn(scrollButtonClasses, read(props.class))}"
532
+ @pointerenter="${start}"
533
+ @pointerleave="${stop}"
534
+ >${direction === "up" ? ChevronUpIcon({ class: "size-4" }) : ChevronDownIcon({ class: "size-4" })}</div>`;
535
+ }
536
+
537
+ function renderList(
538
+ registered: RegisteredContent,
539
+ ids: { listId: string; triggerId: string },
540
+ ): TemplateResult {
541
+ return html`<div
542
+ data-slot="select-content"
543
+ id="${ids.listId}"
544
+ role="listbox"
545
+ aria-labelledby="${ids.triggerId}"
546
+ class="${cn(
547
+ selectContentClasses,
548
+ zoomInOut,
549
+ slideInFromSide,
550
+ read(registered.class),
551
+ )}"
552
+ >${registered.body}</div>`;
553
+ }