@passui/react 0.1.45 → 0.1.47
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/README.md +57 -46
- package/dist/types/src/components/accordion/accordion.d.ts +1 -1
- package/dist/types/src/components/breadcrumb/breadcrumb.d.ts +1 -1
- package/dist/types/src/components/inline-disclosure/inline-disclosure.d.ts +1 -1
- package/dist/types/src/components/popover/anchored-surface.d.ts +5 -1
- package/dist/types/src/components/popover/index.d.ts +41 -0
- package/dist/types/src/components/popover/popover-content.d.ts +38 -0
- package/dist/types/src/components/popover/popover-root.d.ts +22 -0
- package/dist/types/src/components/popover/popover-trigger.d.ts +23 -0
- package/dist/types/src/components/stepper/rolling-digit.d.ts +1 -1
- package/dist/types/src/components/table/table.d.ts +1 -1
- package/dist/types/src/components/view-controller/view-controller.d.ts +2 -2
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/machines/popover.machine.d.ts +30 -0
- package/package.json +307 -307
- package/src/appearance/index.ts +2 -2
- package/src/appearance/platform.tsx +76 -60
- package/src/appearance/theme.tsx +409 -377
- package/src/components/accordion/accordion.tsx +58 -49
- package/src/components/ambient/ambient.tsx +25 -25
- package/src/components/ambient/index.ts +1 -1
- package/src/components/aspect-ratio/aspect-ratio.tsx +21 -16
- package/src/components/breadcrumb/breadcrumb.tsx +63 -57
- package/src/components/button/button-styles.ts +39 -32
- package/src/components/button/button.tsx +91 -84
- package/src/components/button/icon-button.tsx +27 -25
- package/src/components/button/pending-spinner.tsx +8 -8
- package/src/components/button/use-pending-indicator.ts +11 -11
- package/src/components/chip/chip.tsx +35 -30
- package/src/components/code-block/code-block.tsx +40 -39
- package/src/components/combo-box/combo-box.tsx +5 -5
- package/src/components/command/command.tsx +124 -94
- package/src/components/compact-date-picker/calendar-month.ts +75 -53
- package/src/components/compact-date-picker/compact-date-picker.tsx +308 -289
- package/src/components/compact-time-picker/compact-time-picker.tsx +288 -281
- package/src/components/compact-time-picker/time-value.ts +32 -29
- package/src/components/content-unavailable-view/content-unavailable-view.tsx +64 -48
- package/src/components/content-view/content-view.tsx +168 -159
- package/src/components/content-view/index.ts +1 -1
- package/src/components/context-menu/conceal-menu-source.ts +25 -25
- package/src/components/context-menu/context-menu-host.tsx +439 -402
- package/src/components/context-menu/context-menu-store.ts +125 -123
- package/src/components/context-menu/menu-navigation.ts +25 -18
- package/src/components/context-menu/menu-position.ts +33 -29
- package/src/components/context-menu/menu-reveal.ts +32 -32
- package/src/components/context-menu/use-context-menu.ts +63 -56
- package/src/components/conversation/conversation.tsx +265 -263
- package/src/components/conversation/index.ts +1 -1
- package/src/components/date-picker/date-picker.tsx +95 -94
- package/src/components/date-picker/date-value.ts +20 -10
- package/src/components/dialog/dialog-host.tsx +105 -103
- package/src/components/dialog/dialog-store.ts +37 -36
- package/src/components/dialog/dialog.tsx +192 -179
- package/src/components/dialog/index.ts +1 -1
- package/src/components/disclosure-group/disclosure-group.tsx +93 -86
- package/src/components/divider/divider.tsx +24 -22
- package/src/components/inline-disclosure/inline-disclosure.tsx +40 -36
- package/src/components/keyboard-shortcut/keyboard-shortcut.tsx +23 -19
- package/src/components/labeled-content/labeled-content.tsx +68 -68
- package/src/components/locale-switcher/index.ts +1 -1
- package/src/components/locale-switcher/locale-switcher.tsx +278 -251
- package/src/components/menu/menu.tsx +75 -58
- package/src/components/morph/index.ts +2 -2
- package/src/components/morph/morph.tsx +227 -201
- package/src/components/morph/use-morph-disclosure.ts +19 -19
- package/src/components/one-time-code/one-time-code.tsx +216 -200
- package/src/components/picker/picker-trigger.ts +16 -13
- package/src/components/picker/picker.tsx +371 -345
- package/src/components/popover/anchored-surface.tsx +253 -229
- package/src/components/popover/index.ts +50 -0
- package/src/components/popover/popover-content.tsx +374 -0
- package/src/components/popover/popover-root.tsx +97 -0
- package/src/components/popover/popover-trigger.tsx +71 -0
- package/src/components/profile-image/profile-image.tsx +80 -64
- package/src/components/progress-view/progress-view.tsx +61 -59
- package/src/components/progress-view/use-liquid-progress.ts +38 -38
- package/src/components/radio-group/radio-group.tsx +71 -68
- package/src/components/search-field/expanding-search-field.tsx +191 -177
- package/src/components/search-field/search-field.tsx +59 -50
- package/src/components/search-list/search-list.tsx +246 -236
- package/src/components/section/section.tsx +58 -58
- package/src/components/secure-field/secure-field.tsx +3 -3
- package/src/components/segmented-control/segmented-control.tsx +136 -136
- package/src/components/sheet/modal-environment.ts +71 -64
- package/src/components/sheet/sheet-portal.tsx +58 -53
- package/src/components/sheet/sheet.tsx +146 -141
- package/src/components/sheet/use-sheet-focus.ts +97 -82
- package/src/components/sidebar/sidebar.tsx +78 -78
- package/src/components/skeleton/skeleton.tsx +30 -21
- package/src/components/slider/slider.tsx +113 -104
- package/src/components/slosh-slider/liquid-step.ts +32 -31
- package/src/components/slosh-slider/slosh-slider.tsx +265 -228
- package/src/components/stat-tile/index.ts +1 -1
- package/src/components/stat-tile/stat-tile.tsx +79 -68
- package/src/components/status-indicator/status-indicator.tsx +40 -37
- package/src/components/stepper/rolling-digit.tsx +82 -68
- package/src/components/stepper/stepper.tsx +122 -113
- package/src/components/swatch-picker/swatch-picker.tsx +120 -120
- package/src/components/tab-bar/tab-bar.tsx +149 -148
- package/src/components/tab-view/tab-view.tsx +140 -137
- package/src/components/table/table.tsx +117 -98
- package/src/components/text-editor/text-editor.tsx +34 -29
- package/src/components/text-field/text-field.tsx +182 -180
- package/src/components/toast/toast-store.ts +127 -115
- package/src/components/toast/toaster.tsx +361 -347
- package/src/components/toggle/toggle.tsx +297 -282
- package/src/components/tooltip/tooltip-host.tsx +157 -150
- package/src/components/view-controller/index.ts +1 -1
- package/src/components/view-controller/view-controller.tsx +64 -53
- package/src/components/wake-slider/wake-slider.tsx +220 -205
- package/src/hooks/scroll/minimize-decision.ts +11 -7
- package/src/hooks/use-anchored-disclosure.ts +19 -19
- package/src/hooks/use-anchored-placement.ts +29 -25
- package/src/hooks/use-dismiss.ts +33 -29
- package/src/hooks/use-floating-dismiss.ts +24 -22
- package/src/hooks/use-media-query.ts +67 -60
- package/src/hooks/use-minimize-on-scroll.ts +69 -60
- package/src/hooks/use-visual-viewport.ts +63 -63
- package/src/hooks/viewport/types.ts +16 -16
- package/src/hooks/viewport/viewport-metrics.ts +16 -10
- package/src/icons/icons.tsx +440 -328
- package/src/index.ts +91 -91
- package/src/lib/anchored-placement.ts +70 -65
- package/src/lib/class-names.ts +14 -13
- package/src/lib/clipboard.ts +8 -7
- package/src/lib/date.ts +61 -60
- package/src/lib/floating-layers.ts +39 -24
- package/src/lib/haptics.ts +10 -10
- package/src/lib/language.ts +9 -9
- package/src/lib/range-value.ts +27 -21
- package/src/lib/screen-edges.ts +33 -30
- package/src/lib/shapes.ts +38 -34
- package/src/machines/popover.machine.ts +51 -0
- package/src/motion/transitions.ts +46 -43
- package/src/styles/code-block.css +69 -43
- package/src/styles/motion-controls.css +393 -191
- package/src/tokens.ts +1 -1
- package/dist/types/src/components/popover/popover-fit.d.ts +0 -21
- package/dist/types/src/components/popover/popover.d.ts +0 -21
- package/src/components/popover/popover-fit.ts +0 -69
- package/src/components/popover/popover.tsx +0 -334
|
@@ -1,54 +1,62 @@
|
|
|
1
|
-
import { useAnchoredDisclosure } from "../../hooks/use-anchored-disclosure"
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { useAnchoredDisclosure } from "../../hooks/use-anchored-disclosure";
|
|
2
|
+
import {
|
|
3
|
+
type KeyboardEvent,
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useId,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from "react";
|
|
11
|
+
import { createPortal } from "react-dom";
|
|
12
|
+
import { CheckIcon, SearchIcon, SelectorIcon } from "../../icons/icons";
|
|
13
|
+
import { cn } from "../../lib/class-names";
|
|
14
|
+
import { haptic } from "../../lib/haptics";
|
|
15
|
+
import { AnchoredSurface } from "../popover/anchored-surface";
|
|
16
|
+
import { pickerTriggerStyle, type PickerSize } from "./picker-trigger";
|
|
9
17
|
|
|
10
18
|
export interface PickerOption<T extends string> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
value: T;
|
|
20
|
+
label: string;
|
|
21
|
+
/** A quieter second line or trailing note, such as "Free" or a region. */
|
|
22
|
+
disabled?: boolean;
|
|
23
|
+
keywords?: string[];
|
|
24
|
+
detail?: string;
|
|
25
|
+
/** The option's own language, so it is spoken and shaped right. */
|
|
26
|
+
lang?: string;
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
/** Values and callbacks share the option value type, including literal unions. */
|
|
22
30
|
export interface PickerProps<T extends string> {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
31
|
+
/** The selected option's value. */
|
|
32
|
+
value: T;
|
|
33
|
+
/** Available choices. Use a literal union for a fixed set of values. */
|
|
34
|
+
options: ReadonlyArray<PickerOption<T>>;
|
|
35
|
+
/** Called with the selected option value. */
|
|
36
|
+
onChange: (value: NoInfer<T>) => void;
|
|
37
|
+
/** Accessible name for the trigger. */
|
|
38
|
+
label: string;
|
|
39
|
+
/** Shown when the selected value has no matching option. */
|
|
40
|
+
placeholder?: string;
|
|
41
|
+
/** Defaults to on for lists of ten or more options. */
|
|
42
|
+
searchable?: boolean;
|
|
43
|
+
/** Label for the filter field. @default "Search" */
|
|
44
|
+
searchPlaceholder?: string;
|
|
45
|
+
/** Shown when the filter matches nothing. @default "No matches" */
|
|
46
|
+
emptyLabel?: string;
|
|
47
|
+
/** Offers a new value when no option has the same label or value. */
|
|
48
|
+
onCreate?: (query: string) => void;
|
|
49
|
+
/** Localized creation action; receives the trimmed query. */
|
|
50
|
+
createLabel?: (query: string) => string;
|
|
51
|
+
className?: string;
|
|
52
|
+
/** Prevents opening or choosing an option. @default false */
|
|
53
|
+
disabled?: boolean;
|
|
54
|
+
/** Trigger height and text scale. @default "md" */
|
|
55
|
+
size?: PickerSize;
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
/** Long lists get a filter field, as in the system pickers. */
|
|
51
|
-
const SEARCH_FROM = 10
|
|
59
|
+
const SEARCH_FROM = 10;
|
|
52
60
|
|
|
53
61
|
/**
|
|
54
62
|
* A pop-up button: shows the chosen value and opens a list that floats
|
|
@@ -57,322 +65,340 @@ const SEARCH_FROM = 10
|
|
|
57
65
|
* platform.
|
|
58
66
|
*/
|
|
59
67
|
export function Picker<T extends string>({
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
value,
|
|
69
|
+
options,
|
|
70
|
+
onChange,
|
|
71
|
+
label,
|
|
72
|
+
placeholder,
|
|
73
|
+
searchable,
|
|
74
|
+
searchPlaceholder = "Search",
|
|
75
|
+
emptyLabel = "No matches",
|
|
76
|
+
className,
|
|
77
|
+
onCreate,
|
|
78
|
+
createLabel,
|
|
79
|
+
disabled = false,
|
|
80
|
+
size = "md",
|
|
73
81
|
}: PickerProps<T>) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
82
|
+
const panel = useAnchoredDisclosure(disabled);
|
|
83
|
+
const { open } = panel;
|
|
84
|
+
const trigger = useRef<HTMLButtonElement>(null);
|
|
85
|
+
const listId = useId();
|
|
86
|
+
const selected = options.find((option) => option.value === value);
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
const close = useCallback(
|
|
89
|
+
(refocus: boolean) => {
|
|
90
|
+
panel.close();
|
|
91
|
+
if (refocus) trigger.current?.focus({ preventScroll: true });
|
|
92
|
+
},
|
|
93
|
+
[panel.close],
|
|
94
|
+
);
|
|
87
95
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
const onTriggerKey = (event: KeyboardEvent<HTMLButtonElement>) => {
|
|
97
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
panel.show();
|
|
100
|
+
}
|
|
101
|
+
};
|
|
94
102
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
103
|
+
return (
|
|
104
|
+
<>
|
|
105
|
+
<button
|
|
106
|
+
ref={trigger}
|
|
107
|
+
type="button"
|
|
108
|
+
role="combobox"
|
|
109
|
+
aria-label={label}
|
|
110
|
+
aria-haspopup="listbox"
|
|
111
|
+
aria-expanded={open && !disabled}
|
|
112
|
+
aria-controls={open && !disabled ? listId : undefined}
|
|
113
|
+
disabled={disabled}
|
|
114
|
+
onClick={() => panel.toggle()}
|
|
115
|
+
onKeyDown={onTriggerKey}
|
|
116
|
+
className={pickerTriggerStyle(className, size)}
|
|
117
|
+
>
|
|
118
|
+
<span
|
|
119
|
+
lang={selected?.lang}
|
|
120
|
+
className={cn(
|
|
121
|
+
"min-w-0 grow truncate",
|
|
122
|
+
!selected && "text-label-tertiary",
|
|
123
|
+
)}
|
|
124
|
+
>
|
|
125
|
+
{selected?.label ?? placeholder ?? ""}
|
|
126
|
+
</span>
|
|
127
|
+
<SelectorIcon
|
|
128
|
+
size={15}
|
|
129
|
+
strokeWidth={2}
|
|
130
|
+
className="text-label-tertiary shrink-0"
|
|
131
|
+
/>
|
|
132
|
+
</button>
|
|
133
|
+
{typeof document !== "undefined" &&
|
|
134
|
+
createPortal(
|
|
135
|
+
panel.mounted && trigger.current ? (
|
|
136
|
+
<SelectList
|
|
137
|
+
open={open}
|
|
138
|
+
onExited={panel.onExited}
|
|
139
|
+
id={listId}
|
|
140
|
+
anchor={trigger.current}
|
|
141
|
+
options={options}
|
|
142
|
+
value={value}
|
|
143
|
+
label={label}
|
|
144
|
+
searchable={
|
|
145
|
+
searchable ??
|
|
146
|
+
(Boolean(onCreate) || options.length >= SEARCH_FROM)
|
|
147
|
+
}
|
|
148
|
+
searchPlaceholder={searchPlaceholder}
|
|
149
|
+
emptyLabel={emptyLabel}
|
|
150
|
+
onCreate={onCreate}
|
|
151
|
+
createLabel={createLabel}
|
|
152
|
+
onPick={(next) => {
|
|
153
|
+
close(true);
|
|
154
|
+
if (next === value) return;
|
|
155
|
+
haptic("selection");
|
|
156
|
+
onChange(next);
|
|
157
|
+
}}
|
|
158
|
+
onClose={close}
|
|
159
|
+
/>
|
|
160
|
+
) : null,
|
|
161
|
+
document.body,
|
|
162
|
+
)}
|
|
163
|
+
</>
|
|
164
|
+
);
|
|
149
165
|
}
|
|
150
166
|
|
|
151
167
|
function SelectList<T extends string>({
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
168
|
+
open,
|
|
169
|
+
onExited,
|
|
170
|
+
id,
|
|
171
|
+
anchor,
|
|
172
|
+
options,
|
|
173
|
+
value,
|
|
174
|
+
label,
|
|
175
|
+
searchable,
|
|
176
|
+
searchPlaceholder,
|
|
177
|
+
emptyLabel,
|
|
178
|
+
onPick,
|
|
179
|
+
onClose,
|
|
180
|
+
onCreate,
|
|
181
|
+
createLabel,
|
|
166
182
|
}: {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
183
|
+
open: boolean;
|
|
184
|
+
onExited: () => void;
|
|
185
|
+
id: string;
|
|
186
|
+
anchor: HTMLElement;
|
|
187
|
+
options: ReadonlyArray<PickerOption<T>>;
|
|
188
|
+
value: T;
|
|
189
|
+
label: string;
|
|
190
|
+
searchable: boolean;
|
|
191
|
+
searchPlaceholder: string;
|
|
192
|
+
emptyLabel: string;
|
|
193
|
+
onCreate?: (query: string) => void;
|
|
194
|
+
createLabel?: (query: string) => string;
|
|
195
|
+
onPick: (value: T) => void;
|
|
196
|
+
onClose: (refocus: boolean) => void;
|
|
181
197
|
}) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
198
|
+
const list = useRef<HTMLDivElement>(null);
|
|
199
|
+
const search = useRef<HTMLInputElement>(null);
|
|
200
|
+
const [query, setQuery] = useState("");
|
|
201
|
+
const typed = useRef({ text: "", at: 0 });
|
|
186
202
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
203
|
+
const shown = useMemo(() => {
|
|
204
|
+
const needle = query.trim().toLocaleLowerCase();
|
|
205
|
+
if (!needle) return options;
|
|
206
|
+
return options.filter((option) =>
|
|
207
|
+
`${option.label} ${option.detail ?? ""} ${option.value} ${(option.keywords ?? []).join(" ")}`
|
|
208
|
+
.toLocaleLowerCase()
|
|
209
|
+
.includes(needle),
|
|
210
|
+
);
|
|
211
|
+
}, [options, query]);
|
|
196
212
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
213
|
+
const enabled = shown.filter((option) => !option.disabled);
|
|
214
|
+
const trimmed = query.trim();
|
|
215
|
+
const canCreate = Boolean(
|
|
216
|
+
onCreate &&
|
|
217
|
+
trimmed &&
|
|
218
|
+
!options.some(
|
|
219
|
+
(option) =>
|
|
220
|
+
option.label.toLocaleLowerCase() === trimmed.toLocaleLowerCase() ||
|
|
221
|
+
option.value.toLocaleLowerCase() === trimmed.toLocaleLowerCase(),
|
|
222
|
+
),
|
|
223
|
+
);
|
|
224
|
+
const create = () => {
|
|
225
|
+
if (!canCreate) return;
|
|
226
|
+
onClose(true);
|
|
227
|
+
onCreate?.(trimmed);
|
|
228
|
+
};
|
|
229
|
+
const [active, setActive] = useState(() =>
|
|
230
|
+
Math.max(
|
|
231
|
+
0,
|
|
232
|
+
options
|
|
233
|
+
.filter((option) => !option.disabled)
|
|
234
|
+
.findIndex((option) => option.value === value),
|
|
235
|
+
),
|
|
236
|
+
);
|
|
237
|
+
const last = enabled.length + (canCreate ? 1 : 0) - 1;
|
|
238
|
+
const activeOption = enabled[Math.min(active, last)];
|
|
239
|
+
const activeId = activeOption
|
|
240
|
+
? `${id}-${activeOption.value}`
|
|
241
|
+
: canCreate
|
|
242
|
+
? `${id}-create`
|
|
243
|
+
: undefined;
|
|
228
244
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
if (searchable) search.current?.focus();
|
|
247
|
+
else list.current?.focus();
|
|
248
|
+
}, [searchable]);
|
|
233
249
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
250
|
+
// The chosen option starts in view, then follows the keyboard.
|
|
251
|
+
useEffect(() => {
|
|
252
|
+
if (!activeOption) return;
|
|
253
|
+
list.current
|
|
254
|
+
?.querySelector(`[data-value="${CSS.escape(activeOption.value)}"]`)
|
|
255
|
+
?.scrollIntoView({ block: "nearest" });
|
|
256
|
+
}, [activeOption]);
|
|
241
257
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
258
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
259
|
+
const step = (to: number) => {
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
setActive(Math.max(0, Math.min(last, to)));
|
|
262
|
+
};
|
|
263
|
+
const current = Math.min(active, last);
|
|
264
|
+
if (event.key === "ArrowDown") step(current + 1);
|
|
265
|
+
else if (event.key === "ArrowUp") step(current - 1);
|
|
266
|
+
else if (event.key === "Home" && !searchable) step(0);
|
|
267
|
+
else if (event.key === "End" && !searchable) step(last);
|
|
268
|
+
else if (event.key === "PageDown") step(current + 8);
|
|
269
|
+
else if (event.key === "PageUp") step(current - 8);
|
|
270
|
+
else if (event.key === "Enter" || (event.key === " " && !searchable)) {
|
|
271
|
+
event.preventDefault();
|
|
272
|
+
if (activeOption) onPick(activeOption.value);
|
|
273
|
+
else if (canCreate) create();
|
|
274
|
+
} else if (event.key === "Escape") {
|
|
275
|
+
event.preventDefault();
|
|
276
|
+
event.stopPropagation();
|
|
277
|
+
onClose(true);
|
|
278
|
+
} else if (event.key === "Tab") {
|
|
279
|
+
onClose(false);
|
|
280
|
+
} else if (
|
|
281
|
+
!searchable &&
|
|
282
|
+
event.key.length === 1 &&
|
|
283
|
+
!event.metaKey &&
|
|
284
|
+
!event.ctrlKey
|
|
285
|
+
) {
|
|
286
|
+
// Type to jump, as in a native pop-up button.
|
|
287
|
+
const now = Date.now();
|
|
288
|
+
const text =
|
|
289
|
+
(now - typed.current.at < 700 ? typed.current.text : "") + event.key;
|
|
290
|
+
typed.current = { text, at: now };
|
|
291
|
+
const match = enabled.findIndex((option) =>
|
|
292
|
+
option.label.toLocaleLowerCase().startsWith(text.toLocaleLowerCase()),
|
|
293
|
+
);
|
|
294
|
+
if (match >= 0) setActive(match);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
275
297
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
298
|
+
return (
|
|
299
|
+
<AnchoredSurface
|
|
300
|
+
open={open}
|
|
301
|
+
onExited={onExited}
|
|
302
|
+
anchor={anchor}
|
|
303
|
+
width={Math.min(Math.max(anchor.getBoundingClientRect().width, 220), 360)}
|
|
304
|
+
needed={240}
|
|
305
|
+
onClose={onClose}
|
|
306
|
+
onKeyDown={onKeyDown}
|
|
307
|
+
contentClassName="overflow-hidden"
|
|
308
|
+
>
|
|
309
|
+
{searchable && (
|
|
310
|
+
<div className="border-separator/70 flex shrink-0 items-center gap-2 border-b px-3 py-2">
|
|
311
|
+
<SearchIcon size={15} className="text-label-tertiary shrink-0" />
|
|
312
|
+
<input
|
|
313
|
+
ref={search}
|
|
314
|
+
value={query}
|
|
315
|
+
placeholder={searchPlaceholder}
|
|
316
|
+
aria-label={searchPlaceholder}
|
|
317
|
+
aria-controls={id}
|
|
318
|
+
aria-activedescendant={activeId}
|
|
319
|
+
onChange={(event) => {
|
|
320
|
+
setQuery(event.target.value);
|
|
321
|
+
setActive(0);
|
|
322
|
+
}}
|
|
323
|
+
className="text-subheadline text-label placeholder:text-label-tertiary min-w-0 grow bg-transparent outline-none"
|
|
324
|
+
/>
|
|
325
|
+
</div>
|
|
326
|
+
)}
|
|
327
|
+
<div
|
|
328
|
+
ref={list}
|
|
329
|
+
id={id}
|
|
330
|
+
role="listbox"
|
|
331
|
+
aria-label={label}
|
|
332
|
+
tabIndex={-1}
|
|
333
|
+
aria-activedescendant={activeId}
|
|
334
|
+
className="no-scrollbar flex min-h-0 flex-col overflow-y-auto overscroll-contain p-1.5 outline-none"
|
|
335
|
+
>
|
|
336
|
+
{shown.map((option) => {
|
|
337
|
+
const chosen = option.value === value;
|
|
338
|
+
const focused = option === activeOption;
|
|
339
|
+
return (
|
|
340
|
+
// biome-ignore lint/a11y/useKeyWithClickEvents: option div handles keyboard via role=option and aria-selected
|
|
341
|
+
<div
|
|
342
|
+
key={option.value}
|
|
343
|
+
id={`${id}-${option.value}`}
|
|
344
|
+
data-value={option.value}
|
|
345
|
+
role="option"
|
|
346
|
+
tabIndex={-1}
|
|
347
|
+
aria-selected={chosen}
|
|
348
|
+
aria-disabled={option.disabled || undefined}
|
|
349
|
+
lang={option.lang}
|
|
350
|
+
onPointerMove={() =>
|
|
351
|
+
!option.disabled && setActive(enabled.indexOf(option))
|
|
352
|
+
}
|
|
353
|
+
onClick={() => !option.disabled && onPick(option.value)}
|
|
354
|
+
className={cn(
|
|
355
|
+
"text-subheadline flex min-h-8 cursor-default items-center gap-2 rounded-lg px-2 py-1.5 [@media(pointer:coarse)]:min-h-11",
|
|
356
|
+
focused ? "bg-fill" : "",
|
|
357
|
+
option.disabled && "opacity-40",
|
|
358
|
+
chosen ? "text-label font-semibold" : "text-label",
|
|
359
|
+
)}
|
|
360
|
+
>
|
|
361
|
+
<span className="flex w-4 shrink-0 justify-center">
|
|
362
|
+
{chosen && (
|
|
363
|
+
<CheckIcon
|
|
364
|
+
size={15}
|
|
365
|
+
strokeWidth={2.4}
|
|
366
|
+
className="text-accent-text"
|
|
367
|
+
/>
|
|
368
|
+
)}
|
|
369
|
+
</span>
|
|
370
|
+
<span className="min-w-0 grow break-words">{option.label}</span>
|
|
371
|
+
{option.detail && (
|
|
372
|
+
<span className="text-caption text-label-tertiary shrink-0 font-normal">
|
|
373
|
+
{option.detail}
|
|
374
|
+
</span>
|
|
375
|
+
)}
|
|
376
|
+
</div>
|
|
377
|
+
);
|
|
378
|
+
})}
|
|
379
|
+
{canCreate && (
|
|
380
|
+
// biome-ignore lint/a11y/useKeyWithClickEvents: create div handles keyboard via role=option
|
|
381
|
+
<div
|
|
382
|
+
id={`${id}-create`}
|
|
383
|
+
role="option"
|
|
384
|
+
aria-selected={false}
|
|
385
|
+
tabIndex={-1}
|
|
386
|
+
onPointerMove={() => setActive(enabled.length)}
|
|
387
|
+
onClick={create}
|
|
388
|
+
className={cn(
|
|
389
|
+
"text-subheadline text-accent-text flex min-h-9 cursor-default items-center rounded-lg px-2 py-1.5",
|
|
390
|
+
!activeOption && "bg-fill",
|
|
391
|
+
)}
|
|
392
|
+
>
|
|
393
|
+
{createLabel?.(trimmed) ?? trimmed}
|
|
394
|
+
</div>
|
|
395
|
+
)}
|
|
396
|
+
{shown.length === 0 && !canCreate && (
|
|
397
|
+
<p className="text-footnote text-label-tertiary px-3 py-2">
|
|
398
|
+
{emptyLabel}
|
|
399
|
+
</p>
|
|
400
|
+
)}
|
|
401
|
+
</div>
|
|
402
|
+
</AnchoredSurface>
|
|
403
|
+
);
|
|
378
404
|
}
|