@charcuterie/ui 4.28.3 → 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.
Files changed (49) hide show
  1. package/dist/CatalogPicker/CatalogPicker.d.ts +105 -0
  2. package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
  3. package/dist/CatalogPicker/CatalogPicker.js +152 -0
  4. package/dist/CatalogPicker/CatalogPicker.js.map +1 -0
  5. package/dist/CatalogPicker/CatalogPickerMark.d.ts +29 -0
  6. package/dist/CatalogPicker/CatalogPickerMark.d.ts.map +1 -0
  7. package/dist/CatalogPicker/CatalogPickerMark.js +35 -0
  8. package/dist/CatalogPicker/CatalogPickerMark.js.map +1 -0
  9. package/dist/CatalogPicker/CatalogPickerOption.d.ts +40 -0
  10. package/dist/CatalogPicker/CatalogPickerOption.d.ts.map +1 -0
  11. package/dist/CatalogPicker/CatalogPickerOption.js +42 -0
  12. package/dist/CatalogPicker/CatalogPickerOption.js.map +1 -0
  13. package/dist/CatalogPicker/CatalogPickerPanel.d.ts +60 -0
  14. package/dist/CatalogPicker/CatalogPickerPanel.d.ts.map +1 -0
  15. package/dist/CatalogPicker/CatalogPickerPanel.js +409 -0
  16. package/dist/CatalogPicker/CatalogPickerPanel.js.map +1 -0
  17. package/dist/CatalogPicker/CatalogPickerRail.d.ts +50 -0
  18. package/dist/CatalogPicker/CatalogPickerRail.d.ts.map +1 -0
  19. package/dist/CatalogPicker/CatalogPickerRail.js +114 -0
  20. package/dist/CatalogPicker/CatalogPickerRail.js.map +1 -0
  21. package/dist/CatalogPicker/catalogPickerDrawings.d.ts +10 -0
  22. package/dist/CatalogPicker/catalogPickerDrawings.d.ts.map +1 -0
  23. package/dist/CatalogPicker/catalogPickerDrawings.js +29 -0
  24. package/dist/CatalogPicker/catalogPickerDrawings.js.map +1 -0
  25. package/dist/CatalogPicker/catalogPickerView.d.ts +173 -0
  26. package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
  27. package/dist/CatalogPicker/catalogPickerView.js +235 -0
  28. package/dist/CatalogPicker/catalogPickerView.js.map +1 -0
  29. package/dist/index.d.ts +2 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -0
  32. package/dist/index.js.map +1 -1
  33. package/package.json +3 -3
  34. package/src/CatalogPicker/CatalogPicker.mdx +157 -0
  35. package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
  36. package/src/CatalogPicker/CatalogPicker.test.tsx +1352 -0
  37. package/src/CatalogPicker/CatalogPicker.tsx +376 -0
  38. package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
  39. package/src/CatalogPicker/CatalogPickerOption.tsx +138 -0
  40. package/src/CatalogPicker/CatalogPickerPanel.tsx +780 -0
  41. package/src/CatalogPicker/CatalogPickerRail.tsx +214 -0
  42. package/src/CatalogPicker/catalogPicker.storyHelpers.tsx +276 -0
  43. package/src/CatalogPicker/catalogPickerDrawings.tsx +51 -0
  44. package/src/CatalogPicker/catalogPickerView.test.ts +346 -0
  45. package/src/CatalogPicker/catalogPickerView.ts +457 -0
  46. package/src/Dialog/Dialog.test.tsx +20 -289
  47. package/src/Field/Field.test.tsx +11 -405
  48. package/src/index.ts +6 -0
  49. package/src/sourceRules.test.ts +5 -1
