@cognitap/radiant 0.0.12 → 0.0.13
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/CHANGELOG.md +5 -1
- package/dist/library/Combobox.d.ts +88 -0
- package/dist/library/Combobox.d.ts.map +1 -0
- package/dist/library/Combobox.js +365 -0
- package/dist/library/Combobox.js.map +1 -0
- package/dist/library/Select.js +1 -1
- package/dist/library/Select.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +18 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/comboboxStyles.d.ts +6 -0
- package/dist/library/comboboxStyles.d.ts.map +1 -0
- package/dist/library/comboboxStyles.js +38 -0
- package/dist/library/comboboxStyles.js.map +1 -0
- package/dist/library/index.d.ts +2 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +1 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/motion/popup.js +5 -5
- package/dist/library/motion/popup.js.map +1 -1
- package/dist/library/react/useComboboxPopup.d.ts +4 -0
- package/dist/library/react/useComboboxPopup.d.ts.map +1 -0
- package/dist/library/react/useComboboxPopup.js +118 -0
- package/dist/library/react/useComboboxPopup.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +25 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +25 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +23 -17
- package/package.json +1 -1
- package/src/Combobox.tsx +752 -0
- package/src/Select.tsx +1 -1
- package/src/cn.ts +18 -1
- package/src/comboboxStyles.ts +41 -0
- package/src/index.ts +22 -0
- package/src/motion/popup.ts +5 -5
- package/src/react/useComboboxPopup.ts +130 -0
- package/src/themes/components.ts +25 -0
package/src/Combobox.tsx
ADDED
|
@@ -0,0 +1,752 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- An editable combobox uses a native text input and an ARIA listbox; its submitted value has a separate native form bridge. */
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createContext,
|
|
7
|
+
useContext,
|
|
8
|
+
useEffect,
|
|
9
|
+
useId,
|
|
10
|
+
useLayoutEffect,
|
|
11
|
+
useRef,
|
|
12
|
+
useState
|
|
13
|
+
} from 'react'
|
|
14
|
+
import type { ComponentProps, ReactNode, RefObject } from 'react'
|
|
15
|
+
import { createPortal } from 'react-dom'
|
|
16
|
+
import { Button, type ButtonProps } from './Button.js'
|
|
17
|
+
import { useField, useFieldControl } from './Field.js'
|
|
18
|
+
import { cn } from './cn.js'
|
|
19
|
+
import { controlMetrics } from './controlStyles.js'
|
|
20
|
+
import {
|
|
21
|
+
comboboxInputClass,
|
|
22
|
+
comboboxContentClass,
|
|
23
|
+
comboboxItemClass,
|
|
24
|
+
comboboxActionClass
|
|
25
|
+
} from './comboboxStyles.js'
|
|
26
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
27
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
28
|
+
import { useTransitionHost } from './motion/host.js'
|
|
29
|
+
import { useComboboxPopup } from './react/useComboboxPopup.js'
|
|
30
|
+
import type { ValueOwner } from './react/useNativeOwner.js'
|
|
31
|
+
import type { ControlSize } from './presentation.js'
|
|
32
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
33
|
+
|
|
34
|
+
export type ComboboxOption = { value: string; disabled?: boolean } & (
|
|
35
|
+
| { label: string; textValue?: string }
|
|
36
|
+
| { label: ReactNode; textValue: string }
|
|
37
|
+
)
|
|
38
|
+
export type ComboboxProps = ValueOwner<string | null> & {
|
|
39
|
+
items: readonly ComboboxOption[]
|
|
40
|
+
onValueChange?: (value: string | null) => void
|
|
41
|
+
/** Application-owned selected option when it is absent from asynchronous results. */
|
|
42
|
+
selectedItem?: ComboboxOption
|
|
43
|
+
/** Local text matching by default. Pass null for application-filtered results. */
|
|
44
|
+
filter?: ((item: ComboboxOption, query: string) => boolean) | null
|
|
45
|
+
onQueryChange?: (query: string) => void
|
|
46
|
+
loading?: boolean
|
|
47
|
+
error?: ReactNode
|
|
48
|
+
loadingText?: string
|
|
49
|
+
emptyText?: string
|
|
50
|
+
name?: string
|
|
51
|
+
form?: string
|
|
52
|
+
disabled?: boolean
|
|
53
|
+
required?: boolean
|
|
54
|
+
controlSize?: ControlSize
|
|
55
|
+
children?: ReactNode
|
|
56
|
+
} & ({ query: string; defaultQuery?: never } | { query?: undefined; defaultQuery?: string }) &
|
|
57
|
+
({ open: boolean; defaultOpen?: never } | { open?: undefined; defaultOpen?: boolean }) & {
|
|
58
|
+
onOpenChange?: (open: boolean) => void
|
|
59
|
+
}
|
|
60
|
+
export type ComboboxState = {
|
|
61
|
+
value: string | null
|
|
62
|
+
query: string
|
|
63
|
+
open: boolean
|
|
64
|
+
disabled: boolean
|
|
65
|
+
setValue(value: string | null): void
|
|
66
|
+
setQuery(query: string): void
|
|
67
|
+
setOpen(open: boolean): void
|
|
68
|
+
}
|
|
69
|
+
type Context = ComboboxState & {
|
|
70
|
+
items: readonly ComboboxOption[]
|
|
71
|
+
visible: readonly ComboboxOption[]
|
|
72
|
+
active: string | null
|
|
73
|
+
highlight(value: string | null): void
|
|
74
|
+
text: string
|
|
75
|
+
input: HTMLInputElement | null
|
|
76
|
+
setInput(node: HTMLInputElement | null): void
|
|
77
|
+
content: RefObject<HTMLDivElement | null>
|
|
78
|
+
inputId: string
|
|
79
|
+
listId: string
|
|
80
|
+
optionId(value: string): string
|
|
81
|
+
required: boolean
|
|
82
|
+
controlSize: ControlSize
|
|
83
|
+
loading: boolean
|
|
84
|
+
error?: ReactNode
|
|
85
|
+
status: ReactNode
|
|
86
|
+
dismiss(): void
|
|
87
|
+
commit(value: string | null): void
|
|
88
|
+
}
|
|
89
|
+
const ComboboxContext = createContext<Context | null>(null)
|
|
90
|
+
export function useCombobox(): ComboboxState | null {
|
|
91
|
+
return useContext(ComboboxContext)
|
|
92
|
+
}
|
|
93
|
+
function useComboboxContext() {
|
|
94
|
+
const context = useContext(ComboboxContext)
|
|
95
|
+
if (!context) throw new Error('Combobox parts must render inside Combobox.')
|
|
96
|
+
return context
|
|
97
|
+
}
|
|
98
|
+
const textOf = (item?: ComboboxOption) =>
|
|
99
|
+
item?.textValue ?? (typeof item?.label === 'string' ? item.label : '')
|
|
100
|
+
const localFilter = (item: ComboboxOption, query: string) =>
|
|
101
|
+
textOf(item).toLocaleLowerCase().includes(query.trim().toLocaleLowerCase())
|
|
102
|
+
|
|
103
|
+
/** Editable search with explicit single selection. Fetching and validation messages belong to the application. */
|
|
104
|
+
export function Combobox({
|
|
105
|
+
items,
|
|
106
|
+
value,
|
|
107
|
+
defaultValue,
|
|
108
|
+
onValueChange,
|
|
109
|
+
selectedItem,
|
|
110
|
+
filter = localFilter,
|
|
111
|
+
query,
|
|
112
|
+
defaultQuery,
|
|
113
|
+
onQueryChange,
|
|
114
|
+
open,
|
|
115
|
+
defaultOpen,
|
|
116
|
+
onOpenChange,
|
|
117
|
+
loading = false,
|
|
118
|
+
error,
|
|
119
|
+
loadingText = 'Loading results…',
|
|
120
|
+
emptyText = 'No results found.',
|
|
121
|
+
name,
|
|
122
|
+
form,
|
|
123
|
+
disabled: disabledProp,
|
|
124
|
+
required: requiredProp,
|
|
125
|
+
controlSize = 'medium',
|
|
126
|
+
children
|
|
127
|
+
}: ComboboxProps) {
|
|
128
|
+
const ids = useId()
|
|
129
|
+
const field = useField()
|
|
130
|
+
const disabled = !!(disabledProp || field?.disabled)
|
|
131
|
+
const required = !!(requiredProp || field?.required)
|
|
132
|
+
if (new Set(items.map(item => item.value)).size !== items.length)
|
|
133
|
+
throw new TypeError('Combobox item values must be distinct.')
|
|
134
|
+
if (
|
|
135
|
+
[...items, ...(selectedItem ? [selectedItem] : [])].some(
|
|
136
|
+
item => typeof item.value !== 'string' || !textOf(item).trim()
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
throw new TypeError('Combobox items need a string value and readable textValue.')
|
|
140
|
+
const selection = useControllableState({
|
|
141
|
+
name: 'Combobox',
|
|
142
|
+
value,
|
|
143
|
+
defaultValue,
|
|
144
|
+
initialValue: null,
|
|
145
|
+
onChange: onValueChange
|
|
146
|
+
})
|
|
147
|
+
const search = useControllableState({
|
|
148
|
+
name: 'Combobox query',
|
|
149
|
+
value: query,
|
|
150
|
+
defaultValue: defaultQuery,
|
|
151
|
+
initialValue: '',
|
|
152
|
+
onChange: onQueryChange
|
|
153
|
+
})
|
|
154
|
+
const expansion = useControllableState({
|
|
155
|
+
name: 'Combobox open',
|
|
156
|
+
value: open,
|
|
157
|
+
defaultValue: defaultOpen,
|
|
158
|
+
initialValue: false,
|
|
159
|
+
onChange: onOpenChange
|
|
160
|
+
})
|
|
161
|
+
const [input, setInput] = useState<HTMLInputElement | null>(null)
|
|
162
|
+
const content = useRef<HTMLDivElement>(null)
|
|
163
|
+
const bridge = useRef<HTMLInputElement>(null)
|
|
164
|
+
const [active, highlight] = useState<string | null>(null)
|
|
165
|
+
const [searching, setSearching] = useState(!!search.value)
|
|
166
|
+
const [retained, setRetained] = useState(() => new Map<string, ComboboxOption>())
|
|
167
|
+
const currentItem =
|
|
168
|
+
items.find(item => item.value === selection.value) ??
|
|
169
|
+
(selectedItem?.value === selection.value ? selectedItem : undefined)
|
|
170
|
+
if (currentItem && retained.get(currentItem.value) !== currentItem)
|
|
171
|
+
setRetained(new Map(retained).set(currentItem.value, currentItem))
|
|
172
|
+
const selected =
|
|
173
|
+
currentItem ?? (selection.value === null ? undefined : retained.get(selection.value))
|
|
174
|
+
const unavailable = () => disabled || !!input?.matches(':disabled')
|
|
175
|
+
const isOpen = expansion.value && !disabled
|
|
176
|
+
const visible =
|
|
177
|
+
loading || error ? [] : filter ? items.filter(item => filter(item, search.value)) : items
|
|
178
|
+
const enabled = visible.filter(item => !item.disabled)
|
|
179
|
+
const activeValue = enabled.some(item => item.value === active) ? active : null
|
|
180
|
+
if (active !== null && activeValue === null) highlight(null)
|
|
181
|
+
const text =
|
|
182
|
+
isOpen && (searching || search.value !== '')
|
|
183
|
+
? search.value
|
|
184
|
+
: selected
|
|
185
|
+
? textOf(selected)
|
|
186
|
+
: (selection.value ?? '')
|
|
187
|
+
const setOpen = (next: boolean) => {
|
|
188
|
+
if (next && unavailable()) return
|
|
189
|
+
expansion.request(next)
|
|
190
|
+
if (!next) highlight(null)
|
|
191
|
+
}
|
|
192
|
+
const dismiss = () => {
|
|
193
|
+
expansion.request(false)
|
|
194
|
+
highlight(null)
|
|
195
|
+
setSearching(false)
|
|
196
|
+
search.request('')
|
|
197
|
+
}
|
|
198
|
+
const setQuery = (next: string) => {
|
|
199
|
+
if (unavailable()) return
|
|
200
|
+
setSearching(true)
|
|
201
|
+
highlight(null)
|
|
202
|
+
search.request(next)
|
|
203
|
+
setOpen(true)
|
|
204
|
+
}
|
|
205
|
+
const pending = useRef<{ value: string | null } | null>(null)
|
|
206
|
+
const requestValue = (next: string | null) => {
|
|
207
|
+
if (unavailable()) return
|
|
208
|
+
if (next !== selection.value) pending.current = { value: next }
|
|
209
|
+
selection.request(next)
|
|
210
|
+
queueMicrotask(() => {
|
|
211
|
+
pending.current = null
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
const commit = (next: string | null) => {
|
|
215
|
+
if (unavailable() || (next !== null && !enabled.some(item => item.value === next))) return
|
|
216
|
+
const item = enabled.find(item => item.value === next)
|
|
217
|
+
if (item) setRetained(previous => new Map(previous).set(item.value, item))
|
|
218
|
+
requestValue(next)
|
|
219
|
+
dismiss()
|
|
220
|
+
input?.focus({ preventScroll: true })
|
|
221
|
+
}
|
|
222
|
+
const nativeValue = selection.value ?? ''
|
|
223
|
+
const latest = useRef({ selection, search, nativeValue, expansion })
|
|
224
|
+
useLayoutEffect(() => {
|
|
225
|
+
latest.current = { selection, search, nativeValue, expansion }
|
|
226
|
+
})
|
|
227
|
+
useLayoutEffect(() => {
|
|
228
|
+
const node = bridge.current
|
|
229
|
+
if (!node) return
|
|
230
|
+
const changedByUser = pending.current && Object.is(pending.current.value, selection.value)
|
|
231
|
+
if (changedByUser) {
|
|
232
|
+
// Bypass React's value tracker so form onChange observes this accepted selection.
|
|
233
|
+
const setter = Object.getOwnPropertyDescriptor(
|
|
234
|
+
node.ownerDocument.defaultView!.HTMLInputElement.prototype,
|
|
235
|
+
'value'
|
|
236
|
+
)!.set!
|
|
237
|
+
setter.call(node, nativeValue)
|
|
238
|
+
} else node.value = nativeValue
|
|
239
|
+
if (selection.controlled) node.defaultValue = nativeValue
|
|
240
|
+
node.setCustomValidity(required && selection.value === null ? 'Please select an option.' : '')
|
|
241
|
+
if (pending.current && Object.is(pending.current.value, selection.value)) {
|
|
242
|
+
pending.current = null
|
|
243
|
+
const EventType = node.ownerDocument.defaultView!.Event
|
|
244
|
+
node.dispatchEvent(new EventType('input', { bubbles: true }))
|
|
245
|
+
node.dispatchEvent(new EventType('change', { bubbles: true }))
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
useEffect(() => {
|
|
249
|
+
const node = bridge.current
|
|
250
|
+
const owner = node?.form
|
|
251
|
+
if (!node || !owner) return
|
|
252
|
+
const win = node.ownerDocument.defaultView!
|
|
253
|
+
let timer: number | undefined
|
|
254
|
+
const reset = (event: Event) => {
|
|
255
|
+
win.clearTimeout(timer)
|
|
256
|
+
timer = win.setTimeout(() => {
|
|
257
|
+
if (event.defaultPrevented || !node.isConnected) return
|
|
258
|
+
latest.current.selection.reset()
|
|
259
|
+
latest.current.search.reset()
|
|
260
|
+
setSearching(false)
|
|
261
|
+
highlight(null)
|
|
262
|
+
latest.current.expansion.request(false)
|
|
263
|
+
node.value = latest.current.nativeValue
|
|
264
|
+
}, 0)
|
|
265
|
+
}
|
|
266
|
+
owner.addEventListener('reset', reset)
|
|
267
|
+
return () => {
|
|
268
|
+
owner.removeEventListener('reset', reset)
|
|
269
|
+
win.clearTimeout(timer)
|
|
270
|
+
}
|
|
271
|
+
}, [input, form])
|
|
272
|
+
useLayoutEffect(() => {
|
|
273
|
+
if (!isOpen || activeValue === null) return
|
|
274
|
+
const option = content.current?.ownerDocument.getElementById(
|
|
275
|
+
`${ids}-option-${encodeURIComponent(activeValue)}`
|
|
276
|
+
)
|
|
277
|
+
option?.scrollIntoView({ block: 'nearest' })
|
|
278
|
+
}, [activeValue, isOpen, ids])
|
|
279
|
+
const context: Context = {
|
|
280
|
+
value: selection.value,
|
|
281
|
+
query: search.value,
|
|
282
|
+
open: isOpen,
|
|
283
|
+
disabled,
|
|
284
|
+
setValue: next => {
|
|
285
|
+
if (next === null || items.some(item => item.value === next && !item.disabled))
|
|
286
|
+
requestValue(next)
|
|
287
|
+
},
|
|
288
|
+
setQuery,
|
|
289
|
+
setOpen,
|
|
290
|
+
items,
|
|
291
|
+
visible,
|
|
292
|
+
active: activeValue,
|
|
293
|
+
highlight,
|
|
294
|
+
text,
|
|
295
|
+
input,
|
|
296
|
+
setInput,
|
|
297
|
+
content,
|
|
298
|
+
inputId: field?.controlProps?.id ?? `${ids}-input`,
|
|
299
|
+
listId: `${ids}-listbox`,
|
|
300
|
+
optionId: next => `${ids}-option-${encodeURIComponent(next)}`,
|
|
301
|
+
required,
|
|
302
|
+
controlSize,
|
|
303
|
+
loading,
|
|
304
|
+
error,
|
|
305
|
+
status: loading ? loadingText : error || (visible.length === 0 ? emptyText : ''),
|
|
306
|
+
dismiss,
|
|
307
|
+
commit
|
|
308
|
+
}
|
|
309
|
+
return (
|
|
310
|
+
<ComboboxContext.Provider value={context}>
|
|
311
|
+
{children ?? (
|
|
312
|
+
<>
|
|
313
|
+
<div
|
|
314
|
+
className="relative w-full min-w-0"
|
|
315
|
+
data-slot="combobox-control"
|
|
316
|
+
>
|
|
317
|
+
<ComboboxInput />
|
|
318
|
+
<ComboboxTrigger className="absolute inset-y-0 end-0 w-12" />
|
|
319
|
+
</div>
|
|
320
|
+
<ComboboxPortal>
|
|
321
|
+
<ComboboxContent />
|
|
322
|
+
</ComboboxPortal>
|
|
323
|
+
</>
|
|
324
|
+
)}
|
|
325
|
+
<input
|
|
326
|
+
ref={bridge}
|
|
327
|
+
className="sr-only"
|
|
328
|
+
data-slot="combobox-native"
|
|
329
|
+
type="text"
|
|
330
|
+
name={name}
|
|
331
|
+
form={form}
|
|
332
|
+
disabled={disabled}
|
|
333
|
+
aria-hidden="true"
|
|
334
|
+
tabIndex={-1}
|
|
335
|
+
autoComplete="off"
|
|
336
|
+
defaultValue={nativeValue}
|
|
337
|
+
onFocus={() => input?.focus()}
|
|
338
|
+
onInvalid={event => {
|
|
339
|
+
event.preventDefault()
|
|
340
|
+
input?.focus()
|
|
341
|
+
}}
|
|
342
|
+
/>
|
|
343
|
+
</ComboboxContext.Provider>
|
|
344
|
+
)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export type ComboboxInputProps = Omit<
|
|
348
|
+
ComponentProps<'input'>,
|
|
349
|
+
| 'value'
|
|
350
|
+
| 'defaultValue'
|
|
351
|
+
| 'name'
|
|
352
|
+
| 'form'
|
|
353
|
+
| 'type'
|
|
354
|
+
| 'role'
|
|
355
|
+
| 'disabled'
|
|
356
|
+
| 'required'
|
|
357
|
+
| 'style'
|
|
358
|
+
| 'aria-controls'
|
|
359
|
+
| 'aria-expanded'
|
|
360
|
+
| 'aria-autocomplete'
|
|
361
|
+
| 'aria-activedescendant'
|
|
362
|
+
> & { style?: ThemeStyle }
|
|
363
|
+
export function ComboboxInput({
|
|
364
|
+
className,
|
|
365
|
+
ref,
|
|
366
|
+
onChange,
|
|
367
|
+
onClick,
|
|
368
|
+
onKeyDown,
|
|
369
|
+
onBlur,
|
|
370
|
+
...props
|
|
371
|
+
}: ComboboxInputProps) {
|
|
372
|
+
const box = useComboboxContext()
|
|
373
|
+
const control = useFieldControl({
|
|
374
|
+
id: props.id ?? box.inputId,
|
|
375
|
+
disabled: box.disabled,
|
|
376
|
+
required: box.required,
|
|
377
|
+
'aria-describedby': props['aria-describedby'],
|
|
378
|
+
'aria-invalid': props['aria-invalid']
|
|
379
|
+
})
|
|
380
|
+
const inputRef = useComposedRefs(box.setInput, ref)
|
|
381
|
+
return (
|
|
382
|
+
<input
|
|
383
|
+
{...props}
|
|
384
|
+
{...control}
|
|
385
|
+
ref={inputRef}
|
|
386
|
+
type="text"
|
|
387
|
+
role="combobox"
|
|
388
|
+
required={false}
|
|
389
|
+
autoComplete={props.autoComplete ?? 'off'}
|
|
390
|
+
value={box.text}
|
|
391
|
+
className={cn(
|
|
392
|
+
comboboxInputClass,
|
|
393
|
+
controlMetrics({ controlSize: box.controlSize }),
|
|
394
|
+
className
|
|
395
|
+
)}
|
|
396
|
+
data-slot="combobox-input"
|
|
397
|
+
data-control-size={box.controlSize}
|
|
398
|
+
aria-autocomplete="list"
|
|
399
|
+
aria-expanded={box.open}
|
|
400
|
+
aria-controls={box.open ? box.listId : undefined}
|
|
401
|
+
aria-activedescendant={box.open && box.active !== null ? box.optionId(box.active) : undefined}
|
|
402
|
+
onChange={event => {
|
|
403
|
+
onChange?.(event)
|
|
404
|
+
box.setQuery(event.currentTarget.value)
|
|
405
|
+
}}
|
|
406
|
+
onClick={event => {
|
|
407
|
+
onClick?.(event)
|
|
408
|
+
if (!event.defaultPrevented) box.setOpen(true)
|
|
409
|
+
}}
|
|
410
|
+
onBlur={event => {
|
|
411
|
+
onBlur?.(event)
|
|
412
|
+
if (
|
|
413
|
+
!box.content.current?.contains(event.relatedTarget) &&
|
|
414
|
+
!event.currentTarget.parentElement?.contains(event.relatedTarget)
|
|
415
|
+
)
|
|
416
|
+
box.dismiss()
|
|
417
|
+
}}
|
|
418
|
+
onKeyDown={event => {
|
|
419
|
+
onKeyDown?.(event)
|
|
420
|
+
if (
|
|
421
|
+
event.defaultPrevented ||
|
|
422
|
+
event.nativeEvent.isComposing ||
|
|
423
|
+
event.keyCode === 229 ||
|
|
424
|
+
event.ctrlKey ||
|
|
425
|
+
event.metaKey
|
|
426
|
+
)
|
|
427
|
+
return
|
|
428
|
+
if (event.key === 'Escape' && !box.open && box.value !== null) {
|
|
429
|
+
event.preventDefault()
|
|
430
|
+
box.commit(null)
|
|
431
|
+
return
|
|
432
|
+
}
|
|
433
|
+
if (event.key === 'Escape' && box.open) {
|
|
434
|
+
event.preventDefault()
|
|
435
|
+
event.stopPropagation()
|
|
436
|
+
box.dismiss()
|
|
437
|
+
return
|
|
438
|
+
}
|
|
439
|
+
if (event.key === 'Tab') {
|
|
440
|
+
box.dismiss()
|
|
441
|
+
return
|
|
442
|
+
}
|
|
443
|
+
if (event.altKey) {
|
|
444
|
+
if (event.key === 'ArrowDown') {
|
|
445
|
+
event.preventDefault()
|
|
446
|
+
box.setOpen(true)
|
|
447
|
+
} else if (event.key === 'ArrowUp') {
|
|
448
|
+
event.preventDefault()
|
|
449
|
+
box.dismiss()
|
|
450
|
+
}
|
|
451
|
+
return
|
|
452
|
+
}
|
|
453
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
454
|
+
event.preventDefault()
|
|
455
|
+
box.setOpen(true)
|
|
456
|
+
const options = box.visible.filter(item => !item.disabled)
|
|
457
|
+
const index = options.findIndex(item => item.value === box.active)
|
|
458
|
+
const next =
|
|
459
|
+
index < 0
|
|
460
|
+
? event.key === 'ArrowUp'
|
|
461
|
+
? options.at(-1)
|
|
462
|
+
: options[0]
|
|
463
|
+
: options[
|
|
464
|
+
Math.max(
|
|
465
|
+
0,
|
|
466
|
+
Math.min(options.length - 1, index + (event.key === 'ArrowUp' ? -1 : 1))
|
|
467
|
+
)
|
|
468
|
+
]
|
|
469
|
+
box.highlight(next?.value ?? null)
|
|
470
|
+
} else if (event.key === 'Enter' && box.open && box.active !== null) {
|
|
471
|
+
event.preventDefault()
|
|
472
|
+
box.commit(box.active)
|
|
473
|
+
}
|
|
474
|
+
}}
|
|
475
|
+
/>
|
|
476
|
+
)
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export type ComboboxTriggerProps = Omit<
|
|
480
|
+
ButtonProps,
|
|
481
|
+
| 'type'
|
|
482
|
+
| 'disabled'
|
|
483
|
+
| 'pending'
|
|
484
|
+
| 'variant'
|
|
485
|
+
| 'tone'
|
|
486
|
+
| 'controlSize'
|
|
487
|
+
| 'iconOnly'
|
|
488
|
+
| 'fullWidth'
|
|
489
|
+
| 'motion'
|
|
490
|
+
| 'tabIndex'
|
|
491
|
+
| 'aria-controls'
|
|
492
|
+
| 'aria-expanded'
|
|
493
|
+
>
|
|
494
|
+
export function ComboboxTrigger({
|
|
495
|
+
children,
|
|
496
|
+
className,
|
|
497
|
+
render,
|
|
498
|
+
onClick,
|
|
499
|
+
onPointerDown,
|
|
500
|
+
...props
|
|
501
|
+
}: ComboboxTriggerProps) {
|
|
502
|
+
const box = useComboboxContext()
|
|
503
|
+
return (
|
|
504
|
+
<Button
|
|
505
|
+
variant="ghost"
|
|
506
|
+
tone="neutral"
|
|
507
|
+
{...props}
|
|
508
|
+
type="button"
|
|
509
|
+
disabled={box.disabled}
|
|
510
|
+
pending={false}
|
|
511
|
+
motion="off"
|
|
512
|
+
controlSize={box.controlSize}
|
|
513
|
+
render={resolved => {
|
|
514
|
+
const native = { ...resolved, className: cn(comboboxActionClass, className) }
|
|
515
|
+
return render ? render(native) : <button {...native} />
|
|
516
|
+
}}
|
|
517
|
+
tabIndex={-1}
|
|
518
|
+
aria-label={props['aria-label'] ?? 'Show options'}
|
|
519
|
+
aria-controls={box.open ? box.listId : undefined}
|
|
520
|
+
aria-expanded={box.open}
|
|
521
|
+
data-slot="button combobox-trigger"
|
|
522
|
+
onPointerDown={event => {
|
|
523
|
+
onPointerDown?.(event)
|
|
524
|
+
if (!event.defaultPrevented) event.preventDefault()
|
|
525
|
+
}}
|
|
526
|
+
onClick={event => {
|
|
527
|
+
onClick?.(event)
|
|
528
|
+
if (!event.defaultPrevented) {
|
|
529
|
+
box.input?.focus({ preventScroll: true })
|
|
530
|
+
if (box.open) box.dismiss()
|
|
531
|
+
else box.setOpen(true)
|
|
532
|
+
}
|
|
533
|
+
}}
|
|
534
|
+
>
|
|
535
|
+
{children ?? (
|
|
536
|
+
<svg
|
|
537
|
+
aria-hidden="true"
|
|
538
|
+
width="16"
|
|
539
|
+
height="16"
|
|
540
|
+
viewBox="0 0 16 16"
|
|
541
|
+
fill="none"
|
|
542
|
+
>
|
|
543
|
+
<path
|
|
544
|
+
d="m4 6 4 4 4-4"
|
|
545
|
+
stroke="currentColor"
|
|
546
|
+
strokeWidth="1.5"
|
|
547
|
+
/>
|
|
548
|
+
</svg>
|
|
549
|
+
)}
|
|
550
|
+
</Button>
|
|
551
|
+
)
|
|
552
|
+
}
|
|
553
|
+
export type ComboboxClearProps = ComboboxTriggerProps
|
|
554
|
+
export function ComboboxClear({
|
|
555
|
+
children,
|
|
556
|
+
className,
|
|
557
|
+
render,
|
|
558
|
+
onClick,
|
|
559
|
+
onPointerDown,
|
|
560
|
+
...props
|
|
561
|
+
}: ComboboxClearProps) {
|
|
562
|
+
const box = useComboboxContext()
|
|
563
|
+
if (box.value === null) return null
|
|
564
|
+
return (
|
|
565
|
+
<Button
|
|
566
|
+
variant="ghost"
|
|
567
|
+
tone="neutral"
|
|
568
|
+
{...props}
|
|
569
|
+
type="button"
|
|
570
|
+
disabled={box.disabled}
|
|
571
|
+
pending={false}
|
|
572
|
+
motion="off"
|
|
573
|
+
controlSize={box.controlSize}
|
|
574
|
+
render={resolved => {
|
|
575
|
+
const native = { ...resolved, className: cn(comboboxActionClass, className) }
|
|
576
|
+
return render ? render(native) : <button {...native} />
|
|
577
|
+
}}
|
|
578
|
+
tabIndex={-1}
|
|
579
|
+
aria-label={props['aria-label'] ?? 'Clear selection'}
|
|
580
|
+
data-slot="button combobox-clear"
|
|
581
|
+
onPointerDown={event => {
|
|
582
|
+
onPointerDown?.(event)
|
|
583
|
+
if (!event.defaultPrevented) event.preventDefault()
|
|
584
|
+
}}
|
|
585
|
+
onClick={event => {
|
|
586
|
+
onClick?.(event)
|
|
587
|
+
if (!event.defaultPrevented) box.commit(null)
|
|
588
|
+
}}
|
|
589
|
+
>
|
|
590
|
+
{children ?? (
|
|
591
|
+
<svg
|
|
592
|
+
aria-hidden="true"
|
|
593
|
+
width="16"
|
|
594
|
+
height="16"
|
|
595
|
+
viewBox="0 0 16 16"
|
|
596
|
+
fill="none"
|
|
597
|
+
>
|
|
598
|
+
<path
|
|
599
|
+
d="m5 5 6 6m0-6-6 6"
|
|
600
|
+
stroke="currentColor"
|
|
601
|
+
strokeWidth="1.5"
|
|
602
|
+
/>
|
|
603
|
+
</svg>
|
|
604
|
+
)}
|
|
605
|
+
</Button>
|
|
606
|
+
)
|
|
607
|
+
}
|
|
608
|
+
export function ComboboxPortal({ children }: { children?: ReactNode }) {
|
|
609
|
+
const box = useComboboxContext()
|
|
610
|
+
return box.input?.parentElement ? createPortal(children, box.input.parentElement) : null
|
|
611
|
+
}
|
|
612
|
+
export type ComboboxContentProps = Omit<
|
|
613
|
+
ComponentProps<'div'>,
|
|
614
|
+
'style' | 'popover' | 'role' | 'id'
|
|
615
|
+
> & {
|
|
616
|
+
style?: ThemeStyle
|
|
617
|
+
sideOffset?: number
|
|
618
|
+
collisionPadding?: number
|
|
619
|
+
motion?: 'inherit' | 'off'
|
|
620
|
+
}
|
|
621
|
+
export function ComboboxContent({
|
|
622
|
+
children,
|
|
623
|
+
className,
|
|
624
|
+
ref,
|
|
625
|
+
sideOffset = 4,
|
|
626
|
+
collisionPadding = 8,
|
|
627
|
+
motion = 'inherit',
|
|
628
|
+
...props
|
|
629
|
+
}: ComboboxContentProps) {
|
|
630
|
+
const box = useComboboxContext()
|
|
631
|
+
const contentRef = useComposedRefs(box.content, ref)
|
|
632
|
+
const {
|
|
633
|
+
ref: transitionRef,
|
|
634
|
+
active: motionActive,
|
|
635
|
+
off: motionOff
|
|
636
|
+
} = useTransitionHost('popup', contentRef, motion === 'off')
|
|
637
|
+
useComboboxPopup(box.content, box.input, box.open, box.dismiss, sideOffset, collisionPadding)
|
|
638
|
+
return (
|
|
639
|
+
<div
|
|
640
|
+
{...props}
|
|
641
|
+
ref={transitionRef}
|
|
642
|
+
popover="manual"
|
|
643
|
+
data-slot="combobox-content"
|
|
644
|
+
data-motion={motion}
|
|
645
|
+
data-state={box.open ? 'open' : 'closed'}
|
|
646
|
+
className={cn(
|
|
647
|
+
comboboxContentClass,
|
|
648
|
+
controlMetrics({ controlSize: box.controlSize }),
|
|
649
|
+
motionActive &&
|
|
650
|
+
!motionOff &&
|
|
651
|
+
'opacity-[var(--_rad-popup-enter,0)] motion-reduce:opacity-100',
|
|
652
|
+
className
|
|
653
|
+
)}
|
|
654
|
+
>
|
|
655
|
+
<div
|
|
656
|
+
id={box.listId}
|
|
657
|
+
role="listbox"
|
|
658
|
+
aria-label={props['aria-label'] ?? box.input?.getAttribute('aria-label') ?? 'Suggestions'}
|
|
659
|
+
aria-labelledby={props['aria-label'] ? undefined : box.input?.labels?.[0]?.id}
|
|
660
|
+
aria-busy={box.loading || undefined}
|
|
661
|
+
data-slot="combobox-listbox"
|
|
662
|
+
className="max-h-[calc(var(--_rad-combobox-max-height,100dvh)-0.5rem)] overflow-y-auto overscroll-contain"
|
|
663
|
+
>
|
|
664
|
+
{children ??
|
|
665
|
+
box.visible.map(item => (
|
|
666
|
+
<ComboboxItem
|
|
667
|
+
key={item.value}
|
|
668
|
+
value={item.value}
|
|
669
|
+
/>
|
|
670
|
+
))}
|
|
671
|
+
</div>
|
|
672
|
+
<ComboboxStatus />
|
|
673
|
+
</div>
|
|
674
|
+
)
|
|
675
|
+
}
|
|
676
|
+
export type ComboboxItemProps = Omit<
|
|
677
|
+
ComponentProps<'div'>,
|
|
678
|
+
'style' | 'id' | 'role' | 'tabIndex'
|
|
679
|
+
> & { value: string; style?: ThemeStyle }
|
|
680
|
+
export function ComboboxItem({
|
|
681
|
+
value,
|
|
682
|
+
children,
|
|
683
|
+
className,
|
|
684
|
+
onPointerDown,
|
|
685
|
+
onPointerMove,
|
|
686
|
+
onClick,
|
|
687
|
+
...props
|
|
688
|
+
}: ComboboxItemProps) {
|
|
689
|
+
const box = useComboboxContext()
|
|
690
|
+
const item = box.visible.find(item => item.value === value)
|
|
691
|
+
if (!item) return null
|
|
692
|
+
return (
|
|
693
|
+
<div
|
|
694
|
+
{...props}
|
|
695
|
+
id={box.optionId(value)}
|
|
696
|
+
role="option"
|
|
697
|
+
tabIndex={-1}
|
|
698
|
+
onKeyDown={props.onKeyDown}
|
|
699
|
+
aria-selected={box.active === value}
|
|
700
|
+
aria-disabled={item.disabled || undefined}
|
|
701
|
+
data-slot="combobox-item"
|
|
702
|
+
data-value={value}
|
|
703
|
+
data-highlighted={box.active === value ? '' : undefined}
|
|
704
|
+
data-selected={box.value === value ? '' : undefined}
|
|
705
|
+
data-disabled={item.disabled ? '' : undefined}
|
|
706
|
+
className={cn(comboboxItemClass, className)}
|
|
707
|
+
onPointerDown={event => {
|
|
708
|
+
onPointerDown?.(event)
|
|
709
|
+
if (!event.defaultPrevented) event.preventDefault()
|
|
710
|
+
}}
|
|
711
|
+
onPointerMove={event => {
|
|
712
|
+
onPointerMove?.(event)
|
|
713
|
+
if (!event.defaultPrevented && event.pointerType === 'mouse' && !item.disabled)
|
|
714
|
+
box.highlight(value)
|
|
715
|
+
}}
|
|
716
|
+
onClick={event => {
|
|
717
|
+
onClick?.(event)
|
|
718
|
+
if (!event.defaultPrevented && !item.disabled) box.commit(value)
|
|
719
|
+
}}
|
|
720
|
+
>
|
|
721
|
+
{children ?? (
|
|
722
|
+
<>
|
|
723
|
+
<span className="min-w-0 flex-1">{item.label}</span>
|
|
724
|
+
<span
|
|
725
|
+
aria-hidden="true"
|
|
726
|
+
className={cn(box.value !== value && 'invisible')}
|
|
727
|
+
>
|
|
728
|
+
✓
|
|
729
|
+
</span>
|
|
730
|
+
</>
|
|
731
|
+
)}
|
|
732
|
+
</div>
|
|
733
|
+
)
|
|
734
|
+
}
|
|
735
|
+
export type ComboboxStatusProps = Omit<ComponentProps<'div'>, 'style' | 'role'> & {
|
|
736
|
+
style?: ThemeStyle
|
|
737
|
+
}
|
|
738
|
+
export function ComboboxStatus({ children, className, ...props }: ComboboxStatusProps) {
|
|
739
|
+
const box = useComboboxContext()
|
|
740
|
+
return (
|
|
741
|
+
<div
|
|
742
|
+
{...props}
|
|
743
|
+
role="status"
|
|
744
|
+
aria-live="polite"
|
|
745
|
+
aria-atomic="true"
|
|
746
|
+
data-slot="combobox-status"
|
|
747
|
+
className={cn('px-3 py-2 empty:hidden', className)}
|
|
748
|
+
>
|
|
749
|
+
{box.open ? (children ?? box.status) : null}
|
|
750
|
+
</div>
|
|
751
|
+
)
|
|
752
|
+
}
|