@slotform/react-ui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/density.d.ts +4 -0
- package/dist/density.d.ts.map +1 -0
- package/dist/density.js +8 -0
- package/dist/density.js.map +1 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +388 -0
- package/dist/index.js.map +1 -0
- package/dist/node/canvas.d.ts +3 -0
- package/dist/node/canvas.d.ts.map +1 -0
- package/dist/node/canvas.js +13 -0
- package/dist/node/canvas.js.map +1 -0
- package/dist/node/codegen.d.ts +6 -0
- package/dist/node/codegen.d.ts.map +1 -0
- package/dist/node/codegen.js +70 -0
- package/dist/node/codegen.js.map +1 -0
- package/dist/node/css-escape.d.ts +8 -0
- package/dist/node/css-escape.d.ts.map +1 -0
- package/dist/node/css-escape.js +12 -0
- package/dist/node/css-escape.js.map +1 -0
- package/dist/node/fonts.d.ts +6 -0
- package/dist/node/fonts.d.ts.map +1 -0
- package/dist/node/fonts.js +219 -0
- package/dist/node/fonts.js.map +1 -0
- package/dist/node/lint.d.ts +3 -0
- package/dist/node/lint.d.ts.map +1 -0
- package/dist/node/lint.js +23 -0
- package/dist/node/lint.js.map +1 -0
- package/dist/node/mode.d.ts +9 -0
- package/dist/node/mode.d.ts.map +1 -0
- package/dist/node/mode.js +38 -0
- package/dist/node/mode.js.map +1 -0
- package/dist/node/theme.d.ts +13 -0
- package/dist/node/theme.d.ts.map +1 -0
- package/dist/node/theme.js +160 -0
- package/dist/node/theme.js.map +1 -0
- package/dist/types.d.ts +250 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +80 -0
- package/dist/types.js.map +1 -0
- package/guide/acts.md +61 -0
- package/guide/audio.md +77 -0
- package/guide/blocks.md +82 -0
- package/guide/canvas-choosing.md +34 -0
- package/guide/canvas-editing.md +36 -0
- package/guide/canvas-layout.md +46 -0
- package/guide/canvas-opening.md +47 -0
- package/guide/canvas-overview.md +24 -0
- package/guide/canvas-states.md +37 -0
- package/guide/canvas-view.md +46 -0
- package/guide/canvas.md +94 -0
- package/guide/containers.md +60 -0
- package/guide/content.md +40 -0
- package/guide/controls.md +89 -0
- package/guide/density.md +48 -0
- package/guide/dock.md +42 -0
- package/guide/drag.md +38 -0
- package/guide/figures.md +25 -0
- package/guide/forms.md +82 -0
- package/guide/pane-bar.md +11 -0
- package/guide/pane.md +50 -0
- package/guide/reference.md +74 -0
- package/guide/rules.md +109 -0
- package/guide/section.md +29 -0
- package/guide/shell.md +26 -0
- package/guide/shots.md +37 -0
- package/guide/sidebar.md +30 -0
- package/guide/table.md +119 -0
- package/lint/no-arbitrary-value.grit +9 -0
- package/lint/no-class-on-component.grit +12 -0
- package/lint/no-density.grit +8 -0
- package/lint/no-host-look.grit +38 -0
- package/lint/no-img.grit +10 -0
- package/lint/no-navigate-act.grit +22 -0
- package/lint/no-raw-style.grit +8 -0
- package/package.json +150 -3
- package/src/density.ts +9 -0
- package/src/index.ts +503 -0
- package/src/node/canvas.ts +14 -0
- package/src/node/codegen.ts +100 -0
- package/src/node/css-escape.ts +27 -0
- package/src/node/fonts.ts +260 -0
- package/src/node/lint.ts +25 -0
- package/src/node/mode.ts +64 -0
- package/src/node/theme.ts +232 -0
- package/src/types.ts +120 -0
- package/src/ui/atoms/avatar/base.tsx +42 -0
- package/src/ui/atoms/button/glyph.tsx +244 -0
- package/src/ui/atoms/button/link.tsx +81 -0
- package/src/ui/atoms/button/look.ts +32 -0
- package/src/ui/atoms/button/reason.tsx +69 -0
- package/src/ui/atoms/chip/base.tsx +85 -0
- package/src/ui/atoms/count/base.tsx +27 -0
- package/src/ui/atoms/icon/index.tsx +51 -0
- package/src/ui/atoms/message/waiting.tsx +19 -0
- package/src/ui/atoms/prose/painted.tsx +35 -0
- package/src/ui/atoms/row/badges.tsx +226 -0
- package/src/ui/atoms/row/leading.tsx +166 -0
- package/src/ui/atoms/row/lock.tsx +17 -0
- package/src/ui/atoms/row/marks.tsx +79 -0
- package/src/ui/atoms/section/card.tsx +27 -0
- package/src/ui/atoms/spinner/index.tsx +42 -0
- package/src/ui/atoms/status/base.tsx +51 -0
- package/src/ui/atoms/status/change.tsx +29 -0
- package/src/ui/atoms/status/dot.tsx +45 -0
- package/src/ui/atoms/switch/checkbox.tsx +121 -0
- package/src/ui/atoms/turn.tsx +28 -0
- package/src/ui/components/action-bar/base.tsx +276 -0
- package/src/ui/components/action-bar/index.tsx +20 -0
- package/src/ui/components/avatar/index.tsx +21 -0
- package/src/ui/components/banner/index.tsx +207 -0
- package/src/ui/components/banner/pending.tsx +109 -0
- package/src/ui/components/bar-chart/index.tsx +701 -0
- package/src/ui/components/button/base.tsx +214 -0
- package/src/ui/components/button/face.tsx +84 -0
- package/src/ui/components/button/index.tsx +206 -0
- package/src/ui/components/canvas/connection.tsx +26 -0
- package/src/ui/components/canvas/edges.tsx +116 -0
- package/src/ui/components/canvas/elk.ts +260 -0
- package/src/ui/components/canvas/floor.ts +173 -0
- package/src/ui/components/canvas/geometry.ts +772 -0
- package/src/ui/components/canvas/ground.tsx +97 -0
- package/src/ui/components/canvas/group.tsx +156 -0
- package/src/ui/components/canvas/hit.ts +40 -0
- package/src/ui/components/canvas/index.tsx +497 -0
- package/src/ui/components/canvas/label.tsx +53 -0
- package/src/ui/components/canvas/layout.ts +543 -0
- package/src/ui/components/canvas/lift.ts +272 -0
- package/src/ui/components/canvas/look.ts +129 -0
- package/src/ui/components/canvas/node.tsx +689 -0
- package/src/ui/components/canvas/open.ts +243 -0
- package/src/ui/components/canvas/opening.ts +240 -0
- package/src/ui/components/canvas/view.ts +621 -0
- package/src/ui/components/canvas/viewport.ts +417 -0
- package/src/ui/components/canvas/zoom.tsx +113 -0
- package/src/ui/components/card/index.tsx +20 -0
- package/src/ui/components/chip/index.tsx +24 -0
- package/src/ui/components/code/frame.ts +2 -0
- package/src/ui/components/code/index.tsx +234 -0
- package/src/ui/components/columns/index.tsx +51 -0
- package/src/ui/components/count/index.tsx +17 -0
- package/src/ui/components/diff/index.tsx +249 -0
- package/src/ui/components/diff/word.tsx +104 -0
- package/src/ui/components/dock/index.tsx +262 -0
- package/src/ui/components/file-row/index.tsx +213 -0
- package/src/ui/components/form/base.tsx +280 -0
- package/src/ui/components/form/derive.tsx +269 -0
- package/src/ui/components/form/index.tsx +49 -0
- package/src/ui/components/form/saved.tsx +39 -0
- package/src/ui/components/form-field/index.tsx +658 -0
- package/src/ui/components/gallery/index.tsx +83 -0
- package/src/ui/components/gate/index.tsx +183 -0
- package/src/ui/components/image/base.tsx +245 -0
- package/src/ui/components/image/index.tsx +58 -0
- package/src/ui/components/input/base.tsx +246 -0
- package/src/ui/components/input/code.tsx +108 -0
- package/src/ui/components/input/drop.ts +37 -0
- package/src/ui/components/input/file.tsx +154 -0
- package/src/ui/components/input/index.tsx +132 -0
- package/src/ui/components/input/prose.tsx +107 -0
- package/src/ui/components/input/refusal.tsx +30 -0
- package/src/ui/components/item-header/base.tsx +410 -0
- package/src/ui/components/menu/base.tsx +319 -0
- package/src/ui/components/message/attachments.tsx +101 -0
- package/src/ui/components/message/base.tsx +266 -0
- package/src/ui/components/message-input/index.tsx +377 -0
- package/src/ui/components/missing/base.tsx +45 -0
- package/src/ui/components/pane/head.tsx +399 -0
- package/src/ui/components/pane/index.tsx +325 -0
- package/src/ui/components/picker/base.tsx +1551 -0
- package/src/ui/components/picker/index.tsx +224 -0
- package/src/ui/components/picker/list.tsx +423 -0
- package/src/ui/components/picker/segments.tsx +183 -0
- package/src/ui/components/prose/index.tsx +259 -0
- package/src/ui/components/qr-code/index.tsx +65 -0
- package/src/ui/components/row/acts.tsx +126 -0
- package/src/ui/components/row/fact.tsx +54 -0
- package/src/ui/components/row/index.tsx +1370 -0
- package/src/ui/components/row/progress.tsx +38 -0
- package/src/ui/components/row/status.tsx +54 -0
- package/src/ui/components/row/title.ts +7 -0
- package/src/ui/components/rules/index.tsx +290 -0
- package/src/ui/components/section/band.tsx +39 -0
- package/src/ui/components/section/head.tsx +267 -0
- package/src/ui/components/section/index.tsx +409 -0
- package/src/ui/components/section/rows.tsx +356 -0
- package/src/ui/components/sheet/base.tsx +272 -0
- package/src/ui/components/sheet/confirm.tsx +79 -0
- package/src/ui/components/sheet/docked.tsx +218 -0
- package/src/ui/components/sheet/form.tsx +76 -0
- package/src/ui/components/sheet/head.tsx +120 -0
- package/src/ui/components/sheet/index.tsx +45 -0
- package/src/ui/components/sheet/layer.tsx +168 -0
- package/src/ui/components/sheet/outlet.tsx +58 -0
- package/src/ui/components/sheet/task.tsx +41 -0
- package/src/ui/components/shell/cut.tsx +116 -0
- package/src/ui/components/shell/drag.tsx +45 -0
- package/src/ui/components/shell/host.tsx +71 -0
- package/src/ui/components/shell/index.tsx +737 -0
- package/src/ui/components/shell/panes.tsx +76 -0
- package/src/ui/components/shell/preview.ts +230 -0
- package/src/ui/components/shell/rail.tsx +163 -0
- package/src/ui/components/shell/row.ts +11 -0
- package/src/ui/components/shell/switcher.tsx +143 -0
- package/src/ui/components/shell/tree.tsx +252 -0
- package/src/ui/components/slider/base.tsx +139 -0
- package/src/ui/components/slider/index.tsx +57 -0
- package/src/ui/components/stages/index.tsx +198 -0
- package/src/ui/components/stats/count-links.tsx +47 -0
- package/src/ui/components/stats/index.tsx +203 -0
- package/src/ui/components/stats/meter.tsx +213 -0
- package/src/ui/components/stats/parts.ts +10 -0
- package/src/ui/components/step-count/index.tsx +45 -0
- package/src/ui/components/switch/base.tsx +86 -0
- package/src/ui/components/switch/index.tsx +38 -0
- package/src/ui/components/table/columns.tsx +131 -0
- package/src/ui/components/table/index.tsx +2215 -0
- package/src/ui/components/text/index.tsx +37 -0
- package/src/ui/components/thread/index.tsx +503 -0
- package/src/ui/components/thread/latest.tsx +59 -0
- package/src/ui/components/toast/index.tsx +113 -0
- package/src/ui/components/toast/layer.tsx +26 -0
- package/src/ui/components/toolbar/filters.tsx +161 -0
- package/src/ui/components/toolbar/index.tsx +125 -0
- package/src/ui/components/waveform/index.tsx +205 -0
- package/src/ui/globals.css +427 -0
- package/src/ui/lib/act.ts +60 -0
- package/src/ui/lib/age.ts +49 -0
- package/src/ui/lib/bound.ts +16 -0
- package/src/ui/lib/canvas-layout.ts +11 -0
- package/src/ui/lib/caret.ts +9 -0
- package/src/ui/lib/chip-lines.ts +3 -0
- package/src/ui/lib/closed.ts +8 -0
- package/src/ui/lib/code.tsx +184 -0
- package/src/ui/lib/columns.ts +39 -0
- package/src/ui/lib/commit.ts +24 -0
- package/src/ui/lib/container.ts +112 -0
- package/src/ui/lib/copy.ts +29 -0
- package/src/ui/lib/drag.ts +575 -0
- package/src/ui/lib/empty.tsx +85 -0
- package/src/ui/lib/field.ts +39 -0
- package/src/ui/lib/focus.ts +175 -0
- package/src/ui/lib/folds.ts +67 -0
- package/src/ui/lib/follow.ts +220 -0
- package/src/ui/lib/form.ts +19 -0
- package/src/ui/lib/guards.ts +38 -0
- package/src/ui/lib/heading.ts +14 -0
- package/src/ui/lib/ink.ts +26 -0
- package/src/ui/lib/lang.ts +7 -0
- package/src/ui/lib/leave.ts +14 -0
- package/src/ui/lib/lockup.tsx +145 -0
- package/src/ui/lib/measure.ts +22 -0
- package/src/ui/lib/media.ts +63 -0
- package/src/ui/lib/modality.ts +69 -0
- package/src/ui/lib/moment.ts +13 -0
- package/src/ui/lib/more.tsx +76 -0
- package/src/ui/lib/navigate.ts +210 -0
- package/src/ui/lib/noop.ts +3 -0
- package/src/ui/lib/not-found.tsx +40 -0
- package/src/ui/lib/paint.tsx +55 -0
- package/src/ui/lib/parts-line.tsx +54 -0
- package/src/ui/lib/picked.ts +18 -0
- package/src/ui/lib/player-store.ts +38 -0
- package/src/ui/lib/player.ts +139 -0
- package/src/ui/lib/portal.ts +12 -0
- package/src/ui/lib/read.tsx +269 -0
- package/src/ui/lib/region.tsx +49 -0
- package/src/ui/lib/reorder.ts +154 -0
- package/src/ui/lib/rest.ts +16 -0
- package/src/ui/lib/saved.ts +29 -0
- package/src/ui/lib/scramble.tsx +147 -0
- package/src/ui/lib/scrolls.ts +70 -0
- package/src/ui/lib/style.ts +6 -0
- package/src/ui/lib/swap.tsx +107 -0
- package/src/ui/lib/toast.ts +39 -0
- package/src/ui/lib/walk-classes.ts +16 -0
- package/src/ui/lib/window.tsx +376 -0
- package/src/ui/worker.d.ts +4 -0
|
@@ -0,0 +1,1551 @@
|
|
|
1
|
+
import { Combobox } from "@base-ui/react/combobox";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import { Select } from "@base-ui/react/select";
|
|
4
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
5
|
+
import type { IconLinkAct, Option } from "@slotform/ui-core/descriptors";
|
|
6
|
+
import {
|
|
7
|
+
optionBlocked,
|
|
8
|
+
optionLeading,
|
|
9
|
+
optionMeta,
|
|
10
|
+
toggled,
|
|
11
|
+
} from "@slotform/ui-core/list-state";
|
|
12
|
+
import {
|
|
13
|
+
type Grouped,
|
|
14
|
+
groupsOf,
|
|
15
|
+
type OptionRow,
|
|
16
|
+
optionRows,
|
|
17
|
+
type PickOptions,
|
|
18
|
+
pickCurrent,
|
|
19
|
+
SEARCH_PAST,
|
|
20
|
+
type Several,
|
|
21
|
+
} from "@slotform/ui-core/pick";
|
|
22
|
+
import type { Acting, Draft } from "@slotform/ui-core/react/acts";
|
|
23
|
+
import { useOptions } from "@slotform/ui-core/react/read";
|
|
24
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
25
|
+
import { isEnded } from "@slotform/ui-core/read";
|
|
26
|
+
import { nounOf } from "@slotform/ui-core/rules";
|
|
27
|
+
import type { Tone } from "@slotform/ui-core/tokens";
|
|
28
|
+
import {
|
|
29
|
+
CHIPS_RUN,
|
|
30
|
+
CHIPS_TRIGGER,
|
|
31
|
+
FIELD_GLYPH,
|
|
32
|
+
FIELD_PLACEHOLDER,
|
|
33
|
+
FIELD_QUIET,
|
|
34
|
+
field,
|
|
35
|
+
fieldValue,
|
|
36
|
+
HAIRLINE,
|
|
37
|
+
ITEM_FACT_HIT,
|
|
38
|
+
ITEM_PILL,
|
|
39
|
+
ITEM_PILL_ACT,
|
|
40
|
+
ITEM_PILL_GLYPH,
|
|
41
|
+
ITEM_PILL_HEAD,
|
|
42
|
+
ITEM_PILL_HIT,
|
|
43
|
+
ITEM_PILL_STATE_ACT,
|
|
44
|
+
LAYER_POPOVER,
|
|
45
|
+
OPTION_GROUP_LABEL,
|
|
46
|
+
OPTION_ROW,
|
|
47
|
+
PICKER_EMPTY,
|
|
48
|
+
PICKER_PILL,
|
|
49
|
+
PICKER_POPOVER,
|
|
50
|
+
PICKER_VALUE,
|
|
51
|
+
type PickerFit,
|
|
52
|
+
POPOVER,
|
|
53
|
+
PRESS_WASH,
|
|
54
|
+
picker,
|
|
55
|
+
ROW_LEADING,
|
|
56
|
+
row,
|
|
57
|
+
STATE_CHIP_LABEL,
|
|
58
|
+
stateChip,
|
|
59
|
+
text,
|
|
60
|
+
textStrong,
|
|
61
|
+
WORD_ACT,
|
|
62
|
+
} from "@slotform/ui-core/variants";
|
|
63
|
+
import { VIRTUAL_FROM } from "@slotform/ui-core/window";
|
|
64
|
+
import {
|
|
65
|
+
type ComponentProps,
|
|
66
|
+
memo,
|
|
67
|
+
type ReactElement,
|
|
68
|
+
type ReactNode,
|
|
69
|
+
type RefObject,
|
|
70
|
+
use,
|
|
71
|
+
useLayoutEffect,
|
|
72
|
+
useMemo,
|
|
73
|
+
useRef,
|
|
74
|
+
useState,
|
|
75
|
+
} from "react";
|
|
76
|
+
import { Named } from "../../atoms/button/glyph.tsx";
|
|
77
|
+
import { Icon } from "../../atoms/icon/index.tsx";
|
|
78
|
+
import { StatusBase } from "../../atoms/status/base.tsx";
|
|
79
|
+
import { StatusDot } from "../../atoms/status/dot.tsx";
|
|
80
|
+
import { Turn } from "../../atoms/turn.tsx";
|
|
81
|
+
import type { IconAct } from "../../lib/act.ts";
|
|
82
|
+
import { useAct, useHeldDraft } from "../../lib/act.ts";
|
|
83
|
+
import { CHIP_LINES } from "../../lib/chip-lines.ts";
|
|
84
|
+
import { expandedTrigger, rememberOpener } from "../../lib/focus.ts";
|
|
85
|
+
import { useQuietGuards } from "../../lib/guards.ts";
|
|
86
|
+
import { spacing } from "../../lib/measure.ts";
|
|
87
|
+
import { useTouch } from "../../lib/media.ts";
|
|
88
|
+
import { follow } from "../../lib/navigate.ts";
|
|
89
|
+
import { PAINT_CONTROL_HIDDEN } from "../../lib/paint.tsx";
|
|
90
|
+
import { PortalContainer } from "../../lib/portal.ts";
|
|
91
|
+
import { OptionsLine } from "../../lib/read.tsx";
|
|
92
|
+
import { Scroller, useScroller, Window } from "../../lib/window.tsx";
|
|
93
|
+
import { Avatar } from "../avatar/index.tsx";
|
|
94
|
+
import { Chip } from "../chip/index.tsx";
|
|
95
|
+
import { BOX_DISABLED, BOX_HOVER } from "../input/base.tsx";
|
|
96
|
+
import { Input } from "../input/index.tsx";
|
|
97
|
+
import { SheetHead } from "../sheet/head.tsx";
|
|
98
|
+
import { SheetLayer } from "../sheet/layer.tsx";
|
|
99
|
+
import type {
|
|
100
|
+
PickerMenuProps,
|
|
101
|
+
PickOneProps,
|
|
102
|
+
PickSeveralProps,
|
|
103
|
+
} from "./index.tsx";
|
|
104
|
+
|
|
105
|
+
// A field-fit trigger is the field box (at the bar fit, or a form's field
|
|
106
|
+
// height in a `FormField`); open, it keeps the accent edge a focused field
|
|
107
|
+
// takes, and the ring under the keyboard. It yields only past its line, so a
|
|
108
|
+
// long value truncates before the chevron.
|
|
109
|
+
const FIELD_TRIGGER =
|
|
110
|
+
"flex min-w-0 max-w-full items-center text-start focus-visible:outline-offset-(--focus-ring-edge-offset)";
|
|
111
|
+
const FIELD_OPEN =
|
|
112
|
+
"in-data-[modality=keyboard]:outline-2 outline-offset-(--focus-ring-edge-offset) outline-ring";
|
|
113
|
+
// The error edge stands over the accent edge, as on a focused field.
|
|
114
|
+
const OPEN_EDGE = "border-accent";
|
|
115
|
+
const FIELD_VALUE = "min-w-0 grow truncate";
|
|
116
|
+
// A pick of several: the chips wrap in a run, and the trigger that opens the
|
|
117
|
+
// list stands beside it, filling what the run leaves, its chevron at the box's
|
|
118
|
+
// end; the run wraps, so the chevron never takes a line of its own.
|
|
119
|
+
const SEVERAL_BOX = "flex items-stretch min-w-0";
|
|
120
|
+
const SEVERAL_RUN = "flex items-center gap-gap min-w-0";
|
|
121
|
+
const WRAPS = "flex-wrap";
|
|
122
|
+
// In a rule's row the run keeps its one line (it does not wrap): its one chip
|
|
123
|
+
// truncates, and the token that stands for several is the trigger's own, so a
|
|
124
|
+
// press on it opens the list.
|
|
125
|
+
const FOLDED = "flex min-w-0 grow items-center";
|
|
126
|
+
const SEVERAL_TRIGGER =
|
|
127
|
+
"flex grow shrink-0 items-center justify-end min-w-target max-w-full text-start";
|
|
128
|
+
// A row-fit trigger centres in its row and pulls back by its own padding at
|
|
129
|
+
// the row's end; it stands over a row's hit. Its value stands in the body ink
|
|
130
|
+
// (`PICKER_VALUE`) and its chevron in the meta ink; open, it holds the press
|
|
131
|
+
// wash and the chevron takes the body ink too. Its cap counts the pull-back, so a value
|
|
132
|
+
// with room stays whole and a long one still stops at its column. Its box is
|
|
133
|
+
// the control height, the row's, as an icon act's beside it is.
|
|
134
|
+
const ROW_TRIGGER = cn(
|
|
135
|
+
"relative inline-flex min-w-0 min-h-control max-w-[calc(100%+var(--spacing-gap))] self-center items-center -me-gap",
|
|
136
|
+
PRESS_WASH,
|
|
137
|
+
);
|
|
138
|
+
const ROW_OPEN = "bg-wash-press text-ink-body";
|
|
139
|
+
const ROW_VALUE = "truncate";
|
|
140
|
+
// A pick standing as a fact of a record's head (`fact`) is one pill, the
|
|
141
|
+
// trigger itself: the outlined pill around a plain value, named in the body
|
|
142
|
+
// ink as the facts beside it are, or, where the chosen option
|
|
143
|
+
// carries a state, the state pill (its dot and word) with the chevron in its
|
|
144
|
+
// own ink, never a state pill inside an outlined one. The hit is the target's, pulled back to
|
|
145
|
+
// the badge's line (`ITEM_FACT_HIT`), as a fact that opens is. A row's pick whose
|
|
146
|
+
// chosen option carries a state is that same state pill at a chip's height
|
|
147
|
+
// (the caption line between its hairlines), so a status that moves has
|
|
148
|
+
// one shape in a row and in a head: the pill is the trigger, its chevron
|
|
149
|
+
// inside at its end; its hit, the target's, overflows the row's line centred
|
|
150
|
+
// as every end item's does.
|
|
151
|
+
const PILL_HIT = "inline-flex items-center min-w-0 max-w-full";
|
|
152
|
+
const PILL_BOX = "inline-flex items-center min-w-0 max-w-full";
|
|
153
|
+
const PILL_VALUE = "truncate text-ink-body";
|
|
154
|
+
const PILL_STATE_VALUE = "truncate";
|
|
155
|
+
const PILL_CHEVRON = "flex shrink-0";
|
|
156
|
+
const PILL_OPEN = "bg-wash-press";
|
|
157
|
+
const PILL_STATE_OPEN = "border-current";
|
|
158
|
+
// The popover stops at the room Base UI measures below its trigger; its rows
|
|
159
|
+
// scroll inside it, under a search that stays. It stands at the popover's
|
|
160
|
+
// width, or at its anchor's (the field's box, or the trigger's) where that is
|
|
161
|
+
// wider, so a field the width of a sheet opens a list as wide as itself and
|
|
162
|
+
// never one that cuts its options short of the field's own edge.
|
|
163
|
+
const POPUP = "flex flex-col max-h-(--available-height) min-w-(--anchor-width)";
|
|
164
|
+
const POSITIONER = "z-(--layer-popover)";
|
|
165
|
+
const LIST = "flex flex-col gap-gap min-h-0 overflow-y-auto overscroll-contain";
|
|
166
|
+
const GROUP = "flex flex-col";
|
|
167
|
+
// The highlight washes the option under the pointer or the keyboard; the
|
|
168
|
+
// keyboard's highlight rings it inset as well, the wash alone being no focus
|
|
169
|
+
// cue, and the pointer's draws the wash alone.
|
|
170
|
+
const OPTION = "flex items-center";
|
|
171
|
+
const PRESS = "active:bg-wash-press";
|
|
172
|
+
const OPTION_NO_FOCUS = "outline-none";
|
|
173
|
+
const KEYBOARD_RING = "outline-2 -outline-offset-2 outline-ring";
|
|
174
|
+
// A touch option is a button: its text at the start, its focus the
|
|
175
|
+
// keyboard's highlight and its ring; a blocked one takes the ring alone.
|
|
176
|
+
const OPTION_BUTTON = "text-start focus-visible:-outline-offset-2";
|
|
177
|
+
const OPTION_FOCUS_WASH = "focus-visible:bg-wash-hover";
|
|
178
|
+
const OPTION_TEXT = "flex flex-col min-w-0 grow";
|
|
179
|
+
const LINE = "truncate";
|
|
180
|
+
// A blocked option's label and reason; it takes no press wash.
|
|
181
|
+
const BLOCKED_INK = "text-ink-disabled";
|
|
182
|
+
const TICK = "flex shrink-0 text-ink-body";
|
|
183
|
+
// A glyph leads an option in the row's leading region.
|
|
184
|
+
const LEADING = "flex shrink-0 items-center justify-center";
|
|
185
|
+
// An option's own chip stands after its text and keeps its word whole: the text
|
|
186
|
+
// truncates before it does, and only a chip past the short measure is cut.
|
|
187
|
+
const CHIP_AFTER = "flex shrink-0 max-w-measure-short";
|
|
188
|
+
const SEARCH = "flex grow items-center";
|
|
189
|
+
const SEARCH_VALUE =
|
|
190
|
+
"min-w-0 grow truncate outline-none placeholder:text-ink-meta";
|
|
191
|
+
const SEARCH_REGION = "flex px-pad";
|
|
192
|
+
// The sheet's rows stand a pair under its head and the search, clear of a
|
|
193
|
+
// phone's home indicator; a long list scrolls.
|
|
194
|
+
const SHEET_ROWS = "flex flex-col gap-gap pt-gap pb-pad min-h-0";
|
|
195
|
+
const LISTBOX = "flex flex-col min-h-0 overflow-y-auto overscroll-contain";
|
|
196
|
+
// The act that ends the list stands under a hairline across it, a float
|
|
197
|
+
// inset below the line.
|
|
198
|
+
const ACT_REGION = "flex flex-col border-t pt-gap";
|
|
199
|
+
const ACT_ROW = cn(
|
|
200
|
+
"flex items-center text-start",
|
|
201
|
+
PRESS_WASH,
|
|
202
|
+
"focus-visible:-outline-offset-2",
|
|
203
|
+
);
|
|
204
|
+
const ACT_GLYPH = "flex shrink-0 text-ink-meta";
|
|
205
|
+
|
|
206
|
+
const NO_MATCHES = "flex items-center";
|
|
207
|
+
|
|
208
|
+
type Handed = ComponentProps<"button">;
|
|
209
|
+
|
|
210
|
+
// A pick's props once its value is drawn: the viewer's pick or the
|
|
211
|
+
// selection's, no longer a datum.
|
|
212
|
+
type Drawn<V extends string | null> = Omit<
|
|
213
|
+
PickerMenuProps<V>,
|
|
214
|
+
"value" | "onChange"
|
|
215
|
+
> &
|
|
216
|
+
(
|
|
217
|
+
| { value?: V; onChange: (value: V) => void }
|
|
218
|
+
| { value: readonly V[]; onChange: (value: V[]) => void }
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
function isSeveral<V extends string | null>(
|
|
222
|
+
props: Drawn<V>,
|
|
223
|
+
): props is Drawn<V> & Several<V> {
|
|
224
|
+
return Array.isArray(props.value);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Whether an option is in the value: the set of a pick of several, else the
|
|
228
|
+
// one value.
|
|
229
|
+
const inValue = <V extends string | null>(
|
|
230
|
+
several: Several<V> | undefined,
|
|
231
|
+
value: V | undefined,
|
|
232
|
+
one: V,
|
|
233
|
+
) => (several ? several.value.includes(one) : one === value);
|
|
234
|
+
|
|
235
|
+
// An option's row: a blocked one takes no highlight and no press wash.
|
|
236
|
+
const optionLook = (blocked: string | undefined, highlighted = false) =>
|
|
237
|
+
cn(
|
|
238
|
+
row({
|
|
239
|
+
state: highlighted && blocked === undefined ? "highlighted" : "rest",
|
|
240
|
+
}),
|
|
241
|
+
OPTION_ROW,
|
|
242
|
+
OPTION,
|
|
243
|
+
blocked === undefined && PRESS,
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
// An option's label (the empty choice in the placeholder's ink) over its
|
|
247
|
+
// line; an option leading with a state shows its status's dot, one leading
|
|
248
|
+
// with an avatar its avatar, and its chip after the text.
|
|
249
|
+
function OptionText(props: { option: Option<string | null>; chosen: boolean }) {
|
|
250
|
+
const { option } = props;
|
|
251
|
+
const blocked = optionBlocked(option, props.chosen);
|
|
252
|
+
const meta = optionMeta(option, props.chosen);
|
|
253
|
+
const lead = optionLeading(option);
|
|
254
|
+
return (
|
|
255
|
+
<>
|
|
256
|
+
{lead.icon ? (
|
|
257
|
+
<span className={cn(ROW_LEADING, LEADING)}>
|
|
258
|
+
<Icon name={lead.icon} />
|
|
259
|
+
</span>
|
|
260
|
+
) : null}
|
|
261
|
+
{lead.tone ? <StatusDot tone={lead.tone} /> : null}
|
|
262
|
+
{lead.avatar ? (
|
|
263
|
+
<Avatar name={lead.avatar.name} src={lead.avatar.src} />
|
|
264
|
+
) : null}
|
|
265
|
+
<span className={OPTION_TEXT}>
|
|
266
|
+
<span
|
|
267
|
+
className={cn(
|
|
268
|
+
text({ role: "body" }),
|
|
269
|
+
option.value === null && PICKER_EMPTY,
|
|
270
|
+
LINE,
|
|
271
|
+
blocked !== undefined && BLOCKED_INK,
|
|
272
|
+
)}
|
|
273
|
+
>
|
|
274
|
+
{option.label}
|
|
275
|
+
</span>
|
|
276
|
+
{meta ? (
|
|
277
|
+
<span
|
|
278
|
+
className={cn(
|
|
279
|
+
text({ role: "meta" }),
|
|
280
|
+
LINE,
|
|
281
|
+
blocked !== undefined && BLOCKED_INK,
|
|
282
|
+
)}
|
|
283
|
+
>
|
|
284
|
+
{meta}
|
|
285
|
+
</span>
|
|
286
|
+
) : null}
|
|
287
|
+
</span>
|
|
288
|
+
{option.chip ? (
|
|
289
|
+
<span className={CHIP_AFTER}>
|
|
290
|
+
<Chip {...option.chip} />
|
|
291
|
+
</span>
|
|
292
|
+
) : null}
|
|
293
|
+
</>
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// The act that ends the list under a hairline: its glyph and label on a row,
|
|
298
|
+
// washed under the pointer and the press; it closes the list as it runs.
|
|
299
|
+
// The picker holds its act's attempt (`acting`) past the list's close (the
|
|
300
|
+
// list unmounts with it): a rejection toasts with Retry.
|
|
301
|
+
function PickAct(props: {
|
|
302
|
+
act: IconAct | IconLinkAct;
|
|
303
|
+
acting: Acting<true>;
|
|
304
|
+
done: () => void;
|
|
305
|
+
onFocus?: () => void;
|
|
306
|
+
}) {
|
|
307
|
+
const { act, acting } = props;
|
|
308
|
+
// An option's row, in the popover and the touch sheet alike, so on touch it
|
|
309
|
+
// stands at the target.
|
|
310
|
+
const look = cn(row(), OPTION_ROW, ACT_ROW);
|
|
311
|
+
const face = (
|
|
312
|
+
<>
|
|
313
|
+
<span className={ACT_GLYPH}>
|
|
314
|
+
<Icon name={act.icon} />
|
|
315
|
+
</span>
|
|
316
|
+
<span className={cn(text({ role: "body" }), LINE)}>{act.label}</span>
|
|
317
|
+
</>
|
|
318
|
+
);
|
|
319
|
+
return (
|
|
320
|
+
<div className={cn(HAIRLINE, ACT_REGION)}>
|
|
321
|
+
{"href" in act && act.blocked === undefined ? (
|
|
322
|
+
<a
|
|
323
|
+
href={act.href}
|
|
324
|
+
onFocus={props.onFocus}
|
|
325
|
+
onClick={(event) => {
|
|
326
|
+
rememberOpener(expandedTrigger());
|
|
327
|
+
props.done();
|
|
328
|
+
follow(event);
|
|
329
|
+
}}
|
|
330
|
+
className={look}
|
|
331
|
+
>
|
|
332
|
+
{face}
|
|
333
|
+
</a>
|
|
334
|
+
) : (
|
|
335
|
+
<button
|
|
336
|
+
type="button"
|
|
337
|
+
onFocus={props.onFocus}
|
|
338
|
+
aria-disabled={act.blocked !== undefined || undefined}
|
|
339
|
+
onClick={() => {
|
|
340
|
+
if (act.blocked !== undefined) return;
|
|
341
|
+
rememberOpener(expandedTrigger());
|
|
342
|
+
props.done();
|
|
343
|
+
acting.attempt(true, act);
|
|
344
|
+
}}
|
|
345
|
+
className={look}
|
|
346
|
+
>
|
|
347
|
+
{face}
|
|
348
|
+
</button>
|
|
349
|
+
)}
|
|
350
|
+
</div>
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// What a list says on a row of its own when it holds nothing: the search
|
|
355
|
+
// matches no option, or there is no option to choose yet.
|
|
356
|
+
function NoMatches(props: { children: ReactNode }) {
|
|
357
|
+
return (
|
|
358
|
+
<div className={cn(row(), OPTION_ROW, text({ role: "meta" }), NO_MATCHES)}>
|
|
359
|
+
{props.children}
|
|
360
|
+
</div>
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** A group's label over its options, in every list a pick draws. Outside the package's exports. */
|
|
365
|
+
export const GROUP_LABEL = cn(
|
|
366
|
+
OPTION_GROUP_LABEL,
|
|
367
|
+
text({ role: "meta" }),
|
|
368
|
+
textStrong({ role: "meta" }),
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
// Base UI's group label renders through it, handing the id its group is named
|
|
372
|
+
// by, so the given props stand on the element.
|
|
373
|
+
function GroupLabel(props: ComponentProps<"div">) {
|
|
374
|
+
return <div {...props} className={GROUP_LABEL} />;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// What every pick draws: the public `Picker`'s field and row triggers, or a
|
|
378
|
+
// trigger its composer draws (the Shell's switcher), over the one list. Outside
|
|
379
|
+
// the package's exports.
|
|
380
|
+
interface Composed {
|
|
381
|
+
/** Where it stands: a public fit, or `fact`, a fact of a record's head, which stands as the row fit does but for its trigger: one pill, the outlined pill around a plain value (in the body ink, as the facts beside it) or the state pill of a chosen option that carries a state, each with its chevron, at the badge's height (the meta line), in the target's hit box pulled back to that line, so a facts line holds one height whichever kinds of fact it holds. */
|
|
382
|
+
fit?: PickerFit | "fact";
|
|
383
|
+
/** A trigger drawn by the composer, handed Base UI's props and whether the list is open. */
|
|
384
|
+
drawn?: (handed: Handed, open: boolean) => ReactElement;
|
|
385
|
+
/** `-1` where the trigger's host holds the tab stop (a tree's row); absent, the trigger keeps its own. */
|
|
386
|
+
tabIndex?: -1;
|
|
387
|
+
/** Its data is not here: the trigger's box filled at its loaded size, its content not drawn, inert, so no list opens. */
|
|
388
|
+
painted?: boolean;
|
|
389
|
+
/** A term of a rule's row: the trigger reads its value alone, never the bar's "label: value", since the row's other term names what it sets; the label stays its accessible name. */
|
|
390
|
+
term?: boolean;
|
|
391
|
+
/** A pick of several in a rule's row, which stays one line: its chips are one while it holds one value, and two or more fold to one token naming how many, the values ticked in its list. */
|
|
392
|
+
fold?: boolean;
|
|
393
|
+
/** The pill's dot: the tone of the context the Pane reads in, drawn before the label (a Pane's context pick). */
|
|
394
|
+
state?: Tone;
|
|
395
|
+
/** Set by a `FormField` around the pick: the box stands at the form's field height, `edge-error` when the field is in error and the disabled fill when it is disabled, and the field's label names the pick (so no `placeholder` falls back to it). */
|
|
396
|
+
form?: { invalid: boolean; disabled: boolean };
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Base UI's list sets a value of its own when its items change without the
|
|
400
|
+
// held one (null, or the first value it was given), and its registry keeps
|
|
401
|
+
// the regions of unmounted items, so it does so too when they all unmount as
|
|
402
|
+
// the options wait. The viewer only ever picks an option the list holds, or
|
|
403
|
+
// toggles one in or out of a set, so any other change is the list's and never
|
|
404
|
+
// reaches the app.
|
|
405
|
+
function viewerPicked<V extends string | null>(
|
|
406
|
+
flat: readonly Option<V>[],
|
|
407
|
+
held: V | readonly V[] | undefined,
|
|
408
|
+
next: V | readonly V[],
|
|
409
|
+
): boolean {
|
|
410
|
+
const holds = (value: V) => flat.some((option) => option.value === value);
|
|
411
|
+
if (!isSet(next)) return holds(next);
|
|
412
|
+
const before = isSet(held) ? held : [];
|
|
413
|
+
const changed = [
|
|
414
|
+
...next.filter((value) => !before.includes(value)),
|
|
415
|
+
...before.filter((value) => !next.includes(value)),
|
|
416
|
+
];
|
|
417
|
+
const [only] = changed;
|
|
418
|
+
return changed.length === 1 && only !== undefined && holds(only);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// `Array.isArray` narrows a readonly array to `any[]`; this keeps its items.
|
|
422
|
+
const isSet = <V,>(
|
|
423
|
+
value: V | readonly V[] | undefined,
|
|
424
|
+
): value is readonly V[] => Array.isArray(value);
|
|
425
|
+
|
|
426
|
+
/** The trigger and list every Picker renders, on the `draft` its host holds: a field hosts the draft of its picker and draws a rejection in its own line. Waiting options paint the trigger, and ended ones say so in a line under it. Outside the package's exports. */
|
|
427
|
+
export function PickerBase<V extends string | null = string>(
|
|
428
|
+
given: Omit<PickerMenuProps<V>, "value" | "onChange" | "fit"> & {
|
|
429
|
+
draft: Draft<V | readonly V[] | undefined>;
|
|
430
|
+
onChange: (next: V | V[]) => unknown;
|
|
431
|
+
} & Composed,
|
|
432
|
+
) {
|
|
433
|
+
// The value is the viewer's pick once made, else the selection's; a painted
|
|
434
|
+
// pick draws as a single one, as the stand-in is no array.
|
|
435
|
+
const { draft } = given;
|
|
436
|
+
// Waiting options paint the trigger as a waiting value does, and the list
|
|
437
|
+
// has no rows to open.
|
|
438
|
+
const { drawn: waiting, retry } = useOptions(given.options);
|
|
439
|
+
const options: PickOptions<V> = waiting.painted ? [] : waiting.value;
|
|
440
|
+
const groups = groupsOf(options);
|
|
441
|
+
const flat = groups.flatMap((group) => group.options);
|
|
442
|
+
// A value whose read ended leaves the box empty and blocked: opening it
|
|
443
|
+
// says why and opens nothing, so nothing can be picked.
|
|
444
|
+
const ended = draft.ended;
|
|
445
|
+
const painted = given.painted || (draft.painted && !ended) || waiting.painted;
|
|
446
|
+
// The draft's value and the given `onChange` are typed apart, so the
|
|
447
|
+
// compiler cannot see that a set's value comes with a set's `onChange`.
|
|
448
|
+
const props = {
|
|
449
|
+
...given,
|
|
450
|
+
value: draft.painted ? undefined : draft.value,
|
|
451
|
+
onChange: (next: V | V[]) => {
|
|
452
|
+
if (!viewerPicked(flat, draft.value, next)) return;
|
|
453
|
+
draft.change(next, () => given.onChange(next));
|
|
454
|
+
},
|
|
455
|
+
} as Drawn<V>;
|
|
456
|
+
const { label, placeholder, fit: placed = "field", act, drawn, fold } = given;
|
|
457
|
+
// A head's fact stands as a row's pick does but for its trigger.
|
|
458
|
+
const fact = placed === "fact";
|
|
459
|
+
const fit = fact ? "row" : placed;
|
|
460
|
+
const { form } = given;
|
|
461
|
+
const acting = useAct("toast");
|
|
462
|
+
const words = useWords();
|
|
463
|
+
// What an empty box says: the hint, else the generic word in a row (its label
|
|
464
|
+
// is the accessible name, never the trigger's text), else what is picked (a field's own label
|
|
465
|
+
// stands over it already).
|
|
466
|
+
const hint =
|
|
467
|
+
placeholder ?? (form ? undefined : fit === "row" ? words.choose : label);
|
|
468
|
+
const fieldBox = field({
|
|
469
|
+
state: form?.invalid ? "error" : "rest",
|
|
470
|
+
});
|
|
471
|
+
// The field box's hover edge yields to the error edge and the disabled fill.
|
|
472
|
+
const states = form?.disabled ? BOX_DISABLED : !form?.invalid && BOX_HOVER;
|
|
473
|
+
// A pick of several: its value is an array, which `Several` types as one.
|
|
474
|
+
const several = isSeveral(props) ? props : undefined;
|
|
475
|
+
const value = isSeveral(props) ? undefined : props.value;
|
|
476
|
+
const set = several?.value;
|
|
477
|
+
// A bar pick with nothing picked is quiet (`FIELD_QUIET`).
|
|
478
|
+
const quiet =
|
|
479
|
+
fit === "bar" &&
|
|
480
|
+
(several
|
|
481
|
+
? (set?.length ?? 0) === 0
|
|
482
|
+
: value === null || value === undefined);
|
|
483
|
+
const touch = useTouch();
|
|
484
|
+
const box = useRef<HTMLDivElement>(null);
|
|
485
|
+
const run = useRef<HTMLDivElement>(null);
|
|
486
|
+
const [open, setOpenState] = useState(false);
|
|
487
|
+
const setOpen = (next: boolean) => {
|
|
488
|
+
if (ended && next) {
|
|
489
|
+
draft.change(undefined);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
setOpenState(next);
|
|
493
|
+
};
|
|
494
|
+
const current = pickCurrent(flat, value, several !== undefined);
|
|
495
|
+
// Past six options a search leads the list. The form follows the count only
|
|
496
|
+
// while the list is closed, so a count crossing six never tears down an
|
|
497
|
+
// open list and its focus.
|
|
498
|
+
const many = flat.length > SEARCH_PAST;
|
|
499
|
+
const [searching, setSearching] = useState(many);
|
|
500
|
+
if (!open && searching !== many) setSearching(many);
|
|
501
|
+
// A pick of several keeps its list open, an option toggling in and out.
|
|
502
|
+
const chosen = (set ?? []).flatMap(
|
|
503
|
+
(one) => flat.find((option) => option.value === one) ?? [],
|
|
504
|
+
);
|
|
505
|
+
const pick = (next: V) => {
|
|
506
|
+
if (isSeveral(props)) {
|
|
507
|
+
props.onChange(toggled(props.value, next));
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
setOpen(false);
|
|
511
|
+
props.onChange(next);
|
|
512
|
+
};
|
|
513
|
+
// A filled trigger is named by its label and what it shows, "label, value"
|
|
514
|
+
// (WCAG 2.5.3: the name holds the visible text, the chip's word after the
|
|
515
|
+
// value's); an empty one by its label. The bar's pill is named by what it
|
|
516
|
+
// shows alone, which is also all it shows below `tablet`.
|
|
517
|
+
const shownName = current
|
|
518
|
+
? `${current.label}${current.chip ? ` ${current.chip.label}` : ""}`
|
|
519
|
+
: hint;
|
|
520
|
+
const named =
|
|
521
|
+
fit === "pill"
|
|
522
|
+
? (shownName ?? label)
|
|
523
|
+
: current
|
|
524
|
+
? `${label}, ${shownName}`
|
|
525
|
+
: label;
|
|
526
|
+
const leads = optionLeading(current);
|
|
527
|
+
const glyph = leads.icon ? (
|
|
528
|
+
<Icon
|
|
529
|
+
name={leads.icon}
|
|
530
|
+
fit={fit === "row" || fit === "pill" ? "meta" : "control"}
|
|
531
|
+
/>
|
|
532
|
+
) : null;
|
|
533
|
+
// An option carrying a state shows as its status in a field box; at the
|
|
534
|
+
// row fit the trigger is the state pill itself (`pill`).
|
|
535
|
+
const status = leads.tone ? (
|
|
536
|
+
<StatusBase tone={leads.tone} label={current?.label} />
|
|
537
|
+
) : null;
|
|
538
|
+
// The option's kind stands after its value, on the trigger as in the list.
|
|
539
|
+
const kind = current?.chip ? (
|
|
540
|
+
<span className={CHIP_AFTER}>
|
|
541
|
+
<Chip {...current.chip} />
|
|
542
|
+
</span>
|
|
543
|
+
) : null;
|
|
544
|
+
// With no value the trigger shows what is picked in the placeholder's ink.
|
|
545
|
+
const unset = current === undefined;
|
|
546
|
+
const rowValue = (
|
|
547
|
+
<span className={cn(PICKER_VALUE, ROW_VALUE, unset && FIELD_PLACEHOLDER)}>
|
|
548
|
+
{current?.label ?? hint}
|
|
549
|
+
</span>
|
|
550
|
+
);
|
|
551
|
+
// A set pick in a toolbar (the bar fit) reads its
|
|
552
|
+
// label then its value, so a value that other filters could share
|
|
553
|
+
// ("Change") is told apart ("Work type: Change"); the empty choice (a `null`
|
|
554
|
+
// option, the way to clear) reads the label alone, as an unset pick does.
|
|
555
|
+
// Every other place reads the value alone, its label standing over it or
|
|
556
|
+
// beside it already, a rule's term too, whose row names it.
|
|
557
|
+
const toolbar = fit === "bar" && !given.term;
|
|
558
|
+
const lead = (option: Option<V>) => {
|
|
559
|
+
if (!toolbar) return option.label;
|
|
560
|
+
return option.value === null ? label : `${label}: ${option.label}`;
|
|
561
|
+
};
|
|
562
|
+
const fieldShown = (
|
|
563
|
+
<span
|
|
564
|
+
className={cn(
|
|
565
|
+
fieldValue({ kind: "text" }),
|
|
566
|
+
current?.value === null && PICKER_EMPTY,
|
|
567
|
+
unset && FIELD_PLACEHOLDER,
|
|
568
|
+
FIELD_VALUE,
|
|
569
|
+
)}
|
|
570
|
+
>
|
|
571
|
+
{status ?? (current ? lead(current) : hint)}
|
|
572
|
+
</span>
|
|
573
|
+
);
|
|
574
|
+
// A removed chip hands focus to the next chip's remove act, else the
|
|
575
|
+
// previous one's, else the trigger.
|
|
576
|
+
const removeChip = (picks: Several<V>, option: Option<V>, at: number) => {
|
|
577
|
+
const chips = run.current?.children;
|
|
578
|
+
const near = chips?.[at + 1] ?? chips?.[at - 1];
|
|
579
|
+
const next = near?.querySelector("button") ?? box.current?.lastElementChild;
|
|
580
|
+
picks.onChange(toggled(picks.value, option.value));
|
|
581
|
+
if (next instanceof HTMLElement) next.focus();
|
|
582
|
+
};
|
|
583
|
+
// The chips of a pick of several, centred in the box a badge's room clear, each
|
|
584
|
+
// hold their own remove act, so the box is no button: the trigger that
|
|
585
|
+
// opens the list stands after them.
|
|
586
|
+
const severalBox = (handed: Handed, picks: Several<V>) => {
|
|
587
|
+
const folded = fold && chosen.length > 1;
|
|
588
|
+
return (
|
|
589
|
+
<div
|
|
590
|
+
ref={box}
|
|
591
|
+
className={cn(
|
|
592
|
+
fieldBox,
|
|
593
|
+
picker({}),
|
|
594
|
+
FIELD_GLYPH,
|
|
595
|
+
SEVERAL_BOX,
|
|
596
|
+
quiet && !open && FIELD_QUIET,
|
|
597
|
+
open && FIELD_OPEN,
|
|
598
|
+
open && !form?.invalid && OPEN_EDGE,
|
|
599
|
+
states,
|
|
600
|
+
painted && PAINT_CONTROL_HIDDEN,
|
|
601
|
+
)}
|
|
602
|
+
inert={painted || form?.disabled || undefined}
|
|
603
|
+
aria-disabled={ended || undefined}
|
|
604
|
+
>
|
|
605
|
+
{chosen.length > 0 && !folded ? (
|
|
606
|
+
<div
|
|
607
|
+
ref={run}
|
|
608
|
+
className={cn(
|
|
609
|
+
CHIPS_RUN,
|
|
610
|
+
SEVERAL_RUN,
|
|
611
|
+
!fold && cn(WRAPS, CHIP_LINES),
|
|
612
|
+
)}
|
|
613
|
+
>
|
|
614
|
+
{chosen.map((option, at) => (
|
|
615
|
+
<Chip
|
|
616
|
+
key={String(option.value)}
|
|
617
|
+
family="neutral"
|
|
618
|
+
label={option.label}
|
|
619
|
+
onRemove={() => removeChip(picks, option, at)}
|
|
620
|
+
/>
|
|
621
|
+
))}
|
|
622
|
+
</div>
|
|
623
|
+
) : null}
|
|
624
|
+
<button
|
|
625
|
+
{...handed}
|
|
626
|
+
type="button"
|
|
627
|
+
aria-label={named}
|
|
628
|
+
className={cn(CHIPS_TRIGGER, SEVERAL_TRIGGER)}
|
|
629
|
+
>
|
|
630
|
+
{folded ? (
|
|
631
|
+
<span className={cn(FOLDED, CHIPS_RUN)}>
|
|
632
|
+
<Chip
|
|
633
|
+
family="neutral"
|
|
634
|
+
label={`${chosen.length} ${nounOf(label)}`}
|
|
635
|
+
/>
|
|
636
|
+
</span>
|
|
637
|
+
) : null}
|
|
638
|
+
{chosen.length === 0 ? (
|
|
639
|
+
<span
|
|
640
|
+
className={cn(
|
|
641
|
+
fieldValue({ kind: "text" }),
|
|
642
|
+
FIELD_PLACEHOLDER,
|
|
643
|
+
FIELD_VALUE,
|
|
644
|
+
)}
|
|
645
|
+
>
|
|
646
|
+
{hint ?? words.none}
|
|
647
|
+
</span>
|
|
648
|
+
) : null}
|
|
649
|
+
<Turn name="ChevronDown" fit="control" turned={open} by="half" />
|
|
650
|
+
</button>
|
|
651
|
+
</div>
|
|
652
|
+
);
|
|
653
|
+
};
|
|
654
|
+
const stateful = leads.tone;
|
|
655
|
+
// A head's pill stands at the badge's height, the meta line; a row's is a
|
|
656
|
+
// chip's, its caption line between its hairlines.
|
|
657
|
+
const compact = !fact;
|
|
658
|
+
const pill = (handed: Handed) => (
|
|
659
|
+
<button
|
|
660
|
+
{...handed}
|
|
661
|
+
type="button"
|
|
662
|
+
aria-label={named}
|
|
663
|
+
tabIndex={given.tabIndex ?? handed.tabIndex}
|
|
664
|
+
disabled={form?.disabled}
|
|
665
|
+
inert={painted || undefined}
|
|
666
|
+
aria-disabled={ended || undefined}
|
|
667
|
+
className={cn(compact ? ITEM_PILL_HIT : ITEM_FACT_HIT, PILL_HIT)}
|
|
668
|
+
>
|
|
669
|
+
<span
|
|
670
|
+
className={cn(
|
|
671
|
+
text({ role: "caption" }),
|
|
672
|
+
stateful
|
|
673
|
+
? cn(
|
|
674
|
+
stateChip({ tone: stateful }),
|
|
675
|
+
ITEM_PILL_STATE_ACT,
|
|
676
|
+
open && PILL_STATE_OPEN,
|
|
677
|
+
)
|
|
678
|
+
: cn(ITEM_PILL, ITEM_PILL_ACT, open && PILL_OPEN),
|
|
679
|
+
fact && ITEM_PILL_HEAD,
|
|
680
|
+
PILL_BOX,
|
|
681
|
+
painted && PAINT_CONTROL_HIDDEN,
|
|
682
|
+
)}
|
|
683
|
+
>
|
|
684
|
+
{stateful ? <StatusDot tone={stateful} /> : null}
|
|
685
|
+
{glyph}
|
|
686
|
+
<span
|
|
687
|
+
className={
|
|
688
|
+
stateful
|
|
689
|
+
? cn(STATE_CHIP_LABEL, PILL_STATE_VALUE)
|
|
690
|
+
: cn(PILL_VALUE, unset && FIELD_PLACEHOLDER)
|
|
691
|
+
}
|
|
692
|
+
>
|
|
693
|
+
{current?.label ?? hint}
|
|
694
|
+
</span>
|
|
695
|
+
<span className={stateful ? PILL_CHEVRON : ITEM_PILL_GLYPH}>
|
|
696
|
+
<Turn name="ChevronDown" fit="meta" turned={open} by="half" />
|
|
697
|
+
</span>
|
|
698
|
+
</span>
|
|
699
|
+
</button>
|
|
700
|
+
);
|
|
701
|
+
const button = (handed: Handed) => (
|
|
702
|
+
<button
|
|
703
|
+
{...handed}
|
|
704
|
+
type="button"
|
|
705
|
+
aria-label={named}
|
|
706
|
+
tabIndex={given.tabIndex ?? handed.tabIndex}
|
|
707
|
+
disabled={form?.disabled}
|
|
708
|
+
inert={painted || undefined}
|
|
709
|
+
aria-disabled={ended || undefined}
|
|
710
|
+
className={cn(
|
|
711
|
+
fit === "row"
|
|
712
|
+
? cn(WORD_ACT, picker({ fit }), ROW_TRIGGER, open && ROW_OPEN)
|
|
713
|
+
: cn(
|
|
714
|
+
fieldBox,
|
|
715
|
+
picker({ fit }),
|
|
716
|
+
fit !== "pill" && FIELD_GLYPH,
|
|
717
|
+
FIELD_TRIGGER,
|
|
718
|
+
quiet && !open && FIELD_QUIET,
|
|
719
|
+
open && FIELD_OPEN,
|
|
720
|
+
open && !form?.invalid && OPEN_EDGE,
|
|
721
|
+
fit === "pill" ? PICKER_PILL : states,
|
|
722
|
+
),
|
|
723
|
+
painted && PAINT_CONTROL_HIDDEN,
|
|
724
|
+
)}
|
|
725
|
+
>
|
|
726
|
+
{fit === "pill" && given.state ? <StatusDot tone={given.state} /> : null}
|
|
727
|
+
{glyph}
|
|
728
|
+
{fit === "row" ? rowValue : fieldShown}
|
|
729
|
+
{kind}
|
|
730
|
+
<Turn
|
|
731
|
+
name="ChevronDown"
|
|
732
|
+
fit={fit === "row" || fit === "pill" ? "meta" : "control"}
|
|
733
|
+
turned={open}
|
|
734
|
+
by="half"
|
|
735
|
+
/>
|
|
736
|
+
</button>
|
|
737
|
+
);
|
|
738
|
+
// The bar's pill wears its name as a tooltip on a pointer, as an icon act
|
|
739
|
+
// does, so a label its share of the bar cuts is read whole.
|
|
740
|
+
const own = (handed: Handed) =>
|
|
741
|
+
several ? (
|
|
742
|
+
severalBox(handed, several)
|
|
743
|
+
) : fit === "row" && (fact || stateful) ? (
|
|
744
|
+
pill(handed)
|
|
745
|
+
) : fit === "pill" ? (
|
|
746
|
+
<Named label={named}>{button(handed)}</Named>
|
|
747
|
+
) : (
|
|
748
|
+
button(handed)
|
|
749
|
+
);
|
|
750
|
+
const trigger = drawn ? (handed: Handed) => drawn(handed, open) : own;
|
|
751
|
+
const view = touch ? (
|
|
752
|
+
<PickSheet
|
|
753
|
+
label={label}
|
|
754
|
+
groups={groups}
|
|
755
|
+
value={value}
|
|
756
|
+
several={several}
|
|
757
|
+
searching={searching}
|
|
758
|
+
open={open}
|
|
759
|
+
setOpen={setOpen}
|
|
760
|
+
pick={pick}
|
|
761
|
+
trigger={trigger}
|
|
762
|
+
act={act}
|
|
763
|
+
acting={acting}
|
|
764
|
+
/>
|
|
765
|
+
) : searching ? (
|
|
766
|
+
<PickSearch
|
|
767
|
+
label={label}
|
|
768
|
+
groups={groups}
|
|
769
|
+
current={current}
|
|
770
|
+
several={several}
|
|
771
|
+
anchor={several ? box : undefined}
|
|
772
|
+
chosen={chosen}
|
|
773
|
+
open={open}
|
|
774
|
+
setOpen={setOpen}
|
|
775
|
+
pick={pick}
|
|
776
|
+
trigger={trigger}
|
|
777
|
+
act={act}
|
|
778
|
+
acting={acting}
|
|
779
|
+
/>
|
|
780
|
+
) : (
|
|
781
|
+
<PickList
|
|
782
|
+
label={label}
|
|
783
|
+
groups={groups}
|
|
784
|
+
value={value}
|
|
785
|
+
several={several}
|
|
786
|
+
anchor={several ? box : undefined}
|
|
787
|
+
open={open}
|
|
788
|
+
setOpen={setOpen}
|
|
789
|
+
pick={pick}
|
|
790
|
+
trigger={trigger}
|
|
791
|
+
act={act}
|
|
792
|
+
acting={acting}
|
|
793
|
+
/>
|
|
794
|
+
);
|
|
795
|
+
// A field's options that ended draw their sentence and Retry in the value column,
|
|
796
|
+
// in the trigger's place: a trigger over nothing to choose is a blank box, and the
|
|
797
|
+
// line under it left the label centred between the two.
|
|
798
|
+
const lone =
|
|
799
|
+
fit === "field" &&
|
|
800
|
+
form !== undefined &&
|
|
801
|
+
waiting.painted &&
|
|
802
|
+
isEnded(waiting.phase);
|
|
803
|
+
return (
|
|
804
|
+
<>
|
|
805
|
+
{lone ? null : view}
|
|
806
|
+
{/* A row's pick says nothing under itself: its box stands blocked, and the sheet that holds the row says the failure once (`PicksLine`). */}
|
|
807
|
+
{fit === "row" ? null : (
|
|
808
|
+
<OptionsLine
|
|
809
|
+
drawn={waiting}
|
|
810
|
+
retry={retry}
|
|
811
|
+
compact={fit === "pill"}
|
|
812
|
+
lone={lone}
|
|
813
|
+
/>
|
|
814
|
+
)}
|
|
815
|
+
</>
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/** A Picker that holds its own draft, as a bare one and a row's pick do; a rejection toasts. Outside the package's exports. */
|
|
820
|
+
export function HeldPicker<V extends string | null = string>(
|
|
821
|
+
props: Omit<PickOneProps<V>, "fit"> & Composed,
|
|
822
|
+
): ReactElement;
|
|
823
|
+
export function HeldPicker<V extends string | null = string>(
|
|
824
|
+
props: Omit<PickSeveralProps<V>, "fit"> & Composed,
|
|
825
|
+
): ReactElement;
|
|
826
|
+
export function HeldPicker<V extends string | null = string>(
|
|
827
|
+
props: Omit<PickerMenuProps<V>, "fit"> & Composed,
|
|
828
|
+
): ReactElement;
|
|
829
|
+
export function HeldPicker<V extends string | null = string>(
|
|
830
|
+
props: Omit<PickerMenuProps<V>, "fit"> & Composed,
|
|
831
|
+
) {
|
|
832
|
+
const { value, onChange, label, placeholder, options, fit, act } = props;
|
|
833
|
+
return (
|
|
834
|
+
<PickerBase<V>
|
|
835
|
+
draft={useHeldDraft<V | readonly V[] | undefined>(value)}
|
|
836
|
+
// The overloads pair a set's value with a set's `onChange`; the
|
|
837
|
+
// implementation's one signature cannot.
|
|
838
|
+
onChange={onChange as (next: V | V[]) => unknown}
|
|
839
|
+
label={label}
|
|
840
|
+
placeholder={placeholder}
|
|
841
|
+
options={options}
|
|
842
|
+
fit={fit}
|
|
843
|
+
act={act}
|
|
844
|
+
tabIndex={props.tabIndex}
|
|
845
|
+
painted={props.painted}
|
|
846
|
+
drawn={props.drawn}
|
|
847
|
+
fold={props.fold}
|
|
848
|
+
term={props.term}
|
|
849
|
+
state={props.state}
|
|
850
|
+
/>
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// Whether the keyboard moved the highlight last: a key on the trigger or in
|
|
855
|
+
// the popup (an Enter that opens it among them) sets it, the pointer clears it.
|
|
856
|
+
// A searchable list's keys land on its input, which keeps them from the popup:
|
|
857
|
+
// `landed` is the same for those keys, read by the blocked row alone (an
|
|
858
|
+
// enabled row there marks the key's position with the wash).
|
|
859
|
+
function useKeyed(trigger: PickParts<string | null>["trigger"]) {
|
|
860
|
+
const [keyed, setKeyed] = useState(false);
|
|
861
|
+
const [landed, setLanded] = useState(false);
|
|
862
|
+
const keys = () => setKeyed(true);
|
|
863
|
+
const pointer = () => {
|
|
864
|
+
setKeyed(false);
|
|
865
|
+
setLanded(false);
|
|
866
|
+
};
|
|
867
|
+
return {
|
|
868
|
+
keyed,
|
|
869
|
+
landed,
|
|
870
|
+
input: { onKeyDown: () => setLanded(true) },
|
|
871
|
+
trigger: (handed: Handed) =>
|
|
872
|
+
trigger({
|
|
873
|
+
...handed,
|
|
874
|
+
onKeyDown: (event) => {
|
|
875
|
+
keys();
|
|
876
|
+
handed.onKeyDown?.(event);
|
|
877
|
+
},
|
|
878
|
+
onPointerDown: (event) => {
|
|
879
|
+
pointer();
|
|
880
|
+
handed.onPointerDown?.(event);
|
|
881
|
+
},
|
|
882
|
+
}),
|
|
883
|
+
popup: { onKeyDown: keys, onPointerMove: pointer },
|
|
884
|
+
// Focus leaving the options for the act takes the keyboard's ring with it.
|
|
885
|
+
leave: pointer,
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
const optionFocus = (highlighted: boolean, keyed: boolean) =>
|
|
890
|
+
highlighted && keyed ? KEYBOARD_RING : OPTION_NO_FOCUS;
|
|
891
|
+
|
|
892
|
+
interface PickParts<V extends string | null> {
|
|
893
|
+
label: string;
|
|
894
|
+
groups: readonly Grouped<V>[];
|
|
895
|
+
open: boolean;
|
|
896
|
+
setOpen: (open: boolean) => void;
|
|
897
|
+
pick: (value: V) => void;
|
|
898
|
+
trigger: (props: Handed) => ReactElement;
|
|
899
|
+
act?: IconAct | IconLinkAct;
|
|
900
|
+
acting: Acting<true>;
|
|
901
|
+
// A pick of several: its chosen values and what hears their new set.
|
|
902
|
+
several?: Several<V>;
|
|
903
|
+
// The box the list opens against and takes its width from, where the trigger
|
|
904
|
+
// is only the part of it that opens the list (a pick of several's chevron).
|
|
905
|
+
anchor?: RefObject<HTMLElement | null>;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// The desktop list of six options or fewer: Base UI's select supplies the
|
|
909
|
+
// listbox, its keyboard and its typeahead.
|
|
910
|
+
function PickList<V extends string | null>(
|
|
911
|
+
props: PickParts<V> & { value: V | undefined },
|
|
912
|
+
) {
|
|
913
|
+
const container = use(PortalContainer);
|
|
914
|
+
useQuietGuards();
|
|
915
|
+
const words = useWords();
|
|
916
|
+
const keyboard = useKeyed(props.trigger);
|
|
917
|
+
return (
|
|
918
|
+
<Select.Root
|
|
919
|
+
// Not modal: a modal select locks the page's scroll, which changes the
|
|
920
|
+
// root's overflow and gutter and moves the page as the list opens.
|
|
921
|
+
modal={false}
|
|
922
|
+
multiple={props.several !== undefined}
|
|
923
|
+
value={props.several ? [...props.several.value] : (props.value ?? null)}
|
|
924
|
+
// Base UI types `next` by `multiple`, which is decided at run time.
|
|
925
|
+
onValueChange={(next) => {
|
|
926
|
+
if (props.several) props.several.onChange(next as V[]);
|
|
927
|
+
else props.pick(next as V);
|
|
928
|
+
}}
|
|
929
|
+
open={props.open}
|
|
930
|
+
onOpenChange={props.setOpen}
|
|
931
|
+
>
|
|
932
|
+
<Select.Trigger render={(handed) => keyboard.trigger(handed)} />
|
|
933
|
+
<Select.Portal container={container}>
|
|
934
|
+
<Select.Positioner
|
|
935
|
+
className={POSITIONER}
|
|
936
|
+
anchor={props.anchor}
|
|
937
|
+
align="start"
|
|
938
|
+
alignItemWithTrigger={false}
|
|
939
|
+
sideOffset={() => spacing("gap")}
|
|
940
|
+
>
|
|
941
|
+
<Select.Popup
|
|
942
|
+
onKeyDown={keyboard.popup.onKeyDown}
|
|
943
|
+
onPointerMove={keyboard.popup.onPointerMove}
|
|
944
|
+
className={cn(POPOVER, LAYER_POPOVER, PICKER_POPOVER, POPUP)}
|
|
945
|
+
>
|
|
946
|
+
<Select.List aria-label={props.label} className={LIST}>
|
|
947
|
+
{props.groups.map((group, at) => (
|
|
948
|
+
<Select.Group key={group.label ?? at} className={GROUP}>
|
|
949
|
+
{group.label ? (
|
|
950
|
+
<Select.GroupLabel
|
|
951
|
+
render={<GroupLabel>{group.label}</GroupLabel>}
|
|
952
|
+
/>
|
|
953
|
+
) : null}
|
|
954
|
+
{group.options.map((option) => {
|
|
955
|
+
const chosen = inValue(
|
|
956
|
+
props.several,
|
|
957
|
+
props.value,
|
|
958
|
+
option.value,
|
|
959
|
+
);
|
|
960
|
+
const blocked = optionBlocked(option, chosen);
|
|
961
|
+
return (
|
|
962
|
+
<Select.Item
|
|
963
|
+
key={String(option.value)}
|
|
964
|
+
value={option.value}
|
|
965
|
+
label={option.label}
|
|
966
|
+
disabled={blocked !== undefined}
|
|
967
|
+
className={(state) =>
|
|
968
|
+
cn(
|
|
969
|
+
optionLook(blocked, state.highlighted),
|
|
970
|
+
optionFocus(state.highlighted, keyboard.keyed),
|
|
971
|
+
)
|
|
972
|
+
}
|
|
973
|
+
>
|
|
974
|
+
<OptionText option={option} chosen={chosen} />
|
|
975
|
+
<Select.ItemIndicator className={TICK}>
|
|
976
|
+
<Icon name="Check" fit="body" />
|
|
977
|
+
</Select.ItemIndicator>
|
|
978
|
+
</Select.Item>
|
|
979
|
+
);
|
|
980
|
+
})}
|
|
981
|
+
</Select.Group>
|
|
982
|
+
))}
|
|
983
|
+
</Select.List>
|
|
984
|
+
{props.groups.every((group) => group.options.length === 0) ? (
|
|
985
|
+
<NoMatches>{words.noOptions}</NoMatches>
|
|
986
|
+
) : null}
|
|
987
|
+
{props.act ? (
|
|
988
|
+
<PickAct
|
|
989
|
+
act={props.act}
|
|
990
|
+
acting={props.acting}
|
|
991
|
+
done={() => props.setOpen(false)}
|
|
992
|
+
onFocus={keyboard.leave}
|
|
993
|
+
/>
|
|
994
|
+
) : null}
|
|
995
|
+
</Select.Popup>
|
|
996
|
+
</Select.Positioner>
|
|
997
|
+
</Select.Portal>
|
|
998
|
+
</Select.Root>
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
// One option of the search list, on the row its place gives: the label over
|
|
1003
|
+
// its line, the tick on the chosen one. `index` is the option's place in
|
|
1004
|
+
// the filtered list when the list is virtual, which Base UI cannot read off a
|
|
1005
|
+
// DOM that holds only a window of it.
|
|
1006
|
+
interface SearchItemProps<V extends string | null> {
|
|
1007
|
+
option: Option<V>;
|
|
1008
|
+
index?: number;
|
|
1009
|
+
chosen: boolean;
|
|
1010
|
+
/** The key moved the highlight last (a blocked row marks it by the key landing on it). */
|
|
1011
|
+
keyed: boolean;
|
|
1012
|
+
landed: boolean;
|
|
1013
|
+
/** The option's place among all the filtered ones (`place` of `size`, from 1), when the list is virtual. */
|
|
1014
|
+
size?: number;
|
|
1015
|
+
place?: number;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
function SearchItemBase<V extends string | null>(props: SearchItemProps<V>) {
|
|
1019
|
+
const { option, chosen } = props;
|
|
1020
|
+
const blocked = optionBlocked(option, chosen);
|
|
1021
|
+
return (
|
|
1022
|
+
<Combobox.Item
|
|
1023
|
+
value={option}
|
|
1024
|
+
index={props.index}
|
|
1025
|
+
data-index={props.index}
|
|
1026
|
+
aria-setsize={props.size}
|
|
1027
|
+
aria-posinset={props.place}
|
|
1028
|
+
disabled={blocked !== undefined}
|
|
1029
|
+
className={(state) =>
|
|
1030
|
+
cn(
|
|
1031
|
+
optionLook(blocked, state.highlighted),
|
|
1032
|
+
// A blocked row has no wash to mark the key's position, so the key landing on it rings it.
|
|
1033
|
+
optionFocus(
|
|
1034
|
+
state.highlighted,
|
|
1035
|
+
blocked === undefined ? props.keyed : props.landed,
|
|
1036
|
+
),
|
|
1037
|
+
)
|
|
1038
|
+
}
|
|
1039
|
+
>
|
|
1040
|
+
<OptionText option={option} chosen={chosen} />
|
|
1041
|
+
<Combobox.ItemIndicator className={TICK}>
|
|
1042
|
+
<Icon name="Check" fit="body" />
|
|
1043
|
+
</Combobox.ItemIndicator>
|
|
1044
|
+
</Combobox.Item>
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// `memo` returns a component without the type parameter it wraps.
|
|
1049
|
+
const SearchItem = memo(SearchItemBase) as typeof SearchItemBase;
|
|
1050
|
+
|
|
1051
|
+
// The rows of a long search list: the filtered options, a group's label ahead
|
|
1052
|
+
// of its first match, in a box the list's scroller holds. Only the rows near
|
|
1053
|
+
// the scroller's view stand, and the highlighted one stays mounted and is
|
|
1054
|
+
// brought into view as a key moves it, so the keys reach every option.
|
|
1055
|
+
function SearchRows<V extends string | null>(props: {
|
|
1056
|
+
groups: readonly Grouped<V>[];
|
|
1057
|
+
/** The option Base UI highlighted last, and whether to bring it into view. */
|
|
1058
|
+
highlight: { index: number; reveal: boolean } | undefined;
|
|
1059
|
+
several?: Several<V>;
|
|
1060
|
+
current: V | undefined;
|
|
1061
|
+
keyed: boolean;
|
|
1062
|
+
landed: boolean;
|
|
1063
|
+
}) {
|
|
1064
|
+
const matches = Combobox.useFilteredItems<Option<V>>();
|
|
1065
|
+
const box = useRef<HTMLDivElement>(null);
|
|
1066
|
+
const rows = useMemo(() => {
|
|
1067
|
+
const kept = new Set(matches.map((option) => option.value));
|
|
1068
|
+
return optionRows(
|
|
1069
|
+
props.groups.map((group) => ({
|
|
1070
|
+
label: group.label,
|
|
1071
|
+
options: group.options.filter((option) => kept.has(option.value)),
|
|
1072
|
+
})),
|
|
1073
|
+
);
|
|
1074
|
+
}, [matches, props.groups]);
|
|
1075
|
+
const { highlight } = props;
|
|
1076
|
+
const lit = highlight
|
|
1077
|
+
? rows.findIndex((row) => "option" in row && row.index === highlight.index)
|
|
1078
|
+
: -1;
|
|
1079
|
+
useLayoutEffect(() => {
|
|
1080
|
+
if (!highlight?.reveal) return;
|
|
1081
|
+
box.current
|
|
1082
|
+
?.querySelector(`[data-index="${highlight.index}"]`)
|
|
1083
|
+
?.scrollIntoView({ block: "nearest" });
|
|
1084
|
+
}, [highlight]);
|
|
1085
|
+
return (
|
|
1086
|
+
<div ref={box} className={GROUP}>
|
|
1087
|
+
<Window
|
|
1088
|
+
count={rows.length}
|
|
1089
|
+
keyOf={(at) => {
|
|
1090
|
+
// `Window` hands only indexes under its `count`, the list's length.
|
|
1091
|
+
const row = rows[at] as OptionRow<V>;
|
|
1092
|
+
return "option" in row ? String(row.option.value) : row.label;
|
|
1093
|
+
}}
|
|
1094
|
+
row={(at) => {
|
|
1095
|
+
// `Window` hands only indexes under its `count`, the list's length.
|
|
1096
|
+
const row = rows[at] as OptionRow<V>;
|
|
1097
|
+
if (!("option" in row))
|
|
1098
|
+
return <GroupLabel key={row.label}>{row.label}</GroupLabel>;
|
|
1099
|
+
const { option } = row;
|
|
1100
|
+
return (
|
|
1101
|
+
<SearchItem
|
|
1102
|
+
key={String(option.value)}
|
|
1103
|
+
option={option}
|
|
1104
|
+
index={row.index}
|
|
1105
|
+
size={matches.length}
|
|
1106
|
+
place={row.index + 1}
|
|
1107
|
+
chosen={inValue(props.several, props.current, option.value)}
|
|
1108
|
+
keyed={props.keyed}
|
|
1109
|
+
landed={props.landed}
|
|
1110
|
+
/>
|
|
1111
|
+
);
|
|
1112
|
+
}}
|
|
1113
|
+
box={box}
|
|
1114
|
+
keep={lit < 0 ? undefined : lit}
|
|
1115
|
+
/>
|
|
1116
|
+
</div>
|
|
1117
|
+
);
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
// A group as Base UI's combobox reads one: its options under `items`. A group
|
|
1121
|
+
// without them reads as one item, so the list holds one item per group and
|
|
1122
|
+
// drops any highlight past the first.
|
|
1123
|
+
interface SearchGroup<V extends string | null> {
|
|
1124
|
+
label?: string;
|
|
1125
|
+
items: readonly Option<V>[];
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// The desktop list past six options: Base UI's combobox filters the rows by
|
|
1129
|
+
// the search typed at the popover's head. From `VIRTUAL_FROM` options the
|
|
1130
|
+
// list is virtual: Base UI keeps the order and the highlight of every option,
|
|
1131
|
+
// and only a window of them is drawn.
|
|
1132
|
+
function PickSearch<V extends string | null>(
|
|
1133
|
+
props: PickParts<V> & {
|
|
1134
|
+
current: Option<V> | undefined;
|
|
1135
|
+
chosen: Option<V>[];
|
|
1136
|
+
},
|
|
1137
|
+
) {
|
|
1138
|
+
const container = use(PortalContainer);
|
|
1139
|
+
useQuietGuards();
|
|
1140
|
+
const words = useWords();
|
|
1141
|
+
const keyboard = useKeyed(props.trigger);
|
|
1142
|
+
const flat = props.groups.flatMap((group) => group.options);
|
|
1143
|
+
const virtual = flat.length >= VIRTUAL_FROM;
|
|
1144
|
+
const grouped = props.groups.map(
|
|
1145
|
+
(group): SearchGroup<V> => ({ label: group.label, items: group.options }),
|
|
1146
|
+
);
|
|
1147
|
+
const scroll = useScroller();
|
|
1148
|
+
const [highlight, setHighlight] = useState<{
|
|
1149
|
+
index: number;
|
|
1150
|
+
reveal: boolean;
|
|
1151
|
+
}>();
|
|
1152
|
+
return (
|
|
1153
|
+
<Combobox.Root
|
|
1154
|
+
modal={false}
|
|
1155
|
+
items={virtual ? flat : grouped}
|
|
1156
|
+
virtualized={virtual}
|
|
1157
|
+
onItemHighlighted={
|
|
1158
|
+
virtual
|
|
1159
|
+
? (item, details) =>
|
|
1160
|
+
setHighlight(
|
|
1161
|
+
item
|
|
1162
|
+
? {
|
|
1163
|
+
index: details.index,
|
|
1164
|
+
reveal: details.reason !== "pointer",
|
|
1165
|
+
}
|
|
1166
|
+
: undefined,
|
|
1167
|
+
)
|
|
1168
|
+
: undefined
|
|
1169
|
+
}
|
|
1170
|
+
autoHighlight
|
|
1171
|
+
multiple={props.several !== undefined}
|
|
1172
|
+
value={props.several ? props.chosen : (props.current ?? null)}
|
|
1173
|
+
// Base UI types `next` by `multiple`, which is decided at run time.
|
|
1174
|
+
onValueChange={(next) => {
|
|
1175
|
+
if (props.several)
|
|
1176
|
+
props.several.onChange((next as Option<V>[]).map((one) => one.value));
|
|
1177
|
+
else if (next) props.pick((next as Option<V>).value);
|
|
1178
|
+
}}
|
|
1179
|
+
// The search stays typed while several are picked from one query.
|
|
1180
|
+
onInputValueChange={(_, details) => {
|
|
1181
|
+
if (props.several && details.isItemPress) details.cancel();
|
|
1182
|
+
}}
|
|
1183
|
+
open={props.open}
|
|
1184
|
+
onOpenChange={props.setOpen}
|
|
1185
|
+
>
|
|
1186
|
+
<Combobox.Trigger render={(handed) => keyboard.trigger(handed)} />
|
|
1187
|
+
<Combobox.Portal container={container}>
|
|
1188
|
+
<Combobox.Positioner
|
|
1189
|
+
className={POSITIONER}
|
|
1190
|
+
anchor={props.anchor}
|
|
1191
|
+
align="start"
|
|
1192
|
+
sideOffset={() => spacing("gap")}
|
|
1193
|
+
>
|
|
1194
|
+
<Combobox.Popup
|
|
1195
|
+
onKeyDown={keyboard.popup.onKeyDown}
|
|
1196
|
+
onPointerMove={keyboard.popup.onPointerMove}
|
|
1197
|
+
aria-label={props.label}
|
|
1198
|
+
className={cn(POPOVER, LAYER_POPOVER, PICKER_POPOVER, POPUP)}
|
|
1199
|
+
>
|
|
1200
|
+
<div className={cn(field({}), FIELD_GLYPH, SEARCH)}>
|
|
1201
|
+
<Icon name="Search" fit="control" />
|
|
1202
|
+
<Combobox.Input
|
|
1203
|
+
onKeyDown={keyboard.input.onKeyDown}
|
|
1204
|
+
placeholder={words.search}
|
|
1205
|
+
aria-label={words.search}
|
|
1206
|
+
className={cn(fieldValue({ kind: "search" }), SEARCH_VALUE)}
|
|
1207
|
+
/>
|
|
1208
|
+
</div>
|
|
1209
|
+
{virtual ? (
|
|
1210
|
+
<Combobox.List ref={scroll.ref} className={LIST}>
|
|
1211
|
+
<Scroller value={scroll}>
|
|
1212
|
+
<SearchRows<V>
|
|
1213
|
+
groups={props.groups}
|
|
1214
|
+
highlight={highlight}
|
|
1215
|
+
several={props.several}
|
|
1216
|
+
current={props.current?.value}
|
|
1217
|
+
keyed={keyboard.keyed}
|
|
1218
|
+
landed={keyboard.landed}
|
|
1219
|
+
/>
|
|
1220
|
+
</Scroller>
|
|
1221
|
+
</Combobox.List>
|
|
1222
|
+
) : (
|
|
1223
|
+
<Combobox.List className={LIST}>
|
|
1224
|
+
{(group: SearchGroup<V>) => (
|
|
1225
|
+
<Combobox.Group
|
|
1226
|
+
key={group.label ?? ""}
|
|
1227
|
+
items={group.items}
|
|
1228
|
+
className={GROUP}
|
|
1229
|
+
>
|
|
1230
|
+
{group.label ? (
|
|
1231
|
+
<Combobox.GroupLabel
|
|
1232
|
+
render={<GroupLabel>{group.label}</GroupLabel>}
|
|
1233
|
+
/>
|
|
1234
|
+
) : null}
|
|
1235
|
+
<Combobox.Collection>
|
|
1236
|
+
{(option: Option<V>) => (
|
|
1237
|
+
<SearchItem
|
|
1238
|
+
key={String(option.value)}
|
|
1239
|
+
option={option}
|
|
1240
|
+
chosen={inValue(
|
|
1241
|
+
props.several,
|
|
1242
|
+
props.current?.value,
|
|
1243
|
+
option.value,
|
|
1244
|
+
)}
|
|
1245
|
+
keyed={keyboard.keyed}
|
|
1246
|
+
landed={keyboard.landed}
|
|
1247
|
+
/>
|
|
1248
|
+
)}
|
|
1249
|
+
</Combobox.Collection>
|
|
1250
|
+
</Combobox.Group>
|
|
1251
|
+
)}
|
|
1252
|
+
</Combobox.List>
|
|
1253
|
+
)}
|
|
1254
|
+
<Combobox.Empty>
|
|
1255
|
+
<NoMatches>{words.noMatches}</NoMatches>
|
|
1256
|
+
</Combobox.Empty>
|
|
1257
|
+
{props.act ? (
|
|
1258
|
+
<PickAct
|
|
1259
|
+
act={props.act}
|
|
1260
|
+
acting={props.acting}
|
|
1261
|
+
done={() => props.setOpen(false)}
|
|
1262
|
+
onFocus={keyboard.leave}
|
|
1263
|
+
/>
|
|
1264
|
+
) : null}
|
|
1265
|
+
</Combobox.Popup>
|
|
1266
|
+
</Combobox.Positioner>
|
|
1267
|
+
</Combobox.Portal>
|
|
1268
|
+
</Combobox.Root>
|
|
1269
|
+
);
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
type SheetParts<V extends string | null> = PickParts<V> & {
|
|
1273
|
+
value: V | undefined;
|
|
1274
|
+
searching: boolean;
|
|
1275
|
+
};
|
|
1276
|
+
|
|
1277
|
+
// The touch sheet: the rows edge to edge under the sheet's head, a search
|
|
1278
|
+
// leading them past six options, which stands the sheet full height. It opens
|
|
1279
|
+
// focused on the options' tab stop.
|
|
1280
|
+
function PickSheet<V extends string | null>(props: SheetParts<V>) {
|
|
1281
|
+
const [sheet] = useState(() => Dialog.createHandle<unknown>());
|
|
1282
|
+
const first = useRef<HTMLButtonElement>(null);
|
|
1283
|
+
return (
|
|
1284
|
+
<>
|
|
1285
|
+
<Dialog.Trigger
|
|
1286
|
+
handle={sheet}
|
|
1287
|
+
render={(handed) => props.trigger(handed)}
|
|
1288
|
+
/>
|
|
1289
|
+
<SheetLayer
|
|
1290
|
+
tall={props.searching}
|
|
1291
|
+
handle={sheet}
|
|
1292
|
+
open={props.open}
|
|
1293
|
+
onOpen={() => props.setOpen(true)}
|
|
1294
|
+
onClose={() => props.setOpen(false)}
|
|
1295
|
+
focus={first}
|
|
1296
|
+
>
|
|
1297
|
+
<SheetHead title={props.label} onClose={() => props.setOpen(false)} />
|
|
1298
|
+
<PickRows
|
|
1299
|
+
label={props.label}
|
|
1300
|
+
groups={props.groups}
|
|
1301
|
+
value={props.value}
|
|
1302
|
+
several={props.several}
|
|
1303
|
+
searching={props.searching}
|
|
1304
|
+
setOpen={props.setOpen}
|
|
1305
|
+
pick={props.pick}
|
|
1306
|
+
act={props.act}
|
|
1307
|
+
acting={props.acting}
|
|
1308
|
+
first={first}
|
|
1309
|
+
/>
|
|
1310
|
+
</SheetLayer>
|
|
1311
|
+
</>
|
|
1312
|
+
);
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
// One option of the sheet: a button that is a row of the listbox. It reads its
|
|
1316
|
+
// place in its group (`place` of `size`, from 1), which the rows a window
|
|
1317
|
+
// leaves unmounted do not tell, and where it stands among all the options
|
|
1318
|
+
// (`flat`, which the keys move by).
|
|
1319
|
+
interface PickOptionProps<V extends string | null> {
|
|
1320
|
+
option: Option<V>;
|
|
1321
|
+
place: number;
|
|
1322
|
+
size: number;
|
|
1323
|
+
flat: number;
|
|
1324
|
+
chosen: boolean;
|
|
1325
|
+
/** The option holds the listbox's one tab stop. */
|
|
1326
|
+
stands: boolean;
|
|
1327
|
+
/** What the sheet takes focus on as it opens: the tab stop's button. */
|
|
1328
|
+
first: RefObject<HTMLButtonElement | null>;
|
|
1329
|
+
pick: (value: V) => void;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function PickOptionBase<V extends string | null>(props: PickOptionProps<V>) {
|
|
1333
|
+
const { option, chosen, stands } = props;
|
|
1334
|
+
const blocked = optionBlocked(option, chosen);
|
|
1335
|
+
return (
|
|
1336
|
+
<button
|
|
1337
|
+
ref={stands ? props.first : undefined}
|
|
1338
|
+
type="button"
|
|
1339
|
+
role="option"
|
|
1340
|
+
aria-selected={chosen}
|
|
1341
|
+
aria-disabled={blocked !== undefined || undefined}
|
|
1342
|
+
aria-setsize={props.size}
|
|
1343
|
+
aria-posinset={props.place + 1}
|
|
1344
|
+
data-index={props.flat}
|
|
1345
|
+
tabIndex={stands ? 0 : -1}
|
|
1346
|
+
onClick={() => {
|
|
1347
|
+
if (blocked === undefined) props.pick(option.value);
|
|
1348
|
+
}}
|
|
1349
|
+
className={cn(
|
|
1350
|
+
optionLook(blocked),
|
|
1351
|
+
OPTION_BUTTON,
|
|
1352
|
+
blocked === undefined && OPTION_FOCUS_WASH,
|
|
1353
|
+
)}
|
|
1354
|
+
>
|
|
1355
|
+
<OptionText option={option} chosen={chosen} />
|
|
1356
|
+
{chosen ? (
|
|
1357
|
+
<span className={TICK}>
|
|
1358
|
+
<Icon name="Check" fit="body" />
|
|
1359
|
+
</span>
|
|
1360
|
+
) : null}
|
|
1361
|
+
</button>
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
// `memo` returns a component without the type parameter it wraps.
|
|
1366
|
+
const PickOption = memo(PickOptionBase) as typeof PickOptionBase;
|
|
1367
|
+
|
|
1368
|
+
// One group of the sheet's options: its label over its rows, which stand in a
|
|
1369
|
+
// box of their own so a long group windows them, the tab stop's row kept
|
|
1370
|
+
// mounted.
|
|
1371
|
+
function PickGroupBox<V extends string | null>(props: {
|
|
1372
|
+
group: Grouped<V>;
|
|
1373
|
+
/** How many options stand before this group's. */
|
|
1374
|
+
before: number;
|
|
1375
|
+
/** The option holding the tab stop. */
|
|
1376
|
+
stop: V | undefined;
|
|
1377
|
+
first: RefObject<HTMLButtonElement | null>;
|
|
1378
|
+
several?: Several<V>;
|
|
1379
|
+
value: V | undefined;
|
|
1380
|
+
pick: (value: V) => void;
|
|
1381
|
+
}) {
|
|
1382
|
+
const { group, before, stop } = props;
|
|
1383
|
+
const box = useRef<HTMLDivElement>(null);
|
|
1384
|
+
const { options: items } = group;
|
|
1385
|
+
const kept = items.findIndex((option) => option.value === stop);
|
|
1386
|
+
return (
|
|
1387
|
+
// biome-ignore lint/a11y/useSemanticElements: a listbox's group of options
|
|
1388
|
+
<div role="group" className={GROUP}>
|
|
1389
|
+
{group.label ? <GroupLabel>{group.label}</GroupLabel> : null}
|
|
1390
|
+
<div ref={box} className={GROUP}>
|
|
1391
|
+
<Window
|
|
1392
|
+
count={items.length}
|
|
1393
|
+
keyOf={(place) => String(items[place]?.value)}
|
|
1394
|
+
row={(place) => {
|
|
1395
|
+
// `Window` hands only indexes under its `count`, the list's length.
|
|
1396
|
+
const option = items[place] as Option<V>;
|
|
1397
|
+
return (
|
|
1398
|
+
<PickOption
|
|
1399
|
+
key={String(option.value)}
|
|
1400
|
+
option={option}
|
|
1401
|
+
place={place}
|
|
1402
|
+
size={items.length}
|
|
1403
|
+
flat={before + place}
|
|
1404
|
+
chosen={inValue(props.several, props.value, option.value)}
|
|
1405
|
+
stands={place === kept}
|
|
1406
|
+
first={props.first}
|
|
1407
|
+
pick={props.pick}
|
|
1408
|
+
/>
|
|
1409
|
+
);
|
|
1410
|
+
}}
|
|
1411
|
+
box={box}
|
|
1412
|
+
keep={kept < 0 ? undefined : kept}
|
|
1413
|
+
/>
|
|
1414
|
+
</div>
|
|
1415
|
+
</div>
|
|
1416
|
+
);
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
// The sheet's rows hold the search, so it leaves with the sheet however it
|
|
1420
|
+
// closes (a pick, the act, the scrim, Escape) and the next open starts from
|
|
1421
|
+
// every option.
|
|
1422
|
+
function PickRows<V extends string | null>(
|
|
1423
|
+
props: Omit<SheetParts<V>, "open" | "trigger"> & {
|
|
1424
|
+
first: RefObject<HTMLButtonElement | null>;
|
|
1425
|
+
},
|
|
1426
|
+
) {
|
|
1427
|
+
const words = useWords();
|
|
1428
|
+
const [search, setSearch] = useState("");
|
|
1429
|
+
const typed = search.trim().toLowerCase();
|
|
1430
|
+
const shown = props.groups
|
|
1431
|
+
.map((group) => ({
|
|
1432
|
+
...group,
|
|
1433
|
+
options: group.options.filter((option) =>
|
|
1434
|
+
option.label.toLowerCase().includes(typed),
|
|
1435
|
+
),
|
|
1436
|
+
}))
|
|
1437
|
+
.filter((group) => group.options.length > 0);
|
|
1438
|
+
// The options are one tab stop that the arrows move: it starts on the chosen
|
|
1439
|
+
// one, else the first that can be chosen, and follows focus.
|
|
1440
|
+
const listed = shown.flatMap((group) => group.options);
|
|
1441
|
+
const isChosen = (one: V) => inValue(props.several, props.value, one);
|
|
1442
|
+
const [active, setActive] = useState<V>();
|
|
1443
|
+
const kept = listed.find((option) => isChosen(option.value));
|
|
1444
|
+
const free = listed.find(
|
|
1445
|
+
(option) => optionBlocked(option, false) === undefined,
|
|
1446
|
+
);
|
|
1447
|
+
const stop = (
|
|
1448
|
+
listed.find((option) => option.value === active) ??
|
|
1449
|
+
kept ??
|
|
1450
|
+
free ??
|
|
1451
|
+
listed[0]
|
|
1452
|
+
)?.value;
|
|
1453
|
+
// The options are a window's rows, so a press hears the latest pick through
|
|
1454
|
+
// one function and no row renders for another's.
|
|
1455
|
+
const latest = useRef(props.pick);
|
|
1456
|
+
latest.current = props.pick;
|
|
1457
|
+
const [pick] = useState(() => (value: V) => latest.current(value));
|
|
1458
|
+
// The option a key is moving the focus to; it stands mounted by then, the
|
|
1459
|
+
// window keeping the tab stop's row, and takes the focus.
|
|
1460
|
+
const scroll = useScroller();
|
|
1461
|
+
const [wanted, setWanted] = useState<{ index: number }>();
|
|
1462
|
+
useLayoutEffect(() => {
|
|
1463
|
+
if (!wanted) return;
|
|
1464
|
+
scroll.holder.current
|
|
1465
|
+
?.querySelector<HTMLElement>(`[data-index="${wanted.index}"]`)
|
|
1466
|
+
?.focus();
|
|
1467
|
+
}, [wanted]);
|
|
1468
|
+
const indexOf = (target: HTMLElement) =>
|
|
1469
|
+
Number(target.closest<HTMLElement>('[role="option"]')?.dataset.index);
|
|
1470
|
+
return (
|
|
1471
|
+
<div className={SHEET_ROWS}>
|
|
1472
|
+
{props.searching ? (
|
|
1473
|
+
<div className={SEARCH_REGION}>
|
|
1474
|
+
<Input
|
|
1475
|
+
label={words.search}
|
|
1476
|
+
kind="search"
|
|
1477
|
+
value={search}
|
|
1478
|
+
onChange={setSearch}
|
|
1479
|
+
/>
|
|
1480
|
+
</div>
|
|
1481
|
+
) : null}
|
|
1482
|
+
<div
|
|
1483
|
+
ref={scroll.ref}
|
|
1484
|
+
role="listbox"
|
|
1485
|
+
aria-multiselectable={props.several ? true : undefined}
|
|
1486
|
+
aria-label={props.label}
|
|
1487
|
+
onKeyDown={(event) => {
|
|
1488
|
+
const { target } = event;
|
|
1489
|
+
if (!(target instanceof HTMLElement)) return;
|
|
1490
|
+
if (target.getAttribute("role") !== "option") return;
|
|
1491
|
+
// The arrows wrap and Home and End go to the ends, over every
|
|
1492
|
+
// option, mounted or not.
|
|
1493
|
+
const at = indexOf(target);
|
|
1494
|
+
const last = listed.length - 1;
|
|
1495
|
+
const next =
|
|
1496
|
+
event.key === "ArrowDown"
|
|
1497
|
+
? (at + 1) % listed.length
|
|
1498
|
+
: event.key === "ArrowUp"
|
|
1499
|
+
? (at - 1 + listed.length) % listed.length
|
|
1500
|
+
: event.key === "Home"
|
|
1501
|
+
? 0
|
|
1502
|
+
: event.key === "End"
|
|
1503
|
+
? last
|
|
1504
|
+
: undefined;
|
|
1505
|
+
if (next === undefined) return;
|
|
1506
|
+
event.preventDefault();
|
|
1507
|
+
setActive(listed[next]?.value);
|
|
1508
|
+
setWanted({ index: next });
|
|
1509
|
+
}}
|
|
1510
|
+
onFocus={(event) => {
|
|
1511
|
+
const { target } = event;
|
|
1512
|
+
if (!(target instanceof HTMLElement)) return;
|
|
1513
|
+
if (target.getAttribute("role") !== "option") return;
|
|
1514
|
+
setActive(listed[indexOf(target)]?.value);
|
|
1515
|
+
}}
|
|
1516
|
+
className={LISTBOX}
|
|
1517
|
+
>
|
|
1518
|
+
<Scroller value={scroll}>
|
|
1519
|
+
{shown.map((group, at) => (
|
|
1520
|
+
<PickGroupBox<V>
|
|
1521
|
+
key={group.label ?? at}
|
|
1522
|
+
group={group}
|
|
1523
|
+
before={shown
|
|
1524
|
+
.slice(0, at)
|
|
1525
|
+
.reduce((sum, each) => sum + each.options.length, 0)}
|
|
1526
|
+
stop={stop}
|
|
1527
|
+
first={props.first}
|
|
1528
|
+
several={props.several}
|
|
1529
|
+
value={props.value}
|
|
1530
|
+
pick={pick}
|
|
1531
|
+
/>
|
|
1532
|
+
))}
|
|
1533
|
+
</Scroller>
|
|
1534
|
+
</div>
|
|
1535
|
+
{shown.length === 0 ? (
|
|
1536
|
+
<NoMatches>
|
|
1537
|
+
{props.groups.some((group) => group.options.length > 0)
|
|
1538
|
+
? words.noMatches
|
|
1539
|
+
: words.noOptions}
|
|
1540
|
+
</NoMatches>
|
|
1541
|
+
) : null}
|
|
1542
|
+
{props.act ? (
|
|
1543
|
+
<PickAct
|
|
1544
|
+
act={props.act}
|
|
1545
|
+
acting={props.acting}
|
|
1546
|
+
done={() => props.setOpen(false)}
|
|
1547
|
+
/>
|
|
1548
|
+
) : null}
|
|
1549
|
+
</div>
|
|
1550
|
+
);
|
|
1551
|
+
}
|