@vendure-io/ui 2.2.0 → 3.0.0-next.1
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/package.json +19 -5
- package/src/components/atoms/accordion.tsx +21 -6
- package/src/components/atoms/alert-dialog.tsx +31 -6
- package/src/components/atoms/alert.tsx +72 -18
- package/src/components/atoms/avatar.tsx +12 -4
- package/src/components/atoms/badge.tsx +7 -2
- package/src/components/atoms/breadcrumb.tsx +493 -4
- package/src/components/atoms/button-group.tsx +30 -6
- package/src/components/atoms/button.tsx +128 -18
- package/src/components/atoms/calendar.tsx +6 -6
- package/src/components/atoms/card.tsx +130 -16
- package/src/components/atoms/carousel.tsx +16 -9
- package/src/components/atoms/chart.tsx +87 -1
- package/src/components/atoms/checkbox.tsx +85 -7
- package/src/components/atoms/collapsible.tsx +2 -1
- package/src/components/atoms/combobox.tsx +358 -29
- package/src/components/atoms/command.tsx +336 -13
- package/src/components/atoms/context-menu.tsx +28 -23
- package/src/components/atoms/dialog.tsx +173 -29
- package/src/components/atoms/direction.tsx +23 -2
- package/src/components/atoms/drawer.tsx +15 -10
- package/src/components/atoms/dropdown-menu.tsx +43 -28
- package/src/components/atoms/empty.tsx +5 -1
- package/src/components/atoms/field.tsx +41 -15
- package/src/components/atoms/hover-card.tsx +61 -8
- package/src/components/atoms/input-group.tsx +19 -11
- package/src/components/atoms/input-otp.tsx +6 -6
- package/src/components/atoms/input.tsx +7 -1
- package/src/components/atoms/item.tsx +3 -3
- package/src/components/atoms/kbd.tsx +32 -3
- package/src/components/atoms/label-swap.tsx +113 -0
- package/src/components/atoms/label.tsx +1 -1
- package/src/components/atoms/menubar.tsx +28 -26
- package/src/components/atoms/native-select.tsx +3 -2
- package/src/components/atoms/navigation-menu.tsx +13 -8
- package/src/components/atoms/pagination.tsx +2 -2
- package/src/components/atoms/popover.tsx +29 -6
- package/src/components/atoms/progress.tsx +46 -33
- package/src/components/atoms/radio-group.tsx +9 -4
- package/src/components/atoms/resizable.tsx +1 -1
- package/src/components/atoms/scroll-area.tsx +2 -2
- package/src/components/atoms/select.tsx +258 -19
- package/src/components/atoms/sheet.tsx +76 -23
- package/src/components/atoms/sidebar.tsx +405 -23
- package/src/components/atoms/skeleton.tsx +1 -1
- package/src/components/atoms/slider.tsx +1 -1
- package/src/components/atoms/sonner.tsx +128 -38
- package/src/components/atoms/spinner.tsx +1 -1
- package/src/components/atoms/switch.tsx +10 -5
- package/src/components/atoms/table.tsx +147 -7
- package/src/components/atoms/tabs.tsx +3 -3
- package/src/components/atoms/textarea.tsx +3 -1
- package/src/components/atoms/toggle-group.tsx +4 -2
- package/src/components/atoms/toggle.tsx +28 -6
- package/src/components/atoms/tooltip.tsx +85 -15
- package/src/components/molecules/anonymized-token.tsx +24 -8
- package/src/components/molecules/app-shell.tsx +53 -20
- package/src/components/molecules/arrow-link.tsx +40 -0
- package/src/components/molecules/chip.tsx +43 -4
- package/src/components/molecules/code-block.tsx +54 -31
- package/src/components/molecules/collapsible-section.tsx +129 -0
- package/src/components/molecules/confirm-dialog.tsx +122 -9
- package/src/components/molecules/copyable-text.tsx +75 -11
- package/src/components/molecules/count-badge.tsx +115 -0
- package/src/components/molecules/data-table/data-table-bulk-actions.tsx +32 -4
- package/src/components/molecules/data-table/data-table-column-header.tsx +20 -3
- package/src/components/molecules/data-table/data-table-filters.tsx +62 -22
- package/src/components/molecules/data-table/data-table-helpers.tsx +88 -1
- package/src/components/molecules/data-table/data-table-selection-storage.tsx +115 -0
- package/src/components/molecules/data-table/data-table-types.tsx +160 -4
- package/src/components/molecules/data-table/data-table-view-options.tsx +2 -2
- package/src/components/molecules/data-table/data-table.tsx +869 -81
- package/src/components/molecules/data-table/table-pagination.tsx +2 -2
- package/src/components/molecules/date-picker.tsx +13 -7
- package/src/components/molecules/date-range-picker.tsx +13 -6
- package/src/components/molecules/date-time-picker.tsx +13 -7
- package/src/components/molecules/date-time.tsx +8 -1
- package/src/components/molecules/direction-link.tsx +130 -0
- package/src/components/molecules/elapsed-time.tsx +223 -0
- package/src/components/molecules/file-dropzone.tsx +2 -2
- package/src/components/molecules/id-chip.tsx +169 -32
- package/src/components/molecules/inline-code.tsx +4 -0
- package/src/components/molecules/issued-secret.tsx +318 -0
- package/src/components/molecules/list-row.tsx +349 -0
- package/src/components/molecules/live-indicator.tsx +83 -0
- package/src/components/molecules/page-header.tsx +117 -5
- package/src/components/molecules/password-input.tsx +2 -2
- package/src/components/molecules/relative-time.tsx +85 -56
- package/src/components/molecules/search-trigger.tsx +111 -0
- package/src/components/molecules/secret-value.tsx +29 -9
- package/src/components/molecules/skip-link.tsx +1 -1
- package/src/components/molecules/stat-card.tsx +4 -4
- package/src/components/molecules/state-views/empty-state.tsx +35 -17
- package/src/components/molecules/state-views/error-state.tsx +32 -14
- package/src/components/molecules/status-badge.tsx +415 -49
- package/src/components/molecules/time-tooltip.tsx +140 -0
- package/src/components/molecules/timeline.tsx +246 -0
- package/src/components/molecules/visibility-icon.tsx +89 -0
- package/src/hooks/use-active-label-size.ts +66 -0
- package/src/hooks/use-apple-platform.ts +26 -0
- package/src/hooks/use-pinned-height.ts +55 -0
- package/src/hooks/use-scrolled.ts +27 -0
- package/src/lib/content-inset.ts +17 -0
- package/src/lib/control-name.ts +55 -0
- package/src/lib/dialog-padding.ts +48 -0
- package/src/lib/field-surface.ts +31 -0
- package/src/lib/format-elapsed.ts +67 -0
- package/src/lib/format-relative.ts +29 -1
- package/src/lib/highlight.ts +3 -3
- package/src/lib/menu-item.tsx +72 -0
- package/src/lib/outline-edge.ts +21 -0
- package/src/lib/popup-align.ts +67 -0
- package/src/lib/popup-direction.ts +22 -0
- package/src/lib/popup-motion.ts +19 -0
- package/src/lib/state-dictionary.ts +42 -6
- package/src/lib/syntax-colors.ts +8 -0
- package/src/lib/tint-recipes.ts +68 -0
- package/src/lib/tone-icons.ts +24 -0
- package/src/lib/trigger-slot.ts +16 -0
- package/src/lib/utils.ts +97 -1
|
@@ -4,6 +4,10 @@ import * as React from "react"
|
|
|
4
4
|
import { Combobox as ComboboxPrimitive } from "@base-ui/react"
|
|
5
5
|
|
|
6
6
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
7
|
+
import { MenuCheck, menuCheckItem, menuItemDisabled } from "../../lib/menu-item.tsx"
|
|
8
|
+
import { statusChipRing } from "@vendure-io/ui/lib/tint-recipes"
|
|
9
|
+
import { fieldSurface, fieldSurfaceWithin } from "@vendure-io/ui/lib/field-surface"
|
|
10
|
+
import { popupMotion } from "../../lib/popup-motion.ts"
|
|
7
11
|
import { Button } from "@vendure-io/ui/components/atoms/button"
|
|
8
12
|
import {
|
|
9
13
|
InputGroup,
|
|
@@ -11,9 +15,146 @@ import {
|
|
|
11
15
|
InputGroupButton,
|
|
12
16
|
InputGroupInput,
|
|
13
17
|
} from "@vendure-io/ui/components/atoms/input-group"
|
|
14
|
-
import { ChevronDownIcon,
|
|
18
|
+
import { ChevronDownIcon, Loader2Icon, XIcon } from "lucide-react"
|
|
19
|
+
import { usePopupDir } from "../../lib/popup-direction.ts"
|
|
20
|
+
import { usePopupAlign } from "../../lib/popup-align.ts"
|
|
21
|
+
import { hasName, nameFrom, useRegisteredRef } from "../../lib/control-name.ts"
|
|
15
22
|
|
|
16
|
-
|
|
23
|
+
/**
|
|
24
|
+
* The controls of the nearest Combobox. Base UI names the trigger and the
|
|
25
|
+
* input but not the popup or the list, so ComboboxContent and ComboboxList
|
|
26
|
+
* take their name from here: the field trigger when there is one (the search
|
|
27
|
+
* input then sits in the popup), else the input.
|
|
28
|
+
*/
|
|
29
|
+
type ComboboxControls = {
|
|
30
|
+
trigger: HTMLElement | null
|
|
31
|
+
input: HTMLElement | null
|
|
32
|
+
setTrigger: (trigger: HTMLElement | null) => void
|
|
33
|
+
setInput: (input: HTMLElement | null) => void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const ComboboxControlsContext = React.createContext<ComboboxControls | null>(null)
|
|
37
|
+
|
|
38
|
+
type ComboboxPagingProps = {
|
|
39
|
+
/**
|
|
40
|
+
* Asks for the next page of a server-searched list. Called once when the
|
|
41
|
+
* list scrolls near its end, or the arrow keys reach its last option, while
|
|
42
|
+
* `hasMore` is set and `loading` is not. It is called again once the
|
|
43
|
+
* options change, the search changes, the list reopens, or the list
|
|
44
|
+
* scrolls away from its end and back: a failed fetch is retried by
|
|
45
|
+
* scrolling, never in a loop.
|
|
46
|
+
*/
|
|
47
|
+
onLoadMore?: () => void
|
|
48
|
+
/** There is a next page for `onLoadMore` to fetch. */
|
|
49
|
+
hasMore?: boolean
|
|
50
|
+
/**
|
|
51
|
+
* A page is loading: the list shows a loading row at its end, is busy for
|
|
52
|
+
* assistive technology and announces `loadingLabel`. With no options yet,
|
|
53
|
+
* the loading row takes the place of ComboboxEmpty.
|
|
54
|
+
*/
|
|
55
|
+
loading?: boolean
|
|
56
|
+
/** Announced to screen readers while `loading` is set. */
|
|
57
|
+
loadingLabel?: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The paging state of the nearest Combobox, for its list and its empty state.
|
|
62
|
+
* Null when the Combobox takes none of the paging props.
|
|
63
|
+
*/
|
|
64
|
+
type ComboboxPaging = {
|
|
65
|
+
loading: boolean
|
|
66
|
+
loadingLabel: string
|
|
67
|
+
canLoadMore: boolean
|
|
68
|
+
list: HTMLElement | null
|
|
69
|
+
setList: (list: HTMLElement | null) => void
|
|
70
|
+
/** Calls `onLoadMore` once per set of options. */
|
|
71
|
+
requestMore: () => void
|
|
72
|
+
/** Lets the next request through: the options or the search changed. */
|
|
73
|
+
rearm: () => void
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const ComboboxPagingContext = React.createContext<ComboboxPaging | null>(null)
|
|
77
|
+
|
|
78
|
+
function Combobox<Value, Multiple extends boolean | undefined = false, Item = Value>({
|
|
79
|
+
onLoadMore,
|
|
80
|
+
hasMore = false,
|
|
81
|
+
loading,
|
|
82
|
+
loadingLabel = "Loading…",
|
|
83
|
+
onItemHighlighted,
|
|
84
|
+
onInputValueChange,
|
|
85
|
+
...props
|
|
86
|
+
}: ComboboxPrimitive.Root.Props<Value, Multiple, Item> & ComboboxPagingProps) {
|
|
87
|
+
const [trigger, setTrigger] = React.useState<HTMLElement | null>(null)
|
|
88
|
+
const [input, setInput] = React.useState<HTMLElement | null>(null)
|
|
89
|
+
const [list, setList] = React.useState<HTMLElement | null>(null)
|
|
90
|
+
const controls = React.useMemo(() => ({ trigger, input, setTrigger, setInput }), [trigger, input])
|
|
91
|
+
|
|
92
|
+
// Read at call time, so an observer set up on an earlier render sees the
|
|
93
|
+
// current flags.
|
|
94
|
+
const latest = React.useRef({ onLoadMore, hasMore, loading })
|
|
95
|
+
React.useLayoutEffect(() => {
|
|
96
|
+
latest.current = { onLoadMore, hasMore, loading }
|
|
97
|
+
})
|
|
98
|
+
// Set once a page is asked for. Scrolling fires many events before the
|
|
99
|
+
// consumer's `loading` flag turns on; the latch keeps that to one call.
|
|
100
|
+
const latched = React.useRef(false)
|
|
101
|
+
const requestMore = React.useCallback(() => {
|
|
102
|
+
const current = latest.current
|
|
103
|
+
if (!current.onLoadMore || !current.hasMore || current.loading || latched.current) return
|
|
104
|
+
latched.current = true
|
|
105
|
+
current.onLoadMore()
|
|
106
|
+
}, [])
|
|
107
|
+
const rearm = React.useCallback(() => {
|
|
108
|
+
latched.current = false
|
|
109
|
+
}, [])
|
|
110
|
+
|
|
111
|
+
const canLoadMore = onLoadMore !== undefined
|
|
112
|
+
const enabled = canLoadMore || loading !== undefined
|
|
113
|
+
const paging = React.useMemo(
|
|
114
|
+
() =>
|
|
115
|
+
enabled
|
|
116
|
+
? {
|
|
117
|
+
loading: !!loading,
|
|
118
|
+
loadingLabel,
|
|
119
|
+
canLoadMore,
|
|
120
|
+
list,
|
|
121
|
+
setList,
|
|
122
|
+
requestMore,
|
|
123
|
+
rearm,
|
|
124
|
+
}
|
|
125
|
+
: null,
|
|
126
|
+
[enabled, loading, loadingLabel, canLoadMore, list, requestMore, rearm]
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<ComboboxControlsContext.Provider value={controls}>
|
|
131
|
+
<ComboboxPagingContext.Provider value={paging}>
|
|
132
|
+
<ComboboxPrimitive.Root
|
|
133
|
+
{...props}
|
|
134
|
+
onInputValueChange={(value, details) => {
|
|
135
|
+
rearm()
|
|
136
|
+
onInputValueChange?.(value, details)
|
|
137
|
+
}}
|
|
138
|
+
onItemHighlighted={(value, details) => {
|
|
139
|
+
onItemHighlighted?.(value, details)
|
|
140
|
+
// The arrow keys reached the last option: load the next page, as
|
|
141
|
+
// scrolling there would. Counted from the DOM, so groups and rows
|
|
142
|
+
// that are not part of the page line up.
|
|
143
|
+
if (details.reason !== "keyboard" || !list) return
|
|
144
|
+
if (details.index === list.querySelectorAll("[role=option]").length - 1) requestMore()
|
|
145
|
+
}}
|
|
146
|
+
/>
|
|
147
|
+
</ComboboxPagingContext.Provider>
|
|
148
|
+
</ComboboxControlsContext.Provider>
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The name of the Combobox, for its popup and its list. */
|
|
153
|
+
function useComboboxName() {
|
|
154
|
+
const controls = React.useContext(ComboboxControlsContext)
|
|
155
|
+
if (controls?.trigger) return nameFrom(controls.trigger, { orSelf: true })
|
|
156
|
+
return nameFrom(controls?.input ?? null)
|
|
157
|
+
}
|
|
17
158
|
|
|
18
159
|
function ComboboxValue({ ...props }: ComboboxPrimitive.Value.Props) {
|
|
19
160
|
return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />
|
|
@@ -27,16 +168,20 @@ function ComboboxTrigger({
|
|
|
27
168
|
className,
|
|
28
169
|
size = "default",
|
|
29
170
|
children,
|
|
171
|
+
ref,
|
|
30
172
|
...props
|
|
31
173
|
}: ComboboxPrimitive.Trigger.Props & {
|
|
32
174
|
size?: "sm" | "default"
|
|
33
175
|
}) {
|
|
176
|
+
const triggerRef = useRegisteredRef(ref, React.useContext(ComboboxControlsContext)?.setTrigger)
|
|
34
177
|
return (
|
|
35
178
|
<ComboboxPrimitive.Trigger
|
|
179
|
+
ref={triggerRef}
|
|
36
180
|
data-slot="combobox-trigger"
|
|
37
181
|
data-size={size}
|
|
38
182
|
className={cn(
|
|
39
|
-
|
|
183
|
+
fieldSurface,
|
|
184
|
+
"data-placeholder:text-faint-foreground flex w-fit items-center justify-between gap-1.5 px-2.5 py-2 text-sm whitespace-nowrap disabled:cursor-not-allowed disabled:opacity-50 data-[size=default]:h-8.5 data-[size=sm]:h-7 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
40
185
|
className
|
|
41
186
|
)}
|
|
42
187
|
{...props}
|
|
@@ -47,10 +192,15 @@ function ComboboxTrigger({
|
|
|
47
192
|
)
|
|
48
193
|
}
|
|
49
194
|
|
|
50
|
-
|
|
195
|
+
// Icon-only, so it carries a name. Like the open button, it stays out of the
|
|
196
|
+
// tab order: Backspace clears the input from the keyboard, and while the list
|
|
197
|
+
// is open Base UI hides it from assistive technology.
|
|
198
|
+
function ComboboxClear({ className, "aria-label": ariaLabel = "Clear", ...props }: ComboboxPrimitive.Clear.Props) {
|
|
51
199
|
return (
|
|
52
200
|
<ComboboxPrimitive.Clear
|
|
53
201
|
data-slot="combobox-clear"
|
|
202
|
+
aria-label={ariaLabel}
|
|
203
|
+
tabIndex={-1}
|
|
54
204
|
render={<InputGroupButton variant="ghost" size="icon-xs" />}
|
|
55
205
|
className={cn(className)}
|
|
56
206
|
{...props}
|
|
@@ -67,24 +217,38 @@ function ComboboxInput({
|
|
|
67
217
|
showTrigger = true,
|
|
68
218
|
showClear = false,
|
|
69
219
|
triggerLabel = "Open options",
|
|
220
|
+
clearLabel = "Clear",
|
|
221
|
+
ref,
|
|
70
222
|
...props
|
|
71
223
|
}: ComboboxPrimitive.Input.Props & {
|
|
72
224
|
showTrigger?: boolean
|
|
73
225
|
showClear?: boolean
|
|
74
226
|
/** Accessible name of the icon-only trigger button. */
|
|
75
227
|
triggerLabel?: string
|
|
228
|
+
/** Accessible name of the icon-only clear button. */
|
|
229
|
+
clearLabel?: string
|
|
76
230
|
}) {
|
|
231
|
+
const inputRef = useRegisteredRef(ref, React.useContext(ComboboxControlsContext)?.setInput)
|
|
77
232
|
return (
|
|
78
233
|
<InputGroup className={cn("w-auto", className)}>
|
|
79
234
|
<ComboboxPrimitive.Input
|
|
235
|
+
ref={inputRef}
|
|
80
236
|
render={<InputGroupInput disabled={disabled} />}
|
|
81
237
|
{...props}
|
|
82
238
|
/>
|
|
83
239
|
<InputGroupAddon align="inline-end">
|
|
84
240
|
{showTrigger && (
|
|
85
241
|
// The bare primitive, not ComboboxTrigger: the icon button inside
|
|
86
|
-
// the input group must not take the field styling.
|
|
242
|
+
// the input group must not take the field styling. It stays out of
|
|
243
|
+
// the tab order, as in the ARIA combobox pattern: the input opens
|
|
244
|
+
// the list with the arrow keys. While the list is open, Base UI
|
|
245
|
+
// hides everything but the input and the list from assistive
|
|
246
|
+
// technology, and a hidden element must not be tabbable.
|
|
247
|
+
// ComboboxPrimitive.Trigger renders a native <button>, so Base UI is
|
|
248
|
+
// told so: Button turns nativeButton off when `render` is set.
|
|
87
249
|
<InputGroupButton
|
|
250
|
+
nativeButton
|
|
251
|
+
tabIndex={-1}
|
|
88
252
|
size="icon-xs"
|
|
89
253
|
variant="ghost"
|
|
90
254
|
render={<ComboboxPrimitive.Trigger />}
|
|
@@ -96,40 +260,88 @@ function ComboboxInput({
|
|
|
96
260
|
<ChevronDownIcon className="text-muted-foreground pointer-events-none size-4" />
|
|
97
261
|
</InputGroupButton>
|
|
98
262
|
)}
|
|
99
|
-
{showClear && <ComboboxClear disabled={disabled} />}
|
|
263
|
+
{showClear && <ComboboxClear aria-label={clearLabel} disabled={disabled} />}
|
|
100
264
|
</InputGroupAddon>
|
|
101
265
|
{children}
|
|
102
266
|
</InputGroup>
|
|
103
267
|
)
|
|
104
268
|
}
|
|
105
269
|
|
|
270
|
+
/**
|
|
271
|
+
* The element a list lines up with, other than an explicit `anchor`: the
|
|
272
|
+
* field trigger when there is one (the search input then sits in the popup),
|
|
273
|
+
* else the input's whole field group. Base UI would take the bare input,
|
|
274
|
+
* which stops short of the open and clear buttons, so a list lined up on
|
|
275
|
+
* its right edge would end before the field does.
|
|
276
|
+
*/
|
|
277
|
+
function fieldAnchor(controls: ComboboxControls | null): Element | null {
|
|
278
|
+
if (controls?.trigger) return controls.trigger
|
|
279
|
+
return controls?.input?.closest("[data-slot=input-group]") ?? controls?.input ?? null
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function resolveAnchor(anchor: ComboboxPrimitive.Positioner.Props["anchor"]): Element | null {
|
|
283
|
+
const resolved = typeof anchor === "function" ? anchor() : anchor && "current" in anchor ? anchor.current : anchor
|
|
284
|
+
return resolved instanceof Element ? resolved : null
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Without an `align`, the list opens toward the centre of the screen, as a
|
|
289
|
+
* DropdownMenu does: a field in the right half lines the list up with its
|
|
290
|
+
* right edge, one in the left half with its left edge. This shows only when
|
|
291
|
+
* the list is wider than its field. An explicit `align` always wins.
|
|
292
|
+
*/
|
|
106
293
|
function ComboboxContent({
|
|
107
294
|
className,
|
|
108
295
|
side = "bottom",
|
|
109
296
|
sideOffset = 6,
|
|
110
|
-
align
|
|
297
|
+
align,
|
|
111
298
|
alignOffset = 0,
|
|
112
299
|
anchor,
|
|
300
|
+
ref,
|
|
113
301
|
...props
|
|
114
302
|
}: ComboboxPrimitive.Popup.Props &
|
|
115
303
|
Pick<
|
|
116
304
|
ComboboxPrimitive.Positioner.Props,
|
|
117
305
|
"side" | "align" | "sideOffset" | "alignOffset" | "anchor"
|
|
118
306
|
>) {
|
|
307
|
+
const dir = usePopupDir()
|
|
308
|
+
const name = useComboboxName()
|
|
309
|
+
const controls = React.useContext(ComboboxControlsContext)
|
|
310
|
+
// Without a field trigger, the list lines up with the whole field group.
|
|
311
|
+
const fieldGroup = !anchor && !controls?.trigger ? fieldAnchor(controls) : null
|
|
312
|
+
const getTrigger = React.useCallback(
|
|
313
|
+
() => resolveAnchor(anchor) ?? fieldAnchor(controls),
|
|
314
|
+
[anchor, controls]
|
|
315
|
+
)
|
|
316
|
+
const { popupRef, align: positionAlign } = usePopupAlign(align, side, ref, getTrigger)
|
|
119
317
|
return (
|
|
120
318
|
<ComboboxPrimitive.Portal>
|
|
121
319
|
<ComboboxPrimitive.Positioner
|
|
320
|
+
dir={dir}
|
|
122
321
|
side={side}
|
|
123
322
|
sideOffset={sideOffset}
|
|
124
|
-
align={
|
|
323
|
+
align={positionAlign}
|
|
125
324
|
alignOffset={alignOffset}
|
|
126
|
-
anchor={anchor}
|
|
325
|
+
anchor={anchor ?? fieldGroup ?? undefined}
|
|
127
326
|
className="isolate z-50"
|
|
128
327
|
>
|
|
129
328
|
<ComboboxPrimitive.Popup
|
|
329
|
+
ref={popupRef}
|
|
130
330
|
data-slot="combobox-content"
|
|
131
331
|
data-chips={!!anchor}
|
|
132
|
-
|
|
332
|
+
data-field-group={!!fieldGroup}
|
|
333
|
+
className={cn("bg-popover text-popover-foreground shadow-overlay group/combobox-content relative max-h-(--available-height) w-(--anchor-width) max-w-(--available-width) min-w-[calc(var(--anchor-width)+--spacing(7))] overflow-hidden rounded-xl data-[chips=true]:min-w-(--anchor-width) data-[field-group=true]:min-w-(--anchor-width) *:data-[slot=input-group]:m-1 *:data-[slot=input-group]:mb-0 *:data-[slot=input-group]:h-8 *:data-[slot=input-group]:shadow-none", popupMotion, className )}
|
|
334
|
+
// A closing list is gone for assistive technology: the input no
|
|
335
|
+
// longer points at it, and it takes no input while it fades out.
|
|
336
|
+
// With the search input inside it, the popup is a dialog: it takes
|
|
337
|
+
// the Combobox's name unless it has its own.
|
|
338
|
+
render={(popupProps, state) => (
|
|
339
|
+
<div
|
|
340
|
+
{...(popupProps.role === "dialog" && !hasName(popupProps) ? name : {})}
|
|
341
|
+
{...popupProps}
|
|
342
|
+
aria-hidden={state.open ? popupProps["aria-hidden"] : true}
|
|
343
|
+
/>
|
|
344
|
+
)}
|
|
133
345
|
{...props}
|
|
134
346
|
/>
|
|
135
347
|
</ComboboxPrimitive.Positioner>
|
|
@@ -137,19 +349,119 @@ function ComboboxContent({
|
|
|
137
349
|
)
|
|
138
350
|
}
|
|
139
351
|
|
|
140
|
-
|
|
352
|
+
/** How near the end of the list, in pixels, the next page starts to load. */
|
|
353
|
+
const LOAD_MORE_THRESHOLD = 64
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Asks for the next page when the end of the list is in view or near it:
|
|
357
|
+
* when the list opens, when it scrolls (the arrow keys scroll it too), and
|
|
358
|
+
* when its options change, so a page shorter than the list fills it. A new
|
|
359
|
+
* set of options, a reopened list, or a scroll away from the end lets the
|
|
360
|
+
* next request through.
|
|
361
|
+
*/
|
|
362
|
+
function useLoadMore(paging: ComboboxPaging | null, end: React.RefObject<HTMLElement | null>) {
|
|
363
|
+
const list = paging?.list ?? null
|
|
364
|
+
const canLoadMore = !!paging?.canLoadMore
|
|
365
|
+
const requestMore = paging?.requestMore
|
|
366
|
+
const rearm = paging?.rearm
|
|
367
|
+
React.useEffect(() => {
|
|
368
|
+
if (!list || !canLoadMore || !requestMore || !rearm) return
|
|
369
|
+
const check = () => {
|
|
370
|
+
const marker = end.current
|
|
371
|
+
if (!marker || !list.isConnected) return
|
|
372
|
+
if (marker.getBoundingClientRect().top - list.getBoundingClientRect().bottom <= LOAD_MORE_THRESHOLD) {
|
|
373
|
+
requestMore()
|
|
374
|
+
} else {
|
|
375
|
+
rearm()
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const options = () => list.querySelectorAll("[role=option]").length
|
|
379
|
+
let count = options()
|
|
380
|
+
// A page may have landed while the list was closed.
|
|
381
|
+
rearm()
|
|
382
|
+
const observer = new MutationObserver(() => {
|
|
383
|
+
const next = options()
|
|
384
|
+
if (next === count) return
|
|
385
|
+
count = next
|
|
386
|
+
rearm()
|
|
387
|
+
check()
|
|
388
|
+
})
|
|
389
|
+
observer.observe(list, { childList: true, subtree: true })
|
|
390
|
+
list.addEventListener("scroll", check, { passive: true })
|
|
391
|
+
// After the popup has its size.
|
|
392
|
+
const frame = requestAnimationFrame(check)
|
|
393
|
+
return () => {
|
|
394
|
+
cancelAnimationFrame(frame)
|
|
395
|
+
observer.disconnect()
|
|
396
|
+
list.removeEventListener("scroll", check)
|
|
397
|
+
}
|
|
398
|
+
}, [list, canLoadMore, requestMore, rearm, end])
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function ComboboxList({ className, children, ref, ...props }: ComboboxPrimitive.List.Props) {
|
|
402
|
+
const name = useComboboxName()
|
|
403
|
+
const paging = React.useContext(ComboboxPagingContext)
|
|
404
|
+
const listRef = useRegisteredRef(ref, paging?.setList)
|
|
405
|
+
const endRef = React.useRef<HTMLDivElement | null>(null)
|
|
406
|
+
useLoadMore(paging, endRef)
|
|
407
|
+
// A paged list adds rows after the options, so a render function is
|
|
408
|
+
// wrapped in the Collection the primitive would wrap it in.
|
|
409
|
+
const items =
|
|
410
|
+
paging && typeof children === "function" ? (
|
|
411
|
+
<ComboboxPrimitive.Collection>{children}</ComboboxPrimitive.Collection>
|
|
412
|
+
) : (
|
|
413
|
+
children
|
|
414
|
+
)
|
|
141
415
|
return (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
416
|
+
<>
|
|
417
|
+
<ComboboxPrimitive.List
|
|
418
|
+
{...(hasName(props) ? {} : name)}
|
|
419
|
+
ref={listRef}
|
|
420
|
+
data-slot="combobox-list"
|
|
421
|
+
aria-busy={paging?.loading || undefined}
|
|
422
|
+
className={cn(
|
|
423
|
+
"no-scrollbar max-h-[min(calc(--spacing(72)---spacing(9)),calc(var(--available-height)---spacing(9)))] scroll-py-1 overflow-y-auto overscroll-contain p-1 data-empty:p-0",
|
|
424
|
+
className
|
|
425
|
+
)}
|
|
426
|
+
{...props}
|
|
427
|
+
>
|
|
428
|
+
{paging ? (
|
|
429
|
+
<>
|
|
430
|
+
{items}
|
|
431
|
+
{/* Decorative: a listbox holds only options and groups, so the
|
|
432
|
+
row is hidden from assistive technology. The list is busy
|
|
433
|
+
and the status below announces the load. */}
|
|
434
|
+
{paging.loading && (
|
|
435
|
+
<div
|
|
436
|
+
data-slot="combobox-loading"
|
|
437
|
+
aria-hidden="true"
|
|
438
|
+
className="text-muted-foreground flex min-h-8.5 items-center justify-center"
|
|
439
|
+
>
|
|
440
|
+
<Loader2Icon className="size-4 motion-safe:animate-spin" />
|
|
441
|
+
</div>
|
|
442
|
+
)}
|
|
443
|
+
{paging.canLoadMore && <div ref={endRef} data-slot="combobox-list-end" aria-hidden="true" />}
|
|
444
|
+
</>
|
|
445
|
+
) : (
|
|
446
|
+
children
|
|
447
|
+
)}
|
|
448
|
+
</ComboboxPrimitive.List>
|
|
449
|
+
{/* Stays mounted, so screen readers announce the change. Outside the
|
|
450
|
+
listbox, which may hold only options and groups. */}
|
|
451
|
+
{paging && (
|
|
452
|
+
<ComboboxPrimitive.Status data-slot="combobox-status" className="sr-only">
|
|
453
|
+
{paging.loading ? paging.loadingLabel : null}
|
|
454
|
+
</ComboboxPrimitive.Status>
|
|
147
455
|
)}
|
|
148
|
-
|
|
149
|
-
/>
|
|
456
|
+
</>
|
|
150
457
|
)
|
|
151
458
|
}
|
|
152
459
|
|
|
460
|
+
// The input keeps focus and the list moves a virtual highlight
|
|
461
|
+
// (`data-highlighted`), so `focus-visible` never fires on an option. An
|
|
462
|
+
// enabled option shows the highlight as the muted fill. A disabled one takes
|
|
463
|
+
// no fill: the arrow keys still stop on it, and it shows the focus ring at
|
|
464
|
+
// full strength instead, as a disabled menu item does under the arrow keys.
|
|
153
465
|
function ComboboxItem({
|
|
154
466
|
className,
|
|
155
467
|
children,
|
|
@@ -159,19 +471,17 @@ function ComboboxItem({
|
|
|
159
471
|
<ComboboxPrimitive.Item
|
|
160
472
|
data-slot="combobox-item"
|
|
161
473
|
className={cn(
|
|
162
|
-
|
|
474
|
+
menuItemDisabled,
|
|
475
|
+
menuCheckItem,
|
|
476
|
+
"not-data-disabled:data-highlighted:bg-muted not-data-disabled:data-highlighted:text-accent-foreground data-disabled:data-highlighted:outline-solid data-disabled:data-highlighted:outline-2 data-disabled:data-highlighted:-outline-offset-2 data-disabled:data-highlighted:outline-ring not-data-[variant=destructive]:not-data-disabled:data-highlighted:**:text-accent-foreground relative flex min-h-8.5 w-full cursor-default items-center gap-2 rounded-lg py-1.5 pe-8 ps-2 text-sm outline-hidden select-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='stroke-'])]:stroke-[1.75] [&>svg:not([class*='opacity-'])]:opacity-55 [&>svg:not([class*='opacity-'])]:transition-opacity [&>svg:not([class*='opacity-'])]:duration-moderate not-data-disabled:data-highlighted:[&>svg:not([class*='opacity-'])]:opacity-85",
|
|
163
477
|
className
|
|
164
478
|
)}
|
|
165
479
|
{...props}
|
|
166
480
|
>
|
|
167
481
|
{children}
|
|
168
|
-
<
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
>
|
|
173
|
-
<CheckIcon className="pointer-events-none" />
|
|
174
|
-
</ComboboxPrimitive.ItemIndicator>
|
|
482
|
+
<span className="pointer-events-none absolute end-2 flex size-4 items-center justify-center">
|
|
483
|
+
<MenuCheck />
|
|
484
|
+
</span>
|
|
175
485
|
</ComboboxPrimitive.Item>
|
|
176
486
|
)
|
|
177
487
|
}
|
|
@@ -205,12 +515,16 @@ function ComboboxCollection({ ...props }: ComboboxPrimitive.Collection.Props) {
|
|
|
205
515
|
)
|
|
206
516
|
}
|
|
207
517
|
|
|
518
|
+
// While a page loads, the list's loading row stands in for the empty state:
|
|
519
|
+
// a new search has no options yet, but it is not empty.
|
|
208
520
|
function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
|
|
521
|
+
const loading = !!React.useContext(ComboboxPagingContext)?.loading
|
|
209
522
|
return (
|
|
210
523
|
<ComboboxPrimitive.Empty
|
|
211
524
|
data-slot="combobox-empty"
|
|
212
525
|
className={cn(
|
|
213
526
|
"text-muted-foreground hidden w-full justify-center py-2 text-center text-sm group-data-empty/combobox-content:flex",
|
|
527
|
+
loading && "group-data-empty/combobox-content:hidden",
|
|
214
528
|
className
|
|
215
529
|
)}
|
|
216
530
|
{...props}
|
|
@@ -240,7 +554,8 @@ function ComboboxChips({
|
|
|
240
554
|
<ComboboxPrimitive.Chips
|
|
241
555
|
data-slot="combobox-chips"
|
|
242
556
|
className={cn(
|
|
243
|
-
|
|
557
|
+
fieldSurfaceWithin,
|
|
558
|
+
"flex min-h-8.5 flex-wrap items-center gap-1.5 px-2.5 py-1.5 text-sm has-data-[slot=combobox-chip]:p-1.25",
|
|
244
559
|
className
|
|
245
560
|
)}
|
|
246
561
|
{...props}
|
|
@@ -248,19 +563,29 @@ function ComboboxChips({
|
|
|
248
563
|
)
|
|
249
564
|
}
|
|
250
565
|
|
|
566
|
+
// A chip is the v3 look's grey chip: the accent fill, one step above the
|
|
567
|
+
// muted field it sits on, with a grey outline, so each value stands out.
|
|
568
|
+
// It has the Chip height, 24px, and is denser than a standalone Chip: the
|
|
569
|
+
// label is 8px from the edge, so a field fits more of them. In the 34px
|
|
570
|
+
// field it sits 5px from the edge, so its corner is the field's 6px minus that
|
|
571
|
+
// 5px, 1px. The remove button sits 4px inside the chip, so its corner is the
|
|
572
|
+
// chip's 1px minus 4px, which is square.
|
|
251
573
|
function ComboboxChip({
|
|
252
574
|
className,
|
|
253
575
|
children,
|
|
254
576
|
showRemove = true,
|
|
577
|
+
removeLabel = "Remove",
|
|
255
578
|
...props
|
|
256
579
|
}: ComboboxPrimitive.Chip.Props & {
|
|
257
580
|
showRemove?: boolean
|
|
581
|
+
/** Accessible name of the icon-only remove button. */
|
|
582
|
+
removeLabel?: string
|
|
258
583
|
}) {
|
|
259
584
|
return (
|
|
260
585
|
<ComboboxPrimitive.Chip
|
|
261
586
|
data-slot="combobox-chip"
|
|
262
587
|
className={cn(
|
|
263
|
-
"bg-
|
|
588
|
+
"bg-accent [--status-tone:var(--neutral-text)]", statusChipRing, "text-foreground flex h-6 w-fit items-center justify-center gap-1.5 rounded-[calc(var(--radius-lg)-5px)] ps-2 pe-2 text-xs font-medium whitespace-nowrap has-disabled:pointer-events-none has-disabled:cursor-not-allowed has-disabled:opacity-50 has-data-[slot=combobox-chip-remove]:pe-1",
|
|
264
589
|
className
|
|
265
590
|
)}
|
|
266
591
|
{...props}
|
|
@@ -269,8 +594,9 @@ function ComboboxChip({
|
|
|
269
594
|
{showRemove && (
|
|
270
595
|
<ComboboxPrimitive.ChipRemove
|
|
271
596
|
render={<Button variant="ghost" size="icon-xs" />}
|
|
272
|
-
className="-
|
|
597
|
+
className="size-4 rounded-none [--button-box:--spacing(4)] opacity-50 hover:opacity-100"
|
|
273
598
|
data-slot="combobox-chip-remove"
|
|
599
|
+
aria-label={removeLabel}
|
|
274
600
|
>
|
|
275
601
|
<XIcon className="pointer-events-none" />
|
|
276
602
|
</ComboboxPrimitive.ChipRemove>
|
|
@@ -281,10 +607,13 @@ function ComboboxChip({
|
|
|
281
607
|
|
|
282
608
|
function ComboboxChipsInput({
|
|
283
609
|
className,
|
|
610
|
+
ref,
|
|
284
611
|
...props
|
|
285
612
|
}: ComboboxPrimitive.Input.Props) {
|
|
613
|
+
const inputRef = useRegisteredRef(ref, React.useContext(ComboboxControlsContext)?.setInput)
|
|
286
614
|
return (
|
|
287
615
|
<ComboboxPrimitive.Input
|
|
616
|
+
ref={inputRef}
|
|
288
617
|
data-slot="combobox-chip-input"
|
|
289
618
|
className={cn("min-w-16 flex-1 outline-none", className)}
|
|
290
619
|
{...props}
|