@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.
- package/dist/atoms/Avatar.d.ts +36 -9
- package/dist/atoms/Avatar.js +66 -27
- package/dist/atoms/index.d.ts +1 -1
- package/dist/atoms/index.js +1 -1
- package/dist/configure.d.ts +6 -0
- package/dist/configure.js +4 -22
- package/dist/lib/children.d.ts +30 -0
- package/dist/lib/id.d.ts +13 -29
- package/dist/lib/id.js +13 -39
- package/dist/lib/motion.d.ts +10 -0
- package/dist/lib/motion.js +10 -0
- package/dist/molecules/Accordion.d.ts +36 -15
- package/dist/molecules/Accordion.js +68 -45
- package/dist/molecules/Breadcrumb.d.ts +29 -11
- package/dist/molecules/Breadcrumb.js +52 -52
- package/dist/molecules/Collapsible.d.ts +18 -17
- package/dist/molecules/Collapsible.js +42 -34
- package/dist/molecules/Field.d.ts +66 -30
- package/dist/molecules/Field.js +106 -55
- package/dist/molecules/InputGroup.d.ts +39 -21
- package/dist/molecules/InputGroup.js +43 -27
- package/dist/molecules/InputOTP.d.ts +26 -3
- package/dist/molecules/InputOTP.js +109 -76
- package/dist/molecules/Message.js +5 -4
- package/dist/molecules/Pagination.d.ts +36 -11
- package/dist/molecules/Pagination.js +58 -74
- package/dist/molecules/RadioGroup.d.ts +25 -17
- package/dist/molecules/RadioGroup.js +47 -43
- package/dist/molecules/Resizable.d.ts +32 -7
- package/dist/molecules/Resizable.js +76 -49
- package/dist/molecules/Tabs.d.ts +49 -11
- package/dist/molecules/Tabs.js +107 -51
- package/dist/molecules/ToggleGroup.d.ts +16 -10
- package/dist/molecules/ToggleGroup.js +37 -21
- package/dist/molecules/index.d.ts +11 -11
- package/dist/molecules/index.js +11 -11
- package/dist/organisms/AlertDialog.d.ts +80 -28
- package/dist/organisms/AlertDialog.js +192 -67
- package/dist/organisms/Carousel.d.ts +25 -3
- package/dist/organisms/Carousel.js +75 -43
- package/dist/organisms/Combobox.d.ts +2 -1
- package/dist/organisms/Combobox.js +46 -16
- package/dist/organisms/Command.d.ts +73 -32
- package/dist/organisms/Command.js +209 -131
- package/dist/organisms/CommandDialog.js +5 -5
- package/dist/organisms/ContextMenu.d.ts +41 -19
- package/dist/organisms/ContextMenu.js +112 -71
- package/dist/organisms/DataTable.js +42 -8
- package/dist/organisms/Dialog.d.ts +97 -31
- package/dist/organisms/Dialog.js +193 -68
- package/dist/organisms/Drawer.d.ts +33 -10
- package/dist/organisms/Drawer.js +130 -52
- package/dist/organisms/DropdownMenu.d.ts +58 -11
- package/dist/organisms/DropdownMenu.js +137 -50
- package/dist/organisms/Form.js +9 -11
- package/dist/organisms/HoverCard.d.ts +39 -19
- package/dist/organisms/HoverCard.js +116 -40
- package/dist/organisms/Menubar.d.ts +55 -19
- package/dist/organisms/Menubar.js +166 -82
- package/dist/organisms/NavigationMenu.d.ts +77 -31
- package/dist/organisms/NavigationMenu.js +169 -98
- package/dist/organisms/Popover.d.ts +72 -12
- package/dist/organisms/Popover.js +132 -25
- package/dist/organisms/Questionnaire.js +5 -5
- package/dist/organisms/Select.d.ts +99 -18
- package/dist/organisms/Select.js +253 -111
- package/dist/organisms/Sheet.d.ts +57 -13
- package/dist/organisms/Sheet.js +141 -54
- package/dist/organisms/Sidebar.d.ts +146 -56
- package/dist/organisms/Sidebar.js +236 -128
- package/dist/organisms/Tooltip.d.ts +88 -14
- package/dist/organisms/Tooltip.js +210 -47
- package/dist/organisms/index.d.ts +15 -15
- package/dist/organisms/index.js +15 -15
- package/dist/primitives/floating.js +25 -0
- package/dist/primitives/floatingSurface.d.ts +11 -0
- package/dist/primitives/floatingSurface.js +8 -0
- package/dist/primitives/menu.d.ts +45 -1
- package/dist/primitives/menu.js +45 -14
- package/dist/primitives/menuParts.d.ts +113 -0
- package/dist/primitives/menuParts.js +207 -0
- package/dist/stubs.d.ts +13 -0
- package/dist/stubs.js +14 -0
- package/nebula.css +1 -1
- package/package.json +4 -3
- package/registry.json +18 -6
- package/src/atoms/Avatar.ts +141 -37
- package/src/atoms/index.ts +13 -1
- package/src/configure.ts +10 -23
- package/src/lib/children.ts +31 -0
- package/src/lib/id.ts +11 -39
- package/src/lib/motion.ts +12 -0
- package/src/molecules/Accordion.ts +120 -60
- package/src/molecules/Breadcrumb.ts +77 -76
- package/src/molecules/Collapsible.ts +77 -41
- package/src/molecules/Field.ts +187 -72
- package/src/molecules/InputGroup.ts +88 -35
- package/src/molecules/InputOTP.ts +150 -86
- package/src/molecules/Message.ts +6 -4
- package/src/molecules/Pagination.ts +96 -101
- package/src/molecules/RadioGroup.ts +70 -59
- package/src/molecules/Resizable.ts +138 -75
- package/src/molecules/Tabs.ts +187 -74
- package/src/molecules/ToggleGroup.ts +77 -38
- package/src/molecules/index.ts +85 -8
- package/src/organisms/AlertDialog.ts +319 -82
- package/src/organisms/Carousel.ts +129 -52
- package/src/organisms/Combobox.ts +64 -19
- package/src/organisms/Command.ts +342 -159
- package/src/organisms/CommandDialog.ts +6 -6
- package/src/organisms/ContextMenu.ts +185 -83
- package/src/organisms/DataTable.ts +56 -10
- package/src/organisms/Dialog.ts +312 -102
- package/src/organisms/Drawer.ts +219 -71
- package/src/organisms/DropdownMenu.ts +219 -61
- package/src/organisms/Form.ts +48 -34
- package/src/organisms/HoverCard.ts +187 -53
- package/src/organisms/Menubar.ts +247 -87
- package/src/organisms/NavigationMenu.ts +306 -128
- package/src/organisms/Popover.ts +215 -39
- package/src/organisms/Questionnaire.ts +9 -6
- package/src/organisms/Select.ts +397 -130
- package/src/organisms/Sheet.ts +252 -83
- package/src/organisms/Sidebar.ts +438 -185
- package/src/organisms/Tooltip.ts +315 -65
- package/src/organisms/index.ts +284 -14
- package/src/primitives/floating.ts +33 -0
- package/src/primitives/floatingSurface.ts +19 -0
- package/src/primitives/menu.ts +79 -18
- package/src/primitives/menuParts.ts +371 -0
- package/src/stubs.ts +16 -0
- package/stubs/config/nebula.stub +21 -0
package/src/organisms/Select.ts
CHANGED
|
@@ -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
|
-
*
|
|
12
|
-
*
|
|
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 —
|
|
17
|
-
*
|
|
18
|
-
*
|
|
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 {
|
|
27
|
-
|
|
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
|
|
38
|
-
value:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
|
81
|
-
|
|
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((
|
|
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
|
|
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
|
|
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((
|
|
185
|
+
const from = list.findIndex((item) => item.value === active());
|
|
123
186
|
for (let i = 1; i <= list.length; i += 1) {
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
if (
|
|
127
|
-
active(
|
|
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
|
-
|
|
171
|
-
|
|
237
|
+
provide<SelectApi>(SelectContext, {
|
|
238
|
+
triggerId,
|
|
239
|
+
listId,
|
|
172
240
|
open: () => open(),
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
184
|
-
|
|
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
|
-
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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 =
|
|
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
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
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
|
+
}
|