@charcuterie/ui 4.28.4 → 4.30.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/dist/CatalogPicker/CatalogPicker.d.ts +110 -0
- package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPicker.js +155 -0
- package/dist/CatalogPicker/CatalogPicker.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerMark.d.ts +29 -0
- package/dist/CatalogPicker/CatalogPickerMark.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerMark.js +35 -0
- package/dist/CatalogPicker/CatalogPickerMark.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerOption.d.ts +40 -0
- package/dist/CatalogPicker/CatalogPickerOption.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerOption.js +43 -0
- package/dist/CatalogPicker/CatalogPickerOption.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerPanel.d.ts +60 -0
- package/dist/CatalogPicker/CatalogPickerPanel.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerPanel.js +409 -0
- package/dist/CatalogPicker/CatalogPickerPanel.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerRail.d.ts +50 -0
- package/dist/CatalogPicker/CatalogPickerRail.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerRail.js +114 -0
- package/dist/CatalogPicker/CatalogPickerRail.js.map +1 -0
- package/dist/CatalogPicker/catalogPickerDrawings.d.ts +10 -0
- package/dist/CatalogPicker/catalogPickerDrawings.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerDrawings.js +29 -0
- package/dist/CatalogPicker/catalogPickerDrawings.js.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts +185 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.js +245 -0
- package/dist/CatalogPicker/catalogPickerView.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/CatalogPicker/CatalogPicker.mdx +192 -0
- package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
- package/src/CatalogPicker/CatalogPicker.test.tsx +1592 -0
- package/src/CatalogPicker/CatalogPicker.tsx +387 -0
- package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
- package/src/CatalogPicker/CatalogPickerOption.tsx +151 -0
- package/src/CatalogPicker/CatalogPickerPanel.tsx +780 -0
- package/src/CatalogPicker/CatalogPickerRail.tsx +214 -0
- package/src/CatalogPicker/catalogPicker.storyHelpers.tsx +276 -0
- package/src/CatalogPicker/catalogPickerDrawings.tsx +51 -0
- package/src/CatalogPicker/catalogPickerView.test.ts +365 -0
- package/src/CatalogPicker/catalogPickerView.ts +476 -0
- package/src/Dialog/Dialog.test.tsx +20 -289
- package/src/Field/Field.test.tsx +11 -405
- package/src/index.ts +6 -0
- package/src/sourceRules.test.ts +5 -1
|
@@ -0,0 +1,780 @@
|
|
|
1
|
+
import { useUniqueId } from "@charcuterie/logic"
|
|
2
|
+
import type { ControlSize } from "@charcuterie/tokens"
|
|
3
|
+
import type {
|
|
4
|
+
KeyboardEvent as ReactKeyboardEvent,
|
|
5
|
+
ReactNode,
|
|
6
|
+
RefObject,
|
|
7
|
+
} from "react"
|
|
8
|
+
import {
|
|
9
|
+
useEffect,
|
|
10
|
+
useLayoutEffect,
|
|
11
|
+
useMemo,
|
|
12
|
+
useRef,
|
|
13
|
+
useState,
|
|
14
|
+
} from "react"
|
|
15
|
+
|
|
16
|
+
import { SearchInput } from "../SearchInput/SearchInput.tsx"
|
|
17
|
+
import { toClassName } from "../toClassName.ts"
|
|
18
|
+
import { CatalogPickerOption } from "./CatalogPickerOption.tsx"
|
|
19
|
+
import { CatalogPickerRail } from "./CatalogPickerRail.tsx"
|
|
20
|
+
import {
|
|
21
|
+
buildCatalogSections,
|
|
22
|
+
buildCatalogTabs,
|
|
23
|
+
type CatalogPickerGroup,
|
|
24
|
+
type CatalogPickerItem,
|
|
25
|
+
pickInitialTabKey,
|
|
26
|
+
toQueryWords,
|
|
27
|
+
} from "./catalogPickerView.ts"
|
|
28
|
+
|
|
29
|
+
export type CatalogPickerPanelProps = {
|
|
30
|
+
/** The glyph inside the search field's clear button. Without one there is no clear button; Escape clears. */
|
|
31
|
+
clearIcon?: ReactNode
|
|
32
|
+
/** The query the panel opens with. For the stories' boards; `CatalogPicker` always opens empty. */
|
|
33
|
+
defaultQuery?: string
|
|
34
|
+
emptyLabel: string
|
|
35
|
+
favorites: readonly string[]
|
|
36
|
+
favoritesLabel: string
|
|
37
|
+
groups: readonly CatalogPickerGroup[]
|
|
38
|
+
/** The search field, for the overlay's initial focus. */
|
|
39
|
+
inputRef?: RefObject<HTMLInputElement | null>
|
|
40
|
+
/** Already stepped down for a short window by `usePanelItemSize`. */
|
|
41
|
+
itemSize: ControlSize
|
|
42
|
+
items: readonly CatalogPickerItem[]
|
|
43
|
+
/** What the picker is for. Names the search field and the list, and (as "<label> groups") the rail. */
|
|
44
|
+
label: string
|
|
45
|
+
/** Escape on an empty query. The Escape is consumed before this runs, so no enclosing overlay sees it. */
|
|
46
|
+
onDismiss: () => void
|
|
47
|
+
onSelect: (value: string) => void
|
|
48
|
+
searchPlaceholder: string
|
|
49
|
+
value?: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const SEARCH_SIZE: Record<ControlSize, "lg" | "md"> = {
|
|
53
|
+
lg: "lg",
|
|
54
|
+
md: "md",
|
|
55
|
+
sm: "md",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const GROUP_HEADING_INSET_CLASS: Record<
|
|
59
|
+
ControlSize,
|
|
60
|
+
string
|
|
61
|
+
> = {
|
|
62
|
+
lg: "px-(--control-padding-inline-lg)",
|
|
63
|
+
md: "px-(--control-padding-inline-md)",
|
|
64
|
+
sm: "px-(--control-padding-inline-sm)",
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* How many frames the panel waits for its height cap before it gives
|
|
69
|
+
* up on centring the chosen row. `useAnchoredOverlay` writes the cap
|
|
70
|
+
* onto the element inside floating-ui's `size` middleware, a frame or
|
|
71
|
+
* two after mount, and until then the list has nothing to scroll.
|
|
72
|
+
*/
|
|
73
|
+
const SCROLL_ATTEMPTS = 5
|
|
74
|
+
|
|
75
|
+
const nextEnabledIndex = (
|
|
76
|
+
rows: readonly { item: CatalogPickerItem }[],
|
|
77
|
+
from: number,
|
|
78
|
+
step: number,
|
|
79
|
+
) => {
|
|
80
|
+
for (
|
|
81
|
+
let index = from + step;
|
|
82
|
+
index >= 0 && index < rows.length;
|
|
83
|
+
index += step
|
|
84
|
+
) {
|
|
85
|
+
if (rows[index]?.item.isDisabled !== true) {
|
|
86
|
+
return index
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return from
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The panel's contents: the rail, the search field, and the grouped
|
|
95
|
+
* listbox. `CatalogPicker` renders it inside its anchored overlay, and
|
|
96
|
+
* it mounts fresh on every open, so the tab, the query and the
|
|
97
|
+
* highlight all start from the chosen value each time.
|
|
98
|
+
*
|
|
99
|
+
* ### The ARIA shape
|
|
100
|
+
*
|
|
101
|
+
* - The **search field** is the `role="combobox"` and keeps focus,
|
|
102
|
+
* exactly as in `Combobox`: arrows move `aria-activedescendant`
|
|
103
|
+
* through the options, Enter chooses, Escape clears a query and then
|
|
104
|
+
* closes.
|
|
105
|
+
* - The **list** is one `role="listbox"`. Search results sit in
|
|
106
|
+
* labelled `role="group"`s, one per group, the APG grouped-listbox
|
|
107
|
+
* shape `CommandPalette` uses.
|
|
108
|
+
* - The **rail** is a vertical `role="toolbar"` of toggle buttons
|
|
109
|
+
* (`aria-pressed`). See `CatalogPickerRail` for why it is not a
|
|
110
|
+
* `tablist`.
|
|
111
|
+
*
|
|
112
|
+
* Tab and Shift+Tab cycle through the search field, its clear button
|
|
113
|
+
* while it shows, and the rail, and nowhere else: the panel is two tab
|
|
114
|
+
* stops (three with a query and a clear button) however long the rail
|
|
115
|
+
* is.
|
|
116
|
+
*
|
|
117
|
+
* ### The highlight follows a value, not a position
|
|
118
|
+
*
|
|
119
|
+
* The active row is remembered by its `value`, so a catalog that
|
|
120
|
+
* refreshes while the panel is open — rows reordered, rows added — keeps
|
|
121
|
+
* the same row highlighted, and Enter chooses what the person saw. Only
|
|
122
|
+
* when that row leaves the view, or becomes disabled, is the highlight
|
|
123
|
+
* reseeded: to the chosen row if the view shows it, else the first
|
|
124
|
+
* enabled row.
|
|
125
|
+
*/
|
|
126
|
+
export const CatalogPickerPanel = ({
|
|
127
|
+
clearIcon,
|
|
128
|
+
defaultQuery = "",
|
|
129
|
+
emptyLabel,
|
|
130
|
+
favorites,
|
|
131
|
+
favoritesLabel,
|
|
132
|
+
groups,
|
|
133
|
+
inputRef: externalInputRef,
|
|
134
|
+
itemSize,
|
|
135
|
+
items,
|
|
136
|
+
label,
|
|
137
|
+
onDismiss,
|
|
138
|
+
onSelect,
|
|
139
|
+
searchPlaceholder,
|
|
140
|
+
value,
|
|
141
|
+
}: CatalogPickerPanelProps): ReactNode => {
|
|
142
|
+
const listId = useUniqueId()
|
|
143
|
+
|
|
144
|
+
const ownInputRef = useRef<HTMLInputElement>(null)
|
|
145
|
+
|
|
146
|
+
const inputRef = externalInputRef ?? ownInputRef
|
|
147
|
+
|
|
148
|
+
const listRef = useRef<HTMLDivElement>(null)
|
|
149
|
+
|
|
150
|
+
const railRef = useRef<HTMLDivElement>(null)
|
|
151
|
+
|
|
152
|
+
const tabs = useMemo(
|
|
153
|
+
() =>
|
|
154
|
+
buildCatalogTabs({
|
|
155
|
+
favorites,
|
|
156
|
+
favoritesLabel,
|
|
157
|
+
groups,
|
|
158
|
+
items,
|
|
159
|
+
}),
|
|
160
|
+
[favorites, favoritesLabel, groups, items],
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
const [tabKey, setTabKey] = useState(() =>
|
|
164
|
+
pickInitialTabKey({ favorites, items, tabs, value }),
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
const [query, setQuery] = useState(defaultQuery)
|
|
168
|
+
|
|
169
|
+
const isSearching = toQueryWords(query).length > 0
|
|
170
|
+
|
|
171
|
+
// A tab that has since left the rail (its last row went away) falls
|
|
172
|
+
// back to the first one rather than browsing nothing.
|
|
173
|
+
const currentTab =
|
|
174
|
+
tabs.find((tab) => tab.key === tabKey) ?? tabs[0]
|
|
175
|
+
|
|
176
|
+
const sections = useMemo(
|
|
177
|
+
() =>
|
|
178
|
+
buildCatalogSections({
|
|
179
|
+
favorites,
|
|
180
|
+
groups,
|
|
181
|
+
items,
|
|
182
|
+
query,
|
|
183
|
+
tab: currentTab,
|
|
184
|
+
}),
|
|
185
|
+
[currentTab, favorites, groups, items, query],
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
const groupById = useMemo(
|
|
189
|
+
() =>
|
|
190
|
+
new Map(
|
|
191
|
+
groups.map((group) => [group.id, group] as const),
|
|
192
|
+
),
|
|
193
|
+
[groups],
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
const favoriteValues = useMemo(
|
|
197
|
+
() => new Set(favorites),
|
|
198
|
+
[favorites],
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
const indexedSections = useMemo(() => {
|
|
202
|
+
let index = 0
|
|
203
|
+
|
|
204
|
+
return sections.map((section, sectionIndex) => ({
|
|
205
|
+
...section,
|
|
206
|
+
// By position, never from the group's `id`: an `id` with a space
|
|
207
|
+
// in it would split the `aria-labelledby` reference in two.
|
|
208
|
+
headingId: `${listId}-heading-${sectionIndex}`,
|
|
209
|
+
rows: section.items.map((item) => ({
|
|
210
|
+
id: `${listId}-option-${index}`,
|
|
211
|
+
index: index++,
|
|
212
|
+
item,
|
|
213
|
+
})),
|
|
214
|
+
}))
|
|
215
|
+
}, [listId, sections])
|
|
216
|
+
|
|
217
|
+
const rows = indexedSections.flatMap(
|
|
218
|
+
(section) => section.rows,
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Where the highlight lands for a fresh view: the chosen row when the
|
|
223
|
+
* view shows it, else the first enabled row. A live query always
|
|
224
|
+
* takes the top match — the 2026-08-25 `Combobox` rule, a filter
|
|
225
|
+
* owns the highlight.
|
|
226
|
+
*/
|
|
227
|
+
const toSeedValue = (
|
|
228
|
+
viewRows: readonly { item: CatalogPickerItem }[],
|
|
229
|
+
isQueryLive: boolean,
|
|
230
|
+
) => {
|
|
231
|
+
if (!isQueryLive && value !== undefined) {
|
|
232
|
+
const chosenRow = viewRows.find(
|
|
233
|
+
(row) =>
|
|
234
|
+
row.item.value === value &&
|
|
235
|
+
row.item.isDisabled !== true,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
if (chosenRow !== undefined) {
|
|
239
|
+
return chosenRow.item.value
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return viewRows[nextEnabledIndex(viewRows, -1, 1)]?.item
|
|
244
|
+
.value
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const [activeValue, setActiveValue] = useState(() =>
|
|
248
|
+
toSeedValue(rows, isSearching),
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The scroll a fresh view still owes: centre the chosen row, or go
|
|
253
|
+
* back to the top. A new object per view change, so asking twice for
|
|
254
|
+
* the same row is never a no-op.
|
|
255
|
+
*/
|
|
256
|
+
const [pendingScroll, setPendingScroll] =
|
|
257
|
+
useState<null | {
|
|
258
|
+
attempt: number
|
|
259
|
+
isCentred: boolean
|
|
260
|
+
}>({ attempt: 0, isCentred: !isSearching })
|
|
261
|
+
|
|
262
|
+
const resolvedActiveIndex = rows.findIndex(
|
|
263
|
+
(row) =>
|
|
264
|
+
row.item.value === activeValue &&
|
|
265
|
+
row.item.isDisabled !== true,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
// The highlighted row left the view, or was disabled, under a
|
|
269
|
+
// catalog refresh: reseed it now, during this render, rather than let
|
|
270
|
+
// Enter fall on nothing or on whatever row took its place.
|
|
271
|
+
// A refresh is not a new query, so the chosen row comes first even
|
|
272
|
+
// while a search runs.
|
|
273
|
+
if (resolvedActiveIndex < 0) {
|
|
274
|
+
const seedValue = toSeedValue(rows, false)
|
|
275
|
+
|
|
276
|
+
if (seedValue !== activeValue) {
|
|
277
|
+
setActiveValue(seedValue)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const activeRow =
|
|
282
|
+
resolvedActiveIndex < 0
|
|
283
|
+
? undefined
|
|
284
|
+
: rows[resolvedActiveIndex]
|
|
285
|
+
|
|
286
|
+
const moveActive = (index: number) => {
|
|
287
|
+
const row = rows[index]
|
|
288
|
+
|
|
289
|
+
if (row !== undefined) {
|
|
290
|
+
setActiveValue(row.item.value)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const reseed = (
|
|
295
|
+
nextRows: readonly { item: CatalogPickerItem }[],
|
|
296
|
+
isQueryLive: boolean,
|
|
297
|
+
) => {
|
|
298
|
+
setActiveValue(toSeedValue(nextRows, isQueryLive))
|
|
299
|
+
|
|
300
|
+
setPendingScroll({
|
|
301
|
+
attempt: 0,
|
|
302
|
+
isCentred: !isQueryLive,
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const toRows = (nextQuery: string, nextTabKey?: string) =>
|
|
307
|
+
buildCatalogSections({
|
|
308
|
+
favorites,
|
|
309
|
+
groups,
|
|
310
|
+
items,
|
|
311
|
+
query: nextQuery,
|
|
312
|
+
tab:
|
|
313
|
+
tabs.find((tab) => tab.key === nextTabKey) ??
|
|
314
|
+
tabs[0],
|
|
315
|
+
}).flatMap((section) =>
|
|
316
|
+
section.items.map((item) => ({ item })),
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
const changeQuery = (nextQuery: string) => {
|
|
320
|
+
setQuery(nextQuery)
|
|
321
|
+
|
|
322
|
+
reseed(
|
|
323
|
+
toRows(nextQuery, tabKey),
|
|
324
|
+
toQueryWords(nextQuery).length > 0,
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const showTab = (nextTabKey: string) => {
|
|
329
|
+
setTabKey(nextTabKey)
|
|
330
|
+
|
|
331
|
+
// A tab is a request to see that group, so a search in progress
|
|
332
|
+
// gives way to it rather than leaving the click with no effect.
|
|
333
|
+
setQuery("")
|
|
334
|
+
|
|
335
|
+
reseed(toRows("", nextTabKey), false)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// The scroll lives in an effect because the list has to exist, and
|
|
339
|
+
// be capped, before it can move. Writing `scrollTop` on the list
|
|
340
|
+
// itself (rather than `scrollIntoView`) keeps the page behind the
|
|
341
|
+
// panel from scrolling along with it.
|
|
342
|
+
useEffect(() => {
|
|
343
|
+
if (pendingScroll === null) {
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const list = listRef.current
|
|
348
|
+
|
|
349
|
+
if (list === null) {
|
|
350
|
+
setPendingScroll(null)
|
|
351
|
+
|
|
352
|
+
return
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (!pendingScroll.isCentred) {
|
|
356
|
+
list.scrollTop = 0
|
|
357
|
+
|
|
358
|
+
setPendingScroll(null)
|
|
359
|
+
|
|
360
|
+
return
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const row =
|
|
364
|
+
activeRow === undefined
|
|
365
|
+
? null
|
|
366
|
+
: document.getElementById(activeRow.id)
|
|
367
|
+
|
|
368
|
+
if (row === null) {
|
|
369
|
+
list.scrollTop = 0
|
|
370
|
+
|
|
371
|
+
setPendingScroll(null)
|
|
372
|
+
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (
|
|
377
|
+
pendingScroll.attempt < SCROLL_ATTEMPTS &&
|
|
378
|
+
(pendingScroll.attempt === 0 ||
|
|
379
|
+
list.scrollHeight <= list.clientHeight)
|
|
380
|
+
) {
|
|
381
|
+
const frame = requestAnimationFrame(() => {
|
|
382
|
+
setPendingScroll({
|
|
383
|
+
attempt: pendingScroll.attempt + 1,
|
|
384
|
+
isCentred: true,
|
|
385
|
+
})
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
return () => {
|
|
389
|
+
cancelAnimationFrame(frame)
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const listBox = list.getBoundingClientRect()
|
|
394
|
+
const rowBox = row.getBoundingClientRect()
|
|
395
|
+
|
|
396
|
+
list.scrollTop +=
|
|
397
|
+
rowBox.top -
|
|
398
|
+
listBox.top -
|
|
399
|
+
(list.clientHeight - rowBox.height) / 2
|
|
400
|
+
|
|
401
|
+
setPendingScroll(null)
|
|
402
|
+
}, [activeRow, pendingScroll])
|
|
403
|
+
|
|
404
|
+
// Keep the highlight in view as the arrows move it, by the least
|
|
405
|
+
// scroll that shows it.
|
|
406
|
+
useEffect(() => {
|
|
407
|
+
if (pendingScroll !== null || activeRow === undefined) {
|
|
408
|
+
return
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const list = listRef.current
|
|
412
|
+
const row = document.getElementById(activeRow.id)
|
|
413
|
+
|
|
414
|
+
if (list === null || row === null) {
|
|
415
|
+
return
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const listBox = list.getBoundingClientRect()
|
|
419
|
+
const rowBox = row.getBoundingClientRect()
|
|
420
|
+
|
|
421
|
+
if (rowBox.top < listBox.top) {
|
|
422
|
+
list.scrollTop -= listBox.top - rowBox.top
|
|
423
|
+
} else if (rowBox.bottom > listBox.bottom) {
|
|
424
|
+
list.scrollTop += rowBox.bottom - listBox.bottom
|
|
425
|
+
}
|
|
426
|
+
}, [activeRow, pendingScroll])
|
|
427
|
+
|
|
428
|
+
const searchBoxRef = useRef<HTMLDivElement>(null)
|
|
429
|
+
|
|
430
|
+
/** The search field's clear button, while it shows. */
|
|
431
|
+
const findClearButton = () =>
|
|
432
|
+
searchBoxRef.current?.querySelector<HTMLButtonElement>(
|
|
433
|
+
"button",
|
|
434
|
+
) ?? null
|
|
435
|
+
|
|
436
|
+
const focusRail = () => {
|
|
437
|
+
const rail = railRef.current
|
|
438
|
+
|
|
439
|
+
if (rail === null) {
|
|
440
|
+
return false
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const target = rail.querySelector<HTMLElement>(
|
|
444
|
+
'[tabindex="0"]',
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
target?.focus()
|
|
448
|
+
|
|
449
|
+
return target !== null
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* The element inside the panel that last took focus, and whether it
|
|
454
|
+
* was on the rail. A catalog refresh can take that element away — a
|
|
455
|
+
* rail button whose group lost its last row — and the browser then
|
|
456
|
+
* drops focus on `<body>`, outside the panel. The layout effect below
|
|
457
|
+
* puts it back, before anything is painted.
|
|
458
|
+
*/
|
|
459
|
+
const lastFocusRef = useRef<null | {
|
|
460
|
+
element: HTMLElement
|
|
461
|
+
isOnRail: boolean
|
|
462
|
+
}>(null)
|
|
463
|
+
|
|
464
|
+
useLayoutEffect(() => {
|
|
465
|
+
const lastFocus = lastFocusRef.current
|
|
466
|
+
|
|
467
|
+
if (
|
|
468
|
+
lastFocus === null ||
|
|
469
|
+
lastFocus.element.isConnected ||
|
|
470
|
+
(document.activeElement !== null &&
|
|
471
|
+
document.activeElement !== document.body)
|
|
472
|
+
) {
|
|
473
|
+
return
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
lastFocusRef.current = null
|
|
477
|
+
|
|
478
|
+
if (!(lastFocus.isOnRail && focusRail())) {
|
|
479
|
+
inputRef.current?.focus()
|
|
480
|
+
}
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
const handleEscape = (
|
|
484
|
+
keyboardEvent: ReactKeyboardEvent<HTMLElement>,
|
|
485
|
+
) => {
|
|
486
|
+
keyboardEvent.preventDefault()
|
|
487
|
+
|
|
488
|
+
// Handled here, so it goes no further: an enclosing `Dialog` or
|
|
489
|
+
// `Modal` listens for Escape on the document, and would close
|
|
490
|
+
// under the picker on the same key press.
|
|
491
|
+
keyboardEvent.stopPropagation()
|
|
492
|
+
|
|
493
|
+
if (query.length > 0) {
|
|
494
|
+
changeQuery("")
|
|
495
|
+
|
|
496
|
+
return
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
onDismiss()
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const handleSearchKeyDown = (
|
|
503
|
+
keyboardEvent: ReactKeyboardEvent<HTMLInputElement>,
|
|
504
|
+
) => {
|
|
505
|
+
// An input method editor is mid-word: its Enter commits the word,
|
|
506
|
+
// its Escape cancels it, and its arrows move its own candidates.
|
|
507
|
+
// None of them is the picker's. (`keyCode` 229 is the "IME busy"
|
|
508
|
+
// code browsers report on the keydown that ends a composition.)
|
|
509
|
+
if (
|
|
510
|
+
keyboardEvent.nativeEvent.isComposing ||
|
|
511
|
+
keyboardEvent.keyCode === 229
|
|
512
|
+
) {
|
|
513
|
+
return
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
switch (keyboardEvent.key) {
|
|
517
|
+
case "ArrowDown": {
|
|
518
|
+
keyboardEvent.preventDefault()
|
|
519
|
+
moveActive(
|
|
520
|
+
nextEnabledIndex(rows, resolvedActiveIndex, 1),
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
return
|
|
524
|
+
}
|
|
525
|
+
case "ArrowUp": {
|
|
526
|
+
keyboardEvent.preventDefault()
|
|
527
|
+
moveActive(
|
|
528
|
+
resolvedActiveIndex < 0
|
|
529
|
+
? nextEnabledIndex(rows, rows.length, -1)
|
|
530
|
+
: nextEnabledIndex(
|
|
531
|
+
rows,
|
|
532
|
+
resolvedActiveIndex,
|
|
533
|
+
-1,
|
|
534
|
+
),
|
|
535
|
+
)
|
|
536
|
+
|
|
537
|
+
return
|
|
538
|
+
}
|
|
539
|
+
// Home and End are not here: they move the caret, as in any text
|
|
540
|
+
// field and in `Combobox` (the APG editable-combobox contract).
|
|
541
|
+
case "Enter": {
|
|
542
|
+
if (activeRow === undefined) {
|
|
543
|
+
return
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
keyboardEvent.preventDefault()
|
|
547
|
+
onSelect(activeRow.item.value)
|
|
548
|
+
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
case "Escape": {
|
|
552
|
+
handleEscape(keyboardEvent)
|
|
553
|
+
|
|
554
|
+
return
|
|
555
|
+
}
|
|
556
|
+
case "Tab": {
|
|
557
|
+
// Forward, a showing clear button is next in the page's own
|
|
558
|
+
// order, so the browser takes it there.
|
|
559
|
+
if (
|
|
560
|
+
!keyboardEvent.shiftKey &&
|
|
561
|
+
findClearButton() !== null
|
|
562
|
+
) {
|
|
563
|
+
return
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// The cycle stays inside the panel: with no rail, Shift+Tab
|
|
567
|
+
// goes back round to a showing clear button, and a field
|
|
568
|
+
// alone keeps focus.
|
|
569
|
+
keyboardEvent.preventDefault()
|
|
570
|
+
|
|
571
|
+
if (!focusRail() && keyboardEvent.shiftKey) {
|
|
572
|
+
findClearButton()?.focus()
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
return
|
|
576
|
+
}
|
|
577
|
+
default:
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Keys on the clear button, which `SearchInput` draws: Tab goes on to
|
|
583
|
+
* the rail (Shift+Tab goes back to the field by itself), and Escape
|
|
584
|
+
* does what it does in the field.
|
|
585
|
+
*/
|
|
586
|
+
const handleSearchBoxKeyDown = (
|
|
587
|
+
keyboardEvent: ReactKeyboardEvent<HTMLDivElement>,
|
|
588
|
+
) => {
|
|
589
|
+
const clearButton = findClearButton()
|
|
590
|
+
|
|
591
|
+
if (
|
|
592
|
+
clearButton === null ||
|
|
593
|
+
keyboardEvent.target !== clearButton
|
|
594
|
+
) {
|
|
595
|
+
return
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (keyboardEvent.key === "Escape") {
|
|
599
|
+
// The clear button leaves with the query: move off it first.
|
|
600
|
+
inputRef.current?.focus()
|
|
601
|
+
|
|
602
|
+
handleEscape(keyboardEvent)
|
|
603
|
+
|
|
604
|
+
return
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (
|
|
608
|
+
keyboardEvent.key === "Tab" &&
|
|
609
|
+
!keyboardEvent.shiftKey
|
|
610
|
+
) {
|
|
611
|
+
// On to the rail, or round to the field when there is none.
|
|
612
|
+
keyboardEvent.preventDefault()
|
|
613
|
+
|
|
614
|
+
if (!focusRail()) {
|
|
615
|
+
inputRef.current?.focus()
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const hasRows = rows.length > 0
|
|
621
|
+
|
|
622
|
+
return (
|
|
623
|
+
// biome-ignore lint/a11y/noStaticElementInteractions: only records which control inside took focus (see `lastFocusRef`); the controls themselves are the focusable elements.
|
|
624
|
+
<div
|
|
625
|
+
className="flex min-h-0 min-w-0 flex-1"
|
|
626
|
+
onFocus={(focusEvent) => {
|
|
627
|
+
lastFocusRef.current = {
|
|
628
|
+
element: focusEvent.target,
|
|
629
|
+
isOnRail:
|
|
630
|
+
railRef.current?.contains(focusEvent.target) ??
|
|
631
|
+
false,
|
|
632
|
+
}
|
|
633
|
+
}}
|
|
634
|
+
>
|
|
635
|
+
{tabs.length > 0 ? (
|
|
636
|
+
<CatalogPickerRail
|
|
637
|
+
currentTabKey={
|
|
638
|
+
isSearching ? undefined : currentTab?.key
|
|
639
|
+
}
|
|
640
|
+
itemSize={itemSize}
|
|
641
|
+
label={`${label} groups`}
|
|
642
|
+
onEscape={handleEscape}
|
|
643
|
+
onLeave={(isBackward) => {
|
|
644
|
+
// Backward from the rail is the clear button while it
|
|
645
|
+
// shows, the field otherwise; forward is the field.
|
|
646
|
+
const clearButton = isBackward
|
|
647
|
+
? findClearButton()
|
|
648
|
+
: null
|
|
649
|
+
|
|
650
|
+
if (clearButton === null) {
|
|
651
|
+
inputRef.current?.focus()
|
|
652
|
+
} else {
|
|
653
|
+
clearButton.focus()
|
|
654
|
+
}
|
|
655
|
+
}}
|
|
656
|
+
onShowTab={showTab}
|
|
657
|
+
ref={railRef}
|
|
658
|
+
tabs={tabs}
|
|
659
|
+
/>
|
|
660
|
+
) : null}
|
|
661
|
+
|
|
662
|
+
<div className="flex min-h-0 min-w-0 flex-1 flex-col">
|
|
663
|
+
{/* biome-ignore lint/a11y/noStaticElementInteractions: only listens for keys bubbling up from the clear button inside, which is the focusable control. */}
|
|
664
|
+
<div
|
|
665
|
+
className="shrink-0 border-border-subtle border-b p-1.5"
|
|
666
|
+
onKeyDown={handleSearchBoxKeyDown}
|
|
667
|
+
ref={searchBoxRef}
|
|
668
|
+
>
|
|
669
|
+
<SearchInput
|
|
670
|
+
aria-activedescendant={activeRow?.id}
|
|
671
|
+
aria-autocomplete="list"
|
|
672
|
+
aria-controls={hasRows ? listId : undefined}
|
|
673
|
+
aria-expanded={hasRows}
|
|
674
|
+
aria-label={label}
|
|
675
|
+
autoCapitalize="off"
|
|
676
|
+
autoComplete="off"
|
|
677
|
+
autoCorrect="off"
|
|
678
|
+
clearIcon={clearIcon ?? null}
|
|
679
|
+
isClearVisible={
|
|
680
|
+
clearIcon !== undefined && query.length > 0
|
|
681
|
+
}
|
|
682
|
+
onChange={(changeEvent) => {
|
|
683
|
+
changeQuery(changeEvent.currentTarget.value)
|
|
684
|
+
}}
|
|
685
|
+
onClear={() => {
|
|
686
|
+
changeQuery("")
|
|
687
|
+
}}
|
|
688
|
+
onKeyDown={handleSearchKeyDown}
|
|
689
|
+
placeholder={searchPlaceholder}
|
|
690
|
+
ref={inputRef}
|
|
691
|
+
role="combobox"
|
|
692
|
+
size={SEARCH_SIZE[itemSize]}
|
|
693
|
+
spellCheck={false}
|
|
694
|
+
value={query}
|
|
695
|
+
/>
|
|
696
|
+
</div>
|
|
697
|
+
|
|
698
|
+
{hasRows ? (
|
|
699
|
+
<div
|
|
700
|
+
aria-label={label}
|
|
701
|
+
className="charcuterie-scrollbar min-h-0 flex-1 overflow-y-auto overscroll-contain p-1"
|
|
702
|
+
id={listId}
|
|
703
|
+
ref={listRef}
|
|
704
|
+
role="listbox"
|
|
705
|
+
>
|
|
706
|
+
{indexedSections.map((section) => {
|
|
707
|
+
const options = section.rows.map((row) => (
|
|
708
|
+
<CatalogPickerOption
|
|
709
|
+
group={
|
|
710
|
+
row.item.groupId === null
|
|
711
|
+
? undefined
|
|
712
|
+
: groupById.get(row.item.groupId)
|
|
713
|
+
}
|
|
714
|
+
id={row.id}
|
|
715
|
+
isActive={
|
|
716
|
+
row.index === resolvedActiveIndex
|
|
717
|
+
}
|
|
718
|
+
isFavorite={favoriteValues.has(
|
|
719
|
+
row.item.value,
|
|
720
|
+
)}
|
|
721
|
+
isSelected={row.item.value === value}
|
|
722
|
+
item={row.item}
|
|
723
|
+
itemSize={itemSize}
|
|
724
|
+
// By value, never by position: a refresh that moves
|
|
725
|
+
// rows must not hand a half-done click on one row's
|
|
726
|
+
// button to another row. A row that moves to another
|
|
727
|
+
// section gets a new button, which cancels the click.
|
|
728
|
+
key={row.item.value}
|
|
729
|
+
onChoose={onSelect}
|
|
730
|
+
onPoint={() => {
|
|
731
|
+
if (
|
|
732
|
+
row.index !== resolvedActiveIndex &&
|
|
733
|
+
row.item.isDisabled !== true
|
|
734
|
+
) {
|
|
735
|
+
setActiveValue(row.item.value)
|
|
736
|
+
}
|
|
737
|
+
}}
|
|
738
|
+
/>
|
|
739
|
+
))
|
|
740
|
+
|
|
741
|
+
return section.heading === undefined ? (
|
|
742
|
+
<div
|
|
743
|
+
className="not-first:mt-1"
|
|
744
|
+
key={section.key}
|
|
745
|
+
role="presentation"
|
|
746
|
+
>
|
|
747
|
+
{options}
|
|
748
|
+
</div>
|
|
749
|
+
) : (
|
|
750
|
+
// biome-ignore lint/a11y/useSemanticElements: a listbox group is `role="group"` (ARIA APG); a `<fieldset>` is invalid content inside `role="listbox"`.
|
|
751
|
+
<div
|
|
752
|
+
aria-labelledby={section.headingId}
|
|
753
|
+
className="not-first:mt-1"
|
|
754
|
+
key={section.key}
|
|
755
|
+
role="group"
|
|
756
|
+
>
|
|
757
|
+
<div
|
|
758
|
+
className={toClassName(
|
|
759
|
+
GROUP_HEADING_INSET_CLASS[itemSize],
|
|
760
|
+
"pt-1.5 pb-0.5 font-medium text-content-secondary text-xs",
|
|
761
|
+
)}
|
|
762
|
+
id={section.headingId}
|
|
763
|
+
>
|
|
764
|
+
{section.heading}
|
|
765
|
+
</div>
|
|
766
|
+
|
|
767
|
+
{options}
|
|
768
|
+
</div>
|
|
769
|
+
)
|
|
770
|
+
})}
|
|
771
|
+
</div>
|
|
772
|
+
) : (
|
|
773
|
+
<p className="m-0 px-4 py-6 text-center text-content-muted text-sm">
|
|
774
|
+
{emptyLabel}
|
|
775
|
+
</p>
|
|
776
|
+
)}
|
|
777
|
+
</div>
|
|
778
|
+
</div>
|
|
779
|
+
)
|
|
780
|
+
}
|