@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,40 +8,121 @@
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
  *
22
38
  * The panel is sized to the trigger and capped at the space available below
23
39
  * it, so a hundred-option list scrolls rather than running off the screen.
24
40
  */
41
+ import { type TemplateResult } from "@c9up/aurora";
42
+ import type { Parts, Slot } from "../lib/children.js";
25
43
  import { type Reactive } from "../lib/props.js";
26
- export interface SelectOption {
27
- value: string;
28
- label: string;
29
- disabled?: boolean;
30
- /** Groups consecutive options under a heading. */
31
- group?: string;
32
- }
44
+ import type { Align, Side } from "../primitives/floating.js";
45
+ export declare const selectTriggerClasses = "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";
46
+ export declare const selectContentClasses = "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";
33
47
  export interface SelectProps {
34
- options: readonly SelectOption[];
35
48
  name?: string;
36
49
  value?: Reactive<string | undefined>;
37
50
  defaultValue?: string;
38
- placeholder?: string;
39
51
  disabled?: Reactive<boolean>;
40
52
  invalid?: Reactive<boolean>;
41
53
  required?: boolean;
42
- class?: Reactive<string>;
43
- contentClass?: Reactive<string>;
44
54
  onValueChange?: (value: string) => void;
55
+ class?: Reactive<string>;
56
+ children?: Parts;
45
57
  }
46
- export declare const selectTriggerClasses = "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";
47
- export declare const Select: (props?: SelectProps | undefined) => import("@c9up/aurora").TemplateResult;
58
+ export declare const Select: (props?: SelectProps | undefined) => TemplateResult;
59
+ export interface SelectTriggerProps {
60
+ children?: Slot;
61
+ size?: "default" | "sm";
62
+ class?: Reactive<string>;
63
+ }
64
+ export declare const SelectTrigger: (props?: SelectTriggerProps | undefined) => TemplateResult;
65
+ export interface SelectValueProps {
66
+ /** Shown until something is chosen. */
67
+ placeholder?: string;
68
+ class?: Reactive<string>;
69
+ }
70
+ /** The chosen option's text, or the placeholder. */
71
+ export declare const SelectValue: (props?: SelectValueProps | undefined) => TemplateResult;
72
+ export interface SelectContentProps {
73
+ children?: Parts;
74
+ side?: Side;
75
+ align?: Align;
76
+ sideOffset?: number;
77
+ class?: Reactive<string>;
78
+ }
79
+ /**
80
+ * The listbox.
81
+ *
82
+ * Renders nothing where it is written — the select portals it on open — and
83
+ * builds its items HERE, during setup, which is both what puts them within
84
+ * reach of the context and what fixes their order.
85
+ */
86
+ export declare const SelectContent: (props?: SelectContentProps | undefined) => TemplateResult;
87
+ export interface SelectGroupProps {
88
+ children?: Slot;
89
+ class?: Reactive<string>;
90
+ }
91
+ export declare const SelectGroup: (props?: SelectGroupProps | undefined) => TemplateResult;
92
+ export interface SelectLabelProps {
93
+ children?: Slot;
94
+ class?: Reactive<string>;
95
+ }
96
+ export declare const SelectLabel: (props?: SelectLabelProps | undefined) => TemplateResult;
97
+ export interface SelectItemProps {
98
+ value: string;
99
+ children?: Slot;
100
+ /**
101
+ * What type-ahead matches against.
102
+ *
103
+ * Defaults to the children when they are a plain string. An item built from
104
+ * markup has no text a component can read before it is in the DOM, so one
105
+ * that wants type-ahead says so.
106
+ */
107
+ textValue?: string;
108
+ disabled?: boolean;
109
+ class?: Reactive<string>;
110
+ }
111
+ export declare const SelectItem: (props?: SelectItemProps | undefined) => TemplateResult;
112
+ export interface SelectSeparatorProps {
113
+ class?: Reactive<string>;
114
+ }
115
+ export declare const SelectSeparator: (props?: SelectSeparatorProps | undefined) => TemplateResult;
116
+ export interface SelectScrollButtonProps {
117
+ class?: Reactive<string>;
118
+ }
119
+ /**
120
+ * Scroll affordances for a list taller than its panel.
121
+ *
122
+ * Upstream shows them because Radix's item-aligned position hides the native
123
+ * scrollbar. The panel here scrolls natively, so these are an affordance
124
+ * rather than the only way down — which is why they scroll while hovered and
125
+ * need no keyboard equivalent.
126
+ */
127
+ export declare const SelectScrollUpButton: (props?: SelectScrollButtonProps | undefined) => TemplateResult;
128
+ export declare const SelectScrollDownButton: (props?: SelectScrollButtonProps | undefined) => TemplateResult;
@@ -8,36 +8,58 @@
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
  *
