@charcuterie/ui 4.28.4 → 4.29.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 +105 -0
- package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPicker.js +152 -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 +42 -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 +173 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.js +235 -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 +157 -0
- package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
- package/src/CatalogPicker/CatalogPicker.test.tsx +1352 -0
- package/src/CatalogPicker/CatalogPicker.tsx +376 -0
- package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
- package/src/CatalogPicker/CatalogPickerOption.tsx +138 -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 +346 -0
- package/src/CatalogPicker/catalogPickerView.ts +457 -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,376 @@
|
|
|
1
|
+
import { useVisibility } from "@charcuterie/logic"
|
|
2
|
+
import type { ControlSize } from "@charcuterie/tokens"
|
|
3
|
+
import type { Placement } from "@floating-ui/react"
|
|
4
|
+
import {
|
|
5
|
+
FloatingFocusManager,
|
|
6
|
+
FloatingPortal,
|
|
7
|
+
} from "@floating-ui/react"
|
|
8
|
+
import type {
|
|
9
|
+
ComponentPropsWithRef,
|
|
10
|
+
ReactNode,
|
|
11
|
+
} from "react"
|
|
12
|
+
import { useEffect, useMemo, useRef, useState } from "react"
|
|
13
|
+
|
|
14
|
+
import { Button } from "../Button/Button.tsx"
|
|
15
|
+
import { PANEL_SURFACE_CLASS } from "../Overlay/overlayPanelClass.ts"
|
|
16
|
+
import { useAnchoredOverlay } from "../Overlay/useAnchoredOverlay.ts"
|
|
17
|
+
import { usePanelItemSize } from "../Overlay/usePanelItemSize.ts"
|
|
18
|
+
import { toClassName } from "../toClassName.ts"
|
|
19
|
+
import { CatalogPickerMark } from "./CatalogPickerMark.tsx"
|
|
20
|
+
import { CatalogPickerPanel } from "./CatalogPickerPanel.tsx"
|
|
21
|
+
import {
|
|
22
|
+
type CatalogPickerGroup,
|
|
23
|
+
type CatalogPickerItem,
|
|
24
|
+
toUniqueCatalog,
|
|
25
|
+
} from "./catalogPickerView.ts"
|
|
26
|
+
|
|
27
|
+
export type {
|
|
28
|
+
CatalogPickerGroup,
|
|
29
|
+
CatalogPickerItem,
|
|
30
|
+
} from "./catalogPickerView.ts"
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A constant element rather than a component, as in `Picker`: a
|
|
34
|
+
* second component in this file trips the house rule, and this never
|
|
35
|
+
* varies.
|
|
36
|
+
*/
|
|
37
|
+
const CHEVRON_DOWN = (
|
|
38
|
+
<svg
|
|
39
|
+
aria-hidden="true"
|
|
40
|
+
className="size-4"
|
|
41
|
+
fill="none"
|
|
42
|
+
focusable={false}
|
|
43
|
+
stroke="currentColor"
|
|
44
|
+
strokeLinecap="round"
|
|
45
|
+
strokeLinejoin="round"
|
|
46
|
+
strokeWidth={1.75}
|
|
47
|
+
viewBox="0 0 24 24"
|
|
48
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
49
|
+
>
|
|
50
|
+
<path d="m6 9 6 6 6-6" />
|
|
51
|
+
</svg>
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
export type CatalogPickerProps = Omit<
|
|
55
|
+
ComponentPropsWithRef<"button">,
|
|
56
|
+
"children" | "disabled" | "onChange" | "value"
|
|
57
|
+
> & {
|
|
58
|
+
/**
|
|
59
|
+
* The glyph inside the search field's clear button — the app's own,
|
|
60
|
+
* as for `SearchInput`. Without one there is no clear button, and
|
|
61
|
+
* Escape clears the query.
|
|
62
|
+
*/
|
|
63
|
+
clearIcon?: ReactNode
|
|
64
|
+
/** The trigger. As in `Picker`, the outermost box this renders. */
|
|
65
|
+
className?: string
|
|
66
|
+
/** Shown when a search matches nothing. Default "No matches". */
|
|
67
|
+
emptyLabel?: string
|
|
68
|
+
/** Item values, in the order the Favorites tab lists them. No favorites, no Favorites tab. */
|
|
69
|
+
favorites?: readonly string[]
|
|
70
|
+
/** The Favorites tab's accessible name and tooltip. Default "Favorites". */
|
|
71
|
+
favoritesLabel?: string
|
|
72
|
+
/**
|
|
73
|
+
* The rail, top to bottom (after Favorites). A group with no rows is
|
|
74
|
+
* left off. Each `id` must be unique: a repeated one is dropped (the
|
|
75
|
+
* first wins), with a console warning.
|
|
76
|
+
*/
|
|
77
|
+
groups: readonly CatalogPickerGroup[]
|
|
78
|
+
isDisabled?: boolean
|
|
79
|
+
/**
|
|
80
|
+
* Everything that can be chosen. A group's rows keep this order. Each
|
|
81
|
+
* `value` must be unique across the catalog: a repeated one is
|
|
82
|
+
* dropped (the first wins), with a console warning.
|
|
83
|
+
*/
|
|
84
|
+
items: readonly CatalogPickerItem[]
|
|
85
|
+
/**
|
|
86
|
+
* What the control is FOR — "Agent", "Model". Not the current value.
|
|
87
|
+
* The trigger is named `"<label>: <chosen label>"`, as in `Picker`,
|
|
88
|
+
* and the search field and the list are named `label`.
|
|
89
|
+
*/
|
|
90
|
+
label: string
|
|
91
|
+
onChange: (value: string) => void
|
|
92
|
+
/** The trigger's text when nothing is chosen. */
|
|
93
|
+
placeholder?: string
|
|
94
|
+
/** Where the panel opens against the trigger. Default `bottom-start`. */
|
|
95
|
+
placement?: Placement
|
|
96
|
+
/** The search field's placeholder. Default "Search…". */
|
|
97
|
+
searchPlaceholder?: string
|
|
98
|
+
/** The trigger's size, and the size of every row in the panel. Default `md`. */
|
|
99
|
+
size?: ControlSize
|
|
100
|
+
value?: string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Tall enough for about nine rows at `md`, and never taller than the window leaves. */
|
|
104
|
+
const PANEL_MAX_HEIGHT_PX = 448
|
|
105
|
+
|
|
106
|
+
/** Wide enough for a rail and a long model name; capped to the window on a phone. */
|
|
107
|
+
const PANEL_MAX_WIDTH_PX = 416
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* A picker over a large catalog: browsed by group on a rail, or
|
|
111
|
+
* searched across everything at once.
|
|
112
|
+
*
|
|
113
|
+
* The trigger is a `Picker`-style button showing the chosen item's mark
|
|
114
|
+
* and label. The panel has a vertical rail on the inline start — a
|
|
115
|
+
* Favorites tab, then one tab per group — and, beside it, a search
|
|
116
|
+
* field over the list. Browsing a tab lists that group's rows; typing
|
|
117
|
+
* lists matches from every group, under group headings.
|
|
118
|
+
*
|
|
119
|
+
* ### Why not `Combobox` or `Picker`
|
|
120
|
+
*
|
|
121
|
+
* Both are one flat list. Forty-odd rows that differ only after a
|
|
122
|
+
* shared provider prefix are exactly what neither helps with: `Picker`
|
|
123
|
+
* has no filter, and `Combobox` filters one label string and has no
|
|
124
|
+
* way to browse a part of the list. The rail is the browse half, and
|
|
125
|
+
* the search crosses the rail, because a person who types a model's
|
|
126
|
+
* name should not have to know its provider first.
|
|
127
|
+
*
|
|
128
|
+
* ### The ARIA contract
|
|
129
|
+
*
|
|
130
|
+
* The trigger opens a `role="dialog"` named by the trigger. Inside, the
|
|
131
|
+
* search field is the `role="combobox"` controlling a `role="listbox"`,
|
|
132
|
+
* focus stays in the field, and the arrows move
|
|
133
|
+
* `aria-activedescendant` — the `Combobox` contract. The rail is a
|
|
134
|
+
* vertical `role="toolbar"` of `aria-pressed` buttons, and Tab moves
|
|
135
|
+
* between the field, its clear button and the rail. Escape clears a
|
|
136
|
+
* query, then closes, and focus returns to the trigger. The picker
|
|
137
|
+
* consumes every Escape it handles, so one inside a `Dialog` never
|
|
138
|
+
* closes the dialog too.
|
|
139
|
+
*
|
|
140
|
+
* ### Opening
|
|
141
|
+
*
|
|
142
|
+
* Every open is a fresh start: an empty query, and the tab below. The
|
|
143
|
+
* panel lands on Favorites when the chosen value is a favorite or a row
|
|
144
|
+
* listed on every tab (pinned, or in no group), else on the chosen
|
|
145
|
+
* row's group, else on Favorites, else on the first group; and the
|
|
146
|
+
* chosen row is centred in the list, as a `Combobox` opens on its
|
|
147
|
+
* chosen option.
|
|
148
|
+
*
|
|
149
|
+
* ### One value, one row
|
|
150
|
+
*
|
|
151
|
+
* An item's `value` and a group's `id` must each be unique. A repeat is
|
|
152
|
+
* dropped — the first one wins, in the trigger, the list and Favorites
|
|
153
|
+
* alike — and the picker warns in the console.
|
|
154
|
+
*
|
|
155
|
+
* ### Not windowed
|
|
156
|
+
*
|
|
157
|
+
* Every row of the current view is rendered. `Combobox` windows only
|
|
158
|
+
* above 100 rows, and the catalogs this was built for are 40 to 60; a
|
|
159
|
+
* catalog that needs windowing is a change to this component, recorded
|
|
160
|
+
* in its decision.
|
|
161
|
+
*/
|
|
162
|
+
export const CatalogPicker = ({
|
|
163
|
+
className,
|
|
164
|
+
clearIcon,
|
|
165
|
+
emptyLabel = "No matches",
|
|
166
|
+
favorites = [],
|
|
167
|
+
favoritesLabel = "Favorites",
|
|
168
|
+
groups: givenGroups,
|
|
169
|
+
isDisabled = false,
|
|
170
|
+
items: givenItems,
|
|
171
|
+
label,
|
|
172
|
+
onChange,
|
|
173
|
+
placeholder,
|
|
174
|
+
placement,
|
|
175
|
+
searchPlaceholder = "Search…",
|
|
176
|
+
size = "md",
|
|
177
|
+
value,
|
|
178
|
+
...buttonProps
|
|
179
|
+
}: CatalogPickerProps): ReactNode => {
|
|
180
|
+
const {
|
|
181
|
+
duplicateGroupIds,
|
|
182
|
+
duplicateItemValues,
|
|
183
|
+
groups,
|
|
184
|
+
items,
|
|
185
|
+
} = useMemo(
|
|
186
|
+
() =>
|
|
187
|
+
toUniqueCatalog({
|
|
188
|
+
groups: givenGroups,
|
|
189
|
+
items: givenItems,
|
|
190
|
+
}),
|
|
191
|
+
[givenGroups, givenItems],
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
const duplicateWarning = [
|
|
195
|
+
duplicateItemValues.length === 0
|
|
196
|
+
? ""
|
|
197
|
+
: `item values ${JSON.stringify(duplicateItemValues)}`,
|
|
198
|
+
duplicateGroupIds.length === 0
|
|
199
|
+
? ""
|
|
200
|
+
: `group ids ${JSON.stringify(duplicateGroupIds)}`,
|
|
201
|
+
]
|
|
202
|
+
.filter((part) => part !== "")
|
|
203
|
+
.join(" and ")
|
|
204
|
+
|
|
205
|
+
useEffect(() => {
|
|
206
|
+
if (duplicateWarning !== "") {
|
|
207
|
+
console.warn(
|
|
208
|
+
`CatalogPicker: ${duplicateWarning} repeat. A value names one row, so only the first of each is kept.`,
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
}, [duplicateWarning])
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Bumped on every open, and the panel's `key`: a panel still fading
|
|
215
|
+
* out when the picker reopens is replaced, not revived, so its old
|
|
216
|
+
* query and tab never come back.
|
|
217
|
+
*/
|
|
218
|
+
const [session, setSession] = useState(0)
|
|
219
|
+
|
|
220
|
+
const { hide, isVisible, toggle } = useVisibility({
|
|
221
|
+
onChange: (isNextVisible) => {
|
|
222
|
+
if (isNextVisible) {
|
|
223
|
+
setSession((previous) => previous + 1)
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
const isOpen = isVisible && !isDisabled
|
|
229
|
+
|
|
230
|
+
// Disabling closes the picker for good, rather than hiding it until
|
|
231
|
+
// it is enabled again: re-enabling should not open it unasked.
|
|
232
|
+
useEffect(() => {
|
|
233
|
+
if (isDisabled) {
|
|
234
|
+
hide()
|
|
235
|
+
}
|
|
236
|
+
}, [hide, isDisabled])
|
|
237
|
+
|
|
238
|
+
const itemSize = usePanelItemSize(size)
|
|
239
|
+
|
|
240
|
+
const inputRef = useRef<HTMLInputElement>(null)
|
|
241
|
+
|
|
242
|
+
const chosen =
|
|
243
|
+
value === undefined
|
|
244
|
+
? undefined
|
|
245
|
+
: items.find((item) => item.value === value)
|
|
246
|
+
|
|
247
|
+
const chosenGroup =
|
|
248
|
+
chosen?.groupId == null
|
|
249
|
+
? undefined
|
|
250
|
+
: groups.find((group) => group.id === chosen.groupId)
|
|
251
|
+
|
|
252
|
+
const triggerText = chosen?.label ?? placeholder ?? ""
|
|
253
|
+
|
|
254
|
+
const hasChosenMark =
|
|
255
|
+
chosen !== undefined &&
|
|
256
|
+
(chosen.icon !== undefined || chosenGroup !== undefined)
|
|
257
|
+
|
|
258
|
+
const {
|
|
259
|
+
clonedTrigger,
|
|
260
|
+
context,
|
|
261
|
+
floatingStyles,
|
|
262
|
+
getFloatingProps,
|
|
263
|
+
isExiting,
|
|
264
|
+
isMounted,
|
|
265
|
+
motionClassName,
|
|
266
|
+
setFloating,
|
|
267
|
+
triggerId,
|
|
268
|
+
} = useAnchoredOverlay({
|
|
269
|
+
// Escape belongs to the panel: it clears a query before it closes.
|
|
270
|
+
isEscapeDismissable: false,
|
|
271
|
+
isVisible: isOpen,
|
|
272
|
+
maxHeightPx: PANEL_MAX_HEIGHT_PX,
|
|
273
|
+
maxWidthPx: PANEL_MAX_WIDTH_PX,
|
|
274
|
+
offsetValue: 4,
|
|
275
|
+
onDismiss: hide,
|
|
276
|
+
placement,
|
|
277
|
+
role: "dialog",
|
|
278
|
+
trigger: (
|
|
279
|
+
<Button
|
|
280
|
+
{...buttonProps}
|
|
281
|
+
appearance="outline"
|
|
282
|
+
aria-label={`${label}: ${triggerText}`}
|
|
283
|
+
className={toClassName(
|
|
284
|
+
"justify-between",
|
|
285
|
+
className,
|
|
286
|
+
)}
|
|
287
|
+
iconEnd={CHEVRON_DOWN}
|
|
288
|
+
intent="neutral"
|
|
289
|
+
isDisabled={isDisabled}
|
|
290
|
+
onClick={(clickEvent) => {
|
|
291
|
+
buttonProps.onClick?.(clickEvent)
|
|
292
|
+
|
|
293
|
+
toggle()
|
|
294
|
+
}}
|
|
295
|
+
size={size}
|
|
296
|
+
type="button"
|
|
297
|
+
>
|
|
298
|
+
{triggerText === "" && !hasChosenMark ? null : (
|
|
299
|
+
<span className="flex min-w-0 grow items-center gap-2">
|
|
300
|
+
{hasChosenMark ? (
|
|
301
|
+
<CatalogPickerMark
|
|
302
|
+
accentColor={chosenGroup?.accentColor}
|
|
303
|
+
icon={chosen?.icon ?? chosenGroup?.icon}
|
|
304
|
+
initialsOf={chosenGroup?.label}
|
|
305
|
+
size="sm"
|
|
306
|
+
/>
|
|
307
|
+
) : null}
|
|
308
|
+
|
|
309
|
+
{/* Not `truncate`, for the reason `Picker` gives: its
|
|
310
|
+
`overflow: hidden` clips glyph edges. */}
|
|
311
|
+
<span className="-mx-1 min-w-0 grow overflow-x-clip px-1 text-start text-ellipsis whitespace-nowrap">
|
|
312
|
+
{triggerText}
|
|
313
|
+
</span>
|
|
314
|
+
</span>
|
|
315
|
+
)}
|
|
316
|
+
</Button>
|
|
317
|
+
),
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
const choose = (nextValue: string) => {
|
|
321
|
+
onChange(nextValue)
|
|
322
|
+
|
|
323
|
+
hide()
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<>
|
|
328
|
+
{clonedTrigger}
|
|
329
|
+
|
|
330
|
+
{isMounted ? (
|
|
331
|
+
<FloatingPortal>
|
|
332
|
+
<FloatingFocusManager
|
|
333
|
+
context={context}
|
|
334
|
+
disabled={isExiting}
|
|
335
|
+
initialFocus={inputRef}
|
|
336
|
+
modal={false}
|
|
337
|
+
>
|
|
338
|
+
<div
|
|
339
|
+
{...getFloatingProps()}
|
|
340
|
+
aria-hidden={isExiting ? true : undefined}
|
|
341
|
+
aria-labelledby={triggerId}
|
|
342
|
+
className={toClassName(
|
|
343
|
+
PANEL_SURFACE_CLASS,
|
|
344
|
+
"z-[var(--layer-modal)] flex h-[28rem] w-[26rem] overflow-hidden",
|
|
345
|
+
motionClassName,
|
|
346
|
+
)}
|
|
347
|
+
inert={isExiting ? true : undefined}
|
|
348
|
+
ref={setFloating}
|
|
349
|
+
// `useRole` already wrote this through the spread; restated
|
|
350
|
+
// so the lint can see the role `aria-labelledby` belongs to.
|
|
351
|
+
role="dialog"
|
|
352
|
+
style={floatingStyles}
|
|
353
|
+
>
|
|
354
|
+
<CatalogPickerPanel
|
|
355
|
+
clearIcon={clearIcon}
|
|
356
|
+
emptyLabel={emptyLabel}
|
|
357
|
+
favorites={favorites}
|
|
358
|
+
favoritesLabel={favoritesLabel}
|
|
359
|
+
groups={groups}
|
|
360
|
+
inputRef={inputRef}
|
|
361
|
+
itemSize={itemSize}
|
|
362
|
+
items={items}
|
|
363
|
+
key={session}
|
|
364
|
+
label={label}
|
|
365
|
+
onDismiss={hide}
|
|
366
|
+
onSelect={choose}
|
|
367
|
+
searchPlaceholder={searchPlaceholder}
|
|
368
|
+
value={value}
|
|
369
|
+
/>
|
|
370
|
+
</div>
|
|
371
|
+
</FloatingFocusManager>
|
|
372
|
+
</FloatingPortal>
|
|
373
|
+
) : null}
|
|
374
|
+
</>
|
|
375
|
+
)
|
|
376
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { ControlSize } from "@charcuterie/tokens"
|
|
2
|
+
import type { CSSProperties, ReactNode } from "react"
|
|
3
|
+
|
|
4
|
+
import { toClassName } from "../toClassName.ts"
|
|
5
|
+
import { toInitials } from "./catalogPickerView.ts"
|
|
6
|
+
|
|
7
|
+
export type CatalogPickerMarkProps = {
|
|
8
|
+
accentColor?: string
|
|
9
|
+
icon?: ReactNode
|
|
10
|
+
/**
|
|
11
|
+
* The group label the badge's initials come from. `undefined` with
|
|
12
|
+
* no `icon` draws an empty slot of the same size, so a row with no
|
|
13
|
+
* group still lines its label up with the rows above it.
|
|
14
|
+
*/
|
|
15
|
+
initialsOf?: string
|
|
16
|
+
size: ControlSize
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const MARK_SIZE_CLASS: Record<ControlSize, string> = {
|
|
20
|
+
lg: "size-7 [&>img]:size-7 [&>svg]:size-5",
|
|
21
|
+
md: "size-6 [&>img]:size-6 [&>svg]:size-4",
|
|
22
|
+
sm: "size-5 [&>img]:size-5 [&>svg]:size-4",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A group's mark — the app's icon, or the group's initials on a
|
|
27
|
+
* neutral badge — in a fixed square.
|
|
28
|
+
*
|
|
29
|
+
* `accentColor` is drawn as an **outer ring** (`box-shadow`), never as
|
|
30
|
+
* the badge's fill or its text. The initials stay `content-primary` on
|
|
31
|
+
* a neutral surface, which the contrast gate already covers, so a
|
|
32
|
+
* consumer's arbitrary color cannot make the text unreadable. A ring
|
|
33
|
+
* also takes no layout space, so a ringed mark and a plain one are the
|
|
34
|
+
* same size.
|
|
35
|
+
*
|
|
36
|
+
* Decorative throughout (`aria-hidden`): the row, the tab and the
|
|
37
|
+
* trigger all carry the group or item name as text or `aria-label`.
|
|
38
|
+
*/
|
|
39
|
+
export const CatalogPickerMark = ({
|
|
40
|
+
accentColor,
|
|
41
|
+
icon,
|
|
42
|
+
initialsOf,
|
|
43
|
+
size,
|
|
44
|
+
}: CatalogPickerMarkProps): ReactNode => {
|
|
45
|
+
const ringStyle: CSSProperties | undefined =
|
|
46
|
+
accentColor === undefined
|
|
47
|
+
? undefined
|
|
48
|
+
: { boxShadow: `0 0 0 2px ${accentColor}` }
|
|
49
|
+
|
|
50
|
+
if (icon !== undefined && icon !== null) {
|
|
51
|
+
return (
|
|
52
|
+
<span
|
|
53
|
+
aria-hidden="true"
|
|
54
|
+
className={toClassName(
|
|
55
|
+
"flex shrink-0 items-center justify-center overflow-hidden rounded-sm text-content-secondary [&>img]:object-contain",
|
|
56
|
+
MARK_SIZE_CLASS[size],
|
|
57
|
+
)}
|
|
58
|
+
style={ringStyle}
|
|
59
|
+
>
|
|
60
|
+
{icon}
|
|
61
|
+
</span>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (initialsOf === undefined) {
|
|
66
|
+
return (
|
|
67
|
+
<span
|
|
68
|
+
aria-hidden="true"
|
|
69
|
+
className={toClassName(
|
|
70
|
+
"shrink-0",
|
|
71
|
+
MARK_SIZE_CLASS[size],
|
|
72
|
+
)}
|
|
73
|
+
/>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<span
|
|
79
|
+
aria-hidden="true"
|
|
80
|
+
className={toClassName(
|
|
81
|
+
"flex shrink-0 items-center justify-center rounded-sm bg-intent-neutral-surface-hover font-semibold text-content-primary text-xs leading-none",
|
|
82
|
+
MARK_SIZE_CLASS[size],
|
|
83
|
+
)}
|
|
84
|
+
style={ringStyle}
|
|
85
|
+
>
|
|
86
|
+
{toInitials(initialsOf)}
|
|
87
|
+
</span>
|
|
88
|
+
)
|
|
89
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { ControlSize } from "@charcuterie/tokens"
|
|
2
|
+
import type { ReactNode } from "react"
|
|
3
|
+
|
|
4
|
+
import { PANEL_ITEM_SIZE_CLASS } from "../controlStyles.ts"
|
|
5
|
+
import { toClassName } from "../toClassName.ts"
|
|
6
|
+
import { CatalogPickerMark } from "./CatalogPickerMark.tsx"
|
|
7
|
+
import {
|
|
8
|
+
CHOSEN_CHECK,
|
|
9
|
+
ROW_FAVORITE_STAR,
|
|
10
|
+
} from "./catalogPickerDrawings.tsx"
|
|
11
|
+
import type {
|
|
12
|
+
CatalogPickerGroup,
|
|
13
|
+
CatalogPickerItem,
|
|
14
|
+
} from "./catalogPickerView.ts"
|
|
15
|
+
|
|
16
|
+
export type CatalogPickerOptionProps = {
|
|
17
|
+
group: CatalogPickerGroup | undefined
|
|
18
|
+
/** The DOM id the search field's `aria-activedescendant` points at. */
|
|
19
|
+
id: string
|
|
20
|
+
isActive: boolean
|
|
21
|
+
isFavorite: boolean
|
|
22
|
+
isSelected: boolean
|
|
23
|
+
item: CatalogPickerItem
|
|
24
|
+
/** Already stepped down for a short window by `usePanelItemSize`. */
|
|
25
|
+
itemSize: ControlSize
|
|
26
|
+
onChoose: (value: string) => void
|
|
27
|
+
/** The pointer moved over the row. */
|
|
28
|
+
onPoint: () => void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One row of the catalog's listbox: the mark, the label, the optional
|
|
33
|
+
* description, a star on a favorite, and the check gutter.
|
|
34
|
+
*
|
|
35
|
+
* The contract is `ComboboxOption`'s, for the same reasons: focus stays
|
|
36
|
+
* in the search field, so the row is `role="option"` with
|
|
37
|
+
* `tabIndex={-1}` and the active row is announced by
|
|
38
|
+
* `aria-activedescendant`; `aria-selected` is the chosen value; a
|
|
39
|
+
* mouse press is prevented from moving focus, so the caret stays in the
|
|
40
|
+
* field while the click lands.
|
|
41
|
+
*
|
|
42
|
+
* The pointer moves the active row on `pointermove` (as in
|
|
43
|
+
* `CommandPalette`), so the pointer and the arrow keys share one
|
|
44
|
+
* highlight and Enter's target is never ambiguous. The chosen row keeps
|
|
45
|
+
* its accent tint whenever the highlight is elsewhere.
|
|
46
|
+
*
|
|
47
|
+
* The check is always laid out and only hidden on an unchosen row, so
|
|
48
|
+
* choosing never moves the star or the label. It is drawn, not the
|
|
49
|
+
* `✓` `ComboboxOption` types: that glyph measures blank where the font
|
|
50
|
+
* lacks it.
|
|
51
|
+
*/
|
|
52
|
+
export const CatalogPickerOption = ({
|
|
53
|
+
group,
|
|
54
|
+
id,
|
|
55
|
+
isActive,
|
|
56
|
+
isFavorite,
|
|
57
|
+
isSelected,
|
|
58
|
+
item,
|
|
59
|
+
itemSize,
|
|
60
|
+
onChoose,
|
|
61
|
+
onPoint,
|
|
62
|
+
}: CatalogPickerOptionProps): ReactNode => {
|
|
63
|
+
const {
|
|
64
|
+
description,
|
|
65
|
+
icon,
|
|
66
|
+
isDisabled = false,
|
|
67
|
+
label,
|
|
68
|
+
value,
|
|
69
|
+
} = item
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<button
|
|
73
|
+
aria-selected={isSelected}
|
|
74
|
+
className={toClassName(
|
|
75
|
+
"flex w-full cursor-pointer items-center rounded-sm text-start",
|
|
76
|
+
PANEL_ITEM_SIZE_CLASS[itemSize],
|
|
77
|
+
isDisabled
|
|
78
|
+
? "cursor-not-allowed text-content-disabled"
|
|
79
|
+
: "text-content-primary",
|
|
80
|
+
isActive && "bg-intent-neutral-surface-hover",
|
|
81
|
+
isSelected &&
|
|
82
|
+
!isActive &&
|
|
83
|
+
"bg-intent-accent-surface",
|
|
84
|
+
)}
|
|
85
|
+
disabled={isDisabled}
|
|
86
|
+
id={id}
|
|
87
|
+
onClick={() => {
|
|
88
|
+
onChoose(value)
|
|
89
|
+
}}
|
|
90
|
+
onMouseDown={(mouseEvent) => {
|
|
91
|
+
mouseEvent.preventDefault()
|
|
92
|
+
}}
|
|
93
|
+
onPointerMove={onPoint}
|
|
94
|
+
role="option"
|
|
95
|
+
tabIndex={-1}
|
|
96
|
+
type="button"
|
|
97
|
+
>
|
|
98
|
+
<CatalogPickerMark
|
|
99
|
+
accentColor={group?.accentColor}
|
|
100
|
+
icon={icon ?? group?.icon}
|
|
101
|
+
initialsOf={group?.label}
|
|
102
|
+
size={itemSize}
|
|
103
|
+
/>
|
|
104
|
+
|
|
105
|
+
<span className="flex min-w-0 flex-1 flex-wrap items-baseline gap-x-2">
|
|
106
|
+
<span className="min-w-0 wrap-anywhere">
|
|
107
|
+
{label}
|
|
108
|
+
</span>
|
|
109
|
+
|
|
110
|
+
{description === undefined ||
|
|
111
|
+
description === "" ? null : (
|
|
112
|
+
<span
|
|
113
|
+
className={toClassName(
|
|
114
|
+
"min-w-0 text-sm wrap-anywhere",
|
|
115
|
+
isDisabled
|
|
116
|
+
? "text-content-disabled"
|
|
117
|
+
: "text-content-muted",
|
|
118
|
+
)}
|
|
119
|
+
>
|
|
120
|
+
{description}
|
|
121
|
+
</span>
|
|
122
|
+
)}
|
|
123
|
+
</span>
|
|
124
|
+
|
|
125
|
+
{isFavorite ? ROW_FAVORITE_STAR : null}
|
|
126
|
+
|
|
127
|
+
<span
|
|
128
|
+
aria-hidden="true"
|
|
129
|
+
className={toClassName(
|
|
130
|
+
"flex w-4 shrink-0 items-center justify-center text-intent-accent-content",
|
|
131
|
+
!isSelected && "invisible",
|
|
132
|
+
)}
|
|
133
|
+
>
|
|
134
|
+
{CHOSEN_CHECK}
|
|
135
|
+
</span>
|
|
136
|
+
</button>
|
|
137
|
+
)
|
|
138
|
+
}
|