@@ -0,0 +1,214 @@
1
+ import type { ControlSize } from "@charcuterie/tokens"
2
+ import type {
3
+ KeyboardEvent as ReactKeyboardEvent,
4
+ ReactNode,
5
+ Ref,
6
+ } from "react"
7
+ import { useState } from "react"
8
+
9
+ import { FOCUS_RING_CLASS } from "../intentStyles.ts"
10
+ import { Tooltip } from "../Tooltip/Tooltip.tsx"
11
+ import { toClassName } from "../toClassName.ts"
12
+ import { CatalogPickerMark } from "./CatalogPickerMark.tsx"
13
+ import { RAIL_FAVORITE_STAR } from "./catalogPickerDrawings.tsx"
14
+ import type { CatalogPickerTab } from "./catalogPickerView.ts"
15
+
16
+ export type CatalogPickerRailProps = {
17
+ /** The tab whose rows the list shows, or `undefined` while a search shows every group. */
18
+ currentTabKey: string | undefined
19
+ itemSize: ControlSize
20
+ label: string
21
+ /** Escape on a rail button: the same clear-then-close the search field does. */
22
+ onEscape: (
23
+ keyboardEvent: ReactKeyboardEvent<HTMLElement>,
24
+ ) => void
25
+ /** Tab out of the rail (`isBackward` for Shift+Tab), back to the search field or its clear button. */
26
+ onLeave: (isBackward: boolean) => void
27
+ onShowTab: (tabKey: string) => void
28
+ ref?: Ref<HTMLDivElement>
29
+ tabs: readonly CatalogPickerTab[]
30
+ }
31
+
32
+ const RAIL_BUTTON_SIZE_CLASS: Record<ControlSize, string> =
33
+ {
34
+ lg: "size-(--control-height-lg)",
35
+ md: "size-(--control-height-md)",
36
+ sm: "size-(--control-height-sm)",
37
+ }
38
+
39
+ /**
40
+ * The vertical rail: Favorites, then one button per group.
41
+ *
42
+ * ### A toolbar of toggle buttons, not a `tablist`
43
+ *
44
+ * It looks like tabs, and the first build was going to be one. Two
45
+ * things the picker has to do are things a tablist cannot say:
46
+ *
47
+ * - **While a search runs, no tab is current.** The list shows matches
48
+ * from every group, so claiming one tab is selected would be false,
49
+ * and an APG tablist always has a selected tab.
50
+ * - **There is no tab panel.** What the tab changes is the contents of
51
+ * a listbox that the search field also drives; the listbox is not
52
+ * the tab's panel and cannot be `role="tabpanel"` as well.
53
+ *
54
+ * A `role="toolbar"` of `aria-pressed` buttons says exactly what is
55
+ * true: a set of switches, at most one pressed, that change what the
56
+ * list shows. The APG toolbar pattern also gives the keyboard contract
57
+ * for free — one tab stop, arrows move focus (vertically here), Home
58
+ * and End jump, Enter and Space press.
59
+ *
60
+ * ### The pointer keeps focus in the search field
61
+ *
62
+ * A mouse press on a rail button is prevented from taking focus, the
63
+ * way an option's is, so a click changes the list and the caret stays
64
+ * where the person can keep typing or arrowing. A keyboard user who
65
+ * tabbed onto the rail keeps focus on it.
66
+ *
67
+ * Each button's accessible name is the group's label, and the same
68
+ * text is its `Tooltip`, because the mark alone is an icon or two
69
+ * letters.
70
+ */
71
+ export const CatalogPickerRail = ({
72
+ currentTabKey,
73
+ itemSize,
74
+ label,
75
+ onEscape,
76
+ onLeave,
77
+ onShowTab,
78
+ ref,
79
+ tabs,
80
+ }: CatalogPickerRailProps): ReactNode => {
81
+ // The roving tab stop: the current tab while browsing, and whichever
82
+ // button the arrows last reached otherwise.
83
+ const [focusKey, setFocusKey] = useState(currentTabKey)
84
+
85
+ const tabStopKey =
86
+ tabs.find((tab) => tab.key === focusKey)?.key ??
87
+ tabs.find((tab) => tab.key === currentTabKey)?.key ??
88
+ tabs[0]?.key
89
+
90
+ // Wraps at both ends, as the APG toolbar allows. The buttons are
91
+ // found from the one that has focus, so two open pickers could never
92
+ // reach into each other's rail.
93
+ const moveFocus = (from: HTMLElement, index: number) => {
94
+ const wrappedIndex = (index + tabs.length) % tabs.length
95
+
96
+ const tab = tabs[wrappedIndex]
97
+
98
+ if (tab === undefined) {
99
+ return
100
+ }
101
+
102
+ setFocusKey(tab.key)
103
+
104
+ from
105
+ .closest('[role="toolbar"]')
106
+ ?.querySelectorAll<HTMLElement>("button")
107
+ [wrappedIndex]?.focus()
108
+ }
109
+
110
+ const handleKeyDown = (
111
+ keyboardEvent: ReactKeyboardEvent<HTMLButtonElement>,
112
+ index: number,
113
+ ) => {
114
+ switch (keyboardEvent.key) {
115
+ case "ArrowDown": {
116
+ keyboardEvent.preventDefault()
117
+ moveFocus(keyboardEvent.currentTarget, index + 1)
118
+
119
+ return
120
+ }
121
+ case "ArrowUp": {
122
+ keyboardEvent.preventDefault()
123
+ moveFocus(keyboardEvent.currentTarget, index - 1)
124
+
125
+ return
126
+ }
127
+ case "Home": {
128
+ keyboardEvent.preventDefault()
129
+ moveFocus(keyboardEvent.currentTarget, 0)
130
+
131
+ return
132
+ }
133
+ case "End": {
134
+ keyboardEvent.preventDefault()
135
+ moveFocus(
136
+ keyboardEvent.currentTarget,
137
+ tabs.length - 1,
138
+ )
139
+
140
+ return
141
+ }
142
+ case "Escape": {
143
+ onEscape(keyboardEvent)
144
+
145
+ return
146
+ }
147
+ case "Tab": {
148
+ keyboardEvent.preventDefault()
149
+ onLeave(keyboardEvent.shiftKey)
150
+
151
+ return
152
+ }
153
+ default:
154
+ }
155
+ }
156
+
157
+ return (
158
+ <div
159
+ aria-label={label}
160
+ aria-orientation="vertical"
161
+ className="charcuterie-scrollbar flex shrink-0 flex-col gap-1 overflow-y-auto overscroll-contain border-border-subtle border-e p-1.5"
162
+ ref={ref}
163
+ role="toolbar"
164
+ >
165
+ {tabs.map((tab, index) => {
166
+ const isCurrent = tab.key === currentTabKey
167
+
168
+ return (
169
+ <Tooltip
170
+ key={tab.key}
171
+ label={tab.label}
172
+ placement="right"
173
+ >
174
+ <button
175
+ aria-label={tab.label}
176
+ aria-pressed={isCurrent}
177
+ className={toClassName(
178
+ "flex shrink-0 cursor-pointer items-center justify-center rounded-md transition-colors duration-(--duration-fast) ease-standard",
179
+ RAIL_BUTTON_SIZE_CLASS[itemSize],
180
+ isCurrent
181
+ ? "bg-intent-accent-surface text-intent-accent-content"
182
+ : "text-content-secondary hover:bg-intent-neutral-surface-hover hover:text-content-primary",
183
+ FOCUS_RING_CLASS,
184
+ )}
185
+ onClick={() => {
186
+ setFocusKey(tab.key)
187
+ onShowTab(tab.key)
188
+ }}
189
+ onKeyDown={(keyboardEvent) => {
190
+ handleKeyDown(keyboardEvent, index)
191
+ }}
192
+ onMouseDown={(mouseEvent) => {
193
+ mouseEvent.preventDefault()
194
+ }}
195
+ tabIndex={tab.key === tabStopKey ? 0 : -1}
196
+ type="button"
197
+ >
198
+ {tab.kind === "favorites" ? (
199
+ RAIL_FAVORITE_STAR
200
+ ) : (
201
+ <CatalogPickerMark
202
+ accentColor={tab.group.accentColor}
203
+ icon={tab.group.icon}
204
+ initialsOf={tab.group.label}
205
+ size={itemSize}
206
+ />
207
+ )}
208
+ </button>
209
+ </Tooltip>
210
+ )
211
+ })}
212
+ </div>
213
+ )
214
+ }
@@ -0,0 +1,276 @@
1
+ import type { ControlSize } from "@charcuterie/tokens"
2
+ import type { ReactNode } from "react"
3
+ import { useState } from "react"
4
+
5
+ import type {
6
+ CatalogPickerGroup,
7
+ CatalogPickerItem,
8
+ } from "./CatalogPicker.tsx"
9
+ import { CatalogPicker } from "./CatalogPicker.tsx"
10
+ import { CatalogPickerPanel } from "./CatalogPickerPanel.tsx"
11
+
12
+ /**
13
+ * Story-only fixtures for `CatalogPicker`. **Nothing here ships**, and
14
+ * every provider, model and account below is invented.
15
+ *
16
+ * The icons are hand-drawn shapes, as in `icons.storyHelpers.tsx`: an
17
+ * app passes its own provider logos, and a shape inheriting
18
+ * `currentColor` is the honest stand-in for one.
19
+ */
20
+ const iconProps = {
21
+ className: "size-[1.15em] shrink-0",
22
+ fill: "currentColor",
23
+ focusable: false,
24
+ viewBox: "0 0 24 24",
25
+ xmlns: "http://www.w3.org/2000/svg",
26
+ } as const
27
+
28
+ export const TriangleIcon = (): ReactNode => (
29
+ <svg {...iconProps} aria-hidden="true">
30
+ <path d="M12 3 22 20H2z" />
31
+ </svg>
32
+ )
33
+
34
+ export const HexagonIcon = (): ReactNode => (
35
+ <svg {...iconProps} aria-hidden="true">
36
+ <path d="m12 2 8.66 5v10L12 22l-8.66-5V7z" />
37
+ </svg>
38
+ )
39
+
40
+ export const DiamondIcon = (): ReactNode => (
41
+ <svg {...iconProps} aria-hidden="true">
42
+ <path d="m12 2 10 10-10 10L2 12z" />
43
+ </svg>
44
+ )
45
+
46
+ /** The clear button's glyph, drawn: `✕` measures blank in this Chromium. */
47
+ export const ClearIcon = (): ReactNode => (
48
+ <svg
49
+ {...iconProps}
50
+ aria-hidden="true"
51
+ fill="none"
52
+ stroke="currentColor"
53
+ strokeLinecap="round"
54
+ strokeWidth={2}
55
+ >
56
+ <path d="M6 6l12 12M18 6 6 18" />
57
+ </svg>
58
+ )
59
+
60
+ /**
61
+ * Six groups: three with icons, two on initials badges, and a second
62
+ * account of one provider told apart by an accent ring.
63
+ */
64
+ export const CATALOG_GROUPS: readonly CatalogPickerGroup[] =
65
+ [
66
+ {
67
+ icon: <TriangleIcon />,
68
+ id: "northwind",
69
+ label: "Northwind",
70
+ },
71
+ {
72
+ icon: <HexagonIcon />,
73
+ id: "contoso",
74
+ label: "Contoso",
75
+ },
76
+ {
77
+ accentColor: "var(--color-categorical-6-solid)",
78
+ icon: <HexagonIcon />,
79
+ id: "contoso-work",
80
+ label: "Contoso (Work)",
81
+ },
82
+ {
83
+ icon: <DiamondIcon />,
84
+ id: "fabrikam",
85
+ label: "Fabrikam",
86
+ },
87
+ { id: "tailspin", label: "Tailspin Labs" },
88
+ {
89
+ accentColor: "var(--color-categorical-2-solid)",
90
+ id: "litware",
91
+ label: "Litware",
92
+ },
93
+ // No rows: left off the rail.
94
+ { id: "adatum", label: "Adatum" },
95
+ ]
96
+
97
+ const toModels = (
98
+ groupId: string,
99
+ prefix: string,
100
+ names: readonly string[],
101
+ description?: (slug: string) => string,
102
+ ): CatalogPickerItem[] =>
103
+ names.map((name) => {
104
+ const slug = `${prefix}-${name
105
+ .toLowerCase()
106
+ .replace(/[^a-z0-9]+/gu, "-")
107
+ .replace(/-$/u, "")}`
108
+
109
+ return {
110
+ description:
111
+ description === undefined
112
+ ? slug
113
+ : description(slug),
114
+ groupId,
115
+ label: name,
116
+ value: `${groupId}/${slug}`,
117
+ }
118
+ })
119
+
120
+ const CONTOSO_MODELS = [
121
+ "Lumen 4",
122
+ "Lumen 4 Mini",
123
+ "Lumen 3.5",
124
+ "Prism 2",
125
+ "Prism 2 Fast",
126
+ "Beacon 1",
127
+ "Beacon 1 Long Context",
128
+ ]
129
+
130
+ /** Forty-two invented models across six groups, plus a pinned default. */
131
+ export const CATALOG_ITEMS: readonly CatalogPickerItem[] = [
132
+ {
133
+ description: "Whatever the app picks",
134
+ groupId: null,
135
+ label: "Default",
136
+ pinned: "start",
137
+ value: "default",
138
+ },
139
+ ...toModels("northwind", "nw", [
140
+ "Swift 3",
141
+ "Swift 3 Mini",
142
+ "Swift 2.5",
143
+ "Atlas 2",
144
+ "Atlas 2 Reasoning",
145
+ "Atlas 1",
146
+ "Harbor 5",
147
+ "Harbor 5 Turbo",
148
+ "Harbor 4",
149
+ ]),
150
+ ...toModels("contoso", "ct", CONTOSO_MODELS),
151
+ ...toModels(
152
+ "contoso-work",
153
+ "ct",
154
+ CONTOSO_MODELS,
155
+ () => "Work account",
156
+ ),
157
+ ...toModels("fabrikam", "fb", [
158
+ "Loom 7",
159
+ "Loom 7 Small",
160
+ "Loom 6",
161
+ "Weave Code 2",
162
+ "Weave Code 1",
163
+ "Spindle Vision",
164
+ ]),
165
+ {
166
+ ...toModels("fabrikam", "fb", ["Loom 5 (retired)"])[0],
167
+ isDisabled: true,
168
+ } as CatalogPickerItem,
169
+ ...toModels("tailspin", "ts", [
170
+ "Glide 2",
171
+ "Glide 2 Nano",
172
+ "Glide 1",
173
+ "Updraft 3",
174
+ "Updraft 3 Instruct",
175
+ "Tailwind Research Preview With A Very Long Name",
176
+ ]),
177
+ ...toModels("litware", "lw", [
178
+ "Quill 4",
179
+ "Quill 4 Lite",
180
+ "Margin 2",
181
+ "Folio 1",
182
+ "Ledger Experimental",
183
+ ]),
184
+ {
185
+ description: "Ask every time",
186
+ groupId: null,
187
+ label: "Choose per task",
188
+ pinned: "end",
189
+ value: "per-task",
190
+ },
191
+ ]
192
+
193
+ export const CATALOG_FAVORITES: readonly string[] = [
194
+ "contoso/ct-lumen-4",
195
+ "northwind/nw-swift-3",
196
+ "contoso-work/ct-lumen-4",
197
+ "tailspin/ts-glide-2",
198
+ ]
199
+
200
+ /** A picker whose value is the story's own state, so choosing updates the trigger. */
201
+ export const CatalogPickerHarness = ({
202
+ favorites = CATALOG_FAVORITES,
203
+ groups = CATALOG_GROUPS,
204
+ initialValue,
205
+ isDisabled,
206
+ items = CATALOG_ITEMS,
207
+ label = "Agent",
208
+ placeholder,
209
+ size,
210
+ }: {
211
+ favorites?: readonly string[]
212
+ groups?: readonly CatalogPickerGroup[]
213
+ initialValue?: string
214
+ isDisabled?: boolean
215
+ items?: readonly CatalogPickerItem[]
216
+ label?: string
217
+ placeholder?: string
218
+ size?: ControlSize
219
+ }): ReactNode => {
220
+ const [value, setValue] = useState(initialValue)
221
+
222
+ return (
223
+ <CatalogPicker
224
+ clearIcon={<ClearIcon />}
225
+ favorites={favorites}
226
+ groups={groups}
227
+ isDisabled={isDisabled}
228
+ items={items}
229
+ label={label}
230
+ onChange={setValue}
231
+ placeholder={placeholder}
232
+ searchPlaceholder="Search models…"
233
+ size={size}
234
+ value={value}
235
+ />
236
+ )
237
+ }
238
+
239
+ /**
240
+ * The panel drawn in place, on the overlay's surface and at its size,
241
+ * so a board can show a state the closed picker hides — a tab, a
242
+ * search, no matches — side by side and in a screenshot.
243
+ */
244
+ export const InlineCatalogPanel = ({
245
+ defaultQuery,
246
+ label,
247
+ size = "md",
248
+ value,
249
+ }: {
250
+ defaultQuery?: string
251
+ label: string
252
+ size?: ControlSize
253
+ value?: string
254
+ }): ReactNode => {
255
+ const [chosen, setChosen] = useState(value)
256
+
257
+ return (
258
+ <div className="flex h-[28rem] w-[26rem] max-w-full overflow-hidden rounded-md border border-border-default bg-surface-overlay shadow-medium">
259
+ <CatalogPickerPanel
260
+ clearIcon={<ClearIcon />}
261
+ defaultQuery={defaultQuery}
262
+ emptyLabel="No matches"
263
+ favorites={CATALOG_FAVORITES}
264
+ favoritesLabel="Favorites"
265
+ groups={CATALOG_GROUPS}
266
+ itemSize={size}
267
+ items={CATALOG_ITEMS}
268
+ label={label}
269
+ onDismiss={() => undefined}
270
+ onSelect={setChosen}
271
+ searchPlaceholder="Search models…"
272
+ value={chosen}
273
+ />
274
+ </div>
275
+ )
276
+ }
@@ -0,0 +1,51 @@
1
+ import type { CSSProperties } from "react"
2
+
3
+ /**
4
+ * The picker's two small drawings — the favorite star and the chosen
5
+ * row's check — drawn in CSS rather than typed.
6
+ *
7
+ * The check was the `✓` character in the first build, and it measured
8
+ * blank in this repo's headless Chromium: the chosen row showed no
9
+ * mark at all. A drawing has no font to lack.
10
+ */
11
+
12
+ /**
13
+ * A five-point star, drawn in CSS: a `currentColor` box clipped to a
14
+ * polygon. Not a glyph — `★` renders as nothing where the font lacks
15
+ * it — and not an SVG asset, which the library does not ship
16
+ * ([decision](../../../../docs/decisions/2026-07-29-ship-no-icons-and-no-symbol-glyphs.md)).
17
+ * The same reasoning `ActionMenu`'s CSS dots follow.
18
+ */
19
+ const STAR_STYLE: CSSProperties = {
20
+ clipPath:
21
+ "polygon(50% 0%, 61.8% 35.4%, 100% 36.2%, 69.1% 59.1%, 79.4% 95.1%, 50% 74%, 20.6% 95.1%, 30.9% 59.1%, 0% 36.2%, 38.2% 35.4%)",
22
+ }
23
+
24
+ /** The small filled star on a favorite's row. */
25
+ export const ROW_FAVORITE_STAR = (
26
+ <span
27
+ aria-hidden="true"
28
+ className="block size-3 shrink-0 bg-current text-intent-warning-content"
29
+ style={STAR_STYLE}
30
+ />
31
+ )
32
+
33
+ /** The favorites tab's mark on the rail. Takes the tab's text color. */
34
+ export const RAIL_FAVORITE_STAR = (
35
+ <span
36
+ aria-hidden="true"
37
+ className="block size-4 shrink-0 bg-current"
38
+ style={STAR_STYLE}
39
+ />
40
+ )
41
+
42
+ /**
43
+ * The chosen row's check: the bottom and end edges of a narrow box,
44
+ * turned 45 degrees. `currentColor`, so it takes the row's accent.
45
+ */
46
+ export const CHOSEN_CHECK = (
47
+ <span
48
+ aria-hidden="true"
49
+ className="-mt-1 block h-2.5 w-1.5 rotate-45 border-current border-e-2 border-b-2"
50
+ />
51
+ )