22
38
  * The panel is sized to the trigger and capped at the space available below
23
39
  * it, so a hundred-option list scrolls rather than running off the screen.
24
40
  */
25
- import { component, html, signal } from "@c9up/aurora";
41
+ import { component, createContext, html, inject, onUnmount, provide, signal, } from "@c9up/aurora";
42
+ import { slot } from "../lib/children.js";
26
43
  import { cn } from "../lib/cn.js";
27
- import { CheckIcon, ChevronDownIcon } from "../lib/icons.js";
44
+ import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "../lib/icons.js";
28
45
  import { uid } from "../lib/id.js";
29
- import { zoomInOut } from "../lib/motion.js";
46
+ import { slideInFromSide, zoomInOut } from "../lib/motion.js";
30
47
  import { read, readOr } from "../lib/props.js";
31
48
  import { controllable } from "../primitives/controllable.js";
32
49
  import { floatingSurface } from "../primitives/floatingSurface.js";
33
50
  import { focusSilently } from "../primitives/focusable.js";
34
- export const selectTriggerClasses = "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";
51
+ export const selectTriggerClasses = "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";
52
+ export const selectContentClasses = "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";
53
+ const itemClasses = "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";
54
+ const scrollButtonClasses = "flex cursor-default items-center justify-center py-1";
55
+ const SelectContext = createContext("Select");
35
56
  export const Select = component((props) => {
36
57
  const triggerId = uid("select-trigger");
37
58
  const listId = uid("select-list");
59
+ const items = [];
60
+ /** Per instance, not per module: two selects may offer the same value. */
38
61
  const optionIds = new Map();
39
- for (const option of props.options)
40
- optionIds.set(option.value, uid("select-option"));
62
+ let content;
41
63
  const open = signal(false);
42
64
  const selection = controllable({
43
65
  value: props.value,
@@ -49,10 +71,7 @@ export const Select = component((props) => {
49
71
  });
50
72
  /** The option the keyboard is on. Distinct from the selected one. */
51
73
  const active = signal(undefined);
52
- const enabled = () => props.options.filter((option) => option.disabled !== true);
53
- function labelFor(value) {
54
- return props.options.find((option) => option.value === value)?.label;
55
- }
74
+ const enabled = () => items.filter((item) => !item.disabled);
56
75
  function choose(value) {
57
76
  selection.set(value);
58
77
  active(value);
@@ -63,7 +82,7 @@ export const Select = component((props) => {
63
82
  const list = enabled();
64
83
  if (list.length === 0)
65
84
  return;
66
- const from = list.findIndex((option) => option.value === active());
85
+ const from = list.findIndex((item) => item.value === active());
67
86
  const next = from === -1 ? (delta > 0 ? 0 : list.length - 1) : from + delta;
68
87
  const clamped = Math.min(Math.max(next, 0), list.length - 1);
69
88
  const target = list[clamped];
@@ -71,11 +90,11 @@ export const Select = component((props) => {
71
90
  active(target.value);
72
91
  }
73
92
  /**
74
- * Type-ahead over the option *data*, not over elements.
93
+ * Type-ahead over the registered options, not over elements.
75
94
  *
76
95
  * The shared primitive walks DOM nodes, which works for menus where focus
77
96
  * moves. Here focus stays on the trigger and only `aria-activedescendant`
78
- * moves, so the search runs over the options array instead.
97
+ * moves, so the search runs over the registration list instead.
79
98
  */
80
99
  let buffer = "";
81
100
  let bufferTimer;
@@ -87,17 +106,21 @@ export const Select = component((props) => {
87
106
  buffer = "";
88
107
  }, 1000);
89
108
  const list = enabled();
90
- const from = list.findIndex((option) => option.value === active());
109
+ const from = list.findIndex((item) => item.value === active());
91
110
  for (let i = 1; i <= list.length; i += 1) {
92
- const option = list[(Math.max(from, 0) + i) % list.length];
93
- if (option === undefined)
111
+ const item = list[(Math.max(from, 0) + i) % list.length];
112
+ if (item === undefined)
94
113
  continue;
95
- if (option.label.toLowerCase().startsWith(buffer)) {
96
- active(option.value);
114
+ if (item.textValue.toLowerCase().startsWith(buffer)) {
115
+ active(item.value);
97
116
  return;
98
117
  }
99
118
  }
100
119
  }
120
+ onUnmount(() => {
121
+ if (bufferTimer !== undefined)
122
+ clearTimeout(bufferTimer);
123
+ });
101
124
  function onTriggerKeyDown(event) {
102
125
  if (readOr(props.disabled, false))
103
126
  return;
@@ -137,61 +160,58 @@ export const Select = component((props) => {
137
160
  seekByText(event.key);
138
161
  }
139
162
  }
140
- floatingSurface({
141
- anchor: () => document.getElementById(triggerId),
163
+ provide(SelectContext, {
164
+ triggerId,
165
+ listId,
142
166
  open: () => open(),
143
- onClose: () => open(false),
144
- placement: "bottom-start",
145
- offset: 4,
146
- matchWidth: true,
147
- // Focus never enters the panel — the listbox pattern keeps it on the
148
- // trigger — so nothing here traps or moves it.
149
- content: renderList,
150
- onOpened: scrollActiveIntoView,
167
+ value: () => selection.current(),
168
+ active: () => active(),
169
+ disabled: () => readOr(props.disabled, false),
170
+ invalid: () => read(props.invalid) === true,
171
+ required: props.required === true,
172
+ chosenLabel: () => items.find((item) => item.value === selection.current())?.textValue,
173
+ itemId(value) {
174
+ let id = optionIds.get(value);
175
+ if (id === undefined) {
176
+ id = uid("select-item");
177
+ optionIds.set(value, id);
178
+ }
179
+ return id;
180
+ },
181
+ toggle() {
182
+ active(selection.current() ?? enabled()[0]?.value);
183
+ open(!open());
184
+ },
185
+ choose,
186
+ setActive: (value) => active(value),
187
+ onTriggerKeyDown,
188
+ registerItem(item) {
189
+ items.push(item);
190
+ },
191
+ registerContent(registered) {
192
+ content = registered;
193
+ },
151
194
  });
152
- function renderList() {
153
- let lastGroup;
154
- return html `<div
155
- data-slot="select-content"
156
- id="${listId}"
157
- role="listbox"
158
- aria-labelledby="${triggerId}"
159
- class="${cn("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", zoomInOut, read(props.contentClass))}"
160
- >
161
- ${props.options.map((option) => {
162
- const heading = option.group !== undefined && option.group !== lastGroup
163
- ? html `<div class="text-muted-foreground px-2 py-1.5 text-xs">${option.group}</div>`
164
- : null;
165
- lastGroup = option.group;
166
- return [heading, renderOption(option)];
167
- })}
168
- </div>`;
169
- }
170
- function renderOption(option) {
171
- const chosen = () => selection.current() === option.value;
172
- return html `<div
173
- role="option"
174
- id="${optionIds.get(option.value)}"
175
- data-slot="select-item"
176
- data-value="${option.value}"
177
- aria-selected="${() => (chosen() ? "true" : "false")}"
178
- data-active="${() => (active() === option.value ? "" : undefined)}"
179
- data-disabled="${option.disabled === true ? "" : undefined}"
180
- 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"
181
- @click="${() => {
182
- if (option.disabled !== true)
183
- choose(option.value);
184
- }}"
185
- @pointerenter="${() => {
186
- if (option.disabled !== true)
187
- active(option.value);
188
- }}"
189
- >
190
- <span class="flex-1 truncate">${option.label}</span>
191
- <span class="absolute right-2 flex size-3.5 items-center justify-center">
192
- ${() => (chosen() ? CheckIcon({ class: "size-4" }) : null)}
193
- </span>
194
- </div>`;
195
+ // Built HERE: every item registers during this call, in document order,
196
+ // which is the order the arrows and type-ahead walk.
197
+ const parts = props.children?.();
198
+ if (content !== undefined) {
199
+ const registered = content;
200
+ const placement = registered.align === "center"
201
+ ? registered.side
202
+ : `${registered.side}-${registered.align}`;
203
+ floatingSurface({
204
+ anchor: () => document.getElementById(triggerId),
205
+ open: () => open(),
206
+ onClose: () => open(false),
207
+ placement,
208
+ offset: registered.sideOffset,
209
+ matchWidth: true,
210
+ // Focus never enters the panel — the listbox pattern keeps it on the
211
+ // trigger — so nothing here traps or moves it.
212
+ content: () => renderList(registered, { listId, triggerId }),
213
+ onOpened: scrollActiveIntoView,
214
+ });
195
215
  }
196
216
  /**
197
217
  * Bring the highlighted option into view when the panel opens.
@@ -203,48 +223,170 @@ export const Select = component((props) => {
203
223
  const current = active() ?? selection.current();
204
224
  if (current === undefined)
205
225
  return;
206
- const id = optionIds.get(current);
226
+ const id = items.find((item) => item.value === current)?.id;
207
227
  if (id === undefined)
208
228
  return;
209
229
  panel
210
230
  .querySelector(`#${CSS.escape(id)}`)
211
231
  ?.scrollIntoView({ block: "nearest" });
212
232
  }
213
- return html `<div data-slot="select" class="${() => cn("inline-flex", read(props.class))}">
214
- <button
215
- type="button"
216
- data-slot="select-trigger"
217
- id="${triggerId}"
218
- role="combobox"
219
- aria-haspopup="listbox"
220
- aria-expanded="${() => (open() ? "true" : "false")}"
221
- aria-controls="${() => (open() ? listId : undefined)}"
222
- aria-activedescendant="${() => {
223
- const current = active();
224
- return open() && current !== undefined
225
- ? optionIds.get(current)
226
- : undefined;
227
- }}"
228
- aria-invalid="${() => (read(props.invalid) === true ? "true" : undefined)}"
229
- aria-required="${props.required === true ? "true" : undefined}"
230
- data-placeholder="${() => (selection.current() === undefined ? "" : undefined)}"
231
- ?disabled="${() => readOr(props.disabled, false)}"
232
- class="${() => cn(selectTriggerClasses, read(props.class))}"
233
- @click="${() => {
234
- active(selection.current() ?? enabled()[0]?.value);
235
- open(!open());
236
- }}"
237
- @keydown="${onTriggerKeyDown}"
238
- >
239
- <span class="truncate"
240
- >${() => labelFor(selection.current()) ?? props.placeholder ?? "Select…"}</span
241
- >
242
- ${ChevronDownIcon({ class: "size-4 opacity-50" })}
243
- </button>
244
- <input
233
+ return html `<div
234
+ data-slot="select"
235
+ class="${() => cn("inline-flex", read(props.class))}"
236
+ >${parts}<input
245
237
  type="hidden"
246
238
  name="${props.name}"
247
239
  .value="${() => selection.current() ?? ""}"
248
- />
249
- </div>`;
240
+ /></div>`;
241
+ });
242
+ export const SelectTrigger = component((props) => {
243
+ const select = inject(SelectContext);
244
+ return html `<button
245
+ type="button"
246
+ data-slot="select-trigger"
247
+ id="${select.triggerId}"
248
+ role="combobox"
249
+ aria-haspopup="listbox"
250
+ aria-expanded="${() => (select.open() ? "true" : "false")}"
251
+ aria-controls="${() => (select.open() ? select.listId : undefined)}"
252
+ aria-activedescendant="${() => activeDescendant(select)}"
253
+ aria-invalid="${() => (select.invalid() ? "true" : undefined)}"
254
+ aria-required="${select.required ? "true" : undefined}"
255
+ data-size="${props.size ?? "default"}"
256
+ data-state="${() => (select.open() ? "open" : "closed")}"
257
+ data-placeholder="${() => (select.value() === undefined ? "" : undefined)}"
258
+ ?disabled="${() => select.disabled()}"
259
+ class="${() => cn(selectTriggerClasses, read(props.class))}"
260
+ @click="${() => select.toggle()}"
261
+ @keydown="${(event) => select.onTriggerKeyDown(event)}"
262
+ >${slot(props.children)}${ChevronDownIcon({ class: "size-4 opacity-50" })}</button>`;
250
263
  });
264
+ function activeDescendant(select) {
265
+ if (!select.open())
266
+ return undefined;
267
+ const current = select.active();
268
+ return current === undefined ? undefined : select.itemId(current);
269
+ }
270
+ /** The chosen option's text, or the placeholder. */
271
+ export const SelectValue = component((props) => {
272
+ const select = inject(SelectContext);
273
+ return html `<span
274
+ data-slot="select-value"
275
+ class="${() => cn("truncate", read(props.class))}"
276
+ >${() => select.chosenLabel() ?? props.placeholder ?? "Select…"}</span>`;
277
+ });
278
+ /**
279
+ * The listbox.
280
+ *
281
+ * Renders nothing where it is written — the select portals it on open — and
282
+ * builds its items HERE, during setup, which is both what puts them within
283
+ * reach of the context and what fixes their order.
284
+ */
285
+ export const SelectContent = component((props) => {
286
+ const select = inject(SelectContext);
287
+ const body = props.children?.();
288
+ select.registerContent({
289
+ body,
290
+ side: props.side ?? "bottom",
291
+ align: props.align ?? "start",
292
+ sideOffset: props.sideOffset ?? 4,
293
+ class: props.class,
294
+ });
295
+ return html ``;
296
+ });
297
+ export const SelectGroup = component((props) => html `<div
298
+ data-slot="select-group"
299
+ role="group"
300
+ class="${() => cn("p-1", read(props.class))}"
301
+ >${slot(props.children)}</div>`);
302
+ export const SelectLabel = component((props) => html `<div
303
+ data-slot="select-label"
304
+ class="${() => cn("text-muted-foreground px-2 py-1.5 text-xs", read(props.class))}"
305
+ >${slot(props.children)}</div>`);
306
+ export const SelectItem = component((props) => {
307
+ const select = inject(SelectContext);
308
+ const id = select.itemId(props.value);
309
+ const children = props.children;
310
+ select.registerItem({
311
+ value: props.value,
312
+ id,
313
+ textValue: props.textValue ??
314
+ (typeof children === "string" ? children : props.value),
315
+ disabled: props.disabled === true,
316
+ });
317
+ const chosen = () => select.value() === props.value;
318
+ return html `<div
319
+ data-slot="select-item"
320
+ id="${id}"
321
+ role="option"
322
+ data-value="${props.value}"
323
+ aria-selected="${() => (chosen() ? "true" : "false")}"
324
+ data-active="${() => (select.active() === props.value ? "" : undefined)}"
325
+ data-disabled="${props.disabled === true ? "" : undefined}"
326
+ class="${() => cn(itemClasses, read(props.class))}"
327
+ @click="${() => {
328
+ if (props.disabled !== true)
329
+ select.choose(props.value);
330
+ }}"
331
+ @pointerenter="${() => {
332
+ if (props.disabled !== true)
333
+ select.setActive(props.value);
334
+ }}"
335
+ ><span class="flex-1 truncate">${slot(children)}</span><span
336
+ data-slot="select-item-indicator"
337
+ class="absolute right-2 flex size-3.5 items-center justify-center"
338
+ >${() => (chosen() ? CheckIcon({ class: "size-4" }) : null)}</span></div>`;
339
+ });
340
+ export const SelectSeparator = component((props) => html `<div
341
+ data-slot="select-separator"
342
+ role="separator"
343
+ class="${() => cn("bg-border pointer-events-none -mx-1 my-1 h-px", read(props.class))}"
344
+ ></div>`);
345
+ /**
346
+ * Scroll affordances for a list taller than its panel.
347
+ *
348
+ * Upstream shows them because Radix's item-aligned position hides the native
349
+ * scrollbar. The panel here scrolls natively, so these are an affordance
350
+ * rather than the only way down — which is why they scroll while hovered and
351
+ * need no keyboard equivalent.
352
+ */
353
+ export const SelectScrollUpButton = component((props) => scrollButton("up", props));
354
+ export const SelectScrollDownButton = component((props) => scrollButton("down", props));
355
+ const SCROLL_STEP_PX = 24;
356
+ function scrollButton(direction, props) {
357
+ let timer;
358
+ function stop() {
359
+ if (timer !== undefined)
360
+ clearInterval(timer);
361
+ timer = undefined;
362
+ }
363
+ function start(event) {
364
+ stop();
365
+ const target = event.currentTarget;
366
+ if (!(target instanceof Element))
367
+ return;
368
+ const panel = target.closest("[data-slot='select-content']");
369
+ if (panel === null)
370
+ return;
371
+ timer = setInterval(() => {
372
+ panel.scrollTop += direction === "up" ? -SCROLL_STEP_PX : SCROLL_STEP_PX;
373
+ }, 50);
374
+ }
375
+ onUnmount(stop);
376
+ return html `<div
377
+ data-slot="${`select-scroll-${direction}-button`}"
378
+ aria-hidden="true"
379
+ class="${() => cn(scrollButtonClasses, read(props.class))}"
380
+ @pointerenter="${start}"
381
+ @pointerleave="${stop}"
382
+ >${direction === "up" ? ChevronUpIcon({ class: "size-4" }) : ChevronDownIcon({ class: "size-4" })}</div>`;
383
+ }
384
+ function renderList(registered, ids) {
385
+ return html `<div
386
+ data-slot="select-content"
387
+ id="${ids.listId}"
388
+ role="listbox"
389
+ aria-labelledby="${ids.triggerId}"
390
+ class="${cn(selectContentClasses, zoomInOut, slideInFromSide, read(registered.class))}"
391
+ >${registered.body}</div>`;
392
+ }