@cognitap/radiant 0.0.52 → 0.0.53
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/Autocomplete.d.ts.map +1 -1
- package/dist/library/Autocomplete.js +1 -1
- package/dist/library/Autocomplete.js.map +1 -1
- package/dist/library/Combobox.d.ts +44 -1
- package/dist/library/Combobox.d.ts.map +1 -1
- package/dist/library/Combobox.js +59 -21
- package/dist/library/Combobox.js.map +1 -1
- package/dist/library/Command.d.ts +49 -0
- package/dist/library/Command.d.ts.map +1 -0
- package/dist/library/Command.js +110 -0
- package/dist/library/Command.js.map +1 -0
- package/dist/library/Dialog.d.ts +2 -2
- package/dist/library/Dialog.d.ts.map +1 -1
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/Field.d.ts +4 -0
- package/dist/library/Field.d.ts.map +1 -1
- package/dist/library/Field.js +4 -0
- package/dist/library/Field.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +1 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +1 -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/react/useNativeOwner.d.ts +9 -0
- package/dist/library/react/useNativeOwner.d.ts.map +1 -1
- package/dist/library/react/useNativeOwner.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +3 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +1 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +43 -44
- package/package.json +1 -1
- package/src/Autocomplete.tsx +1 -0
- package/src/Combobox.tsx +113 -31
- package/src/Command.tsx +317 -0
- package/src/Dialog.tsx +4 -2
- package/src/Field.tsx +5 -0
- package/src/cn.ts +1 -0
- package/src/index.ts +23 -0
- package/src/react/useNativeOwner.ts +4 -0
- package/src/themes/components.ts +1 -0
package/src/Command.tsx
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- Grouped results use the actual Combobox ARIA listbox. */
|
|
3
|
+
import { createContext, useContext } from 'react'
|
|
4
|
+
import type { ComponentProps, ReactNode } from 'react'
|
|
5
|
+
import {
|
|
6
|
+
DialogRoot,
|
|
7
|
+
DialogTrigger,
|
|
8
|
+
DialogContent,
|
|
9
|
+
DialogTitle,
|
|
10
|
+
DialogDescription,
|
|
11
|
+
DialogClose,
|
|
12
|
+
useDialog,
|
|
13
|
+
useDialogFamily,
|
|
14
|
+
type DialogProps,
|
|
15
|
+
type DialogRenderProps,
|
|
16
|
+
type DialogContentProps,
|
|
17
|
+
type DialogTriggerProps,
|
|
18
|
+
type DialogTitleProps,
|
|
19
|
+
type DialogDescriptionProps
|
|
20
|
+
} from './Dialog.js'
|
|
21
|
+
import {
|
|
22
|
+
ComboboxRoot,
|
|
23
|
+
ComboboxInput,
|
|
24
|
+
ComboboxInlineContent,
|
|
25
|
+
ComboboxItem,
|
|
26
|
+
useCommandParts,
|
|
27
|
+
type ComboboxOption,
|
|
28
|
+
type ComboboxInputProps,
|
|
29
|
+
type ComboboxItemProps
|
|
30
|
+
} from './Combobox.js'
|
|
31
|
+
import type { StateOwner } from './react/useNativeOwner.js'
|
|
32
|
+
import type { DistributiveOmit } from './internal/types.js'
|
|
33
|
+
import { requireAccessibleName, type AccessibleName } from './internal/accessibleName.js'
|
|
34
|
+
import { WithoutField } from './Field.js'
|
|
35
|
+
import { Kbd, KbdGroup } from './Kbd.js'
|
|
36
|
+
import { cn } from './cn.js'
|
|
37
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
38
|
+
import type { ControlSize } from './presentation.js'
|
|
39
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
40
|
+
|
|
41
|
+
export type CommandOption = ComboboxOption & { group?: string; shortcut?: readonly string[] }
|
|
42
|
+
export type CommandProps = DialogProps &
|
|
43
|
+
StateOwner<'query', string> & {
|
|
44
|
+
items: readonly CommandOption[]
|
|
45
|
+
onQueryChange?: (query: string) => void
|
|
46
|
+
/** Runs on explicit activation, then requests Dialog close. */
|
|
47
|
+
onAction: (value: string) => void
|
|
48
|
+
filter?: ((item: CommandOption, query: string) => boolean) | null
|
|
49
|
+
loading?: boolean
|
|
50
|
+
errorText?: ReactNode
|
|
51
|
+
loadingText?: string
|
|
52
|
+
emptyText?: string
|
|
53
|
+
controlSize?: ControlSize
|
|
54
|
+
}
|
|
55
|
+
export type CommandRenderProps = DialogRenderProps
|
|
56
|
+
export type CommandTriggerProps = DistributiveOmit<
|
|
57
|
+
DialogTriggerProps,
|
|
58
|
+
'pressed' | 'defaultPressed' | 'onPressedChange'
|
|
59
|
+
>
|
|
60
|
+
export type CommandCloseProps = DialogTriggerProps
|
|
61
|
+
export type CommandContentProps = DialogContentProps
|
|
62
|
+
export type CommandTitleProps = DialogTitleProps
|
|
63
|
+
export type CommandDescriptionProps = DialogDescriptionProps
|
|
64
|
+
export type CommandInputProps = Omit<ComboboxInputProps, 'aria-label' | 'aria-labelledby'> &
|
|
65
|
+
(AccessibleName | { 'aria-label'?: undefined; 'aria-labelledby'?: undefined })
|
|
66
|
+
export type CommandListProps = Omit<ComponentProps<'div'>, 'style' | 'role' | 'id'> & {
|
|
67
|
+
style?: ThemeStyle
|
|
68
|
+
}
|
|
69
|
+
export type CommandItemProps = ComboboxItemProps
|
|
70
|
+
type Options = { items: readonly CommandOption[] }
|
|
71
|
+
const CommandContext = createContext<Options | null>(null)
|
|
72
|
+
function useCommandContext() {
|
|
73
|
+
const context = useContext(CommandContext)
|
|
74
|
+
const family = useDialogFamily()
|
|
75
|
+
if (!context || family !== 'command') throw new Error('Command parts must render inside Command.')
|
|
76
|
+
return context
|
|
77
|
+
}
|
|
78
|
+
/** Modal actions and editable query are independent; applications register their shortcuts. */
|
|
79
|
+
export function Command({
|
|
80
|
+
query,
|
|
81
|
+
defaultQuery,
|
|
82
|
+
onQueryChange,
|
|
83
|
+
items,
|
|
84
|
+
onAction,
|
|
85
|
+
filter,
|
|
86
|
+
loading,
|
|
87
|
+
errorText,
|
|
88
|
+
loadingText,
|
|
89
|
+
emptyText,
|
|
90
|
+
controlSize,
|
|
91
|
+
children,
|
|
92
|
+
...dialog
|
|
93
|
+
}: CommandProps) {
|
|
94
|
+
const search = useControllableState({
|
|
95
|
+
name: 'Command query',
|
|
96
|
+
value: query,
|
|
97
|
+
defaultValue: defaultQuery,
|
|
98
|
+
initialValue: '',
|
|
99
|
+
onChange: onQueryChange
|
|
100
|
+
})
|
|
101
|
+
if (
|
|
102
|
+
typeof search.value !== 'string' ||
|
|
103
|
+
(defaultQuery !== undefined && typeof defaultQuery !== 'string')
|
|
104
|
+
)
|
|
105
|
+
throw new TypeError('Command queries must be strings.')
|
|
106
|
+
// Groups follow their first appearance. Keyboard and rendered order share this registry.
|
|
107
|
+
const groups = new Map<string | undefined, CommandOption[]>()
|
|
108
|
+
for (const item of items) {
|
|
109
|
+
if (item.group !== undefined && !item.group.trim())
|
|
110
|
+
throw new TypeError('Command groups need readable names.')
|
|
111
|
+
const group = groups.get(item.group) ?? []
|
|
112
|
+
group.push(item)
|
|
113
|
+
groups.set(item.group, group)
|
|
114
|
+
}
|
|
115
|
+
const ordered = [...groups.values()].flat()
|
|
116
|
+
return (
|
|
117
|
+
<DialogRoot
|
|
118
|
+
{...dialog}
|
|
119
|
+
family="command"
|
|
120
|
+
>
|
|
121
|
+
<CommandContext.Provider value={{ items: ordered }}>
|
|
122
|
+
<CommandResults
|
|
123
|
+
query={search.value}
|
|
124
|
+
onQueryChange={search.request}
|
|
125
|
+
items={ordered}
|
|
126
|
+
onAction={onAction}
|
|
127
|
+
filter={filter}
|
|
128
|
+
loading={loading}
|
|
129
|
+
errorText={errorText}
|
|
130
|
+
loadingText={loadingText}
|
|
131
|
+
emptyText={emptyText}
|
|
132
|
+
controlSize={controlSize}
|
|
133
|
+
>
|
|
134
|
+
{children}
|
|
135
|
+
</CommandResults>
|
|
136
|
+
</CommandContext.Provider>
|
|
137
|
+
</DialogRoot>
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
function CommandResults({
|
|
141
|
+
items,
|
|
142
|
+
onAction,
|
|
143
|
+
filter,
|
|
144
|
+
errorText,
|
|
145
|
+
children,
|
|
146
|
+
...props
|
|
147
|
+
}: Pick<
|
|
148
|
+
CommandProps,
|
|
149
|
+
| 'items'
|
|
150
|
+
| 'query'
|
|
151
|
+
| 'onQueryChange'
|
|
152
|
+
| 'onAction'
|
|
153
|
+
| 'filter'
|
|
154
|
+
| 'loading'
|
|
155
|
+
| 'errorText'
|
|
156
|
+
| 'loadingText'
|
|
157
|
+
| 'emptyText'
|
|
158
|
+
| 'controlSize'
|
|
159
|
+
| 'children'
|
|
160
|
+
>) {
|
|
161
|
+
const dialog = useDialog()!
|
|
162
|
+
const registry = new Map(items.map(item => [item.value, item]))
|
|
163
|
+
return (
|
|
164
|
+
<WithoutField>
|
|
165
|
+
<ComboboxRoot
|
|
166
|
+
{...props}
|
|
167
|
+
items={items}
|
|
168
|
+
value={null}
|
|
169
|
+
error={errorText}
|
|
170
|
+
open={dialog.open}
|
|
171
|
+
filter={
|
|
172
|
+
filter === null
|
|
173
|
+
? null
|
|
174
|
+
: filter
|
|
175
|
+
? (item, text) => filter(registry.get(item.value)!, text)
|
|
176
|
+
: undefined
|
|
177
|
+
}
|
|
178
|
+
command={{
|
|
179
|
+
onCommit(value) {
|
|
180
|
+
onAction(value)
|
|
181
|
+
dialog.setOpen(false)
|
|
182
|
+
}
|
|
183
|
+
}}
|
|
184
|
+
>
|
|
185
|
+
{children ?? false}
|
|
186
|
+
</ComboboxRoot>
|
|
187
|
+
</WithoutField>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
export function CommandTrigger(props: CommandTriggerProps) {
|
|
191
|
+
useCommandContext()
|
|
192
|
+
return (
|
|
193
|
+
<DialogTrigger
|
|
194
|
+
{...props}
|
|
195
|
+
pressed={undefined}
|
|
196
|
+
defaultPressed={undefined}
|
|
197
|
+
onPressedChange={undefined}
|
|
198
|
+
/>
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
export function CommandClose(props: CommandCloseProps) {
|
|
202
|
+
useCommandContext()
|
|
203
|
+
return <DialogClose {...props} />
|
|
204
|
+
}
|
|
205
|
+
export function CommandContent({ className, ...props }: CommandContentProps) {
|
|
206
|
+
useCommandContext()
|
|
207
|
+
return (
|
|
208
|
+
<DialogContent
|
|
209
|
+
{...props}
|
|
210
|
+
className={cn('open:flex open:flex-col', className)}
|
|
211
|
+
/>
|
|
212
|
+
)
|
|
213
|
+
}
|
|
214
|
+
export function CommandTitle(props: CommandTitleProps) {
|
|
215
|
+
useCommandContext()
|
|
216
|
+
return <DialogTitle {...props} />
|
|
217
|
+
}
|
|
218
|
+
export function CommandDescription(props: CommandDescriptionProps) {
|
|
219
|
+
useCommandContext()
|
|
220
|
+
return <DialogDescription {...props} />
|
|
221
|
+
}
|
|
222
|
+
export function CommandInput({ className, ...props }: CommandInputProps) {
|
|
223
|
+
useCommandContext()
|
|
224
|
+
useCommandParts()
|
|
225
|
+
const name = {
|
|
226
|
+
'aria-label': props['aria-labelledby']
|
|
227
|
+
? props['aria-label']
|
|
228
|
+
: (props['aria-label'] ?? 'Search commands'),
|
|
229
|
+
'aria-labelledby': props['aria-labelledby']
|
|
230
|
+
}
|
|
231
|
+
requireAccessibleName('CommandInput', name)
|
|
232
|
+
return (
|
|
233
|
+
<ComboboxInput
|
|
234
|
+
{...props}
|
|
235
|
+
{...name}
|
|
236
|
+
className={cn('shrink-0 pe-[var(--_rad-control-padding,1rem)]', className)}
|
|
237
|
+
/>
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
export function CommandList({ children, className, ...props }: CommandListProps) {
|
|
241
|
+
const { items } = useCommandContext()
|
|
242
|
+
const box = useCommandParts()
|
|
243
|
+
const visible = new Set(box.visible.map(item => item.value))
|
|
244
|
+
const groups = new Map<string | undefined, CommandOption[]>()
|
|
245
|
+
for (const item of items)
|
|
246
|
+
if (visible.has(item.value)) {
|
|
247
|
+
const group = groups.get(item.group) ?? []
|
|
248
|
+
group.push(item)
|
|
249
|
+
groups.set(item.group, group)
|
|
250
|
+
}
|
|
251
|
+
return (
|
|
252
|
+
<ComboboxInlineContent
|
|
253
|
+
{...props}
|
|
254
|
+
className={cn(
|
|
255
|
+
'min-h-[var(--_rad-control-height,2.5rem)] max-h-[min(50dvh,var(--rad-command-results-max-height,24rem))]',
|
|
256
|
+
className
|
|
257
|
+
)}
|
|
258
|
+
>
|
|
259
|
+
{children ??
|
|
260
|
+
[...groups].map(([group, options]) =>
|
|
261
|
+
group === undefined ? (
|
|
262
|
+
options.map(item => (
|
|
263
|
+
<CommandItem
|
|
264
|
+
key={item.value}
|
|
265
|
+
value={item.value}
|
|
266
|
+
/>
|
|
267
|
+
))
|
|
268
|
+
) : (
|
|
269
|
+
<div
|
|
270
|
+
key={group}
|
|
271
|
+
role="group"
|
|
272
|
+
aria-label={group}
|
|
273
|
+
data-slot="command-group"
|
|
274
|
+
>
|
|
275
|
+
<p
|
|
276
|
+
aria-hidden="true"
|
|
277
|
+
className="m-0 px-3 py-2 text-sm font-semibold"
|
|
278
|
+
data-slot="command-group-label"
|
|
279
|
+
>
|
|
280
|
+
{group}
|
|
281
|
+
</p>
|
|
282
|
+
{options.map(item => (
|
|
283
|
+
<CommandItem
|
|
284
|
+
key={item.value}
|
|
285
|
+
value={item.value}
|
|
286
|
+
/>
|
|
287
|
+
))}
|
|
288
|
+
</div>
|
|
289
|
+
)
|
|
290
|
+
)}
|
|
291
|
+
</ComboboxInlineContent>
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
export function CommandItem({ children, value, ...props }: CommandItemProps) {
|
|
295
|
+
const { items } = useCommandContext()
|
|
296
|
+
useCommandParts()
|
|
297
|
+
const item = items.find(item => item.value === value)
|
|
298
|
+
return (
|
|
299
|
+
<ComboboxItem
|
|
300
|
+
{...props}
|
|
301
|
+
value={value}
|
|
302
|
+
>
|
|
303
|
+
{children ?? (
|
|
304
|
+
<>
|
|
305
|
+
<span className="min-w-0 flex-1">{item?.label}</span>
|
|
306
|
+
{item?.shortcut?.length ? (
|
|
307
|
+
<KbdGroup className="shrink-0">
|
|
308
|
+
{item.shortcut.map((label, index) => (
|
|
309
|
+
<Kbd key={index}>{label}</Kbd>
|
|
310
|
+
))}
|
|
311
|
+
</KbdGroup>
|
|
312
|
+
) : null}
|
|
313
|
+
</>
|
|
314
|
+
)}
|
|
315
|
+
</ComboboxItem>
|
|
316
|
+
)
|
|
317
|
+
}
|
package/src/Dialog.tsx
CHANGED
|
@@ -108,7 +108,9 @@ export type DialogDescriptionProps = Omit<ComponentProps<'p'>, 'style'> & { styl
|
|
|
108
108
|
export type DialogCloseProps = DialogTriggerProps
|
|
109
109
|
|
|
110
110
|
const DialogContext = createContext<DialogState | null>(null)
|
|
111
|
-
const DialogFamilyContext = createContext<'dialog' | 'alert-dialog' | 'drawer'>(
|
|
111
|
+
const DialogFamilyContext = createContext<'dialog' | 'alert-dialog' | 'drawer' | 'command'>(
|
|
112
|
+
'dialog'
|
|
113
|
+
)
|
|
112
114
|
|
|
113
115
|
/** Private composition seam for specialized Dialog families; not exported from the package. */
|
|
114
116
|
export function useDialogFamily() {
|
|
@@ -144,7 +146,7 @@ export function DialogRoot({
|
|
|
144
146
|
closeOnEscape = true,
|
|
145
147
|
children,
|
|
146
148
|
family = 'dialog'
|
|
147
|
-
}: DialogProps & { family?: 'dialog' | 'alert-dialog' | 'drawer' }) {
|
|
149
|
+
}: DialogProps & { family?: 'dialog' | 'alert-dialog' | 'drawer' | 'command' }) {
|
|
148
150
|
const contentId = useId()
|
|
149
151
|
const titleId = useId()
|
|
150
152
|
const descriptionId = useId()
|
package/src/Field.tsx
CHANGED
|
@@ -109,6 +109,11 @@ export type FieldProps = FieldControl | FieldGroup
|
|
|
109
109
|
|
|
110
110
|
const FieldContext = createContext<FieldState | null>(null)
|
|
111
111
|
|
|
112
|
+
/** Private boundary for non-form composites nested inside a Field. */
|
|
113
|
+
export function WithoutField({ children }: { children?: ReactNode }) {
|
|
114
|
+
return <FieldContext.Provider value={null}>{children}</FieldContext.Provider>
|
|
115
|
+
}
|
|
116
|
+
|
|
112
117
|
/** Nearest Field metadata, or null. Groups have no single target: controlProps is null. */
|
|
113
118
|
export function useField(): FieldState | null {
|
|
114
119
|
return useContext(FieldContext)
|
package/src/cn.ts
CHANGED
|
@@ -88,6 +88,7 @@ export const cn: CnFunction = createCn({
|
|
|
88
88
|
'w-(--rad-slider-length)'
|
|
89
89
|
],
|
|
90
90
|
'min-w': ['min-w-(--rad-pagination-item-min-width)'],
|
|
91
|
+
'max-h': ['max-h-(--rad-command-results-max-height)'],
|
|
91
92
|
'max-w': ['max-w-(--rad-tooltip-max-width)'],
|
|
92
93
|
size: [
|
|
93
94
|
'size-(--rad-avatar-size)',
|
package/src/index.ts
CHANGED
|
@@ -613,3 +613,26 @@ export {
|
|
|
613
613
|
type AutocompleteItemProps,
|
|
614
614
|
type AutocompleteStatusProps
|
|
615
615
|
} from './Autocomplete.js'
|
|
616
|
+
|
|
617
|
+
export {
|
|
618
|
+
Command,
|
|
619
|
+
CommandTrigger,
|
|
620
|
+
CommandClose,
|
|
621
|
+
CommandContent,
|
|
622
|
+
CommandTitle,
|
|
623
|
+
CommandDescription,
|
|
624
|
+
CommandInput,
|
|
625
|
+
CommandList,
|
|
626
|
+
CommandItem,
|
|
627
|
+
type CommandProps,
|
|
628
|
+
type CommandRenderProps,
|
|
629
|
+
type CommandOption,
|
|
630
|
+
type CommandTriggerProps,
|
|
631
|
+
type CommandCloseProps,
|
|
632
|
+
type CommandContentProps,
|
|
633
|
+
type CommandTitleProps,
|
|
634
|
+
type CommandDescriptionProps,
|
|
635
|
+
type CommandInputProps,
|
|
636
|
+
type CommandListProps,
|
|
637
|
+
type CommandItemProps
|
|
638
|
+
} from './Command.js'
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import { useState } from 'react'
|
|
4
4
|
|
|
5
|
+
export type StateOwner<Name extends string, Value> =
|
|
6
|
+
| ({ [Key in Name]: Value } & { [Key in `default${Capitalize<Name>}`]?: never })
|
|
7
|
+
| ({ [Key in Name]?: undefined } & { [Key in `default${Capitalize<Name>}`]?: Value })
|
|
8
|
+
|
|
5
9
|
export type ValueOwner<Value> =
|
|
6
10
|
| { value: Value; defaultValue?: never }
|
|
7
11
|
| { value?: undefined; defaultValue?: Value }
|
package/src/themes/components.ts
CHANGED