@dinachi/cli 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/dist/index.js +244 -72
- package/package.json +5 -1
- package/templates/agent/agent.tsx +35 -18
- package/templates/alert/alert.tsx +345 -0
- package/templates/alert/index.ts +16 -0
- package/templates/ask-bar/ask-bar.tsx +1061 -0
- package/templates/ask-bar/index.ts +1 -0
- package/templates/autocomplete/autocomplete.tsx +0 -1
- package/templates/breadcrumb/breadcrumb.tsx +606 -0
- package/templates/breadcrumb/index.ts +9 -0
- package/templates/calendar/calendar.tsx +1155 -0
- package/templates/calendar/index.ts +15 -0
- package/templates/combobox/combobox.tsx +0 -1
- package/templates/command-items/command-items.tsx +69 -0
- package/templates/command-items/index.ts +1 -0
- package/templates/command-palette/command-palette.tsx +954 -0
- package/templates/command-palette/index.ts +1 -0
- package/templates/comparison-table/comparison-table.tsx +540 -0
- package/templates/comparison-table/index.ts +15 -0
- package/templates/data-table/data-table.tsx +2077 -0
- package/templates/data-table/index.ts +14 -0
- package/templates/date-picker/date-picker.tsx +527 -0
- package/templates/date-picker/index.ts +2 -0
- package/templates/date-range-picker/date-range-picker.tsx +504 -0
- package/templates/date-range-picker/index.ts +2 -0
- package/templates/detail-list/detail-list.tsx +419 -0
- package/templates/detail-list/index.ts +12 -0
- package/templates/heat-table/heat-table.tsx +613 -0
- package/templates/heat-table/index.ts +9 -0
- package/templates/menu/menu.tsx +8 -3
- package/templates/month-picker/index.ts +1 -0
- package/templates/month-picker/month-picker.tsx +351 -0
- package/templates/omnibar/index.ts +1 -0
- package/templates/omnibar/omnibar.tsx +1009 -0
- package/templates/pagination/index.ts +8 -0
- package/templates/pagination/pagination.tsx +389 -0
- package/templates/table/index.ts +13 -0
- package/templates/table/table.tsx +265 -0
- package/templates/time-slots/index.ts +1 -0
- package/templates/time-slots/time-slots.tsx +589 -0
- package/templates/week-strip/index.ts +1 -0
- package/templates/week-strip/week-strip.tsx +329 -0
- package/templates/year-view/index.ts +1 -0
- package/templates/year-view/year-view.tsx +362 -0
|
@@ -0,0 +1,1009 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Popover as PopoverPrimitive } from "@base-ui/react/popover"
|
|
5
|
+
import { Autocomplete as AutocompletePrimitive } from "@base-ui/react/autocomplete"
|
|
6
|
+
import { MoreHorizontal, Search } from "lucide-react"
|
|
7
|
+
import { cn } from "@/lib/utils"
|
|
8
|
+
import {
|
|
9
|
+
changeAsAgent,
|
|
10
|
+
createAgentChangeDetails,
|
|
11
|
+
matchAgentChoice,
|
|
12
|
+
useAgentControl,
|
|
13
|
+
useAgentLatest,
|
|
14
|
+
useHasAgent,
|
|
15
|
+
useMergedRef,
|
|
16
|
+
waitForAgent,
|
|
17
|
+
} from "@/components/ui/agent"
|
|
18
|
+
import { Drawer, DrawerContent, DrawerDescription, DrawerTitle } from "@/components/ui/drawer"
|
|
19
|
+
|
|
20
|
+
// The item shape the Command Palette uses too, so one list feeds both.
|
|
21
|
+
import { commandsHint, filterCommands, type CommandAction, type CommandItem, type CommandRunDetails, type CommandRunReason } from "@/components/ui/command-items"
|
|
22
|
+
export { filterCommands }
|
|
23
|
+
export type { CommandAction, CommandItem, CommandRunDetails, CommandRunReason }
|
|
24
|
+
|
|
25
|
+
/** A slice of the list, picked by chip or by typing its prefix first. */
|
|
26
|
+
export interface OmnibarScope {
|
|
27
|
+
id: string
|
|
28
|
+
/** The chip's text. By default the scope holds the items whose `group` is this. */
|
|
29
|
+
label: string
|
|
30
|
+
/** Typed at the start of the search, such as "@", it switches to this scope. */
|
|
31
|
+
prefix?: string
|
|
32
|
+
/** Which items the scope holds, when `group` is not the way to tell. */
|
|
33
|
+
match?: (item: CommandItem) => boolean
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How `onRun` was reached. `cancel()` keeps the omnibar open and tells an agent it was refused. */
|
|
37
|
+
export type OmnibarRunDetails = CommandRunDetails
|
|
38
|
+
|
|
39
|
+
type OpenDetails = PopoverPrimitive.Root.ChangeEventDetails
|
|
40
|
+
type ValueDetails = AutocompletePrimitive.Root.ChangeEventDetails
|
|
41
|
+
type Group = { value: string; items: CommandItem[] }
|
|
42
|
+
|
|
43
|
+
function groupsOf(items: CommandItem[]): Group[] {
|
|
44
|
+
const groups: Group[] = []
|
|
45
|
+
for (const item of items) {
|
|
46
|
+
const value = item.group ?? ""
|
|
47
|
+
const last = groups.find((g) => g.value === value)
|
|
48
|
+
if (last) last.items.push(item)
|
|
49
|
+
else groups.push({ value, items: [item] })
|
|
50
|
+
}
|
|
51
|
+
return groups
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A sub-list has no level of its own here: its items are listed in its place. */
|
|
55
|
+
const flatten = (items: CommandItem[]): CommandItem[] => items.flatMap((i) => (i.items ? flatten(i.items) : [i]))
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Disabled items last in their group, and groups with nothing to run last, so the
|
|
59
|
+
* highlight Base UI puts on the first row lands where Enter does something.
|
|
60
|
+
*/
|
|
61
|
+
const usableFirst = (groups: Group[]): Group[] =>
|
|
62
|
+
groups
|
|
63
|
+
.map((g) => ({ ...g, items: [...g.items.filter((i) => !i.disabled), ...g.items.filter((i) => i.disabled)] }))
|
|
64
|
+
.sort((a, b) => Number(Boolean(a.items[0]?.disabled)) - Number(Boolean(b.items[0]?.disabled)))
|
|
65
|
+
|
|
66
|
+
const inScope = (scope: OmnibarScope, item: CommandItem) => (scope.match ? scope.match(item) : item.group === scope.label)
|
|
67
|
+
|
|
68
|
+
/** Change details for a person's change that Base UI did not make itself. */
|
|
69
|
+
function personDetails<D>(reason: string, event?: Event): D {
|
|
70
|
+
const details = {
|
|
71
|
+
reason,
|
|
72
|
+
event,
|
|
73
|
+
isCanceled: false,
|
|
74
|
+
isPropagationAllowed: false,
|
|
75
|
+
trigger: undefined,
|
|
76
|
+
cancel() {
|
|
77
|
+
details.isCanceled = true
|
|
78
|
+
},
|
|
79
|
+
allowPropagation() {},
|
|
80
|
+
preventUnmountOnClose() {},
|
|
81
|
+
}
|
|
82
|
+
return details as unknown as D
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const PHONE = "(max-width: 639px)"
|
|
86
|
+
|
|
87
|
+
function usePhone(): boolean {
|
|
88
|
+
return React.useSyncExternalStore(
|
|
89
|
+
(onChange) => {
|
|
90
|
+
const query = window.matchMedia?.(PHONE)
|
|
91
|
+
query?.addEventListener?.("change", onChange)
|
|
92
|
+
return () => query?.removeEventListener?.("change", onChange)
|
|
93
|
+
},
|
|
94
|
+
() => Boolean(window.matchMedia?.(PHONE).matches),
|
|
95
|
+
() => false
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const isMac = () => typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)
|
|
100
|
+
|
|
101
|
+
/** A field, a text area or anything editable, where a typed "/" belongs to the text. */
|
|
102
|
+
const isEditable = (el: EventTarget | null) =>
|
|
103
|
+
el instanceof HTMLElement && (el.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName))
|
|
104
|
+
|
|
105
|
+
interface OmnibarContext {
|
|
106
|
+
/** Inside an AgentProvider: marks the omnibar's own buttons so the agent sees one control. */
|
|
107
|
+
part: "" | undefined
|
|
108
|
+
label: string
|
|
109
|
+
open: boolean
|
|
110
|
+
disabled: boolean
|
|
111
|
+
phone: boolean
|
|
112
|
+
query: string
|
|
113
|
+
setQuery: (query: string, details: ValueDetails) => void
|
|
114
|
+
scopes: OmnibarScope[]
|
|
115
|
+
scope: OmnibarScope | undefined
|
|
116
|
+
/** `quiet` when the change already announces itself, as a chip's aria-pressed does. */
|
|
117
|
+
setScope: (id: string | null, quiet?: boolean) => void
|
|
118
|
+
shown: Group[]
|
|
119
|
+
/** How many matched before `limit` cut the list. */
|
|
120
|
+
total: number
|
|
121
|
+
/** The search's words, for bolding them in a row. */
|
|
122
|
+
marks: RegExp | null
|
|
123
|
+
sheet: CommandItem | undefined
|
|
124
|
+
/** Opens the action sheet for `item` from the button `from`, or closes it. */
|
|
125
|
+
showSheet: (item?: CommandItem, from?: HTMLElement) => void
|
|
126
|
+
run: (item: CommandItem, action: CommandAction | undefined, details: OmnibarRunDetails) => string | undefined
|
|
127
|
+
close: (event?: Event) => void
|
|
128
|
+
shortcut: string | false
|
|
129
|
+
trigger: React.MutableRefObject<HTMLElement | null>
|
|
130
|
+
/** The ⋯ button that opened the sheet, where focus goes back to. */
|
|
131
|
+
opener: React.MutableRefObject<HTMLElement | null>
|
|
132
|
+
input: React.MutableRefObject<HTMLInputElement | null>
|
|
133
|
+
live: Live
|
|
134
|
+
/** The id of the text naming what Enter runs on the highlighted row. */
|
|
135
|
+
hintId: string
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface LiveState {
|
|
139
|
+
highlighted: CommandItem | undefined
|
|
140
|
+
/** Which of the highlighted row's actions Enter runs. Tab moves it. */
|
|
141
|
+
action: number
|
|
142
|
+
/** The latest polite announcement. */
|
|
143
|
+
say: string
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The highlight lives outside React state, so moving it re-renders only the two rows it
|
|
148
|
+
* leaves and lands on, not the whole list.
|
|
149
|
+
*/
|
|
150
|
+
function createLive() {
|
|
151
|
+
let state: LiveState = { highlighted: undefined, action: 0, say: "" }
|
|
152
|
+
const subs = new Set<() => void>()
|
|
153
|
+
return {
|
|
154
|
+
get: () => state,
|
|
155
|
+
set(next: Partial<LiveState>) {
|
|
156
|
+
state = { ...state, ...next }
|
|
157
|
+
subs.forEach((f) => f())
|
|
158
|
+
},
|
|
159
|
+
subscribe(f: () => void) {
|
|
160
|
+
subs.add(f)
|
|
161
|
+
return () => void subs.delete(f)
|
|
162
|
+
},
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
type Live = ReturnType<typeof createLive>
|
|
166
|
+
|
|
167
|
+
function useLive<T>(live: Live, pick: (state: LiveState) => T): T {
|
|
168
|
+
return React.useSyncExternalStore(live.subscribe, () => pick(live.get()), () => pick(live.get()))
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A stable function that always calls the latest `fn`. */
|
|
172
|
+
function useStable<A extends unknown[], R>(fn: (...args: A) => R): (...args: A) => R {
|
|
173
|
+
const latest = useAgentLatest(fn)
|
|
174
|
+
return React.useCallback((...args: A) => latest.current(...args), [latest])
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const escape = (word: string) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
178
|
+
|
|
179
|
+
const OmnibarContext = React.createContext<OmnibarContext | null>(null)
|
|
180
|
+
|
|
181
|
+
function useOmnibar(part: string): OmnibarContext {
|
|
182
|
+
const context = React.useContext(OmnibarContext)
|
|
183
|
+
if (!context) throw new Error(`${part} must be inside <Omnibar>.`)
|
|
184
|
+
return context
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface OmnibarProps {
|
|
188
|
+
/** Every command, in the order groups and items are listed. */
|
|
189
|
+
items: CommandItem[]
|
|
190
|
+
/** The chips after All. A typed `prefix` switches to its scope. */
|
|
191
|
+
scopes?: OmnibarScope[]
|
|
192
|
+
/** Ids listed first, under "Recent", while the search is empty and no scope is picked. */
|
|
193
|
+
recent?: string[]
|
|
194
|
+
recentLabel?: string
|
|
195
|
+
/** Ids listed after Recent while the search is empty and no scope is picked, under their own groups. */
|
|
196
|
+
suggested?: string[]
|
|
197
|
+
/** How many rows show while the search is empty, when `recent` or `suggested` is set. */
|
|
198
|
+
idleLimit?: number
|
|
199
|
+
/** The most rows the list renders. Keep typing to narrow past it. */
|
|
200
|
+
limit?: number
|
|
201
|
+
/** Runs a picked item. `details.cancel()` keeps the omnibar open. */
|
|
202
|
+
onRun?: (item: CommandItem, action: CommandAction | undefined, details: OmnibarRunDetails) => void
|
|
203
|
+
open?: boolean
|
|
204
|
+
defaultOpen?: boolean
|
|
205
|
+
onOpenChange?: (open: boolean, details: OpenDetails) => void
|
|
206
|
+
/** The search text. */
|
|
207
|
+
value?: string
|
|
208
|
+
defaultValue?: string
|
|
209
|
+
onValueChange?: (value: string, details: ValueDetails) => void
|
|
210
|
+
/** The picked scope's id, or null for All. */
|
|
211
|
+
scope?: string | null
|
|
212
|
+
defaultScope?: string | null
|
|
213
|
+
onScopeChange?: (scope: string | null) => void
|
|
214
|
+
/**
|
|
215
|
+
* The key that focuses it from anywhere but another field, such as "/". "mod+j" asks for
|
|
216
|
+
* Cmd or Ctrl and works in fields too. `false` turns it off.
|
|
217
|
+
*/
|
|
218
|
+
shortcut?: string | false
|
|
219
|
+
/** Names the field and the panel, and is what an agent finds it by. */
|
|
220
|
+
label?: string
|
|
221
|
+
filter?: (item: CommandItem, query: string) => boolean
|
|
222
|
+
disabled?: boolean
|
|
223
|
+
children?: React.ReactNode
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The header's search field and the results panel it grows into: a Base UI popover over
|
|
228
|
+
* the field, holding an Autocomplete. No dialog and no scrim, so the page stays in view.
|
|
229
|
+
* Forgets the search and scope when it closes.
|
|
230
|
+
*/
|
|
231
|
+
function Omnibar({
|
|
232
|
+
items,
|
|
233
|
+
scopes = [],
|
|
234
|
+
recent,
|
|
235
|
+
recentLabel = "Recent",
|
|
236
|
+
suggested,
|
|
237
|
+
idleLimit = 8,
|
|
238
|
+
limit = 50,
|
|
239
|
+
onRun,
|
|
240
|
+
open: openProp,
|
|
241
|
+
defaultOpen = false,
|
|
242
|
+
onOpenChange,
|
|
243
|
+
value: valueProp,
|
|
244
|
+
defaultValue = "",
|
|
245
|
+
onValueChange,
|
|
246
|
+
scope: scopeProp,
|
|
247
|
+
defaultScope = null,
|
|
248
|
+
onScopeChange,
|
|
249
|
+
shortcut = "/",
|
|
250
|
+
label = "Search",
|
|
251
|
+
filter = filterCommands,
|
|
252
|
+
disabled = false,
|
|
253
|
+
children,
|
|
254
|
+
}: OmnibarProps) {
|
|
255
|
+
const [openInner, setOpenInner] = React.useState(defaultOpen)
|
|
256
|
+
const [queryInner, setQueryInner] = React.useState(defaultValue)
|
|
257
|
+
const [scopeInner, setScopeInner] = React.useState(defaultScope)
|
|
258
|
+
const [live] = React.useState(createLive)
|
|
259
|
+
const hintId = React.useId()
|
|
260
|
+
const [sheet, setSheet] = React.useState<CommandItem | undefined>()
|
|
261
|
+
const open = openProp ?? openInner
|
|
262
|
+
const query = valueProp ?? queryInner
|
|
263
|
+
const scopeId = scopeProp === undefined ? scopeInner : scopeProp
|
|
264
|
+
const scope = scopes.find((s) => s.id === scopeId)
|
|
265
|
+
const phone = usePhone()
|
|
266
|
+
const trigger = React.useRef<HTMLElement | null>(null)
|
|
267
|
+
const input = React.useRef<HTMLInputElement | null>(null)
|
|
268
|
+
const opener = React.useRef<HTMLElement | null>(null)
|
|
269
|
+
|
|
270
|
+
const setScope = useStable((id: string | null, quiet?: boolean) => {
|
|
271
|
+
onScopeChange?.(id)
|
|
272
|
+
if (scopeProp === undefined) setScopeInner(id)
|
|
273
|
+
const picked = scopes.find((s) => s.id === id)
|
|
274
|
+
live.set({ action: 0, say: quiet ? "" : `Scope: ${picked ? picked.label : "All"}` })
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
// Closed by the app or by the omnibar, it starts over next time.
|
|
278
|
+
React.useEffect(() => {
|
|
279
|
+
if (!open) live.set({ highlighted: undefined, action: 0, say: "" })
|
|
280
|
+
}, [open, live])
|
|
281
|
+
const [wasOpen, setWasOpen] = React.useState(open)
|
|
282
|
+
if (wasOpen !== open) {
|
|
283
|
+
setWasOpen(open)
|
|
284
|
+
if (!open) {
|
|
285
|
+
setSheet(undefined)
|
|
286
|
+
if (valueProp === undefined) setQueryInner("")
|
|
287
|
+
if (scopeProp === undefined) setScopeInner(null)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const changeOpen = React.useCallback(
|
|
292
|
+
(next: boolean, details: OpenDetails) => {
|
|
293
|
+
onOpenChange?.(next, details)
|
|
294
|
+
if (!details.isCanceled && openProp === undefined) setOpenInner(next)
|
|
295
|
+
},
|
|
296
|
+
[onOpenChange, openProp]
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
// A typed prefix picks its scope and leaves the field.
|
|
300
|
+
const setQuery = useStable((next: string, details: ValueDetails) => {
|
|
301
|
+
const typed = scope ? undefined : scopes.find((s) => s.prefix && next.startsWith(s.prefix))
|
|
302
|
+
if (typed) {
|
|
303
|
+
setScope(typed.id)
|
|
304
|
+
next = next.slice(typed.prefix!.length).trimStart()
|
|
305
|
+
}
|
|
306
|
+
onValueChange?.(next, details)
|
|
307
|
+
if (!details.isCanceled && valueProp === undefined) setQueryInner(next)
|
|
308
|
+
live.set({ action: 0 })
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
const all = React.useMemo(() => flatten(items), [items])
|
|
312
|
+
const { shown, total } = React.useMemo(() => {
|
|
313
|
+
const cap = (groups: Group[]) => {
|
|
314
|
+
let left = limit
|
|
315
|
+
const kept = groups.map((g) => {
|
|
316
|
+
const items = g.items.slice(0, Math.max(0, left))
|
|
317
|
+
left -= items.length
|
|
318
|
+
return { ...g, items }
|
|
319
|
+
})
|
|
320
|
+
return { shown: kept.filter((g) => g.items.length), total: groups.reduce((n, g) => n + g.items.length, 0) }
|
|
321
|
+
}
|
|
322
|
+
const list = scope ? all.filter((item) => inScope(scope, item)) : all
|
|
323
|
+
if (query.trim()) return cap(usableFirst(groupsOf(list.filter((item) => filter(item, query)))))
|
|
324
|
+
if (scope || (!recent?.length && !suggested)) return cap(usableFirst(groupsOf(list)))
|
|
325
|
+
const pick = (ids: string[]) => ids.map((id) => list.find((item) => item.id === id)).filter((item): item is CommandItem => !!item)
|
|
326
|
+
const first = pick(recent ?? []).slice(0, idleLimit)
|
|
327
|
+
const next = (suggested ? pick(suggested) : list).filter((item) => !first.includes(item)).slice(0, idleLimit - first.length)
|
|
328
|
+
return cap(usableFirst([{ value: recentLabel, items: first }, ...groupsOf(next)].filter((g) => g.items.length)))
|
|
329
|
+
}, [all, scope, query, recent, recentLabel, suggested, idleLimit, limit, filter])
|
|
330
|
+
|
|
331
|
+
const marks = React.useMemo(() => {
|
|
332
|
+
const words = query.split(/\s+/).filter(Boolean).map(escape)
|
|
333
|
+
return words.length ? new RegExp(`(${words.join("|")})`, "ig") : null
|
|
334
|
+
}, [query])
|
|
335
|
+
|
|
336
|
+
const showSheet = useStable((item?: CommandItem, from?: HTMLElement) => {
|
|
337
|
+
if (item) {
|
|
338
|
+
opener.current = from!
|
|
339
|
+
input.current?.blur()
|
|
340
|
+
}
|
|
341
|
+
setSheet(item)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
const close = useStable((event?: Event) => changeOpen(false, personDetails("imperative-action", event)))
|
|
345
|
+
|
|
346
|
+
const run = useStable((item: CommandItem, action: CommandAction | undefined, details: OmnibarRunDetails) => {
|
|
347
|
+
onRun?.(item, action, details)
|
|
348
|
+
if (details.isCanceled) return "The app refused the command."
|
|
349
|
+
if (details.event?.type === "agent") changeOpen(false, createAgentChangeDetails<OpenDetails>())
|
|
350
|
+
else close(details.event)
|
|
351
|
+
return undefined
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
const latest = useAgentLatest({ open, disabled, changeOpen })
|
|
355
|
+
React.useEffect(() => {
|
|
356
|
+
if (shortcut === false || disabled) return
|
|
357
|
+
const mod = shortcut.toLowerCase().startsWith("mod+")
|
|
358
|
+
const key = mod ? shortcut.slice(4).toLowerCase() : shortcut
|
|
359
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
360
|
+
if (event.defaultPrevented || event.isComposing || event.repeat || latest.current.open || event.altKey) return
|
|
361
|
+
if (mod ? event.key.toLowerCase() !== key || !(event.metaKey || event.ctrlKey) : event.key !== key || event.metaKey || event.ctrlKey || isEditable(event.target)) return
|
|
362
|
+
event.preventDefault()
|
|
363
|
+
latest.current.changeOpen(true, personDetails("none", event))
|
|
364
|
+
}
|
|
365
|
+
document.addEventListener("keydown", onKeyDown)
|
|
366
|
+
return () => document.removeEventListener("keydown", onKeyDown)
|
|
367
|
+
}, [shortcut, disabled, latest])
|
|
368
|
+
|
|
369
|
+
const part = useHasAgent() ? "" : undefined
|
|
370
|
+
const context = React.useMemo<OmnibarContext>(
|
|
371
|
+
() => ({ part, label, open, disabled, phone, query, setQuery, scopes, scope, setScope, shown, total, marks, sheet, showSheet, run, close, shortcut, trigger, opener, input, live, hintId }),
|
|
372
|
+
[part, label, open, disabled, phone, query, setQuery, scopes, scope, setScope, shown, total, marks, sheet, showSheet, run, close, shortcut, live, hintId]
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
useOmnibarAgent(context, all, changeOpen)
|
|
376
|
+
|
|
377
|
+
// An open action sheet is part of the omnibar, so a tap on it does not close the panel.
|
|
378
|
+
const onPopoverOpenChange = (next: boolean, details: OpenDetails) => {
|
|
379
|
+
if (!next && sheet && details.reason === "outside-press") return details.cancel()
|
|
380
|
+
changeOpen(next, details)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return (
|
|
384
|
+
<OmnibarContext.Provider value={context}>
|
|
385
|
+
<PopoverPrimitive.Root open={open} onOpenChange={onPopoverOpenChange} modal={phone}>
|
|
386
|
+
{children}
|
|
387
|
+
</PopoverPrimitive.Root>
|
|
388
|
+
</OmnibarContext.Provider>
|
|
389
|
+
)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Undefined keys drop out when the result is serialized for the model. */
|
|
393
|
+
const describe = ({ label, group, actions }: CommandItem) => ({ label, group, actions: actions?.map((a) => a.label) })
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* One combobox control for the agent: the field while closed, the search while open. It
|
|
397
|
+
* lists every command while closed and the matches while open.
|
|
398
|
+
*/
|
|
399
|
+
function useOmnibarAgent(c: OmnibarContext, all: CommandItem[], changeOpen: (next: boolean, details: OpenDetails) => void) {
|
|
400
|
+
const latest = useAgentLatest(c)
|
|
401
|
+
const opened = () => (latest.current.open ? undefined : changeAsAgent(changeOpen, true))
|
|
402
|
+
|
|
403
|
+
useAgentControl(() => ({
|
|
404
|
+
kind: "combobox",
|
|
405
|
+
element: c.open ? c.input.current : c.trigger.current,
|
|
406
|
+
label: c.label,
|
|
407
|
+
hint: commandsHint(all),
|
|
408
|
+
open: c.open,
|
|
409
|
+
disabled: c.disabled,
|
|
410
|
+
...(c.open ? { value: (c.scope?.prefix ?? "") + c.query } : {}),
|
|
411
|
+
items: (c.open ? c.shown : groupsOf(all)).flatMap((g) => g.items).map((i) => i.label),
|
|
412
|
+
actions: {
|
|
413
|
+
[c.open ? "close" : "open"]: () => changeAsAgent(changeOpen, !c.open),
|
|
414
|
+
search: async (text: unknown, scopeName?: unknown) => {
|
|
415
|
+
let id = c.scope?.id ?? null
|
|
416
|
+
if (scopeName != null) {
|
|
417
|
+
const found = matchAgentChoice([{ value: null, label: "All" }, ...c.scopes.map((s) => ({ ...s, value: s.id }))], scopeName)
|
|
418
|
+
if (typeof found === "string") return found
|
|
419
|
+
id = found.value
|
|
420
|
+
}
|
|
421
|
+
const refused = opened()
|
|
422
|
+
if (refused) return refused
|
|
423
|
+
c.setScope(id)
|
|
424
|
+
const failed = changeAsAgent(latest.current.setQuery, String(text ?? ""))
|
|
425
|
+
if (failed) return failed
|
|
426
|
+
await waitForAgent(() => latest.current.open && latest.current.input.current?.isConnected)
|
|
427
|
+
return { result: latest.current.shown.flatMap((g) => g.items).map(describe) }
|
|
428
|
+
},
|
|
429
|
+
select: (wanted: unknown, actionName?: unknown) => {
|
|
430
|
+
const match = matchAgentChoice(all.map((value) => ({ ...value, value })), wanted)
|
|
431
|
+
if (typeof match === "string") return match
|
|
432
|
+
const item = match.value
|
|
433
|
+
let action = item.actions?.[0]
|
|
434
|
+
if (actionName != null) {
|
|
435
|
+
const found = matchAgentChoice((item.actions ?? []).map((value) => ({ value, label: value.label })), actionName)
|
|
436
|
+
if (typeof found === "string") return found
|
|
437
|
+
action = found.value
|
|
438
|
+
}
|
|
439
|
+
return c.run(item, action, createAgentChangeDetails("item-press"))
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
}))
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const kbd = "rounded border border-b-2 bg-background px-[5px] py-px font-mono text-[10.5px] font-normal leading-normal text-muted-foreground"
|
|
446
|
+
|
|
447
|
+
/** The shortcut as a person reads it: "/" as is, "mod+j" as ⌘ J or Ctrl J. */
|
|
448
|
+
const keyHint = (shortcut: string) =>
|
|
449
|
+
shortcut.toLowerCase().startsWith("mod+") ? `${isMac() ? "⌘" : "Ctrl"} ${shortcut.slice(4).toUpperCase()}` : shortcut
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The field in the header while closed. A click, Enter or its shortcut opens the panel
|
|
453
|
+
* over it, and typing a letter here starts the search with it.
|
|
454
|
+
*/
|
|
455
|
+
interface OmnibarTriggerProps extends PopoverPrimitive.Trigger.Props {
|
|
456
|
+
/** The field's text while closed. */
|
|
457
|
+
placeholder?: string
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const OmnibarTrigger = React.forwardRef<HTMLButtonElement, OmnibarTriggerProps>(
|
|
461
|
+
({ className, children, disabled, placeholder = "Search or run a command", onKeyDown, ...props }, ref) => {
|
|
462
|
+
const c = useOmnibar("OmnibarTrigger")
|
|
463
|
+
return (
|
|
464
|
+
<PopoverPrimitive.Trigger
|
|
465
|
+
ref={useMergedRef(ref, c.trigger)}
|
|
466
|
+
data-agent-part={c.part}
|
|
467
|
+
disabled={disabled || c.disabled}
|
|
468
|
+
aria-label={c.label}
|
|
469
|
+
data-slot="omnibar-trigger"
|
|
470
|
+
onKeyDown={(event) => {
|
|
471
|
+
onKeyDown?.(event)
|
|
472
|
+
// The shortcut only opens it; the global listener sees the same key.
|
|
473
|
+
if (event.key === c.shortcut) return
|
|
474
|
+
if (event.key.length !== 1 || event.key === " " || event.metaKey || event.ctrlKey || event.altKey) return
|
|
475
|
+
event.preventDefault()
|
|
476
|
+
c.setQuery(event.key, personDetails("input-change", event.nativeEvent))
|
|
477
|
+
event.currentTarget.click()
|
|
478
|
+
}}
|
|
479
|
+
className={cn(
|
|
480
|
+
"relative flex h-10 w-full items-center gap-2 rounded-[10px] bg-muted px-3 after:absolute after:inset-x-0 after:-inset-y-0.5 after:content-[''] sm:after:hidden text-left text-base text-muted-foreground outline-none",
|
|
481
|
+
"sm:h-8 sm:w-56 sm:rounded-[9px] sm:border sm:bg-card sm:pl-2.5 sm:pr-2 sm:text-[13px]",
|
|
482
|
+
"focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50",
|
|
483
|
+
"[&_svg]:h-4 [&_svg]:w-4 [&_svg]:flex-none",
|
|
484
|
+
className
|
|
485
|
+
)}
|
|
486
|
+
{...props}
|
|
487
|
+
>
|
|
488
|
+
{children ?? (
|
|
489
|
+
<>
|
|
490
|
+
<Search aria-hidden />
|
|
491
|
+
<span className="min-w-0 flex-1 truncate">{placeholder}</span>
|
|
492
|
+
{c.shortcut && <kbd className={cn(kbd, "hidden sm:inline-block")}>{keyHint(c.shortcut)}</kbd>}
|
|
493
|
+
</>
|
|
494
|
+
)}
|
|
495
|
+
</PopoverPrimitive.Trigger>
|
|
496
|
+
)
|
|
497
|
+
}
|
|
498
|
+
)
|
|
499
|
+
OmnibarTrigger.displayName = "OmnibarTrigger"
|
|
500
|
+
|
|
501
|
+
/** Keeps the phone layout above the on-screen keyboard, which `100dvh` does not account for. */
|
|
502
|
+
function useKeyboardFit(ref: React.RefObject<HTMLElement | null>, active: boolean) {
|
|
503
|
+
React.useEffect(() => {
|
|
504
|
+
const viewport = window.visualViewport
|
|
505
|
+
const el = ref.current
|
|
506
|
+
if (!active || !viewport || !el) return
|
|
507
|
+
const fit = () => {
|
|
508
|
+
el.style.setProperty("--omnibar-height", `${viewport.height}px`)
|
|
509
|
+
el.style.setProperty("--omnibar-top", `${viewport.offsetTop}px`)
|
|
510
|
+
}
|
|
511
|
+
fit()
|
|
512
|
+
viewport.addEventListener("resize", fit)
|
|
513
|
+
viewport.addEventListener("scroll", fit)
|
|
514
|
+
return () => {
|
|
515
|
+
viewport.removeEventListener("resize", fit)
|
|
516
|
+
viewport.removeEventListener("scroll", fit)
|
|
517
|
+
}
|
|
518
|
+
}, [ref, active])
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Hides the rest of the page from assistive tech while the full-screen panel is up, but
|
|
523
|
+
* not live regions, so toasts and other announcements still speak.
|
|
524
|
+
*/
|
|
525
|
+
function useHideOthers(el: HTMLElement | null, active: boolean) {
|
|
526
|
+
React.useEffect(() => {
|
|
527
|
+
if (!active || !el) return
|
|
528
|
+
const keep = [el, ...document.querySelectorAll("[aria-live]")]
|
|
529
|
+
const hidden: Element[] = []
|
|
530
|
+
const walk = (parent: Element): void =>
|
|
531
|
+
Array.from(parent.children).forEach((n) => {
|
|
532
|
+
if (keep.includes(n) || n.hasAttribute("aria-hidden")) return
|
|
533
|
+
if (keep.some((k) => n.contains(k))) return walk(n)
|
|
534
|
+
hidden.push(n)
|
|
535
|
+
n.setAttribute("aria-hidden", "true")
|
|
536
|
+
})
|
|
537
|
+
walk(document.body)
|
|
538
|
+
return () => hidden.forEach((n) => n.removeAttribute("aria-hidden"))
|
|
539
|
+
}, [el, active])
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// The panel sits over the field and lines its search up with it.
|
|
543
|
+
const overField: PopoverPrimitive.Positioner.Props["sideOffset"] = ({ anchor }) => -anchor.height - (40 - anchor.height) / 2
|
|
544
|
+
const FILL: React.CSSProperties = { position: "fixed", inset: 0, transform: "none" }
|
|
545
|
+
|
|
546
|
+
interface OmnibarContentProps extends React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Popup> {
|
|
547
|
+
/** Which edge of the field the panel keeps when it is wider. */
|
|
548
|
+
align?: PopoverPrimitive.Positioner.Props["align"]
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* The panel the field grows into: 560px wide over the field on a desktop, the whole
|
|
553
|
+
* screen on a phone. Opened many times a day, so it appears and changes with no motion.
|
|
554
|
+
*/
|
|
555
|
+
const OmnibarContent = React.forwardRef<HTMLDivElement, OmnibarContentProps>(
|
|
556
|
+
({ className, children, align = "end", ...props }, ref) => {
|
|
557
|
+
const c = useOmnibar("OmnibarContent")
|
|
558
|
+
const popup = React.useRef<HTMLDivElement | null>(null)
|
|
559
|
+
const [node, setNode] = React.useState<HTMLDivElement | null>(null)
|
|
560
|
+
const merged = useMergedRef(ref, popup, setNode)
|
|
561
|
+
useKeyboardFit(popup, c.open && c.phone)
|
|
562
|
+
useHideOthers(node, c.open && c.phone)
|
|
563
|
+
return (
|
|
564
|
+
<PopoverPrimitive.Portal>
|
|
565
|
+
<PopoverPrimitive.Positioner
|
|
566
|
+
side="bottom"
|
|
567
|
+
align={align}
|
|
568
|
+
sideOffset={overField}
|
|
569
|
+
collisionAvoidance={{ side: "none", align: "shift" }}
|
|
570
|
+
collisionPadding={8}
|
|
571
|
+
style={c.phone ? FILL : undefined}
|
|
572
|
+
className="z-50"
|
|
573
|
+
>
|
|
574
|
+
<PopoverPrimitive.Popup
|
|
575
|
+
ref={merged}
|
|
576
|
+
data-agent-part={c.part}
|
|
577
|
+
aria-label={c.label}
|
|
578
|
+
aria-modal={c.phone || undefined}
|
|
579
|
+
initialFocus={c.input}
|
|
580
|
+
data-slot="omnibar-content"
|
|
581
|
+
className={cn(
|
|
582
|
+
"flex flex-col overflow-hidden text-popover-foreground outline-none",
|
|
583
|
+
c.phone
|
|
584
|
+
? "fixed inset-x-0 top-[var(--omnibar-top,0px)] h-[var(--omnibar-height,100dvh)] bg-background"
|
|
585
|
+
: "w-[560px] min-w-[var(--anchor-width)] max-w-[var(--available-width,calc(100vw-16px))] rounded-[13px] border bg-popover shadow-[0_24px_60px_-20px_rgb(0_0_0/0.35)]",
|
|
586
|
+
className
|
|
587
|
+
)}
|
|
588
|
+
{...props}
|
|
589
|
+
>
|
|
590
|
+
<AutocompletePrimitive.Root
|
|
591
|
+
inline
|
|
592
|
+
open
|
|
593
|
+
items={c.shown}
|
|
594
|
+
filteredItems={c.shown}
|
|
595
|
+
value={c.query}
|
|
596
|
+
onValueChange={c.setQuery}
|
|
597
|
+
autoHighlight="always"
|
|
598
|
+
keepHighlight
|
|
599
|
+
itemToStringValue={(item: CommandItem) => item.label}
|
|
600
|
+
// A move the person made clears the last announcement, so the same text can speak again.
|
|
601
|
+
onItemHighlighted={(item: CommandItem | undefined, { reason }) =>
|
|
602
|
+
c.live.set({ highlighted: item, action: 0, ...(reason !== "none" && { say: "" }) })
|
|
603
|
+
}
|
|
604
|
+
>
|
|
605
|
+
{children}
|
|
606
|
+
</AutocompletePrimitive.Root>
|
|
607
|
+
<Spoken />
|
|
608
|
+
</PopoverPrimitive.Popup>
|
|
609
|
+
</PopoverPrimitive.Positioner>
|
|
610
|
+
</PopoverPrimitive.Portal>
|
|
611
|
+
)
|
|
612
|
+
}
|
|
613
|
+
)
|
|
614
|
+
OmnibarContent.displayName = "OmnibarContent"
|
|
615
|
+
|
|
616
|
+
/** What Enter runs on the highlighted row. */
|
|
617
|
+
function enterHint(state: LiveState): string {
|
|
618
|
+
const action = state.highlighted?.disabled ? undefined : state.highlighted?.actions?.[state.action]
|
|
619
|
+
return action ? `Enter: ${action.label}. Tab: next action` : ""
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** The text the highlighted row is described by, and the polite announcements. */
|
|
623
|
+
function Spoken() {
|
|
624
|
+
const c = useOmnibar("OmnibarContent")
|
|
625
|
+
const hint = useLive(c.live, enterHint)
|
|
626
|
+
const say = useLive(c.live, (s) => s.say)
|
|
627
|
+
return (
|
|
628
|
+
<>
|
|
629
|
+
<span id={c.hintId} hidden>
|
|
630
|
+
{hint}
|
|
631
|
+
</span>
|
|
632
|
+
<span aria-live="polite" className="sr-only">
|
|
633
|
+
{say}
|
|
634
|
+
</span>
|
|
635
|
+
</>
|
|
636
|
+
)
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
interface OmnibarInputProps extends React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Input> {
|
|
640
|
+
/** The phone's button that closes the omnibar. */
|
|
641
|
+
cancelLabel?: string
|
|
642
|
+
/** The placeholder on a phone, where the field is narrower. Defaults to `placeholder`. */
|
|
643
|
+
phonePlaceholder?: string
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* The search, with the picked scope as a chip before it. Backspace on an empty search
|
|
648
|
+
* drops the scope, and Tab moves between the highlighted row's actions.
|
|
649
|
+
*/
|
|
650
|
+
const OmnibarInput = React.forwardRef<HTMLInputElement, OmnibarInputProps>(
|
|
651
|
+
({ className, cancelLabel = "Cancel", placeholder = "Search or run a command", phonePlaceholder = placeholder, onKeyDown, ...props }, ref) => {
|
|
652
|
+
const c = useOmnibar("OmnibarInput")
|
|
653
|
+
const assign = useMergedRef(ref, c.input)
|
|
654
|
+
return (
|
|
655
|
+
<div
|
|
656
|
+
className={cn(
|
|
657
|
+
"flex flex-none items-center gap-2 border-b [&>svg]:h-4 [&>svg]:w-4 [&>svg]:flex-none [&>svg]:text-muted-foreground",
|
|
658
|
+
c.phone ? "h-[52px] pl-3.5 pr-1" : "h-10 px-2.5"
|
|
659
|
+
)}
|
|
660
|
+
>
|
|
661
|
+
<Search aria-hidden />
|
|
662
|
+
{c.scope && !c.phone && (
|
|
663
|
+
<span className="flex-none rounded-md bg-primary px-[7px] py-0.5 text-xs font-medium text-primary-foreground">{c.scope.label}</span>
|
|
664
|
+
)}
|
|
665
|
+
<AutocompletePrimitive.Input
|
|
666
|
+
ref={assign}
|
|
667
|
+
data-slot="omnibar-input"
|
|
668
|
+
aria-label={c.label}
|
|
669
|
+
placeholder={c.scope ? `Search ${c.scope.label.toLowerCase()}` : c.phone ? phonePlaceholder : placeholder}
|
|
670
|
+
enterKeyHint="go"
|
|
671
|
+
onKeyDown={(event) => {
|
|
672
|
+
onKeyDown?.(event)
|
|
673
|
+
if (event.key === "Backspace" && !c.query && c.scope) {
|
|
674
|
+
event.preventDefault()
|
|
675
|
+
return c.setScope(null)
|
|
676
|
+
}
|
|
677
|
+
const { highlighted, action } = c.live.get()
|
|
678
|
+
const count = highlighted?.disabled ? 0 : (highlighted?.actions?.length ?? 0)
|
|
679
|
+
if (event.key === "Tab" && count > 1) {
|
|
680
|
+
event.preventDefault()
|
|
681
|
+
const next = (action + (event.shiftKey ? count - 1 : 1)) % count
|
|
682
|
+
c.live.set({ action: next, say: `Enter: ${highlighted!.actions![next].label}` })
|
|
683
|
+
}
|
|
684
|
+
}}
|
|
685
|
+
className={cn(
|
|
686
|
+
"min-w-0 flex-1 text-ellipsis bg-transparent outline-none placeholder:text-muted-foreground",
|
|
687
|
+
c.phone ? "text-base" : "text-sm",
|
|
688
|
+
className
|
|
689
|
+
)}
|
|
690
|
+
{...props}
|
|
691
|
+
/>
|
|
692
|
+
{c.phone ? (
|
|
693
|
+
<button
|
|
694
|
+
type="button"
|
|
695
|
+
data-agent-part={c.part}
|
|
696
|
+
onClick={(event) => c.close(event.nativeEvent)}
|
|
697
|
+
className="h-11 flex-none px-2.5 text-[15px] text-[color:color-mix(in_oklab,var(--info),var(--foreground)_20%)]"
|
|
698
|
+
>
|
|
699
|
+
{cancelLabel}
|
|
700
|
+
</button>
|
|
701
|
+
) : (
|
|
702
|
+
<kbd className={kbd}>esc</kbd>
|
|
703
|
+
)}
|
|
704
|
+
</div>
|
|
705
|
+
)
|
|
706
|
+
}
|
|
707
|
+
)
|
|
708
|
+
OmnibarInput.displayName = "OmnibarInput"
|
|
709
|
+
|
|
710
|
+
/** All and one chip per scope, with its prefix on a desktop. Changes are instant. */
|
|
711
|
+
const OmnibarScopes = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement> & { allLabel?: string }>(
|
|
712
|
+
({ className, allLabel = "All", "aria-label": ariaLabel = "Scope", ...props }, ref) => {
|
|
713
|
+
const c = useOmnibar("OmnibarScopes")
|
|
714
|
+
if (!c.scopes.length) return null
|
|
715
|
+
const chips = [{ id: null, label: allLabel, prefix: undefined }, ...c.scopes]
|
|
716
|
+
return (
|
|
717
|
+
<div
|
|
718
|
+
ref={ref}
|
|
719
|
+
role="group"
|
|
720
|
+
aria-label={ariaLabel}
|
|
721
|
+
data-slot="omnibar-scopes"
|
|
722
|
+
className={cn(
|
|
723
|
+
"flex flex-none overflow-x-auto [scrollbar-width:none]",
|
|
724
|
+
c.phone ? "gap-1.5 px-3.5 pb-1.5 pt-2.5 after:flex-[0_0_8px] after:content-['']" : "gap-1 px-2.5 pb-1 pt-2",
|
|
725
|
+
className
|
|
726
|
+
)}
|
|
727
|
+
{...props}
|
|
728
|
+
>
|
|
729
|
+
{chips.map((s) => (
|
|
730
|
+
<button
|
|
731
|
+
key={s.id ?? ""}
|
|
732
|
+
type="button"
|
|
733
|
+
data-agent-part={c.part}
|
|
734
|
+
aria-pressed={(c.scope?.id ?? null) === s.id}
|
|
735
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
736
|
+
onClick={() => {
|
|
737
|
+
c.setScope(s.id, true)
|
|
738
|
+
c.input.current?.focus()
|
|
739
|
+
}}
|
|
740
|
+
className={cn(
|
|
741
|
+
"flex-none whitespace-nowrap rounded-full border text-muted-foreground",
|
|
742
|
+
"aria-pressed:border-transparent aria-pressed:bg-muted aria-pressed:text-foreground",
|
|
743
|
+
c.phone ? "relative min-h-9 px-3 text-[13.5px] after:absolute after:inset-x-0 after:-inset-y-1 after:content-['']" : "px-[9px] py-[3px] text-xs"
|
|
744
|
+
)}
|
|
745
|
+
>
|
|
746
|
+
{s.label}
|
|
747
|
+
{s.prefix && !c.phone && <span className="ml-[3px] font-mono text-foreground">{s.prefix}</span>}
|
|
748
|
+
</button>
|
|
749
|
+
))}
|
|
750
|
+
</div>
|
|
751
|
+
)
|
|
752
|
+
}
|
|
753
|
+
)
|
|
754
|
+
OmnibarScopes.displayName = "OmnibarScopes"
|
|
755
|
+
|
|
756
|
+
function Highlight({ text, marks }: { text: string; marks: RegExp | null }) {
|
|
757
|
+
if (!marks) return <>{text}</>
|
|
758
|
+
const parts = text.split(marks)
|
|
759
|
+
return (
|
|
760
|
+
<>
|
|
761
|
+
{parts.map((part, i) =>
|
|
762
|
+
i % 2 ? (
|
|
763
|
+
<mark key={i} className="bg-transparent font-semibold text-foreground">
|
|
764
|
+
{part}
|
|
765
|
+
</mark>
|
|
766
|
+
) : (
|
|
767
|
+
part
|
|
768
|
+
)
|
|
769
|
+
)}
|
|
770
|
+
</>
|
|
771
|
+
)
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
const actionHint = "whitespace-nowrap rounded-[7px] border bg-background px-2 py-[3px] text-xs"
|
|
775
|
+
const primary = "border-transparent bg-primary text-primary-foreground"
|
|
776
|
+
|
|
777
|
+
interface OmnibarItemProps extends Omit<React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Item>, "value"> {
|
|
778
|
+
item: CommandItem
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* One result. Highlighted on a desktop, it shows its actions inline: a click on one runs
|
|
783
|
+
* it, and Tab picks which one Enter runs. On a phone the top hit carries them as buttons
|
|
784
|
+
* and other rows keep them behind a menu beside the row.
|
|
785
|
+
*/
|
|
786
|
+
const OmnibarItemRow = React.forwardRef<HTMLDivElement, OmnibarItemProps>(({ item, className, children, onClick, ...props }, ref) => {
|
|
787
|
+
const c = useOmnibar("OmnibarItem")
|
|
788
|
+
const actions = item.actions ?? []
|
|
789
|
+
const many = actions.length > 1 && !item.disabled
|
|
790
|
+
// The picked action while this row is highlighted, otherwise -1.
|
|
791
|
+
const picked = useLive(c.live, (s) => (s.highlighted === item ? s.action : -1))
|
|
792
|
+
const on = picked >= 0
|
|
793
|
+
const top = c.phone && many && Boolean(c.query.trim()) && c.shown[0]?.items[0] === item
|
|
794
|
+
const menu = c.phone && many && !top
|
|
795
|
+
const press = (action: CommandAction | undefined, event: React.MouseEvent) => {
|
|
796
|
+
event.stopPropagation()
|
|
797
|
+
c.run(item, action, personDetails("action-press", event.nativeEvent))
|
|
798
|
+
}
|
|
799
|
+
const row = (
|
|
800
|
+
<AutocompletePrimitive.Item
|
|
801
|
+
ref={ref}
|
|
802
|
+
value={item}
|
|
803
|
+
disabled={item.disabled}
|
|
804
|
+
data-slot="omnibar-item"
|
|
805
|
+
aria-describedby={on && many ? c.hintId : undefined}
|
|
806
|
+
onClick={(event) => {
|
|
807
|
+
onClick?.(event)
|
|
808
|
+
const hint = (event.target as Element).closest?.("[data-omnibar-action]")
|
|
809
|
+
const index = hint ? Number(hint.getAttribute("data-omnibar-action")) : Math.max(picked, 0)
|
|
810
|
+
c.run(item, actions[index], personDetails(hint ? "action-press" : "item-press", event.nativeEvent))
|
|
811
|
+
}}
|
|
812
|
+
className={cn(
|
|
813
|
+
// The highlight follows the keys and the pointer, so it moves with no transition.
|
|
814
|
+
"group relative flex cursor-default select-none items-center gap-2.5 rounded-[9px] outline-none",
|
|
815
|
+
"data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
|
816
|
+
c.phone ? "min-h-11 px-2 py-[11px] text-[15px]" : "px-2.5 py-2 text-[13.5px] data-[highlighted]:bg-muted",
|
|
817
|
+
menu && "min-w-0 flex-1",
|
|
818
|
+
className
|
|
819
|
+
)}
|
|
820
|
+
{...props}
|
|
821
|
+
// Base UI leaves it unset in autocomplete mode; after the spread so it always holds.
|
|
822
|
+
aria-selected={on}
|
|
823
|
+
>
|
|
824
|
+
{children ?? (
|
|
825
|
+
<>
|
|
826
|
+
{item.icon && (
|
|
827
|
+
<span
|
|
828
|
+
className={cn(
|
|
829
|
+
"grid flex-none place-items-center bg-muted text-muted-foreground [&_svg]:h-4 [&_svg]:w-4",
|
|
830
|
+
c.phone ? "h-8 w-8 rounded-[9px]" : "h-[26px] w-[26px] rounded-[7px] group-data-[highlighted]:bg-background group-data-[highlighted]:text-foreground",
|
|
831
|
+
top && "bg-background text-foreground"
|
|
832
|
+
)}
|
|
833
|
+
>
|
|
834
|
+
{item.icon}
|
|
835
|
+
</span>
|
|
836
|
+
)}
|
|
837
|
+
<span className={cn("min-w-0 flex-1", !c.phone && "truncate")}>
|
|
838
|
+
<Highlight text={item.label} marks={c.marks} />
|
|
839
|
+
{item.description && (
|
|
840
|
+
<span className={cn("text-muted-foreground", c.phone ? "block text-[13px]" : "ml-1.5 text-[12.5px]")}>
|
|
841
|
+
<Highlight text={item.description} marks={c.marks} />
|
|
842
|
+
</span>
|
|
843
|
+
)}
|
|
844
|
+
</span>
|
|
845
|
+
{menu ? null : on && many && !c.phone ? (
|
|
846
|
+
// Pointer targets only: the row is the one control, and aria-describedby names what Enter runs.
|
|
847
|
+
<span aria-hidden className="ml-2 flex flex-none items-center gap-1">
|
|
848
|
+
{actions.map((action, i) => (
|
|
849
|
+
<span key={action.id} data-omnibar-action={i} className={cn(actionHint, "cursor-pointer", i === picked && primary)}>
|
|
850
|
+
{action.label}
|
|
851
|
+
</span>
|
|
852
|
+
))}
|
|
853
|
+
<kbd className={cn(kbd, "ml-1")}>tab</kbd>
|
|
854
|
+
</span>
|
|
855
|
+
) : (
|
|
856
|
+
<span className="ml-auto flex flex-none items-center gap-1.5 text-xs tabular-nums text-muted-foreground">
|
|
857
|
+
{item.meta}
|
|
858
|
+
{item.shortcut && !c.phone && <kbd className={kbd}>{item.shortcut}</kbd>}
|
|
859
|
+
</span>
|
|
860
|
+
)}
|
|
861
|
+
</>
|
|
862
|
+
)}
|
|
863
|
+
</AutocompletePrimitive.Item>
|
|
864
|
+
)
|
|
865
|
+
if (menu) {
|
|
866
|
+
// Touch targets only, like the top hit's buttons: OmnibarList lists the same menus outside the listbox.
|
|
867
|
+
return (
|
|
868
|
+
<div className="flex items-center">
|
|
869
|
+
{row}
|
|
870
|
+
<button
|
|
871
|
+
type="button"
|
|
872
|
+
aria-hidden
|
|
873
|
+
tabIndex={-1}
|
|
874
|
+
onClick={(event) => c.showSheet(item, event.currentTarget)}
|
|
875
|
+
className="grid h-11 w-11 flex-none place-items-center rounded-[9px] text-muted-foreground [&_svg]:h-4 [&_svg]:w-4"
|
|
876
|
+
>
|
|
877
|
+
<MoreHorizontal aria-hidden />
|
|
878
|
+
</button>
|
|
879
|
+
</div>
|
|
880
|
+
)
|
|
881
|
+
}
|
|
882
|
+
if (!top) return row
|
|
883
|
+
return (
|
|
884
|
+
<div className="mb-1.5 mt-0.5 rounded-[14px] bg-muted pb-2.5 pt-0.5">
|
|
885
|
+
{row}
|
|
886
|
+
<div aria-hidden className="flex gap-1.5 px-2">
|
|
887
|
+
{actions.map((action, i) => (
|
|
888
|
+
<button
|
|
889
|
+
key={action.id}
|
|
890
|
+
type="button"
|
|
891
|
+
tabIndex={-1}
|
|
892
|
+
onClick={(event) => press(action, event)}
|
|
893
|
+
className={cn("relative h-10 min-w-0 flex-1 after:absolute after:inset-x-0 after:-inset-y-0.5 after:content-[''] truncate rounded-[10px] border bg-background px-2 text-sm font-medium", i === Math.max(picked, 0) && primary)}
|
|
894
|
+
>
|
|
895
|
+
{action.label}
|
|
896
|
+
</button>
|
|
897
|
+
))}
|
|
898
|
+
</div>
|
|
899
|
+
</div>
|
|
900
|
+
)
|
|
901
|
+
})
|
|
902
|
+
OmnibarItemRow.displayName = "OmnibarItem"
|
|
903
|
+
|
|
904
|
+
/** Memoized, so a row renders again only when its item, the search or its highlight changes. */
|
|
905
|
+
const OmnibarItem = React.memo(OmnibarItemRow)
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
interface OmnibarListProps extends Omit<React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.List>, "children"> {
|
|
909
|
+
/** Renders each row. Defaults to an `OmnibarItem` for the item. */
|
|
910
|
+
children?: (item: CommandItem) => React.ReactNode
|
|
911
|
+
/** Shown when nothing matches. */
|
|
912
|
+
empty?: React.ReactNode | ((query: string) => React.ReactNode)
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/** The grouped results, and on a phone the sheet a row's menu opens. */
|
|
916
|
+
const OmnibarList = React.forwardRef<HTMLDivElement, OmnibarListProps>(({ className, children, empty, ...props }, ref) => {
|
|
917
|
+
const c = useOmnibar("OmnibarList")
|
|
918
|
+
const row = children ?? ((item: CommandItem) => <OmnibarItem key={item.id} item={item} />)
|
|
919
|
+
const sheet = c.sheet
|
|
920
|
+
const count = c.shown.reduce((n, g) => n + g.items.length, 0)
|
|
921
|
+
return (
|
|
922
|
+
<>
|
|
923
|
+
<AutocompletePrimitive.Status className="sr-only">
|
|
924
|
+
{/* Empty announces itself when nothing matches. */}
|
|
925
|
+
{c.query.trim() && count ? (count < c.total ? `${count} of ${c.total} results` : count === 1 ? "1 result" : `${count} results`) : ""}
|
|
926
|
+
</AutocompletePrimitive.Status>
|
|
927
|
+
<AutocompletePrimitive.Empty
|
|
928
|
+
data-slot="omnibar-empty"
|
|
929
|
+
className="px-3 py-[30px] text-center text-[13px] text-muted-foreground empty:hidden"
|
|
930
|
+
>
|
|
931
|
+
{typeof empty === "function" ? empty(c.query) : (empty ?? `Nothing matches “${c.query}”`)}
|
|
932
|
+
</AutocompletePrimitive.Empty>
|
|
933
|
+
<AutocompletePrimitive.List
|
|
934
|
+
ref={ref}
|
|
935
|
+
aria-label={c.label}
|
|
936
|
+
data-slot="omnibar-list"
|
|
937
|
+
className={cn(
|
|
938
|
+
"overflow-y-auto [scrollbar-width:thin] empty:hidden",
|
|
939
|
+
c.phone ? "min-h-0 flex-1 px-2 pb-5 pt-1" : "max-h-[360px] px-1.5 pb-1.5 pt-1",
|
|
940
|
+
className
|
|
941
|
+
)}
|
|
942
|
+
{...props}
|
|
943
|
+
>
|
|
944
|
+
{(group: Group) => (
|
|
945
|
+
<AutocompletePrimitive.Group key={group.value} items={group.items} data-slot="omnibar-group">
|
|
946
|
+
{group.value && (
|
|
947
|
+
<AutocompletePrimitive.GroupLabel
|
|
948
|
+
data-slot="omnibar-group-label"
|
|
949
|
+
className="px-2.5 pb-1 pt-2.5 text-[11.5px] font-medium text-muted-foreground"
|
|
950
|
+
>
|
|
951
|
+
{group.value}
|
|
952
|
+
</AutocompletePrimitive.GroupLabel>
|
|
953
|
+
)}
|
|
954
|
+
<AutocompletePrimitive.Collection>{(item: CommandItem) => row(item)}</AutocompletePrimitive.Collection>
|
|
955
|
+
</AutocompletePrimitive.Group>
|
|
956
|
+
)}
|
|
957
|
+
</AutocompletePrimitive.List>
|
|
958
|
+
{c.phone && (
|
|
959
|
+
// A screen reader's way to each row's ⋯ menu, which is a touch target inside the listbox.
|
|
960
|
+
<div className="sr-only">
|
|
961
|
+
{c.shown.flatMap((g) => g.items).map(
|
|
962
|
+
(item) =>
|
|
963
|
+
!item.disabled &&
|
|
964
|
+
item.actions?.[1] && (
|
|
965
|
+
<button key={item.id} type="button" tabIndex={-1} onClick={(event) => c.showSheet(item, event.currentTarget)}>
|
|
966
|
+
Actions for {item.label}
|
|
967
|
+
</button>
|
|
968
|
+
)
|
|
969
|
+
)}
|
|
970
|
+
</div>
|
|
971
|
+
)}
|
|
972
|
+
{c.phone && (
|
|
973
|
+
<Drawer open={Boolean(sheet)} onOpenChange={(next) => !next && c.showSheet()}>
|
|
974
|
+
<DrawerContent
|
|
975
|
+
side="bottom"
|
|
976
|
+
finalFocus={() => (c.opener.current?.isConnected ? c.opener.current : c.input.current)}
|
|
977
|
+
className="gap-0 rounded-t-[20px] border-0 bg-popover px-2 pb-[max(26px,env(safe-area-inset-bottom))] pt-2">
|
|
978
|
+
<div aria-hidden className="mx-auto mb-2.5 mt-0.5 h-[5px] w-9 rounded-full bg-muted-foreground opacity-40" />
|
|
979
|
+
{sheet && (
|
|
980
|
+
<>
|
|
981
|
+
<DrawerTitle className="px-3.5 text-[15px] font-semibold leading-snug tracking-normal">{sheet.label}</DrawerTitle>
|
|
982
|
+
{sheet.description && <DrawerDescription className="px-3.5 text-[13px]">{sheet.description}</DrawerDescription>}
|
|
983
|
+
<div className="mt-2">
|
|
984
|
+
{sheet.actions?.map((action) => (
|
|
985
|
+
<button
|
|
986
|
+
key={action.id}
|
|
987
|
+
type="button"
|
|
988
|
+
onClick={(event) => {
|
|
989
|
+
c.showSheet()
|
|
990
|
+
c.run(sheet, action, personDetails("action-press", event.nativeEvent))
|
|
991
|
+
}}
|
|
992
|
+
className="block min-h-[50px] w-full rounded-xl px-3.5 text-left text-base hover:bg-muted active:bg-muted"
|
|
993
|
+
>
|
|
994
|
+
{action.label}
|
|
995
|
+
</button>
|
|
996
|
+
))}
|
|
997
|
+
</div>
|
|
998
|
+
</>
|
|
999
|
+
)}
|
|
1000
|
+
</DrawerContent>
|
|
1001
|
+
</Drawer>
|
|
1002
|
+
)}
|
|
1003
|
+
</>
|
|
1004
|
+
)
|
|
1005
|
+
})
|
|
1006
|
+
OmnibarList.displayName = "OmnibarList"
|
|
1007
|
+
|
|
1008
|
+
export { Omnibar, OmnibarTrigger, OmnibarContent, OmnibarInput, OmnibarScopes, OmnibarList, OmnibarItem }
|
|
1009
|
+
export type { OmnibarTriggerProps, OmnibarContentProps, OmnibarInputProps, OmnibarItemProps, OmnibarListProps }
|