@glasshome/ui 1.41.0 → 1.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SPEC.md +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/lib/anchor-placement.d.ts +34 -0
- package/dist/lib/balance-rows.d.ts +10 -4
- package/dist/lib/behavior/anchored.d.ts +26 -0
- package/dist/lib/behavior/button-root.d.ts +7 -0
- package/dist/lib/behavior/collection.d.ts +32 -0
- package/dist/lib/behavior/controllable.d.ts +8 -0
- package/dist/lib/behavior/dismiss.d.ts +13 -0
- package/dist/lib/behavior/focus-scope.d.ts +24 -0
- package/dist/lib/behavior/hide-outside.d.ts +7 -0
- package/dist/lib/behavior/layer-stack.d.ts +34 -0
- package/dist/lib/behavior/menu.d.ts +59 -0
- package/dist/lib/behavior/modal-root.d.ts +68 -0
- package/dist/lib/behavior/polymorphic.d.ts +6 -0
- package/dist/lib/behavior/presence.d.ts +18 -0
- package/dist/lib/behavior/range.d.ts +44 -0
- package/dist/lib/behavior/solid.d.ts +13 -0
- package/dist/lib/color/color.d.ts +41 -0
- package/dist/lib/color/index.d.ts +2 -0
- package/dist/lib/color/intl.d.ts +40 -0
- package/dist/lib/dock-slot-classes.d.ts +2 -0
- package/dist/lib/hold.d.ts +1 -1
- package/dist/lib/locale.d.ts +3 -0
- package/dist/lib/menu-classes.d.ts +2 -0
- package/dist/lib/motion-classes.d.ts +5 -2
- package/dist/lib/overlay-classes.d.ts +4 -0
- package/dist/lib/page-cover.d.ts +8 -0
- package/dist/lib/scroll-track.d.ts +3 -2
- package/dist/lib/section-tokens.d.ts +1 -1
- package/dist/lib/segment-classes.d.ts +2 -0
- package/dist/lib/shadow-layer.d.ts +0 -43
- package/dist/lib/toggle-set.d.ts +44 -0
- package/dist/lib/visually-hidden.d.ts +3 -0
- package/dist/solid/accordion.d.ts +35 -11
- package/dist/solid/alert-dialog.d.ts +5 -8
- package/dist/solid/avatar.d.ts +8 -4
- package/dist/solid/bottom-sheet/bottom-sheet.d.ts +2 -2
- package/dist/solid/bottom-sheet/constants.d.ts +1 -1
- package/dist/solid/button.d.ts +5 -4
- package/dist/solid/charts.d.ts +1 -1
- package/dist/solid/checkbox.d.ts +14 -4
- package/dist/solid/collapsible.d.ts +33 -5
- package/dist/solid/color-disc.d.ts +1 -1
- package/dist/solid/color-slider.d.ts +1 -1
- package/dist/solid/color-wheel.d.ts +1 -1
- package/dist/solid/context-menu.d.ts +58 -24
- package/dist/solid/dialog-parts.d.ts +53 -16
- package/dist/solid/dialog.d.ts +7 -10
- package/dist/solid/dropdown-menu.d.ts +55 -24
- package/dist/solid/empty.d.ts +1 -1
- package/dist/solid/form.d.ts +5 -6
- package/dist/solid/hover-card.d.ts +65 -9
- package/dist/solid/index.d.ts +5 -2
- package/dist/solid/index.js +8671 -14317
- package/dist/solid/input-group.d.ts +6 -6
- package/dist/solid/item.d.ts +1 -1
- package/dist/solid/menu-parts.d.ts +175 -24
- package/dist/solid/option-card.d.ts +1 -4
- package/dist/solid/picker-row.d.ts +2 -0
- package/dist/solid/popover.d.ts +77 -12
- package/dist/solid/progress.d.ts +11 -3
- package/dist/solid/radio-group.d.ts +31 -6
- package/dist/solid/responsive-dialog.d.ts +3 -1
- package/dist/solid/scroll-area.d.ts +1 -1
- package/dist/solid/section-card.d.ts +2 -3
- package/dist/solid/select.d.ts +107 -10
- package/dist/solid/selection.d.ts +10 -1
- package/dist/solid/sheet.d.ts +6 -9
- package/dist/solid/sliding-indicator.d.ts +1 -1
- package/dist/solid/sonner.d.ts +1 -1
- package/dist/solid/stale-veil.d.ts +7 -0
- package/dist/solid/tabs.d.ts +28 -9
- package/dist/solid/toggle-group.d.ts +30 -5
- package/dist/solid/toggle.d.ts +11 -3
- package/dist/solid/tooltip.d.ts +71 -9
- package/dist/tokens/index.js +54 -51
- package/dist/{utils-CGN4X-2O.js → utils-3Codx-NZ.js} +848 -618
- package/package.json +6 -7
- package/src/index.ts +1 -0
- package/src/lib/anchor-placement.ts +77 -4
- package/src/lib/balance-rows.ts +115 -40
- package/src/lib/behavior/anchored.ts +106 -0
- package/src/lib/behavior/button-root.tsx +103 -0
- package/src/lib/behavior/collection.ts +218 -0
- package/src/lib/behavior/controllable.ts +22 -0
- package/src/lib/behavior/dismiss.ts +185 -0
- package/src/lib/behavior/focus-scope.ts +227 -0
- package/src/lib/behavior/hide-outside.ts +85 -0
- package/src/lib/behavior/layer-stack.ts +150 -0
- package/src/lib/behavior/menu.ts +470 -0
- package/src/lib/behavior/modal-root.tsx +452 -0
- package/src/lib/behavior/polymorphic.tsx +30 -0
- package/src/lib/behavior/presence.ts +89 -0
- package/src/lib/behavior/range.ts +212 -0
- package/src/lib/behavior/solid.ts +30 -0
- package/src/lib/clipboard.ts +4 -4
- package/src/lib/color/color.ts +746 -0
- package/src/lib/color/index.ts +2 -0
- package/src/lib/color/intl.ts +60 -0
- package/src/lib/dock-slot-classes.ts +3 -0
- package/src/lib/hold.ts +1 -1
- package/src/lib/locale.ts +62 -0
- package/src/lib/menu-classes.ts +3 -0
- package/src/lib/motion-classes.ts +33 -8
- package/src/lib/overlay-classes.ts +8 -3
- package/src/lib/page-cover.ts +47 -0
- package/src/lib/scroll-track.ts +11 -6
- package/src/lib/section-tokens.ts +1 -1
- package/src/lib/segment-classes.ts +3 -0
- package/src/lib/shadow-layer.ts +0 -136
- package/src/lib/toggle-set.ts +133 -0
- package/src/lib/visually-hidden.ts +15 -0
- package/src/solid/accordion.tsx +294 -20
- package/src/solid/alert-dialog.tsx +36 -85
- package/src/solid/area-picker.tsx +5 -3
- package/src/solid/avatar.tsx +98 -24
- package/src/solid/badge.tsx +5 -5
- package/src/solid/bottom-sheet/bottom-sheet.tsx +73 -129
- package/src/solid/bottom-sheet/constants.ts +3 -1
- package/src/solid/bottom-sheet/drag-controller.ts +11 -0
- package/src/solid/breadcrumb.tsx +4 -4
- package/src/solid/button-group.tsx +4 -4
- package/src/solid/button.tsx +12 -6
- package/src/solid/card.tsx +5 -4
- package/src/solid/charts.tsx +1 -1
- package/src/solid/checkbox.tsx +167 -44
- package/src/solid/collapsible.tsx +260 -12
- package/src/solid/color-disc.tsx +11 -12
- package/src/solid/color-slider.tsx +114 -47
- package/src/solid/color-wheel.tsx +137 -39
- package/src/solid/context-menu.tsx +287 -86
- package/src/solid/dialog-parts.tsx +135 -46
- package/src/solid/dialog.tsx +44 -76
- package/src/solid/dock-edit.tsx +5 -4
- package/src/solid/dock.tsx +6 -5
- package/src/solid/dropdown-menu.tsx +252 -107
- package/src/solid/empty.tsx +1 -1
- package/src/solid/entity-selector.tsx +5 -3
- package/src/solid/face-toggle.tsx +43 -18
- package/src/solid/form.tsx +9 -9
- package/src/solid/glyph-tile.tsx +48 -21
- package/src/solid/hover-card.tsx +288 -21
- package/src/solid/icon-picker.tsx +2 -1
- package/src/solid/image-picker.tsx +6 -5
- package/src/solid/index.ts +5 -4
- package/src/solid/input-group.tsx +7 -5
- package/src/solid/input-otp.tsx +2 -1
- package/src/solid/item.tsx +5 -5
- package/src/solid/logo.tsx +5 -4
- package/src/solid/menu-parts.tsx +619 -145
- package/src/solid/option-card.tsx +6 -10
- package/src/solid/picker-row.tsx +10 -5
- package/src/solid/picker-search.tsx +2 -1
- package/src/solid/popover.tsx +424 -71
- package/src/solid/preview-tile.tsx +1 -1
- package/src/solid/progress.tsx +84 -15
- package/src/solid/radio-group.tsx +271 -34
- package/src/solid/responsive-dialog.tsx +10 -10
- package/src/solid/schema-form.tsx +1 -6
- package/src/solid/scroll-area.tsx +1 -1
- package/src/solid/section-card.tsx +3 -4
- package/src/solid/select.tsx +950 -95
- package/src/solid/selection.tsx +142 -66
- package/src/solid/sheet.tsx +48 -80
- package/src/solid/slider.tsx +115 -65
- package/src/solid/sliding-indicator.tsx +1 -1
- package/src/solid/sonner.tsx +1 -1
- package/src/solid/spotlight.tsx +23 -11
- package/src/solid/stale-veil.tsx +20 -0
- package/src/solid/switch.tsx +8 -6
- package/src/solid/tabs.tsx +377 -52
- package/src/solid/term.tsx +3 -4
- package/src/solid/toggle-group.tsx +237 -95
- package/src/solid/toggle.tsx +43 -9
- package/src/solid/tooltip.tsx +285 -26
- package/src/styles/globals.css +64 -10
- package/src/styles/theme.css +12 -4
- package/src/tokens/material.ts +11 -2
- package/dist/lib/when-animations-settle.d.ts +0 -4
- package/dist/solid/bottom-sheet/focus-trap.d.ts +0 -6
- package/dist/solid/bottom-sheet/scroll-lock.d.ts +0 -2
- package/dist/solid/bottom-sheet/sheet-motion.d.ts +0 -1
- package/src/lib/when-animations-settle.ts +0 -12
- package/src/solid/bottom-sheet/focus-trap.ts +0 -63
- package/src/solid/bottom-sheet/scroll-lock.ts +0 -66
- package/src/solid/bottom-sheet/sheet-motion.ts +0 -4
package/src/solid/select.tsx
CHANGED
|
@@ -1,150 +1,1005 @@
|
|
|
1
|
-
import { Select as SelectPrimitive, useSelectContext } from "@kobalte/core/select";
|
|
2
|
-
import { usePortalMount } from "./portal-mount.js";
|
|
3
1
|
import {
|
|
2
|
+
type Accessor,
|
|
4
3
|
type Component,
|
|
5
4
|
type ComponentProps,
|
|
5
|
+
createComponent,
|
|
6
|
+
createContext,
|
|
7
|
+
createMemo,
|
|
6
8
|
createSignal,
|
|
7
|
-
|
|
9
|
+
createUniqueId,
|
|
10
|
+
For,
|
|
11
|
+
type JSX,
|
|
12
|
+
onCleanup,
|
|
13
|
+
Show,
|
|
8
14
|
splitProps,
|
|
15
|
+
untrack,
|
|
16
|
+
useContext,
|
|
9
17
|
} from "solid-js";
|
|
10
|
-
import {
|
|
18
|
+
import { Portal } from "solid-js/web";
|
|
19
|
+
import { createAnchored } from "../lib/behavior/anchored.js";
|
|
20
|
+
import { ButtonRoot, type ButtonRootProps } from "../lib/behavior/button-root.js";
|
|
21
|
+
import { createCollection, createTypeahead } from "../lib/behavior/collection.js";
|
|
22
|
+
import { createControllableSignal } from "../lib/behavior/controllable.js";
|
|
23
|
+
import { createDismiss } from "../lib/behavior/dismiss.js";
|
|
24
|
+
import { focusWithoutTracking } from "../lib/behavior/focus-scope.js";
|
|
25
|
+
import { registerLayer } from "../lib/behavior/layer-stack.js";
|
|
26
|
+
import {
|
|
27
|
+
type InteractOutsideEvent,
|
|
28
|
+
interactOutside,
|
|
29
|
+
type PointerDownOutsideEvent,
|
|
30
|
+
vetoed,
|
|
31
|
+
} from "../lib/behavior/modal-root.js";
|
|
32
|
+
import { createPresence } from "../lib/behavior/presence.js";
|
|
33
|
+
import { effect, merge } from "../lib/behavior/solid.js";
|
|
11
34
|
import { CONTROL_H, INPUT_SURFACE } from "../lib/input-classes.js";
|
|
12
35
|
import { nestedLayerAttrs, Z_CLASS } from "../lib/layers.js";
|
|
13
|
-
import { MENU_ITEM, MENU_LABEL, MENU_SEPARATOR } from "../lib/menu-classes.js";
|
|
14
|
-
import { FIELD_MOTION, STAGGER } from "../lib/motion-classes.js";
|
|
15
|
-
import { ignoreOpeningRelease } from "../lib/opening-release.js";
|
|
16
|
-
import { anchorToTriggerTop } from "../lib/overlay-classes.js";
|
|
36
|
+
import { MENU_ITEM, MENU_ITEM_RADIUS, MENU_LABEL, MENU_SEPARATOR } from "../lib/menu-classes.js";
|
|
37
|
+
import { FIELD_MOTION, MOTION_EXIT_MS, STAGGER } from "../lib/motion-classes.js";
|
|
17
38
|
import { PICKER_LIST, PICKER_TRIGGER } from "../lib/picker-classes.js";
|
|
18
39
|
import { cn } from "../lib/utils.js";
|
|
19
40
|
import { Icon } from "./icon.js";
|
|
41
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
20
42
|
import { SelectionHighlight, SelectionScope } from "./selection.js";
|
|
21
43
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
44
|
+
/** One row of the list: an option, or the heading of a group of options. */
|
|
45
|
+
interface SelectNode<T> {
|
|
46
|
+
type: "item" | "section";
|
|
47
|
+
key: string;
|
|
48
|
+
rawValue: T;
|
|
49
|
+
textValue: string;
|
|
50
|
+
disabled: boolean;
|
|
51
|
+
level: number;
|
|
52
|
+
index: number;
|
|
53
|
+
prevKey?: string;
|
|
54
|
+
nextKey?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Field<T, V> = keyof Exclude<T, null> | ((option: Exclude<T, null>) => V);
|
|
58
|
+
|
|
59
|
+
interface SelectBaseProps<Option, OptGroup> extends Omit<
|
|
60
|
+
ComponentProps<"div">,
|
|
61
|
+
"onChange" | "placeholder" | "children"
|
|
62
|
+
> {
|
|
63
|
+
options: (Option | OptGroup)[];
|
|
64
|
+
optionValue?: Field<Option, string | number>;
|
|
65
|
+
optionTextValue?: Field<Option, string>;
|
|
66
|
+
optionDisabled?: Field<Option, boolean>;
|
|
67
|
+
optionGroupChildren?: keyof Exclude<OptGroup, null>;
|
|
68
|
+
itemComponent?: Component<{ item: SelectNode<Option> }>;
|
|
69
|
+
sectionComponent?: Component<{ section: SelectNode<OptGroup> }>;
|
|
70
|
+
placeholder?: JSX.Element;
|
|
71
|
+
open?: boolean;
|
|
72
|
+
defaultOpen?: boolean;
|
|
73
|
+
onOpenChange?: (open: boolean) => void;
|
|
74
|
+
closeOnSelection?: boolean;
|
|
75
|
+
disabled?: boolean;
|
|
76
|
+
required?: boolean;
|
|
77
|
+
readOnly?: boolean;
|
|
78
|
+
validationState?: "valid" | "invalid";
|
|
79
|
+
children?: JSX.Element;
|
|
80
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
81
|
+
name?: string;
|
|
82
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
83
|
+
placement?: string;
|
|
84
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
85
|
+
gutter?: number;
|
|
86
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
87
|
+
flip?: boolean | string;
|
|
88
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
89
|
+
overlap?: boolean;
|
|
90
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
91
|
+
sameWidth?: boolean;
|
|
92
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
93
|
+
getAnchorRect?: (anchor?: HTMLElement) => unknown;
|
|
94
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
95
|
+
modal?: boolean;
|
|
96
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
97
|
+
preventScroll?: boolean;
|
|
98
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
99
|
+
disallowEmptySelection?: boolean;
|
|
100
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
101
|
+
allowDuplicateSelectionEvents?: boolean;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface SingleProps<Option> {
|
|
105
|
+
multiple?: false;
|
|
106
|
+
value?: Option | null;
|
|
107
|
+
defaultValue?: Option;
|
|
108
|
+
onChange?: (value: Option | null) => void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface MultipleProps<Option> {
|
|
112
|
+
multiple: true;
|
|
113
|
+
value?: Option[];
|
|
114
|
+
defaultValue?: Option[];
|
|
115
|
+
onChange?: (value: Option[]) => void;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
type SelectProps<Option, OptGroup = never> = SelectBaseProps<Option, OptGroup> &
|
|
119
|
+
(SingleProps<Option> | MultipleProps<Option>);
|
|
120
|
+
|
|
121
|
+
type CloseReason = "commit" | "outside" | "parent";
|
|
122
|
+
|
|
123
|
+
interface SelectContextValue {
|
|
124
|
+
ids: { root: string; trigger: Accessor<string>; value: string; listbox: string };
|
|
125
|
+
setTriggerId: (id: string) => void;
|
|
126
|
+
open: Accessor<boolean>;
|
|
127
|
+
openFrom: Accessor<"first" | "last">;
|
|
128
|
+
show: (from: "first" | "last") => void;
|
|
129
|
+
close: (reason: CloseReason) => void;
|
|
130
|
+
/** The panel unmounted: a close by an outside press refocuses the trigger only if focus fell to the page. */
|
|
131
|
+
settle: () => void;
|
|
132
|
+
setCloseAutoFocus: (handler: ((event: Event) => void) | undefined) => void;
|
|
133
|
+
nodes: Accessor<readonly SelectNode<unknown>[]>;
|
|
134
|
+
optionId: (key: string | undefined) => string | undefined;
|
|
135
|
+
multiple: Accessor<boolean>;
|
|
136
|
+
isSelected: (key: string) => boolean;
|
|
137
|
+
firstSelected: Accessor<string | undefined>;
|
|
138
|
+
empty: Accessor<boolean>;
|
|
139
|
+
choose: (key: string) => void;
|
|
140
|
+
step: (by: 1 | -1) => void;
|
|
141
|
+
typeahead: (event: KeyboardEvent) => boolean;
|
|
142
|
+
selectedOptions: Accessor<unknown[]>;
|
|
143
|
+
remove: (option: unknown) => void;
|
|
144
|
+
clear: () => void;
|
|
145
|
+
placeholder: () => JSX.Element;
|
|
146
|
+
itemComponent: Accessor<Component<{ item: SelectNode<unknown> }> | undefined>;
|
|
147
|
+
sectionComponent: Accessor<Component<{ section: SelectNode<unknown> }> | undefined>;
|
|
148
|
+
trigger: Accessor<HTMLElement | undefined>;
|
|
149
|
+
setTrigger: (el: HTMLElement | undefined) => void;
|
|
150
|
+
labelId: Accessor<string | undefined>;
|
|
151
|
+
setLabelId: (id: string | undefined) => void;
|
|
152
|
+
listboxLabelledBy: Accessor<string | undefined>;
|
|
153
|
+
setListboxLabelledBy: (ids: string | undefined) => void;
|
|
154
|
+
blocked: Accessor<boolean>;
|
|
155
|
+
disabled: Accessor<boolean>;
|
|
156
|
+
attrs: StateAttrs;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
type StateAttrs = {
|
|
160
|
+
[
|
|
161
|
+
K in
|
|
162
|
+
| "data-expanded"
|
|
163
|
+
| "data-closed"
|
|
164
|
+
| "data-valid"
|
|
165
|
+
| "data-invalid"
|
|
166
|
+
| "data-required"
|
|
167
|
+
| "data-disabled"
|
|
168
|
+
| "data-readonly"
|
|
169
|
+
]: "" | undefined;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const SelectContext = createContext<SelectContextValue | null>(null);
|
|
173
|
+
|
|
174
|
+
const useSelect = (): SelectContextValue => {
|
|
175
|
+
const ctx = useContext(SelectContext);
|
|
176
|
+
if (!ctx) throw new Error("Select parts render inside a Select");
|
|
177
|
+
return ctx;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
interface SelectListValue {
|
|
181
|
+
highlighted: Accessor<string | undefined>;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const SelectListContext = createContext<SelectListValue | null>(null);
|
|
185
|
+
|
|
186
|
+
const useSelectList = (): SelectListValue => {
|
|
187
|
+
const ctx = useContext(SelectListContext);
|
|
188
|
+
if (!ctx) throw new Error("SelectItem renders inside SelectContent, through itemComponent");
|
|
189
|
+
return ctx;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const ITEM = '[role="option"][data-key]';
|
|
193
|
+
const TAP_SLOP_PX = 10;
|
|
194
|
+
const SHADOW_ROOT_NODE = 11;
|
|
195
|
+
|
|
196
|
+
const flag = (on: boolean) => (on ? "" : undefined);
|
|
197
|
+
|
|
198
|
+
function focusFellToPage(near: Element): boolean {
|
|
199
|
+
const root = near.getRootNode();
|
|
200
|
+
const shadow = root.nodeType === SHADOW_ROOT_NODE ? (root as ShadowRoot) : undefined;
|
|
201
|
+
const active = shadow ? shadow.activeElement : document.activeElement;
|
|
202
|
+
if (active && active !== document.body) return false;
|
|
203
|
+
const outer = document.activeElement;
|
|
204
|
+
return !outer || outer === document.body || outer === shadow?.host;
|
|
205
|
+
}
|
|
26
206
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
207
|
+
function read<T, V>(option: T, by: Field<T, V> | undefined): unknown {
|
|
208
|
+
if (by === undefined || option === null || option === undefined) return undefined;
|
|
209
|
+
if (typeof by === "function") return by(option as Exclude<T, null>);
|
|
210
|
+
return (option as Record<PropertyKey, unknown>)[by as PropertyKey];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const sameKeys = (a: readonly string[], b: readonly string[]) =>
|
|
214
|
+
a.length === b.length && a.every((key, i) => key === b[i]);
|
|
215
|
+
|
|
216
|
+
type SelectionProps = {
|
|
217
|
+
multiple?: unknown;
|
|
218
|
+
value?: unknown;
|
|
219
|
+
defaultValue?: unknown;
|
|
220
|
+
onChange?: unknown;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
function Select<Option, OptGroup = never>(props: SelectProps<Option, OptGroup>): JSX.Element {
|
|
224
|
+
const [local, , rest] = splitProps(
|
|
225
|
+
props as SelectBaseProps<Option, OptGroup> & SelectionProps,
|
|
226
|
+
[
|
|
227
|
+
"options",
|
|
228
|
+
"optionValue",
|
|
229
|
+
"optionTextValue",
|
|
230
|
+
"optionDisabled",
|
|
231
|
+
"optionGroupChildren",
|
|
232
|
+
"itemComponent",
|
|
233
|
+
"sectionComponent",
|
|
234
|
+
"placeholder",
|
|
235
|
+
"open",
|
|
236
|
+
"defaultOpen",
|
|
237
|
+
"onOpenChange",
|
|
238
|
+
"closeOnSelection",
|
|
239
|
+
"disabled",
|
|
240
|
+
"required",
|
|
241
|
+
"readOnly",
|
|
242
|
+
"validationState",
|
|
243
|
+
"children",
|
|
244
|
+
"id",
|
|
245
|
+
"name",
|
|
246
|
+
"placement",
|
|
247
|
+
"gutter",
|
|
248
|
+
"flip",
|
|
249
|
+
"overlap",
|
|
250
|
+
"sameWidth",
|
|
251
|
+
"getAnchorRect",
|
|
252
|
+
"modal",
|
|
253
|
+
"preventScroll",
|
|
254
|
+
"disallowEmptySelection",
|
|
255
|
+
"allowDuplicateSelectionEvents",
|
|
256
|
+
],
|
|
257
|
+
["multiple", "value", "defaultValue", "onChange"],
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
const keyOf = (option: Option): string =>
|
|
261
|
+
local.optionValue === undefined ? String(option) : String(read(option, local.optionValue));
|
|
262
|
+
const textOf = (option: Option): string =>
|
|
263
|
+
local.optionTextValue === undefined
|
|
264
|
+
? String(option)
|
|
265
|
+
: String(read(option, local.optionTextValue));
|
|
266
|
+
const disabledOf = (option: Option): boolean => read(option, local.optionDisabled) === true;
|
|
267
|
+
|
|
268
|
+
const nodes = createMemo(() => {
|
|
269
|
+
const out: SelectNode<unknown>[] = [];
|
|
270
|
+
let items = 0;
|
|
271
|
+
const groupKey = local.optionGroupChildren;
|
|
272
|
+
const add = (option: Option, level: number) =>
|
|
273
|
+
out.push({
|
|
274
|
+
type: "item",
|
|
275
|
+
key: keyOf(option),
|
|
276
|
+
rawValue: option,
|
|
277
|
+
textValue: textOf(option),
|
|
278
|
+
disabled: disabledOf(option),
|
|
279
|
+
level,
|
|
280
|
+
index: items++,
|
|
281
|
+
});
|
|
282
|
+
for (const entry of local.options) {
|
|
283
|
+
const children =
|
|
284
|
+
groupKey !== undefined && entry !== null && typeof entry === "object"
|
|
285
|
+
? (entry as Record<PropertyKey, unknown>)[groupKey as PropertyKey]
|
|
286
|
+
: undefined;
|
|
287
|
+
if (Array.isArray(children)) {
|
|
288
|
+
out.push({
|
|
289
|
+
type: "section",
|
|
290
|
+
key: `section-${out.length}`,
|
|
291
|
+
rawValue: entry,
|
|
292
|
+
textValue: "",
|
|
293
|
+
disabled: false,
|
|
294
|
+
level: 0,
|
|
295
|
+
index: out.length,
|
|
296
|
+
});
|
|
297
|
+
for (const child of children as Option[]) add(child, 1);
|
|
298
|
+
} else add(entry as Option, 0);
|
|
299
|
+
}
|
|
300
|
+
out.forEach((node, i) => {
|
|
301
|
+
node.prevKey = out[i - 1]?.key;
|
|
302
|
+
node.nextKey = out[i + 1]?.key;
|
|
303
|
+
});
|
|
304
|
+
return out;
|
|
305
|
+
});
|
|
306
|
+
const items = createMemo(() => nodes().filter((node) => node.type === "item"));
|
|
307
|
+
const byKey = createMemo(() => new Map(items().map((node) => [node.key, node])));
|
|
308
|
+
|
|
309
|
+
const toKeys = (value: Option | Option[] | null | undefined): string[] | undefined => {
|
|
310
|
+
if (value === undefined) return undefined;
|
|
311
|
+
if (value === null) return [];
|
|
312
|
+
return props.multiple ? (value as Option[]).map(keyOf) : [keyOf(value as Option)];
|
|
313
|
+
};
|
|
314
|
+
const optionsOf = (keys: readonly string[]) =>
|
|
315
|
+
keys.flatMap((key) => {
|
|
316
|
+
const node = byKey().get(key);
|
|
317
|
+
return node ? [node.rawValue as Option] : [];
|
|
318
|
+
});
|
|
319
|
+
const [keys, setKeys] = createControllableSignal<readonly string[]>({
|
|
320
|
+
value: () => toKeys(props.value),
|
|
321
|
+
defaultValue: () => toKeys(props.defaultValue) ?? [],
|
|
322
|
+
equals: sameKeys,
|
|
323
|
+
onChange: (next) => {
|
|
324
|
+
const chosen = optionsOf(next);
|
|
325
|
+
if (props.multiple) props.onChange?.(chosen);
|
|
326
|
+
else props.onChange?.(chosen[0] ?? null);
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
const selected = createMemo(() => new Set(keys()));
|
|
330
|
+
|
|
331
|
+
const [open, setOpen] = createControllableSignal<boolean>({
|
|
332
|
+
value: () => local.open,
|
|
333
|
+
defaultValue: () => local.defaultOpen ?? false,
|
|
334
|
+
onChange: (next) => local.onOpenChange?.(next),
|
|
335
|
+
});
|
|
336
|
+
const [openFrom, setOpenFrom] = createSignal<"first" | "last">("first");
|
|
337
|
+
const [trigger, setTrigger] = createSignal<HTMLElement>();
|
|
338
|
+
const [triggerId, setTriggerId] = createSignal<string>("");
|
|
339
|
+
const [labelId, setLabelId] = createSignal<string>();
|
|
340
|
+
const [listboxLabelledBy, setListboxLabelledBy] = createSignal<string>();
|
|
341
|
+
let closeAutoFocus: ((event: Event) => void) | undefined;
|
|
342
|
+
let refocusIfLost = false;
|
|
343
|
+
|
|
344
|
+
const blocked = () => !!local.disabled || !!local.readOnly;
|
|
345
|
+
const focusTrigger = () => {
|
|
346
|
+
const event = new Event("closeautofocus", { cancelable: true });
|
|
347
|
+
closeAutoFocus?.(event);
|
|
348
|
+
if (!event.defaultPrevented) focusWithoutTracking(trigger());
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const id = untrack(() => local.id) ?? createUniqueId();
|
|
352
|
+
const typer = createTypeahead();
|
|
353
|
+
|
|
354
|
+
const ctx: SelectContextValue = {
|
|
355
|
+
ids: { root: id, trigger: triggerId, value: `${id}-value`, listbox: `${id}-listbox` },
|
|
356
|
+
setTriggerId,
|
|
357
|
+
open,
|
|
358
|
+
openFrom,
|
|
359
|
+
show: (from) => {
|
|
360
|
+
if (blocked() || untrack(open)) return;
|
|
361
|
+
if (items().length === 0 && local.open === undefined) return;
|
|
362
|
+
refocusIfLost = false;
|
|
363
|
+
setOpenFrom(from);
|
|
364
|
+
setOpen(true);
|
|
365
|
+
},
|
|
366
|
+
close: (reason) => {
|
|
367
|
+
if (!untrack(open)) return;
|
|
368
|
+
refocusIfLost = false;
|
|
369
|
+
setOpen(false);
|
|
370
|
+
if (untrack(open)) return;
|
|
371
|
+
if (reason === "commit") focusTrigger();
|
|
372
|
+
else if (reason === "outside") refocusIfLost = true;
|
|
373
|
+
},
|
|
374
|
+
settle: () => {
|
|
375
|
+
const near = trigger();
|
|
376
|
+
if (!refocusIfLost || !near) return;
|
|
377
|
+
refocusIfLost = false;
|
|
378
|
+
if (focusFellToPage(near)) focusTrigger();
|
|
379
|
+
},
|
|
380
|
+
setCloseAutoFocus: (handler) => {
|
|
381
|
+
closeAutoFocus = handler;
|
|
382
|
+
},
|
|
383
|
+
nodes,
|
|
384
|
+
optionId: (key) => {
|
|
385
|
+
const node = key === undefined ? undefined : byKey().get(key);
|
|
386
|
+
return node ? `${id}-option-${node.index}` : undefined;
|
|
387
|
+
},
|
|
388
|
+
multiple: () => !!props.multiple,
|
|
389
|
+
isSelected: (key) => selected().has(key),
|
|
390
|
+
firstSelected: () => keys()[0],
|
|
391
|
+
empty: () => keys().length === 0,
|
|
392
|
+
choose: (key) => {
|
|
393
|
+
const node = byKey().get(key);
|
|
394
|
+
if (!node || node.disabled || blocked()) return;
|
|
395
|
+
if (props.multiple) {
|
|
396
|
+
const current = untrack(keys);
|
|
397
|
+
setKeys(current.includes(key) ? current.filter((k) => k !== key) : [...current, key]);
|
|
398
|
+
} else if (!untrack(selected).has(key)) setKeys([key]);
|
|
399
|
+
if (local.closeOnSelection ?? !props.multiple) ctx.close("commit");
|
|
400
|
+
},
|
|
401
|
+
step: (by) => {
|
|
402
|
+
if (props.multiple || blocked()) return;
|
|
403
|
+
const enabled = items().filter((node) => !node.disabled);
|
|
404
|
+
const at = enabled.findIndex((node) => node.key === untrack(keys)[0]);
|
|
405
|
+
const next = at < 0 ? enabled[0] : enabled[at + by];
|
|
406
|
+
if (next) setKeys([next.key]);
|
|
407
|
+
},
|
|
408
|
+
typeahead: (event) => {
|
|
409
|
+
const enabled = items().filter((node) => !node.disabled);
|
|
410
|
+
const from = enabled.findIndex((node) => node.key === untrack(keys)[0]);
|
|
411
|
+
const index = typer.match(
|
|
412
|
+
event,
|
|
413
|
+
enabled.map((node) => node.textValue),
|
|
414
|
+
from,
|
|
415
|
+
);
|
|
416
|
+
if (index === undefined) return false;
|
|
417
|
+
const next = enabled[index];
|
|
418
|
+
if (next && !props.multiple && !blocked()) setKeys([next.key]);
|
|
419
|
+
return true;
|
|
420
|
+
},
|
|
421
|
+
selectedOptions: () => optionsOf(keys()),
|
|
422
|
+
remove: (option) => {
|
|
423
|
+
const key = keyOf(option as Option);
|
|
424
|
+
setKeys(untrack(keys).filter((k) => k !== key));
|
|
425
|
+
},
|
|
426
|
+
clear: () => setKeys([]),
|
|
427
|
+
placeholder: () => local.placeholder,
|
|
428
|
+
itemComponent: () =>
|
|
429
|
+
local.itemComponent as Component<{ item: SelectNode<unknown> }> | undefined,
|
|
430
|
+
sectionComponent: () =>
|
|
431
|
+
local.sectionComponent as Component<{ section: SelectNode<unknown> }> | undefined,
|
|
432
|
+
trigger,
|
|
433
|
+
setTrigger,
|
|
434
|
+
labelId,
|
|
435
|
+
setLabelId,
|
|
436
|
+
listboxLabelledBy,
|
|
437
|
+
setListboxLabelledBy,
|
|
438
|
+
blocked,
|
|
439
|
+
disabled: () => !!local.disabled,
|
|
440
|
+
attrs: {
|
|
441
|
+
get "data-expanded"() {
|
|
442
|
+
return flag(open());
|
|
443
|
+
},
|
|
444
|
+
get "data-closed"() {
|
|
445
|
+
return flag(!open());
|
|
446
|
+
},
|
|
447
|
+
get "data-valid"() {
|
|
448
|
+
return flag(local.validationState === "valid");
|
|
449
|
+
},
|
|
450
|
+
get "data-invalid"() {
|
|
451
|
+
return flag(local.validationState === "invalid");
|
|
452
|
+
},
|
|
453
|
+
get "data-required"() {
|
|
454
|
+
return flag(!!local.required);
|
|
455
|
+
},
|
|
456
|
+
get "data-disabled"() {
|
|
457
|
+
return flag(!!local.disabled);
|
|
458
|
+
},
|
|
459
|
+
get "data-readonly"() {
|
|
460
|
+
return flag(!!local.readOnly);
|
|
461
|
+
},
|
|
462
|
+
},
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
return (
|
|
466
|
+
<SelectContext.Provider value={ctx}>
|
|
467
|
+
<div {...merge(rest, ctx.attrs, { role: "group" as const, id })}>{local.children}</div>
|
|
468
|
+
</SelectContext.Provider>
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
interface SelectValueState<Option> {
|
|
473
|
+
selectedOption: () => Option;
|
|
474
|
+
selectedOptions: () => Option[];
|
|
475
|
+
remove: (option: Option) => void;
|
|
476
|
+
clear: () => void;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
interface SelectValueProps<Option> extends Omit<ComponentProps<"span">, "children"> {
|
|
480
|
+
children?: JSX.Element | ((state: SelectValueState<Option>) => JSX.Element);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function SelectValue<Option>(props: SelectValueProps<Option>): JSX.Element {
|
|
484
|
+
const ctx = useSelect();
|
|
485
|
+
const [local, others] = splitProps(props, ["children"]);
|
|
486
|
+
const child = createMemo(() => local.children);
|
|
487
|
+
const state: SelectValueState<Option> = {
|
|
488
|
+
selectedOption: () => ctx.selectedOptions()[0] as Option,
|
|
489
|
+
selectedOptions: () => ctx.selectedOptions() as Option[],
|
|
490
|
+
remove: (option) => ctx.remove(option),
|
|
491
|
+
clear: () => ctx.clear(),
|
|
492
|
+
};
|
|
493
|
+
const shown = () => {
|
|
494
|
+
const value = child();
|
|
495
|
+
return typeof value === "function" ? value(state) : value;
|
|
496
|
+
};
|
|
497
|
+
return (
|
|
498
|
+
<span
|
|
499
|
+
{...merge(others, ctx.attrs, {
|
|
500
|
+
id: ctx.ids.value,
|
|
501
|
+
get "data-placeholder-shown"() {
|
|
502
|
+
return flag(ctx.empty());
|
|
503
|
+
},
|
|
504
|
+
})}
|
|
505
|
+
>
|
|
506
|
+
<Show when={!ctx.empty()} fallback={ctx.placeholder()}>
|
|
507
|
+
{shown()}
|
|
508
|
+
</Show>
|
|
509
|
+
</span>
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
type SelectTriggerProps = ButtonRootProps<"button"> & { size?: "sm" | "default" };
|
|
514
|
+
|
|
515
|
+
const SelectTrigger: Component<SelectTriggerProps> = (props) => {
|
|
516
|
+
const ctx = useSelect();
|
|
517
|
+
const [local, rest] = splitProps(props as SelectTriggerProps & { ref?: unknown }, [
|
|
518
|
+
"class",
|
|
519
|
+
"children",
|
|
520
|
+
"size",
|
|
521
|
+
"ref",
|
|
522
|
+
"id",
|
|
523
|
+
"aria-label",
|
|
524
|
+
"aria-labelledby",
|
|
525
|
+
]);
|
|
31
526
|
const size = () => local.size ?? "default";
|
|
527
|
+
const triggerId = () => local.id ?? `${ctx.ids.root}-trigger`;
|
|
528
|
+
const labelledBy = () =>
|
|
529
|
+
[
|
|
530
|
+
local["aria-labelledby"] ?? ctx.labelId(),
|
|
531
|
+
local["aria-label"] !== undefined ? triggerId() : undefined,
|
|
532
|
+
]
|
|
533
|
+
.filter(Boolean)
|
|
534
|
+
.join(" ") || undefined;
|
|
535
|
+
effect(triggerId, (next) => {
|
|
536
|
+
ctx.setTriggerId(next);
|
|
537
|
+
});
|
|
538
|
+
effect(labelledBy, (next) => {
|
|
539
|
+
ctx.setListboxLabelledBy(next);
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
effect(ctx.trigger, (el) => {
|
|
543
|
+
if (!el) return;
|
|
544
|
+
let touch: { x: number; y: number } | undefined;
|
|
545
|
+
let pressed = false;
|
|
546
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
547
|
+
touch = undefined;
|
|
548
|
+
pressed = true;
|
|
549
|
+
if (ctx.blocked()) return;
|
|
550
|
+
if (event.pointerType === "touch") {
|
|
551
|
+
touch = { x: event.clientX, y: event.clientY };
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
if (event.button !== 0 || event.ctrlKey) return;
|
|
555
|
+
event.preventDefault();
|
|
556
|
+
if (ctx.open()) ctx.close("commit");
|
|
557
|
+
else ctx.show("first");
|
|
558
|
+
};
|
|
559
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
560
|
+
if (touch && Math.hypot(event.clientX - touch.x, event.clientY - touch.y) > TAP_SLOP_PX)
|
|
561
|
+
touch = undefined;
|
|
562
|
+
};
|
|
563
|
+
const onPointerCancel = () => {
|
|
564
|
+
touch = undefined;
|
|
565
|
+
pressed = false;
|
|
566
|
+
};
|
|
567
|
+
// A click with no pointer press before it comes from assistive technology.
|
|
568
|
+
const onClick = () => {
|
|
569
|
+
const tapped = touch !== undefined;
|
|
570
|
+
const unpressed = !pressed;
|
|
571
|
+
touch = undefined;
|
|
572
|
+
pressed = false;
|
|
573
|
+
if (!tapped && (!unpressed || ctx.blocked())) return;
|
|
574
|
+
if (ctx.open()) ctx.close("commit");
|
|
575
|
+
else ctx.show("first");
|
|
576
|
+
};
|
|
577
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
578
|
+
if (event.ctrlKey || event.metaKey || event.altKey || ctx.blocked() || ctx.open()) return;
|
|
579
|
+
if (event.key === " " && ctx.typeahead(event)) return event.preventDefault();
|
|
580
|
+
if (event.key === "Enter" || event.key === " " || event.key === "ArrowDown") {
|
|
581
|
+
event.preventDefault();
|
|
582
|
+
ctx.show("first");
|
|
583
|
+
} else if (event.key === "ArrowUp") {
|
|
584
|
+
event.preventDefault();
|
|
585
|
+
ctx.show("last");
|
|
586
|
+
} else if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
|
|
587
|
+
if (ctx.multiple()) return;
|
|
588
|
+
event.preventDefault();
|
|
589
|
+
ctx.step(event.key === "ArrowLeft" ? -1 : 1);
|
|
590
|
+
} else if (ctx.typeahead(event)) event.preventDefault();
|
|
591
|
+
};
|
|
592
|
+
el.addEventListener("pointerdown", onPointerDown);
|
|
593
|
+
el.addEventListener("pointermove", onPointerMove);
|
|
594
|
+
el.addEventListener("pointercancel", onPointerCancel);
|
|
595
|
+
el.addEventListener("click", onClick);
|
|
596
|
+
el.addEventListener("keydown", onKeyDown);
|
|
597
|
+
return () => {
|
|
598
|
+
el.removeEventListener("pointerdown", onPointerDown);
|
|
599
|
+
el.removeEventListener("pointermove", onPointerMove);
|
|
600
|
+
el.removeEventListener("pointercancel", onPointerCancel);
|
|
601
|
+
el.removeEventListener("click", onClick);
|
|
602
|
+
el.removeEventListener("keydown", onKeyDown);
|
|
603
|
+
};
|
|
604
|
+
});
|
|
605
|
+
|
|
32
606
|
return (
|
|
33
|
-
<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
607
|
+
<ButtonRoot
|
|
608
|
+
{...merge(rest, ctx.attrs, {
|
|
609
|
+
ref: (el: HTMLButtonElement) => {
|
|
610
|
+
ctx.setTrigger(el);
|
|
611
|
+
if (typeof local.ref === "function") local.ref(el);
|
|
612
|
+
},
|
|
613
|
+
get id() {
|
|
614
|
+
return triggerId();
|
|
615
|
+
},
|
|
616
|
+
get disabled() {
|
|
617
|
+
return ctx.disabled();
|
|
618
|
+
},
|
|
619
|
+
"aria-haspopup": "listbox" as const,
|
|
620
|
+
get "aria-expanded"() {
|
|
621
|
+
return ctx.open() ? ("true" as const) : ("false" as const);
|
|
622
|
+
},
|
|
623
|
+
get "aria-controls"() {
|
|
624
|
+
return ctx.open() ? ctx.ids.listbox : undefined;
|
|
625
|
+
},
|
|
626
|
+
get "aria-label"() {
|
|
627
|
+
return local["aria-label"];
|
|
628
|
+
},
|
|
629
|
+
get "aria-labelledby"() {
|
|
630
|
+
return [labelledBy(), ctx.ids.value].filter(Boolean).join(" ");
|
|
631
|
+
},
|
|
632
|
+
"data-slot": "select-trigger",
|
|
633
|
+
get "data-size"() {
|
|
634
|
+
return size();
|
|
635
|
+
},
|
|
636
|
+
get class() {
|
|
637
|
+
return cn(
|
|
638
|
+
PICKER_TRIGGER,
|
|
639
|
+
// The one deviation from the shared recipe: a Select sizes to its
|
|
640
|
+
// value, every other picker fills its field.
|
|
641
|
+
"data-[placeholder]:text-muted-foreground [&_svg:not([class*='text-'])]:text-muted-foreground w-fit whitespace-nowrap [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
642
|
+
size() === "sm" && CONTROL_H.sm,
|
|
643
|
+
local.class,
|
|
644
|
+
);
|
|
645
|
+
},
|
|
646
|
+
})}
|
|
45
647
|
>
|
|
46
648
|
{local.children}
|
|
47
|
-
<
|
|
649
|
+
<span aria-hidden="true" {...ctx.attrs}>
|
|
48
650
|
<Icon
|
|
49
651
|
icon="lucide:chevron-down"
|
|
50
652
|
width={16}
|
|
51
653
|
height={16}
|
|
52
654
|
class="text-muted-foreground size-4 opacity-50"
|
|
53
655
|
/>
|
|
54
|
-
</
|
|
55
|
-
</
|
|
656
|
+
</span>
|
|
657
|
+
</ButtonRoot>
|
|
56
658
|
);
|
|
57
659
|
};
|
|
58
660
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
661
|
+
interface SelectContentProps extends Omit<ComponentProps<"div">, "children"> {
|
|
662
|
+
listboxClass?: string;
|
|
663
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void;
|
|
664
|
+
onPointerDownOutside?: (event: PointerDownOutsideEvent) => void;
|
|
665
|
+
onInteractOutside?: (event: InteractOutsideEvent) => void;
|
|
666
|
+
onCloseAutoFocus?: (event: Event) => void;
|
|
667
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
668
|
+
children?: JSX.Element;
|
|
669
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
670
|
+
forceMount?: boolean;
|
|
671
|
+
/** @deprecated Ignored; removed in 2.0.0. */
|
|
672
|
+
onOpenAutoFocus?: (event: Event) => void;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const SelectContent: Component<SelectContentProps> = (props) => {
|
|
676
|
+
const ctx = useSelect();
|
|
677
|
+
const [local, others] = splitProps(props, [
|
|
63
678
|
"class",
|
|
64
679
|
"children",
|
|
65
680
|
"listboxClass",
|
|
66
681
|
"ref",
|
|
682
|
+
"onEscapeKeyDown",
|
|
683
|
+
"onPointerDownOutside",
|
|
67
684
|
"onInteractOutside",
|
|
68
685
|
"onCloseAutoFocus",
|
|
686
|
+
"onOpenAutoFocus",
|
|
687
|
+
"forceMount",
|
|
69
688
|
]);
|
|
70
|
-
const select = useSelectContext();
|
|
71
689
|
const [panel, setPanel] = createSignal<HTMLElement>();
|
|
72
|
-
const [
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
690
|
+
const [listbox, setListbox] = createSignal<HTMLElement>();
|
|
691
|
+
|
|
692
|
+
effect(
|
|
693
|
+
() => local.onCloseAutoFocus,
|
|
694
|
+
(handler) => {
|
|
695
|
+
ctx.setCloseAutoFocus(handler);
|
|
696
|
+
return () => ctx.setCloseAutoFocus(undefined);
|
|
697
|
+
},
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
const presence = createPresence({
|
|
701
|
+
present: ctx.open,
|
|
702
|
+
element: panel,
|
|
703
|
+
exitMs: MOTION_EXIT_MS.field,
|
|
79
704
|
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
)
|
|
99
|
-
|
|
705
|
+
const layer = registerLayer({
|
|
706
|
+
element: panel,
|
|
707
|
+
open: ctx.open,
|
|
708
|
+
kind: "popover",
|
|
709
|
+
escape: () => true,
|
|
710
|
+
dismiss: () => ctx.close("parent"),
|
|
711
|
+
trigger: ctx.trigger,
|
|
712
|
+
});
|
|
713
|
+
createDismiss({
|
|
714
|
+
layer,
|
|
715
|
+
onDismiss: (reason, event) => {
|
|
716
|
+
if (reason === "escape") {
|
|
717
|
+
if (vetoed(local.onEscapeKeyDown, event as KeyboardEvent)) return;
|
|
718
|
+
ctx.close("commit");
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
const outside = interactOutside(event as PointerEvent, (e) => {
|
|
722
|
+
local.onPointerDownOutside?.(e);
|
|
723
|
+
local.onInteractOutside?.(e);
|
|
724
|
+
});
|
|
725
|
+
if (outside.defaultPrevented) return;
|
|
726
|
+
ctx.close("outside");
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
createAnchored({
|
|
730
|
+
anchor: ctx.trigger,
|
|
731
|
+
floating: panel,
|
|
732
|
+
open: ctx.open,
|
|
733
|
+
side: () => "bottom",
|
|
734
|
+
align: () => "start",
|
|
735
|
+
offset: 0,
|
|
736
|
+
matchAnchorWidth: true,
|
|
737
|
+
coverAnchor: true,
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
const collection = createCollection({
|
|
741
|
+
root: listbox,
|
|
742
|
+
itemSelector: ITEM,
|
|
743
|
+
orientation: () => "vertical",
|
|
744
|
+
focus: "virtual",
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
effect(
|
|
748
|
+
() => [ctx.open(), listbox()] as const,
|
|
749
|
+
([open, list]) => {
|
|
750
|
+
if (!open || !list) return;
|
|
751
|
+
const enabled = [...list.querySelectorAll<HTMLElement>(ITEM)].filter(
|
|
752
|
+
(el) => el.getAttribute("aria-disabled") !== "true",
|
|
753
|
+
);
|
|
754
|
+
const chosen = ctx.firstSelected();
|
|
755
|
+
const start =
|
|
756
|
+
enabled.find((el) => el.dataset.key === chosen) ??
|
|
757
|
+
(ctx.openFrom() === "last" ? enabled.at(-1) : enabled[0]);
|
|
758
|
+
if (start?.dataset.key) collection.highlight(start.dataset.key, "keyboard");
|
|
759
|
+
focusWithoutTracking(list);
|
|
760
|
+
},
|
|
761
|
+
);
|
|
762
|
+
|
|
763
|
+
const keyAt = (target: EventTarget | null): string | undefined => {
|
|
764
|
+
const list = listbox();
|
|
765
|
+
const option = target instanceof Element ? target.closest<HTMLElement>(ITEM) : null;
|
|
766
|
+
return option && list?.contains(option) ? option.dataset.key : undefined;
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
let press: { x: number; y: number; type: string; moved: boolean } | undefined;
|
|
770
|
+
let pointerPressed = false;
|
|
771
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
772
|
+
pointerPressed = true;
|
|
773
|
+
press =
|
|
774
|
+
event.button === 0
|
|
775
|
+
? { x: event.clientX, y: event.clientY, type: event.pointerType, moved: false }
|
|
776
|
+
: undefined;
|
|
777
|
+
};
|
|
778
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
779
|
+
if (press && Math.hypot(event.clientX - press.x, event.clientY - press.y) > TAP_SLOP_PX)
|
|
780
|
+
press.moved = true;
|
|
781
|
+
if (event.pointerType !== "mouse" || !ctx.open()) return;
|
|
782
|
+
const key = keyAt(event.target);
|
|
783
|
+
if (key && key !== collection.highlighted()) collection.highlight(key, "pointer");
|
|
784
|
+
};
|
|
785
|
+
const onPointerUp = (event: PointerEvent) => {
|
|
786
|
+
const started = press;
|
|
787
|
+
press = undefined;
|
|
788
|
+
if (!started || !ctx.open()) return;
|
|
789
|
+
if (started.type !== "mouse" && started.moved) return;
|
|
790
|
+
const key = keyAt(event.target);
|
|
791
|
+
if (key) ctx.choose(key);
|
|
792
|
+
};
|
|
793
|
+
const onPointerCancel = () => {
|
|
794
|
+
press = undefined;
|
|
795
|
+
pointerPressed = false;
|
|
796
|
+
};
|
|
797
|
+
// A click with no pointer press before it comes from assistive technology.
|
|
798
|
+
const onClick = (event: MouseEvent) => {
|
|
799
|
+
const pressed = pointerPressed;
|
|
800
|
+
pointerPressed = false;
|
|
801
|
+
if (pressed || !ctx.open()) return;
|
|
802
|
+
const key = keyAt(event.target);
|
|
803
|
+
if (key) ctx.choose(key);
|
|
804
|
+
};
|
|
805
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
806
|
+
if (!ctx.open()) return;
|
|
807
|
+
collection.onKeyDown(event);
|
|
808
|
+
if (event.defaultPrevented) return;
|
|
809
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
810
|
+
event.preventDefault();
|
|
811
|
+
const key = collection.highlighted();
|
|
812
|
+
if (key) ctx.choose(key);
|
|
813
|
+
} else if (event.key === "Tab") ctx.close("commit");
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
const Row: Component<{ node: SelectNode<unknown> }> = (row) => (
|
|
817
|
+
<Show when={ctx.itemComponent()} keyed>
|
|
818
|
+
{(Item) =>
|
|
819
|
+
createComponent(Item, {
|
|
820
|
+
get item() {
|
|
821
|
+
return row.node;
|
|
822
|
+
},
|
|
823
|
+
})
|
|
824
|
+
}
|
|
825
|
+
</Show>
|
|
826
|
+
);
|
|
827
|
+
const Section: Component<{ node: SelectNode<unknown> }> = (row) => (
|
|
828
|
+
<li role="presentation">
|
|
829
|
+
<Show when={ctx.sectionComponent()} keyed>
|
|
830
|
+
{(Heading) =>
|
|
831
|
+
createComponent(Heading, {
|
|
832
|
+
get section() {
|
|
833
|
+
return row.node;
|
|
834
|
+
},
|
|
835
|
+
})
|
|
836
|
+
}
|
|
837
|
+
</Show>
|
|
838
|
+
</li>
|
|
839
|
+
);
|
|
840
|
+
|
|
841
|
+
const Panel: Component = () => {
|
|
842
|
+
onCleanup(() => {
|
|
843
|
+
setPanel(undefined);
|
|
844
|
+
setListbox(undefined);
|
|
845
|
+
ctx.settle();
|
|
846
|
+
});
|
|
847
|
+
return (
|
|
848
|
+
<div
|
|
849
|
+
{...merge(others, nestedLayerAttrs(false), {
|
|
850
|
+
ref: (el: HTMLDivElement) => {
|
|
851
|
+
setPanel(el);
|
|
852
|
+
if (typeof local.ref === "function") local.ref(el);
|
|
853
|
+
},
|
|
854
|
+
"data-slot": "select-content",
|
|
855
|
+
get "data-expanded"() {
|
|
856
|
+
return flag(ctx.open());
|
|
857
|
+
},
|
|
858
|
+
get "data-closed"() {
|
|
859
|
+
return flag(!ctx.open());
|
|
860
|
+
},
|
|
861
|
+
get class() {
|
|
862
|
+
return cn(
|
|
863
|
+
// Same concave surface as the trigger: opening reads as the input
|
|
864
|
+
// expanding, not a separate floating panel.
|
|
865
|
+
INPUT_SURFACE,
|
|
866
|
+
FIELD_MOTION,
|
|
867
|
+
`relative ${Z_CLASS.overlay} text-popover-foreground overflow-hidden rounded-md p-1`,
|
|
868
|
+
local.class,
|
|
869
|
+
);
|
|
870
|
+
},
|
|
871
|
+
})}
|
|
100
872
|
>
|
|
101
873
|
<SelectionScope
|
|
102
|
-
ref={(el: HTMLElement) => {
|
|
103
|
-
ignoreOpeningRelease(el);
|
|
104
|
-
setList(el);
|
|
105
|
-
}}
|
|
106
874
|
marker="highlighted"
|
|
875
|
+
radius={MENU_ITEM_RADIUS}
|
|
107
876
|
class={cn("w-full", PICKER_LIST)}
|
|
108
877
|
>
|
|
109
|
-
|
|
110
|
-
|
|
878
|
+
<ul
|
|
879
|
+
ref={setListbox}
|
|
880
|
+
role="listbox"
|
|
881
|
+
id={ctx.ids.listbox}
|
|
882
|
+
tabindex="-1"
|
|
883
|
+
aria-labelledby={ctx.listboxLabelledBy()}
|
|
884
|
+
aria-multiselectable={ctx.multiple() ? "true" : undefined}
|
|
885
|
+
aria-activedescendant={ctx.optionId(collection.highlighted())}
|
|
886
|
+
class={cn(STAGGER, local.listboxClass)}
|
|
887
|
+
on:keydown={onKeyDown}
|
|
888
|
+
on:pointerdown={onPointerDown}
|
|
889
|
+
on:pointermove={onPointerMove}
|
|
890
|
+
on:pointerup={onPointerUp}
|
|
891
|
+
on:pointercancel={onPointerCancel}
|
|
892
|
+
on:click={onClick}
|
|
893
|
+
>
|
|
894
|
+
<For each={ctx.nodes()}>
|
|
895
|
+
{(node) => (node.type === "section" ? <Section node={node} /> : <Row node={node} />)}
|
|
896
|
+
</For>
|
|
897
|
+
</ul>
|
|
111
898
|
</SelectionScope>
|
|
112
|
-
</
|
|
113
|
-
|
|
899
|
+
</div>
|
|
900
|
+
);
|
|
901
|
+
};
|
|
902
|
+
|
|
903
|
+
return (
|
|
904
|
+
<SelectListContext.Provider value={{ highlighted: collection.highlighted }}>
|
|
905
|
+
<Show when={presence.mounted()}>
|
|
906
|
+
<Portal mount={usePortalMount()}>
|
|
907
|
+
<Panel />
|
|
908
|
+
</Portal>
|
|
909
|
+
</Show>
|
|
910
|
+
</SelectListContext.Provider>
|
|
114
911
|
);
|
|
115
912
|
};
|
|
116
913
|
|
|
117
|
-
const SelectLabel: Component<ComponentProps<
|
|
118
|
-
const
|
|
914
|
+
const SelectLabel: Component<ComponentProps<"span">> = (props) => {
|
|
915
|
+
const ctx = useSelect();
|
|
916
|
+
const [local, rest] = splitProps(props, ["class", "id"]);
|
|
917
|
+
const labelId = () => local.id ?? `${ctx.ids.root}-label`;
|
|
918
|
+
effect(labelId, (next) => {
|
|
919
|
+
ctx.setLabelId(next);
|
|
920
|
+
return () => ctx.setLabelId(undefined);
|
|
921
|
+
});
|
|
119
922
|
return (
|
|
120
|
-
<
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
923
|
+
<span
|
|
924
|
+
{...merge(rest, ctx.attrs, {
|
|
925
|
+
get id() {
|
|
926
|
+
return labelId();
|
|
927
|
+
},
|
|
928
|
+
"data-slot": "select-label",
|
|
929
|
+
get class() {
|
|
930
|
+
return cn(MENU_LABEL, "font-normal", local.class);
|
|
931
|
+
},
|
|
932
|
+
})}
|
|
933
|
+
on:click={() => focusWithoutTracking(ctx.trigger())}
|
|
124
934
|
/>
|
|
125
935
|
);
|
|
126
936
|
};
|
|
127
937
|
|
|
128
|
-
|
|
129
|
-
|
|
938
|
+
interface SelectItemProps extends Omit<ComponentProps<"li">, "children"> {
|
|
939
|
+
item: SelectNode<unknown>;
|
|
940
|
+
children?: JSX.Element;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
const SelectItem: Component<SelectItemProps> = (props) => {
|
|
944
|
+
const ctx = useSelect();
|
|
945
|
+
const list = useSelectList();
|
|
946
|
+
const [local, rest] = splitProps(props, ["class", "children", "item"]);
|
|
947
|
+
const key = () => local.item.key;
|
|
948
|
+
const optionId = () => ctx.optionId(key());
|
|
949
|
+
const labelId = () => `${optionId()}-label`;
|
|
950
|
+
const isSelected = () => ctx.isSelected(key());
|
|
951
|
+
const marks = {
|
|
952
|
+
get "data-selected"() {
|
|
953
|
+
return flag(isSelected());
|
|
954
|
+
},
|
|
955
|
+
get "data-highlighted"() {
|
|
956
|
+
return flag(list.highlighted() === key());
|
|
957
|
+
},
|
|
958
|
+
get "data-disabled"() {
|
|
959
|
+
return flag(local.item.disabled);
|
|
960
|
+
},
|
|
961
|
+
};
|
|
130
962
|
return (
|
|
131
|
-
<
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
963
|
+
<li
|
|
964
|
+
{...merge(rest, marks, {
|
|
965
|
+
role: "option" as const,
|
|
966
|
+
get id() {
|
|
967
|
+
return optionId();
|
|
968
|
+
},
|
|
969
|
+
get "data-key"() {
|
|
970
|
+
return key();
|
|
971
|
+
},
|
|
972
|
+
get "aria-selected"() {
|
|
973
|
+
return isSelected() ? ("true" as const) : ("false" as const);
|
|
974
|
+
},
|
|
975
|
+
get "aria-disabled"() {
|
|
976
|
+
return local.item.disabled ? ("true" as const) : ("false" as const);
|
|
977
|
+
},
|
|
978
|
+
get "aria-labelledby"() {
|
|
979
|
+
return labelId();
|
|
980
|
+
},
|
|
981
|
+
"data-slot": "select-item",
|
|
982
|
+
get class() {
|
|
983
|
+
return cn(
|
|
984
|
+
MENU_ITEM,
|
|
985
|
+
"focus:text-foreground data-[highlighted]:text-foreground w-full pr-8 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
|
|
986
|
+
local.class,
|
|
987
|
+
);
|
|
988
|
+
},
|
|
989
|
+
})}
|
|
139
990
|
>
|
|
140
|
-
<SelectionHighlight />
|
|
141
|
-
<
|
|
142
|
-
<
|
|
143
|
-
<
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
991
|
+
<SelectionHighlight active={list.highlighted() === key()} />
|
|
992
|
+
<Show when={isSelected()}>
|
|
993
|
+
<span class="absolute right-2 flex size-3.5 items-center justify-center">
|
|
994
|
+
<div aria-hidden="true" {...marks}>
|
|
995
|
+
<Icon icon="lucide:check" width={16} height={16} class="size-4 text-current" />
|
|
996
|
+
</div>
|
|
997
|
+
</span>
|
|
998
|
+
</Show>
|
|
999
|
+
<div id={labelId()} {...marks}>
|
|
1000
|
+
{local.children}
|
|
1001
|
+
</div>
|
|
1002
|
+
</li>
|
|
148
1003
|
);
|
|
149
1004
|
};
|
|
150
1005
|
|