@dinachi/cli 1.0.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 +69 -0
- package/dist/index.js +244 -72
- package/package.json +5 -1
- package/templates/accordion/accordion.tsx +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/alert-dialog/alert-dialog.tsx +14 -3
- package/templates/animated-icon/animated-icon.tsx +1 -0
- package/templates/animated-list/animated-list.tsx +2 -1
- package/templates/animated-tabs/animated-tabs.tsx +4 -0
- package/templates/ask-bar/ask-bar.tsx +1061 -0
- package/templates/ask-bar/index.ts +1 -0
- package/templates/autocomplete/autocomplete.tsx +14 -4
- package/templates/avatar/avatar.tsx +3 -0
- package/templates/badge/badge.tsx +1 -0
- package/templates/breadcrumb/breadcrumb.tsx +606 -0
- package/templates/breadcrumb/index.ts +9 -0
- package/templates/button/button.tsx +1 -0
- package/templates/calendar/calendar.tsx +1155 -0
- package/templates/calendar/index.ts +15 -0
- package/templates/card/card.tsx +6 -1
- package/templates/carousel/carousel.tsx +7 -0
- package/templates/checkbox/checkbox.tsx +1 -0
- package/templates/checkbox-group/checkbox-group.tsx +1 -0
- package/templates/collapsible/collapsible.tsx +3 -1
- package/templates/combobox/combobox.tsx +15 -4
- 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/compare-slider/compare-slider.tsx +1 -0
- package/templates/comparison-table/comparison-table.tsx +540 -0
- package/templates/comparison-table/index.ts +15 -0
- package/templates/context-menu/context-menu.tsx +61 -14
- 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/dialog/dialog.tsx +13 -3
- package/templates/drawer/drawer.tsx +19 -6
- package/templates/expandable-card/expandable-card.tsx +5 -1
- package/templates/field/field.tsx +5 -1
- package/templates/fieldset/fieldset.tsx +2 -0
- package/templates/form/form.tsx +1 -0
- package/templates/heat-table/heat-table.tsx +613 -0
- package/templates/heat-table/index.ts +9 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +1 -0
- package/templates/input/input.tsx +1 -0
- package/templates/label/label.tsx +1 -0
- package/templates/link/link.tsx +1 -0
- package/templates/load-transition/load-transition.tsx +1 -0
- package/templates/marquee/marquee.tsx +1 -0
- package/templates/menu/menu.tsx +27 -6
- package/templates/menubar/menubar.tsx +15 -3
- package/templates/meter/meter.tsx +5 -1
- package/templates/month-picker/index.ts +1 -0
- package/templates/month-picker/month-picker.tsx +351 -0
- package/templates/navigation-menu/navigation-menu.tsx +13 -2
- package/templates/number-field/number-field.tsx +7 -1
- package/templates/number-ticker/number-ticker.tsx +3 -0
- package/templates/omnibar/index.ts +1 -0
- package/templates/omnibar/omnibar.tsx +1009 -0
- package/templates/otp-field/otp-field.tsx +4 -0
- package/templates/pagination/index.ts +8 -0
- package/templates/pagination/pagination.tsx +389 -0
- package/templates/popover/popover.tsx +19 -5
- package/templates/preview-card/preview-card.tsx +11 -3
- package/templates/progress/progress.tsx +5 -1
- package/templates/progress-ring/progress-ring.tsx +1 -0
- package/templates/radio/radio.tsx +3 -0
- package/templates/scroll-area/scroll-area.tsx +14 -4
- package/templates/scroll-progress/scroll-progress.tsx +1 -0
- package/templates/scroll-reveal/scroll-reveal.tsx +1 -0
- package/templates/select/select.tsx +18 -2
- package/templates/separator/separator.tsx +1 -0
- package/templates/skeleton/skeleton.tsx +1 -0
- package/templates/slider/slider.tsx +7 -0
- package/templates/sortable/sortable.tsx +3 -0
- package/templates/stagger-list/stagger-list.tsx +2 -0
- package/templates/streaming-text/streaming-text.tsx +1 -0
- package/templates/swipeable-row/swipeable-row.tsx +1 -0
- package/templates/switch/switch.tsx +2 -0
- package/templates/table/index.ts +13 -0
- package/templates/table/table.tsx +265 -0
- package/templates/tabs/tabs.tsx +5 -0
- package/templates/text/text.tsx +1 -0
- package/templates/text-morph/text-morph.tsx +2 -0
- package/templates/text-shimmer/text-shimmer.tsx +1 -0
- package/templates/textarea/textarea.tsx +1 -0
- package/templates/time-slots/index.ts +1 -0
- package/templates/time-slots/time-slots.tsx +589 -0
- package/templates/toast/toast.tsx +10 -1
- package/templates/toggle/toggle.tsx +1 -0
- package/templates/toggle-group/toggle-group.tsx +2 -0
- package/templates/toolbar/toolbar.tsx +6 -0
- package/templates/tooltip/tooltip.tsx +9 -3
- 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,954 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Dialog as BaseDialog } from "@base-ui/react/dialog"
|
|
5
|
+
import { Autocomplete as AutocompletePrimitive } from "@base-ui/react/autocomplete"
|
|
6
|
+
import { mergeProps } from "@base-ui/react/merge-props"
|
|
7
|
+
import { ChevronLeft, ChevronRight, Search } from "lucide-react"
|
|
8
|
+
import { cn } from "@/lib/utils"
|
|
9
|
+
import {
|
|
10
|
+
changeAsAgent,
|
|
11
|
+
createAgentChangeDetails,
|
|
12
|
+
isAgentChange,
|
|
13
|
+
matchAgentChoice,
|
|
14
|
+
useAgentControl,
|
|
15
|
+
useAgentLatest,
|
|
16
|
+
useHasAgent,
|
|
17
|
+
useMergedRef,
|
|
18
|
+
waitForAgent,
|
|
19
|
+
} from "@/components/ui/agent"
|
|
20
|
+
import { commandsHint, filterCommands, type CommandAction, type CommandItem, type CommandRunDetails, type CommandRunReason } from "@/components/ui/command-items"
|
|
21
|
+
|
|
22
|
+
export { filterCommands }
|
|
23
|
+
export type { CommandAction, CommandItem, CommandRunDetails, CommandRunReason }
|
|
24
|
+
|
|
25
|
+
/** @deprecated Kept for the first release's name; use `CommandRunDetails`. */
|
|
26
|
+
export type CommandPaletteRunDetails = CommandRunDetails
|
|
27
|
+
|
|
28
|
+
type OpenDetails = BaseDialog.Root.ChangeEventDetails
|
|
29
|
+
type ValueDetails = AutocompletePrimitive.Root.ChangeEventDetails
|
|
30
|
+
type Group = { key: string; label: string; items: CommandItem[] }
|
|
31
|
+
|
|
32
|
+
/** The Recent group's key, apart from any group the items name. */
|
|
33
|
+
const RECENT = "recent"
|
|
34
|
+
|
|
35
|
+
function groupsOf(items: CommandItem[]): Group[] {
|
|
36
|
+
const groups: Group[] = []
|
|
37
|
+
for (const item of items) {
|
|
38
|
+
const label = item.group ?? ""
|
|
39
|
+
const last = groups.find((g) => g.label === label)
|
|
40
|
+
if (last) last.items.push(item)
|
|
41
|
+
else groups.push({ key: `group:${label}`, label, items: [item] })
|
|
42
|
+
}
|
|
43
|
+
return groups
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Change details for a person's change that Base UI did not make itself. */
|
|
47
|
+
function personDetails<D>(reason: string, event?: Event): D {
|
|
48
|
+
const details = {
|
|
49
|
+
reason,
|
|
50
|
+
event,
|
|
51
|
+
isCanceled: false,
|
|
52
|
+
isPropagationAllowed: false,
|
|
53
|
+
trigger: undefined,
|
|
54
|
+
cancel() {
|
|
55
|
+
details.isCanceled = true
|
|
56
|
+
},
|
|
57
|
+
allowPropagation() {},
|
|
58
|
+
preventUnmountOnClose() {},
|
|
59
|
+
}
|
|
60
|
+
return details as unknown as D
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const PHONE = "(max-width: 639px)"
|
|
64
|
+
|
|
65
|
+
function usePhone(): boolean {
|
|
66
|
+
return React.useSyncExternalStore(
|
|
67
|
+
(onChange) => {
|
|
68
|
+
const query = window.matchMedia?.(PHONE)
|
|
69
|
+
query?.addEventListener?.("change", onChange)
|
|
70
|
+
return () => query?.removeEventListener?.("change", onChange)
|
|
71
|
+
},
|
|
72
|
+
() => Boolean(window.matchMedia?.(PHONE).matches),
|
|
73
|
+
() => false
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const isMac = () => typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)
|
|
78
|
+
const never = () => () => {}
|
|
79
|
+
|
|
80
|
+
/** False on the server and through hydration, so the first client render matches. */
|
|
81
|
+
function useMac(): boolean {
|
|
82
|
+
return React.useSyncExternalStore(never, isMac, () => false)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Cmd on a Mac, Ctrl elsewhere. Not both: Ctrl+K on a Mac is a text field's kill-line. */
|
|
86
|
+
function isMod(event: { metaKey: boolean; ctrlKey: boolean; altKey: boolean; shiftKey: boolean }): boolean {
|
|
87
|
+
if (event.altKey || event.shiftKey) return false
|
|
88
|
+
return isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Matches by the letter, or by the physical key when the layout types another script. */
|
|
92
|
+
function isKey(event: KeyboardEvent, key: string): boolean {
|
|
93
|
+
if (event.key.toLowerCase() === key) return true
|
|
94
|
+
if (/^[a-z0-9]$/i.test(event.key)) return false
|
|
95
|
+
return event.code === `Key${key.toUpperCase()}` || event.code === `Digit${key}`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const reversed = new WeakSet<Event>()
|
|
99
|
+
|
|
100
|
+
interface PaletteContext {
|
|
101
|
+
/** Inside an AgentProvider: marks the palette's own buttons so the agent sees one control. */
|
|
102
|
+
part: "" | undefined
|
|
103
|
+
label: string
|
|
104
|
+
open: boolean
|
|
105
|
+
disabled: boolean
|
|
106
|
+
phone: boolean
|
|
107
|
+
mac: boolean
|
|
108
|
+
shortcut: string | false
|
|
109
|
+
query: string
|
|
110
|
+
setQuery: (query: string, details: ValueDetails) => void
|
|
111
|
+
page: CommandItem | undefined
|
|
112
|
+
pop: () => void
|
|
113
|
+
shown: Group[]
|
|
114
|
+
/** How many matched the search, past `limit` included. */
|
|
115
|
+
total: number
|
|
116
|
+
/** The search's words, built once per search for every row's highlight. */
|
|
117
|
+
pattern: RegExp | undefined
|
|
118
|
+
/** The highlighted item, read when a key needs it, so a moving highlight re-renders nothing here. */
|
|
119
|
+
latest: React.MutableRefObject<{ highlighted: CommandItem | undefined }>
|
|
120
|
+
setHighlighted: (item: CommandItem | undefined) => void
|
|
121
|
+
detail: CommandItem | undefined
|
|
122
|
+
setDetail: (item: CommandItem | undefined) => void
|
|
123
|
+
pick: (item: CommandItem) => void
|
|
124
|
+
run: (item: CommandItem, action: CommandAction | undefined, details: CommandRunDetails) => string | undefined
|
|
125
|
+
close: (event?: Event) => void
|
|
126
|
+
trigger: React.MutableRefObject<HTMLElement | null>
|
|
127
|
+
input: React.MutableRefObject<HTMLInputElement | null>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const PaletteContext = React.createContext<PaletteContext | null>(null)
|
|
131
|
+
/** Apart from the rest, so only what shows the highlighted item re-renders as it moves. */
|
|
132
|
+
const HighlightContext = React.createContext<CommandItem | undefined>(undefined)
|
|
133
|
+
|
|
134
|
+
function usePalette(part: string): PaletteContext {
|
|
135
|
+
const context = React.useContext(PaletteContext)
|
|
136
|
+
if (!context) throw new Error(`${part} must be inside <CommandPalette>.`)
|
|
137
|
+
return context
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface CommandPaletteProps {
|
|
141
|
+
/** Every command, in the order groups and items are listed. */
|
|
142
|
+
items: CommandItem[]
|
|
143
|
+
/** Ids listed first, under "Recent", while the search is empty. */
|
|
144
|
+
recent?: string[]
|
|
145
|
+
recentLabel?: string
|
|
146
|
+
/** Ids listed after the recent ones while the search is empty, under their own groups. */
|
|
147
|
+
suggested?: string[]
|
|
148
|
+
/** How many rows show while the search is empty, when `recent` or `suggested` is set. */
|
|
149
|
+
idleLimit?: number
|
|
150
|
+
/** The most rows a search or a full list shows. Typing narrows it. */
|
|
151
|
+
limit?: number
|
|
152
|
+
/** Runs a picked item. `details.cancel()` keeps the palette open. */
|
|
153
|
+
onRun?: (item: CommandItem, action: CommandAction | undefined, details: CommandRunDetails) => void
|
|
154
|
+
open?: boolean
|
|
155
|
+
defaultOpen?: boolean
|
|
156
|
+
onOpenChange?: (open: boolean, details: OpenDetails) => void
|
|
157
|
+
/** The search text. */
|
|
158
|
+
value?: string
|
|
159
|
+
defaultValue?: string
|
|
160
|
+
onValueChange?: (value: string, details: ValueDetails) => void
|
|
161
|
+
/** The key that opens it with Cmd on a Mac and Ctrl elsewhere. `false` turns the shortcut off. */
|
|
162
|
+
shortcut?: string | false
|
|
163
|
+
/** Names the dialog and the search field, and is what an agent finds it by. */
|
|
164
|
+
label?: string
|
|
165
|
+
filter?: (item: CommandItem, query: string) => boolean
|
|
166
|
+
disabled?: boolean
|
|
167
|
+
children?: React.ReactNode
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A Base UI dialog holding the search, list and preview. Opens on Cmd or Ctrl+K and
|
|
172
|
+
* forgets the search, sub-list and highlight when it closes.
|
|
173
|
+
*/
|
|
174
|
+
function CommandPalette({
|
|
175
|
+
items,
|
|
176
|
+
recent,
|
|
177
|
+
recentLabel = "Recent",
|
|
178
|
+
suggested,
|
|
179
|
+
idleLimit = 8,
|
|
180
|
+
limit = 50,
|
|
181
|
+
onRun,
|
|
182
|
+
open: openProp,
|
|
183
|
+
defaultOpen = false,
|
|
184
|
+
onOpenChange,
|
|
185
|
+
value: valueProp,
|
|
186
|
+
defaultValue = "",
|
|
187
|
+
onValueChange,
|
|
188
|
+
shortcut: shortcutProp = "k",
|
|
189
|
+
label = "Command palette",
|
|
190
|
+
filter = filterCommands,
|
|
191
|
+
disabled = false,
|
|
192
|
+
children,
|
|
193
|
+
}: CommandPaletteProps) {
|
|
194
|
+
const [openInner, setOpenInner] = React.useState(defaultOpen)
|
|
195
|
+
const [queryInner, setQueryInner] = React.useState(defaultValue)
|
|
196
|
+
const [pages, setPages] = React.useState<CommandItem[]>([])
|
|
197
|
+
const [detail, setDetail] = React.useState<CommandItem | undefined>()
|
|
198
|
+
const [highlighted, setHighlighted] = React.useState<CommandItem | undefined>()
|
|
199
|
+
const open = openProp ?? openInner
|
|
200
|
+
const query = valueProp ?? queryInner
|
|
201
|
+
const page = pages[pages.length - 1]
|
|
202
|
+
const phone = usePhone()
|
|
203
|
+
const mac = useMac()
|
|
204
|
+
const shortcut: string | false = shortcutProp === false ? false : shortcutProp.toLowerCase()
|
|
205
|
+
const trigger = React.useRef<HTMLElement | null>(null)
|
|
206
|
+
const input = React.useRef<HTMLInputElement | null>(null)
|
|
207
|
+
|
|
208
|
+
// Closed by the app or by the palette, it starts over next time.
|
|
209
|
+
const [wasOpen, setWasOpen] = React.useState(open)
|
|
210
|
+
if (wasOpen !== open) {
|
|
211
|
+
setWasOpen(open)
|
|
212
|
+
if (!open) {
|
|
213
|
+
setPages([])
|
|
214
|
+
setDetail(undefined)
|
|
215
|
+
setHighlighted(undefined)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Remembers who closed it, so clearing the search afterwards is marked the same way.
|
|
220
|
+
const closedBy = React.useRef<OpenDetails | undefined>(undefined)
|
|
221
|
+
const changeOpen = (next: boolean, details: OpenDetails) => {
|
|
222
|
+
if (!next) closedBy.current = details
|
|
223
|
+
onOpenChange?.(next, details)
|
|
224
|
+
if (!details.isCanceled && openProp === undefined) setOpenInner(next)
|
|
225
|
+
}
|
|
226
|
+
const setQuery = (next: string, details: ValueDetails) => {
|
|
227
|
+
onValueChange?.(next, details)
|
|
228
|
+
if (!details.isCanceled && valueProp === undefined) setQueryInner(next)
|
|
229
|
+
}
|
|
230
|
+
const resetQuery = () => setQuery("", personDetails("none"))
|
|
231
|
+
|
|
232
|
+
// Cleared through `setQuery` in both modes, so a controlled search hears about it.
|
|
233
|
+
const wasOpenRef = React.useRef(open)
|
|
234
|
+
React.useEffect(() => {
|
|
235
|
+
const closed = wasOpenRef.current && !open
|
|
236
|
+
wasOpenRef.current = open
|
|
237
|
+
if (!closed || !latest.current.query) return
|
|
238
|
+
const details = isAgentChange(closedBy.current) ? createAgentChangeDetails<ValueDetails>("none") : personDetails<ValueDetails>("none")
|
|
239
|
+
latest.current.setQuery("", details)
|
|
240
|
+
}, [open])
|
|
241
|
+
|
|
242
|
+
const [shown, total = 0] = React.useMemo((): [Group[], number?] => {
|
|
243
|
+
const list = page?.items ?? items
|
|
244
|
+
if (query.trim()) {
|
|
245
|
+
const matches = list.filter((item) => filter(item, query))
|
|
246
|
+
return [groupsOf(matches.slice(0, limit)), matches.length]
|
|
247
|
+
}
|
|
248
|
+
if (page || (!recent?.length && !suggested)) return [groupsOf(list.slice(0, limit)), list.length]
|
|
249
|
+
const byId = (ids: string[]) => ids.map((id) => list.find((item) => item.id === id)).filter((item): item is CommandItem => !!item)
|
|
250
|
+
const first = byId(recent ?? []).slice(0, idleLimit)
|
|
251
|
+
const rest = (suggested ? byId(suggested) : list).filter((item) => !first.includes(item)).slice(0, idleLimit - first.length)
|
|
252
|
+
return [[{ key: RECENT, label: recentLabel, items: first }, ...groupsOf(rest)].filter((g) => g.items.length)]
|
|
253
|
+
}, [items, page, query, recent, recentLabel, suggested, idleLimit, limit, filter])
|
|
254
|
+
|
|
255
|
+
const pattern = React.useMemo(() => {
|
|
256
|
+
const words = query.split(/\s+/).filter(Boolean).map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
257
|
+
return words.length ? new RegExp(`(${words.join("|")})`, "ig") : undefined
|
|
258
|
+
}, [query])
|
|
259
|
+
|
|
260
|
+
const close = (event?: Event) => changeOpen(false, personDetails("imperative-action", event))
|
|
261
|
+
|
|
262
|
+
const run: PaletteContext["run"] = (item, action, details) => {
|
|
263
|
+
if (item.disabled) return "That command is disabled."
|
|
264
|
+
onRun?.(item, action, details)
|
|
265
|
+
if (details.isCanceled) return "The app refused the command."
|
|
266
|
+
if (!latest.current.open) return undefined
|
|
267
|
+
if (isAgentChange(details)) changeOpen(false, createAgentChangeDetails<OpenDetails>())
|
|
268
|
+
else close(details.event)
|
|
269
|
+
return undefined
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const push = (item: CommandItem) => {
|
|
273
|
+
setPages((p) => [...p, item])
|
|
274
|
+
setDetail(undefined)
|
|
275
|
+
resetQuery()
|
|
276
|
+
// The row that opened it is gone, so the field keeps focus for the next search.
|
|
277
|
+
requestAnimationFrame(() => input.current?.focus())
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const pop = () => {
|
|
281
|
+
if (detail) return setDetail(undefined)
|
|
282
|
+
setPages((p) => p.slice(0, -1))
|
|
283
|
+
resetQuery()
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const pick = (item: CommandItem) => {
|
|
287
|
+
if (item.disabled) return
|
|
288
|
+
if (item.items) return push(item)
|
|
289
|
+
if (phone && (item.actions?.length ?? 0) > 1) {
|
|
290
|
+
input.current?.blur()
|
|
291
|
+
return setDetail(item)
|
|
292
|
+
}
|
|
293
|
+
run(item, item.actions?.[0], personDetails("item-press"))
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Escape steps back out of a detail or sub-list before it closes the palette.
|
|
297
|
+
const onDialogOpenChange = (next: boolean, details: OpenDetails) => {
|
|
298
|
+
if (!next && details.reason === "escape-key" && (detail || page)) {
|
|
299
|
+
details.cancel()
|
|
300
|
+
return pop()
|
|
301
|
+
}
|
|
302
|
+
changeOpen(next, details)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Back from a record, the field shows again and takes focus with the keyboard.
|
|
306
|
+
const hadDetail = React.useRef(false)
|
|
307
|
+
React.useEffect(() => {
|
|
308
|
+
if (hadDetail.current && !detail && open) input.current?.focus()
|
|
309
|
+
hadDetail.current = Boolean(detail)
|
|
310
|
+
}, [detail, open])
|
|
311
|
+
|
|
312
|
+
React.useEffect(() => {
|
|
313
|
+
if (shortcut === false || disabled) return
|
|
314
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
315
|
+
if (event.defaultPrevented || event.repeat || !isMod(event) || !isKey(event, shortcut)) return
|
|
316
|
+
event.preventDefault()
|
|
317
|
+
latest.current.changeOpen(!latest.current.open, personDetails("none", event))
|
|
318
|
+
}
|
|
319
|
+
document.addEventListener("keydown", onKeyDown)
|
|
320
|
+
return () => document.removeEventListener("keydown", onKeyDown)
|
|
321
|
+
}, [shortcut, disabled])
|
|
322
|
+
|
|
323
|
+
// Read by handlers when they run. Its stable callers keep the context and memoized rows still.
|
|
324
|
+
const latest = useAgentLatest({ open, query, highlighted, setQuery, changeOpen, push, pop, pick, run, close, onDialogOpenChange })
|
|
325
|
+
const [stable] = React.useState(() => {
|
|
326
|
+
const calls: Record<string, (...args: never[]) => unknown> = {}
|
|
327
|
+
const now = latest.current as unknown as typeof calls
|
|
328
|
+
for (const key in now) if (typeof now[key] == "function") calls[key] = (...args) => (latest.current as unknown as typeof calls)[key](...args)
|
|
329
|
+
return calls as unknown as Pick<typeof latest.current, "setQuery" | "changeOpen" | "push" | "pop" | "pick" | "run" | "close" | "onDialogOpenChange">
|
|
330
|
+
})
|
|
331
|
+
const state = { part: useHasAgent() ? ("" as const) : undefined, label, open, disabled, phone, mac, shortcut, query, page, shown, total, pattern, detail }
|
|
332
|
+
// Rebuilt only when what it holds changes; the highlight has its own context.
|
|
333
|
+
const context: PaletteContext = React.useMemo(
|
|
334
|
+
() => ({ ...state, ...stable, latest, setHighlighted, setDetail, trigger, input }),
|
|
335
|
+
Object.values(state)
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
usePaletteAgent(context, page?.items ?? items, pages[pages.length - 2]?.items ?? items, stable.changeOpen, stable.push)
|
|
339
|
+
|
|
340
|
+
return (
|
|
341
|
+
<PaletteContext.Provider value={context}>
|
|
342
|
+
<HighlightContext.Provider value={highlighted}>
|
|
343
|
+
<BaseDialog.Root open={open} onOpenChange={stable.onDialogOpenChange}>
|
|
344
|
+
{children}
|
|
345
|
+
</BaseDialog.Root>
|
|
346
|
+
</HighlightContext.Provider>
|
|
347
|
+
</PaletteContext.Provider>
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const describe = (item: CommandItem) => ({
|
|
352
|
+
label: item.label,
|
|
353
|
+
...(item.group ? { group: item.group } : {}),
|
|
354
|
+
...(item.actions ? { actions: item.actions.map((a) => a.label) } : {}),
|
|
355
|
+
...(item.items ? { opens: item.items.length + " more" } : {}),
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* One combobox control for the agent: the trigger while closed, the search field while
|
|
360
|
+
* open. It lists every command on the current level while closed and the matches while open.
|
|
361
|
+
*/
|
|
362
|
+
function usePaletteAgent(
|
|
363
|
+
c: PaletteContext,
|
|
364
|
+
level: CommandItem[],
|
|
365
|
+
parent: CommandItem[],
|
|
366
|
+
changeOpen: (next: boolean, details: OpenDetails) => void,
|
|
367
|
+
push: (item: CommandItem) => void
|
|
368
|
+
) {
|
|
369
|
+
const latest = useAgentLatest(c)
|
|
370
|
+
const settle = async () => {
|
|
371
|
+
await waitForAgent(() => latest.current.open && latest.current.input.current?.isConnected)
|
|
372
|
+
return latest.current.shown.flatMap((g) => g.items).map(describe)
|
|
373
|
+
}
|
|
374
|
+
const opened = () => (latest.current.open ? undefined : changeAsAgent(changeOpen, true))
|
|
375
|
+
|
|
376
|
+
useAgentControl(() => ({
|
|
377
|
+
kind: "combobox",
|
|
378
|
+
element: c.open ? c.input.current : c.trigger.current,
|
|
379
|
+
label: c.label,
|
|
380
|
+
hint: commandsHint(level),
|
|
381
|
+
open: c.open,
|
|
382
|
+
disabled: c.disabled,
|
|
383
|
+
...(c.open ? { value: c.query } : {}),
|
|
384
|
+
items: (c.open ? c.shown.flatMap((g) => g.items) : level).map((i) => i.label),
|
|
385
|
+
actions: {
|
|
386
|
+
[c.open ? "close" : "open"]: () => changeAsAgent(changeOpen, !c.open),
|
|
387
|
+
...(c.page ? { back: () => (c.pop(), { result: parent.map(describe) }) } : {}),
|
|
388
|
+
search: async (text: unknown) => {
|
|
389
|
+
const refused = opened() || changeAsAgent(c.setQuery, String(text ?? ""))
|
|
390
|
+
if (refused) return refused
|
|
391
|
+
return { result: await settle() }
|
|
392
|
+
},
|
|
393
|
+
select: async (wanted: unknown, actionName?: unknown) => {
|
|
394
|
+
const choices = level.map((item) => ({ value: item, label: item.label, disabled: item.disabled }))
|
|
395
|
+
const match = matchAgentChoice(choices, wanted)
|
|
396
|
+
if (typeof match === "string") return match
|
|
397
|
+
const item = match.value
|
|
398
|
+
if (item.items) {
|
|
399
|
+
const refused = opened()
|
|
400
|
+
if (refused) return refused
|
|
401
|
+
push(item)
|
|
402
|
+
return { result: item.items.map(describe) }
|
|
403
|
+
}
|
|
404
|
+
if (item.disabled) return "That command is disabled."
|
|
405
|
+
let action = item.actions?.[0]
|
|
406
|
+
let reason: CommandRunReason = "item-press"
|
|
407
|
+
if (actionName != null) {
|
|
408
|
+
const found = matchAgentChoice((item.actions ?? []).map((a) => ({ value: a, label: a.label })), actionName)
|
|
409
|
+
if (typeof found === "string") return found
|
|
410
|
+
action = found.value
|
|
411
|
+
reason = "action-press"
|
|
412
|
+
}
|
|
413
|
+
return c.run(item, action, createAgentChangeDetails(reason))
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
}))
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const CommandPaletteTrigger = React.forwardRef<HTMLButtonElement, BaseDialog.Trigger.Props>(
|
|
420
|
+
({ className, children, disabled, ...props }, ref) => {
|
|
421
|
+
const c = usePalette("CommandPaletteTrigger")
|
|
422
|
+
return (
|
|
423
|
+
<BaseDialog.Trigger
|
|
424
|
+
ref={useMergedRef(ref, c.trigger)}
|
|
425
|
+
data-agent-part={c.part}
|
|
426
|
+
disabled={disabled || c.disabled}
|
|
427
|
+
data-slot="command-palette-trigger"
|
|
428
|
+
className={cn(
|
|
429
|
+
"inline-flex h-9 w-full max-w-64 items-center gap-2 rounded-md border border-input bg-background px-3 text-sm text-muted-foreground",
|
|
430
|
+
"hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
431
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
432
|
+
className
|
|
433
|
+
)}
|
|
434
|
+
{...props}
|
|
435
|
+
>
|
|
436
|
+
{children ?? (
|
|
437
|
+
<>
|
|
438
|
+
<Search className="h-4 w-4" aria-hidden />
|
|
439
|
+
<span className="flex-1 text-left">Search</span>
|
|
440
|
+
{c.shortcut && (
|
|
441
|
+
<CommandPaletteShortcut className="hidden sm:inline-block">{`${c.mac ? "⌘" : "Ctrl"} ${c.shortcut.toUpperCase()}`}</CommandPaletteShortcut>
|
|
442
|
+
)}
|
|
443
|
+
</>
|
|
444
|
+
)}
|
|
445
|
+
</BaseDialog.Trigger>
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
)
|
|
449
|
+
CommandPaletteTrigger.displayName = "CommandPaletteTrigger"
|
|
450
|
+
|
|
451
|
+
/** Keeps the phone layout above the on-screen keyboard, which `100dvh` does not account for. */
|
|
452
|
+
function useKeyboardFit(el: HTMLElement | null, active: boolean) {
|
|
453
|
+
React.useEffect(() => {
|
|
454
|
+
const viewport = window.visualViewport
|
|
455
|
+
if (!active || !viewport || !el) return
|
|
456
|
+
const fit = () => {
|
|
457
|
+
el.style.setProperty("--command-palette-height", `${viewport.height}px`)
|
|
458
|
+
el.style.setProperty("--command-palette-top", `${viewport.offsetTop}px`)
|
|
459
|
+
}
|
|
460
|
+
fit()
|
|
461
|
+
viewport.addEventListener("resize", fit)
|
|
462
|
+
viewport.addEventListener("scroll", fit)
|
|
463
|
+
return () => {
|
|
464
|
+
viewport.removeEventListener("resize", fit)
|
|
465
|
+
viewport.removeEventListener("scroll", fit)
|
|
466
|
+
}
|
|
467
|
+
}, [el, active])
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// --info alone is under 4.5:1 on a light background; a quarter of the text colour lifts it.
|
|
471
|
+
const LINK = "text-[color:color-mix(in_oklab,var(--info)_75%,var(--foreground))]"
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* The dialog, with Base UI's Autocomplete held open inside it. Full screen on a phone,
|
|
475
|
+
* with the search at the bottom and the best match just above it.
|
|
476
|
+
*/
|
|
477
|
+
const CommandPaletteContent = React.forwardRef<HTMLDivElement, React.ComponentProps<typeof BaseDialog.Popup>>(
|
|
478
|
+
({ className, children, ...props }, ref) => {
|
|
479
|
+
const c = usePalette("CommandPaletteContent")
|
|
480
|
+
// State, not a ref: the portal mounts the popup after this renders.
|
|
481
|
+
const [popup, setPopup] = React.useState<HTMLDivElement | null>(null)
|
|
482
|
+
const merged = useMergedRef(ref, setPopup)
|
|
483
|
+
useKeyboardFit(popup, c.open && c.phone)
|
|
484
|
+
return (
|
|
485
|
+
// Opened from the keyboard many times a day, so it never animates: Base UI's parts
|
|
486
|
+
// directly, with no fade or scale.
|
|
487
|
+
<BaseDialog.Portal>
|
|
488
|
+
<BaseDialog.Backdrop className="fixed inset-0 z-50 hidden bg-black/30 sm:block dark:bg-black/55" />
|
|
489
|
+
<BaseDialog.Viewport className="fixed inset-0 z-50 flex items-stretch justify-center overflow-auto sm:items-start sm:px-5 sm:pt-[min(12vh,7rem)]">
|
|
490
|
+
<BaseDialog.Popup
|
|
491
|
+
ref={merged}
|
|
492
|
+
data-agent-part={c.part}
|
|
493
|
+
aria-label={c.label}
|
|
494
|
+
initialFocus={c.input}
|
|
495
|
+
data-slot="command-palette-content"
|
|
496
|
+
className={cn(
|
|
497
|
+
"relative flex flex-col overflow-hidden bg-background text-foreground outline-none",
|
|
498
|
+
"fixed inset-x-0 top-[var(--command-palette-top,0px)] h-[var(--command-palette-height,100dvh)] w-full",
|
|
499
|
+
"sm:static sm:h-auto sm:w-[720px] sm:max-w-full sm:rounded-2xl sm:border sm:bg-popover sm:text-popover-foreground sm:shadow-[0_30px_80px_-20px_rgb(0_0_0/0.4)]",
|
|
500
|
+
className
|
|
501
|
+
)}
|
|
502
|
+
{...props}
|
|
503
|
+
>
|
|
504
|
+
<AutocompletePrimitive.Root
|
|
505
|
+
inline
|
|
506
|
+
open
|
|
507
|
+
items={c.shown}
|
|
508
|
+
filteredItems={c.shown}
|
|
509
|
+
value={c.query}
|
|
510
|
+
onValueChange={c.setQuery}
|
|
511
|
+
autoHighlight="always"
|
|
512
|
+
keepHighlight
|
|
513
|
+
itemToStringValue={(item: CommandItem) => item.label}
|
|
514
|
+
onItemHighlighted={(item: CommandItem | undefined) => c.setHighlighted(item)}
|
|
515
|
+
>
|
|
516
|
+
{children}
|
|
517
|
+
</AutocompletePrimitive.Root>
|
|
518
|
+
</BaseDialog.Popup>
|
|
519
|
+
</BaseDialog.Viewport>
|
|
520
|
+
</BaseDialog.Portal>
|
|
521
|
+
)
|
|
522
|
+
}
|
|
523
|
+
)
|
|
524
|
+
CommandPaletteContent.displayName = "CommandPaletteContent"
|
|
525
|
+
|
|
526
|
+
interface CommandPaletteInputProps extends React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Input> {
|
|
527
|
+
/** The phone's button that closes the palette. */
|
|
528
|
+
cancelLabel?: string
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* The search field. On a desktop it sits on top with an Esc hint; on a phone it sits at
|
|
533
|
+
* the bottom with a Cancel button. Inside a sub-list it shows where you are.
|
|
534
|
+
*/
|
|
535
|
+
const CommandPaletteInput = React.forwardRef<HTMLInputElement, CommandPaletteInputProps>(
|
|
536
|
+
({ className, cancelLabel = "Cancel", placeholder = "Search or run a command", onKeyDown, ...props }, ref) => {
|
|
537
|
+
const c = usePalette("CommandPaletteInput")
|
|
538
|
+
const assign = useMergedRef(ref, c.input)
|
|
539
|
+
return (
|
|
540
|
+
<div
|
|
541
|
+
className={cn(
|
|
542
|
+
"order-last flex flex-none items-center border-t pb-[env(safe-area-inset-bottom)] sm:order-none sm:border-b sm:border-t-0 sm:pb-0",
|
|
543
|
+
// A phone's record screen stands alone, with the keyboard down.
|
|
544
|
+
c.detail && "hidden"
|
|
545
|
+
)}
|
|
546
|
+
>
|
|
547
|
+
<div className="ml-3 mr-0.5 my-2 flex h-11 min-w-0 flex-1 items-center gap-2.5 rounded-xl bg-muted px-3 sm:m-0 sm:h-auto sm:rounded-none sm:bg-transparent sm:px-4 sm:py-3.5">
|
|
548
|
+
<Search className="h-4 w-4 flex-none text-muted-foreground" aria-hidden />
|
|
549
|
+
{c.page && (
|
|
550
|
+
<button
|
|
551
|
+
type="button"
|
|
552
|
+
data-agent-part={c.part}
|
|
553
|
+
onClick={c.pop}
|
|
554
|
+
className="flex h-11 min-w-11 flex-none items-center justify-center sm:h-auto sm:min-w-0"
|
|
555
|
+
aria-label={`Back from ${c.page.label}`}
|
|
556
|
+
>
|
|
557
|
+
<span className="rounded-md bg-background px-2 py-0.5 text-xs font-medium text-foreground sm:bg-muted">{c.page.label}</span>
|
|
558
|
+
</button>
|
|
559
|
+
)}
|
|
560
|
+
<AutocompletePrimitive.Input
|
|
561
|
+
ref={assign}
|
|
562
|
+
data-slot="command-palette-input"
|
|
563
|
+
// Inside a sub-list the field is named after it, and the status announces it.
|
|
564
|
+
aria-label={c.page ? c.page.label : c.label}
|
|
565
|
+
placeholder={c.page ? `Search ${c.page.label.toLowerCase()}` : placeholder}
|
|
566
|
+
enterKeyHint="go"
|
|
567
|
+
onKeyDown={(event) => {
|
|
568
|
+
onKeyDown?.(event)
|
|
569
|
+
if (event.nativeEvent.isComposing) return
|
|
570
|
+
if (event.key === "Backspace" && event.currentTarget.value === "" && c.page) {
|
|
571
|
+
event.preventDefault()
|
|
572
|
+
return c.pop()
|
|
573
|
+
}
|
|
574
|
+
// Tab completes the highlighted item into the search, or opens its sub-list.
|
|
575
|
+
// Once the search already reads it, Tab moves on to the preview's actions.
|
|
576
|
+
const item = c.latest.current.highlighted
|
|
577
|
+
if (event.key === "Tab" && !event.shiftKey && !isMod(event) && item && !item.disabled) {
|
|
578
|
+
if (item.items) {
|
|
579
|
+
event.preventDefault()
|
|
580
|
+
return c.pick(item)
|
|
581
|
+
}
|
|
582
|
+
if (c.query !== item.label) {
|
|
583
|
+
event.preventDefault()
|
|
584
|
+
return c.setQuery(item.label, personDetails("none", event.nativeEvent))
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
if (event.key === "Enter" && isMod(event)) {
|
|
588
|
+
event.preventDefault()
|
|
589
|
+
event.preventBaseUIHandler()
|
|
590
|
+
if (item && !item.items && !item.disabled) {
|
|
591
|
+
c.run(item, item.actions?.[1] ?? item.actions?.[0], personDetails("keyboard", event.nativeEvent))
|
|
592
|
+
}
|
|
593
|
+
return
|
|
594
|
+
}
|
|
595
|
+
// The phone list runs bottom up, so each arrow moves the other way through it.
|
|
596
|
+
const flip = { ArrowUp: "ArrowDown", ArrowDown: "ArrowUp" }[event.key]
|
|
597
|
+
if (c.phone && flip && !reversed.has(event.nativeEvent)) {
|
|
598
|
+
event.preventDefault()
|
|
599
|
+
event.preventBaseUIHandler()
|
|
600
|
+
const next = new KeyboardEvent("keydown", { key: flip, code: flip, bubbles: true, cancelable: true })
|
|
601
|
+
reversed.add(next)
|
|
602
|
+
event.currentTarget.dispatchEvent(next)
|
|
603
|
+
}
|
|
604
|
+
}}
|
|
605
|
+
className={cn(
|
|
606
|
+
"min-w-0 flex-1 text-ellipsis bg-transparent text-base outline-none placeholder:text-muted-foreground sm:text-[15px]",
|
|
607
|
+
className
|
|
608
|
+
)}
|
|
609
|
+
{...props}
|
|
610
|
+
/>
|
|
611
|
+
<kbd className="hidden rounded border border-b-2 bg-background px-[5px] py-px font-mono text-[10.5px] text-muted-foreground sm:inline-block">
|
|
612
|
+
esc
|
|
613
|
+
</kbd>
|
|
614
|
+
</div>
|
|
615
|
+
<button
|
|
616
|
+
type="button"
|
|
617
|
+
data-agent-part={c.part}
|
|
618
|
+
onClick={(event) => c.close(event.nativeEvent)}
|
|
619
|
+
className={cn("h-11 flex-none pl-0.5 pr-3.5 text-[15px] sm:hidden", LINK)}
|
|
620
|
+
>
|
|
621
|
+
{cancelLabel}
|
|
622
|
+
</button>
|
|
623
|
+
</div>
|
|
624
|
+
)
|
|
625
|
+
}
|
|
626
|
+
)
|
|
627
|
+
CommandPaletteInput.displayName = "CommandPaletteInput"
|
|
628
|
+
|
|
629
|
+
/** Lays the list and the preview side by side. */
|
|
630
|
+
const CommandPaletteBody = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
631
|
+
({ className, ...props }, ref) => (
|
|
632
|
+
<div
|
|
633
|
+
ref={ref}
|
|
634
|
+
data-slot="command-palette-body"
|
|
635
|
+
className={cn("relative flex min-h-0 flex-1 sm:h-[380px] sm:flex-none", className)}
|
|
636
|
+
{...props}
|
|
637
|
+
/>
|
|
638
|
+
)
|
|
639
|
+
)
|
|
640
|
+
CommandPaletteBody.displayName = "CommandPaletteBody"
|
|
641
|
+
|
|
642
|
+
function Highlight({ text, pattern }: { text: string; pattern: RegExp | undefined }) {
|
|
643
|
+
if (!pattern) return <>{text}</>
|
|
644
|
+
const parts = text.split(pattern)
|
|
645
|
+
return (
|
|
646
|
+
<>
|
|
647
|
+
{parts.map((part, i) =>
|
|
648
|
+
i % 2 ? (
|
|
649
|
+
<mark key={i} className="bg-transparent font-semibold text-foreground">
|
|
650
|
+
{part}
|
|
651
|
+
</mark>
|
|
652
|
+
) : (
|
|
653
|
+
part
|
|
654
|
+
)
|
|
655
|
+
)}
|
|
656
|
+
</>
|
|
657
|
+
)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const iconBox =
|
|
661
|
+
"grid flex-none place-items-center bg-muted text-muted-foreground [&_svg]:h-4 [&_svg]:w-4 group-data-[highlighted]:bg-background group-data-[highlighted]:text-foreground"
|
|
662
|
+
|
|
663
|
+
interface CommandPaletteItemProps extends Omit<React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Item>, "value"> {
|
|
664
|
+
item: CommandItem
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
interface RowProps extends CommandPaletteItemProps {
|
|
668
|
+
pattern: RegExp | undefined
|
|
669
|
+
pick: (item: CommandItem) => void
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function joinRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
|
|
673
|
+
return (node) => {
|
|
674
|
+
for (const ref of refs) {
|
|
675
|
+
if (typeof ref === "function") ref(node)
|
|
676
|
+
else if (ref) (ref as React.MutableRefObject<T | null>).current = node
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Memoized, so a row re-renders only when its item or the search changes. */
|
|
682
|
+
const Row = React.memo(
|
|
683
|
+
React.forwardRef<HTMLDivElement, RowProps>(({ item, pattern, pick, className, children, onClick, render, ...props }, ref) => {
|
|
684
|
+
const drills = Boolean(item.items) || (item.actions?.length ?? 0) > 1
|
|
685
|
+
return (
|
|
686
|
+
<AutocompletePrimitive.Item
|
|
687
|
+
ref={ref}
|
|
688
|
+
value={item}
|
|
689
|
+
disabled={item.disabled}
|
|
690
|
+
data-slot="command-palette-item"
|
|
691
|
+
// Base UI renders the highlighted row again on its own, which keeps this current.
|
|
692
|
+
render={(rowProps, state) => {
|
|
693
|
+
const own = { ...rowProps, "aria-selected": state.highlighted }
|
|
694
|
+
if (typeof render === "function") return render(own, state)
|
|
695
|
+
if (!render) return <div {...own} />
|
|
696
|
+
const theirs = render.props as React.HTMLAttributes<HTMLDivElement> & { ref?: React.Ref<HTMLDivElement> }
|
|
697
|
+
return React.cloneElement(render, { ...mergeProps<"div">(own, theirs), ref: joinRefs(rowProps.ref, theirs.ref) } as object)
|
|
698
|
+
}}
|
|
699
|
+
onClick={(event) => {
|
|
700
|
+
onClick?.(event)
|
|
701
|
+
pick(item)
|
|
702
|
+
}}
|
|
703
|
+
className={cn(
|
|
704
|
+
// The highlight follows the keys and the pointer, so it moves with no transition.
|
|
705
|
+
"group relative flex cursor-default select-none items-center gap-2.5 rounded-[9px] px-2.5 py-[11px] text-[15px] outline-none sm:py-2 sm:text-[13.5px]",
|
|
706
|
+
"data-[highlighted]:bg-muted data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
|
707
|
+
className
|
|
708
|
+
)}
|
|
709
|
+
{...props}
|
|
710
|
+
>
|
|
711
|
+
{children ?? (
|
|
712
|
+
<>
|
|
713
|
+
{item.icon && <span className={cn(iconBox, "h-8 w-8 rounded-[9px] sm:h-[26px] sm:w-[26px] sm:rounded-[7px]")}>{item.icon}</span>}
|
|
714
|
+
<span className="min-w-0 flex-1">
|
|
715
|
+
<Highlight text={item.label} pattern={pattern} />
|
|
716
|
+
{item.description && (
|
|
717
|
+
<span className="block text-[13px] text-muted-foreground sm:ml-1.5 sm:inline sm:text-[12.5px]">
|
|
718
|
+
<Highlight text={item.description} pattern={pattern} />
|
|
719
|
+
</span>
|
|
720
|
+
)}
|
|
721
|
+
</span>
|
|
722
|
+
<span className="ml-auto flex flex-none items-center gap-1.5 text-xs tabular-nums text-muted-foreground">
|
|
723
|
+
{item.meta}
|
|
724
|
+
{item.shortcut && <CommandPaletteShortcut className="hidden sm:inline-block">{item.shortcut}</CommandPaletteShortcut>}
|
|
725
|
+
{drills && <ChevronRight className="h-4 w-4 sm:hidden" aria-hidden />}
|
|
726
|
+
</span>
|
|
727
|
+
</>
|
|
728
|
+
)}
|
|
729
|
+
</AutocompletePrimitive.Item>
|
|
730
|
+
)
|
|
731
|
+
})
|
|
732
|
+
)
|
|
733
|
+
Row.displayName = "CommandPaletteRow"
|
|
734
|
+
|
|
735
|
+
/** One row. Its children replace the icon, label, meta and shortcut it shows by default. */
|
|
736
|
+
const CommandPaletteItem = React.forwardRef<HTMLDivElement, CommandPaletteItemProps>((props, ref) => {
|
|
737
|
+
const c = usePalette("CommandPaletteItem")
|
|
738
|
+
return <Row ref={ref} {...props} pattern={c.pattern} pick={c.pick} />
|
|
739
|
+
})
|
|
740
|
+
CommandPaletteItem.displayName = "CommandPaletteItem"
|
|
741
|
+
|
|
742
|
+
interface CommandPaletteListProps extends Omit<React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.List>, "children"> {
|
|
743
|
+
/** Renders each row. Defaults to a `CommandPaletteItem` for the item. */
|
|
744
|
+
children?: (item: CommandItem) => React.ReactNode
|
|
745
|
+
/** Shown when nothing matches. */
|
|
746
|
+
empty?: React.ReactNode | ((query: string) => React.ReactNode)
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* The grouped results. On a phone the groups and rows run bottom up, so the best match is
|
|
751
|
+
* nearest the field and the thumb, while the reading order stays best match first.
|
|
752
|
+
*/
|
|
753
|
+
const CommandPaletteList = React.forwardRef<HTMLDivElement, CommandPaletteListProps>(
|
|
754
|
+
({ className, children, empty, ...props }, ref) => {
|
|
755
|
+
const c = usePalette("CommandPaletteList")
|
|
756
|
+
const row = children ?? ((item: CommandItem) => <Row key={item.id} item={item} pattern={c.pattern} pick={c.pick} />)
|
|
757
|
+
const count = c.shown.reduce((n, g) => n + g.items.length, 0)
|
|
758
|
+
const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? "" : "s"}`
|
|
759
|
+
const status = c.query.trim()
|
|
760
|
+
? count ? `${count < c.total ? count + " of " : ""}${plural(c.total, "result")}` : null
|
|
761
|
+
: c.page && `${c.page.label}, ${count < c.total ? count + " of " : ""}${plural(c.total, "option")}`
|
|
762
|
+
return (
|
|
763
|
+
// A phone's record covers the list, so it leaves the tab order and the reading order.
|
|
764
|
+
<div className="flex min-h-0 min-w-0 flex-1 flex-col" inert={Boolean(c.detail)}>
|
|
765
|
+
{/* Counts matches, and names a sub-list as it opens. The empty state is its own live region. */}
|
|
766
|
+
<AutocompletePrimitive.Status className="sr-only">{status}</AutocompletePrimitive.Status>
|
|
767
|
+
<AutocompletePrimitive.Empty
|
|
768
|
+
data-slot="command-palette-empty"
|
|
769
|
+
className="mt-auto px-3 py-[30px] text-center text-[13px] text-muted-foreground empty:hidden sm:mt-0"
|
|
770
|
+
>
|
|
771
|
+
{typeof empty === "function" ? empty(c.query) : (empty ?? `Nothing matches “${c.query}”`)}
|
|
772
|
+
</AutocompletePrimitive.Empty>
|
|
773
|
+
<AutocompletePrimitive.List
|
|
774
|
+
ref={ref}
|
|
775
|
+
data-slot="command-palette-list"
|
|
776
|
+
aria-label={`${c.label} results`}
|
|
777
|
+
className={cn(
|
|
778
|
+
"flex min-h-0 flex-1 flex-col-reverse overflow-y-auto px-2 pb-1 pt-1.5 [scrollbar-width:thin] sm:flex-col sm:px-1.5 sm:pb-2 sm:pt-1",
|
|
779
|
+
className
|
|
780
|
+
)}
|
|
781
|
+
{...props}
|
|
782
|
+
>
|
|
783
|
+
{(group: Group) => (
|
|
784
|
+
<AutocompletePrimitive.Group key={group.key} items={group.items} data-slot="command-palette-group">
|
|
785
|
+
{group.label && (
|
|
786
|
+
<AutocompletePrimitive.GroupLabel
|
|
787
|
+
data-slot="command-palette-group-label"
|
|
788
|
+
className="px-2.5 pb-1 pt-2.5 text-[11.5px] font-medium text-muted-foreground"
|
|
789
|
+
>
|
|
790
|
+
{group.label}
|
|
791
|
+
</AutocompletePrimitive.GroupLabel>
|
|
792
|
+
)}
|
|
793
|
+
<div className="flex flex-col-reverse sm:flex-col">
|
|
794
|
+
<AutocompletePrimitive.Collection>{(item: CommandItem) => row(item)}</AutocompletePrimitive.Collection>
|
|
795
|
+
</div>
|
|
796
|
+
</AutocompletePrimitive.Group>
|
|
797
|
+
)}
|
|
798
|
+
</AutocompletePrimitive.List>
|
|
799
|
+
</div>
|
|
800
|
+
)
|
|
801
|
+
}
|
|
802
|
+
)
|
|
803
|
+
CommandPaletteList.displayName = "CommandPaletteList"
|
|
804
|
+
|
|
805
|
+
/** A key hint. */
|
|
806
|
+
const CommandPaletteShortcut = React.forwardRef<HTMLElement, React.HTMLAttributes<HTMLElement>>(
|
|
807
|
+
({ className, ...props }, ref) => (
|
|
808
|
+
<kbd
|
|
809
|
+
ref={ref}
|
|
810
|
+
data-slot="command-palette-shortcut"
|
|
811
|
+
className={cn(
|
|
812
|
+
"rounded border border-b-2 bg-background px-[5px] py-px font-mono text-[10.5px] font-normal text-muted-foreground",
|
|
813
|
+
className
|
|
814
|
+
)}
|
|
815
|
+
{...props}
|
|
816
|
+
/>
|
|
817
|
+
)
|
|
818
|
+
)
|
|
819
|
+
CommandPaletteShortcut.displayName = "CommandPaletteShortcut"
|
|
820
|
+
|
|
821
|
+
interface CommandPalettePreviewProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "children"> {
|
|
822
|
+
/** The body under the item's name, such as a record's fields. Defaults to its description. */
|
|
823
|
+
children?: (item: CommandItem) => React.ReactNode
|
|
824
|
+
/** Shown while nothing is highlighted. */
|
|
825
|
+
placeholder?: React.ReactNode
|
|
826
|
+
/** The button for an item with no actions of its own. */
|
|
827
|
+
actionLabel?: string
|
|
828
|
+
/** The phone's button back to the results. */
|
|
829
|
+
backLabel?: string
|
|
830
|
+
/** That button's accessible name. Defaults to "Back to", then `backLabel` in lower case. */
|
|
831
|
+
backAriaLabel?: string
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* The highlighted item, with its actions. Beside the list on a desktop; on a phone it is
|
|
836
|
+
* the screen a record opens into, with the keyboard down and the actions at the bottom.
|
|
837
|
+
*/
|
|
838
|
+
const CommandPalettePreview = React.forwardRef<HTMLDivElement, CommandPalettePreviewProps>(
|
|
839
|
+
({ className, children, placeholder, actionLabel = "Open", backLabel = "Search", backAriaLabel, ...props }, ref) => {
|
|
840
|
+
const c = usePalette("CommandPalettePreview")
|
|
841
|
+
const highlighted = React.useContext(HighlightContext)
|
|
842
|
+
const item = c.phone ? c.detail : highlighted
|
|
843
|
+
// A record opening on a phone takes focus, so it is announced and the keys stay in it.
|
|
844
|
+
const backButton = React.useRef<HTMLButtonElement | null>(null)
|
|
845
|
+
React.useEffect(() => {
|
|
846
|
+
if (c.phone && c.detail) backButton.current?.focus()
|
|
847
|
+
}, [c.phone, c.detail])
|
|
848
|
+
if (c.phone && !item) return null
|
|
849
|
+
const mod = c.mac ? "⌘" : "Ctrl"
|
|
850
|
+
const actions = item?.actions ?? [{ id: "", label: actionLabel }]
|
|
851
|
+
const press = (action: CommandAction, event: React.MouseEvent) => {
|
|
852
|
+
if (!item || item.disabled) return
|
|
853
|
+
if (item.items) return c.pick(item)
|
|
854
|
+
c.run(item, item.actions ? action : undefined, personDetails("action-press", event.nativeEvent))
|
|
855
|
+
}
|
|
856
|
+
const back = () => c.setDetail(undefined)
|
|
857
|
+
return (
|
|
858
|
+
<div
|
|
859
|
+
ref={ref}
|
|
860
|
+
data-slot="command-palette-preview"
|
|
861
|
+
className={cn(
|
|
862
|
+
"absolute inset-0 z-10 flex flex-col bg-background",
|
|
863
|
+
"sm:static sm:z-auto sm:w-[270px] sm:flex-none sm:gap-3.5 sm:border-l sm:bg-card sm:p-[18px]",
|
|
864
|
+
className
|
|
865
|
+
)}
|
|
866
|
+
{...props}
|
|
867
|
+
>
|
|
868
|
+
{c.phone && (
|
|
869
|
+
<div className="flex items-center px-3.5 py-2.5">
|
|
870
|
+
<button
|
|
871
|
+
ref={backButton}
|
|
872
|
+
type="button"
|
|
873
|
+
onClick={back}
|
|
874
|
+
aria-label={backAriaLabel ?? `Back to ${backLabel.toLowerCase()}`}
|
|
875
|
+
className={cn(
|
|
876
|
+
"flex h-11 min-w-11 items-center rounded-md px-0.5 text-base focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
877
|
+
LINK
|
|
878
|
+
)}
|
|
879
|
+
>
|
|
880
|
+
<ChevronLeft className="h-5 w-5" aria-hidden />
|
|
881
|
+
{backLabel}
|
|
882
|
+
</button>
|
|
883
|
+
</div>
|
|
884
|
+
)}
|
|
885
|
+
{item ? (
|
|
886
|
+
<>
|
|
887
|
+
<div className="grid gap-4 px-5 py-1.5 sm:flex sm:flex-col sm:gap-3.5 sm:p-0">
|
|
888
|
+
<div className="flex items-center gap-2.5">
|
|
889
|
+
{item.icon && <span className={cn(iconBox, "hidden h-[34px] w-[34px] rounded-[9px] sm:grid")}>{item.icon}</span>}
|
|
890
|
+
<div className="min-w-0">
|
|
891
|
+
<div className="text-[22px] font-semibold tracking-[-0.02em] sm:text-[15px] sm:tracking-normal">{item.label}</div>
|
|
892
|
+
{(c.phone ? item.description : item.kind) && (
|
|
893
|
+
<div className="mt-1 text-[15px] text-muted-foreground sm:mt-0 sm:text-[12.5px]">
|
|
894
|
+
{c.phone ? item.description : item.kind}
|
|
895
|
+
</div>
|
|
896
|
+
)}
|
|
897
|
+
</div>
|
|
898
|
+
</div>
|
|
899
|
+
{children ? (
|
|
900
|
+
children(item)
|
|
901
|
+
) : (
|
|
902
|
+
item.description && !c.phone && <div className="text-[12.5px] leading-normal text-muted-foreground">{item.description}</div>
|
|
903
|
+
)}
|
|
904
|
+
</div>
|
|
905
|
+
<div className="mt-auto grid gap-2 px-4 pb-[max(30px,env(safe-area-inset-bottom))] pt-3.5 sm:gap-1 sm:p-0">
|
|
906
|
+
{actions.map((action, i) => (
|
|
907
|
+
<button
|
|
908
|
+
key={action.id || i}
|
|
909
|
+
type="button"
|
|
910
|
+
disabled={item.disabled}
|
|
911
|
+
data-agent-part={c.part}
|
|
912
|
+
onClick={(event) => press(action, event)}
|
|
913
|
+
className={cn(
|
|
914
|
+
"flex items-center justify-center rounded-[13px] border bg-card p-3.5 text-base",
|
|
915
|
+
"sm:justify-between sm:rounded-[9px] sm:bg-background sm:px-2.5 sm:py-[7px] sm:text-[13px]",
|
|
916
|
+
i === 0 && "border-transparent bg-primary text-primary-foreground sm:bg-primary"
|
|
917
|
+
)}
|
|
918
|
+
>
|
|
919
|
+
{action.label}
|
|
920
|
+
{i < 2 && (
|
|
921
|
+
<CommandPaletteShortcut
|
|
922
|
+
className={cn(
|
|
923
|
+
"hidden sm:inline-block",
|
|
924
|
+
i === 0 && "border-[color:color-mix(in_srgb,var(--primary-foreground)_35%,transparent)] bg-transparent text-inherit"
|
|
925
|
+
)}
|
|
926
|
+
>
|
|
927
|
+
{i === 0 ? "↵" : `${mod} ↵`}
|
|
928
|
+
</CommandPaletteShortcut>
|
|
929
|
+
)}
|
|
930
|
+
</button>
|
|
931
|
+
))}
|
|
932
|
+
</div>
|
|
933
|
+
</>
|
|
934
|
+
) : (
|
|
935
|
+
<div className="text-[12.5px] leading-normal text-muted-foreground">{placeholder}</div>
|
|
936
|
+
)}
|
|
937
|
+
</div>
|
|
938
|
+
)
|
|
939
|
+
}
|
|
940
|
+
)
|
|
941
|
+
CommandPalettePreview.displayName = "CommandPalettePreview"
|
|
942
|
+
|
|
943
|
+
export {
|
|
944
|
+
CommandPalette,
|
|
945
|
+
CommandPaletteTrigger,
|
|
946
|
+
CommandPaletteContent,
|
|
947
|
+
CommandPaletteInput,
|
|
948
|
+
CommandPaletteBody,
|
|
949
|
+
CommandPaletteList,
|
|
950
|
+
CommandPaletteItem,
|
|
951
|
+
CommandPaletteShortcut,
|
|
952
|
+
CommandPalettePreview,
|
|
953
|
+
}
|
|
954
|
+
export type { CommandPaletteInputProps, CommandPaletteItemProps, CommandPaletteListProps, CommandPalettePreviewProps }
|