mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-12 10:21:39 +00:00
* feat: init * fix * fix * fix * feat * feat * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: implement icons * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: update init command * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: dialog * feat * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: add registry:base item type * feat: rename frame to canva * fix * feat * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fi * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: add all colors * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: add outfit font * fix * fix * fix * fix * fix * chore: changeset * fix * fix * fix * fix * fix * fix * fix * fix
526 lines
18 KiB
TypeScript
526 lines
18 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { useRouter } from "next/navigation"
|
|
import { type DialogProps } from "@radix-ui/react-dialog"
|
|
import { IconArrowRight } from "@tabler/icons-react"
|
|
import { useDocsSearch } from "fumadocs-core/search/client"
|
|
import { CornerDownLeftIcon, SquareDashedIcon } from "lucide-react"
|
|
|
|
import { type Color, type ColorPalette } from "@/lib/colors"
|
|
import { trackEvent } from "@/lib/events"
|
|
import { showMcpDocs } from "@/lib/flags"
|
|
import { type source } from "@/lib/source"
|
|
import { cn } from "@/lib/utils"
|
|
import { useConfig } from "@/hooks/use-config"
|
|
import { useMutationObserver } from "@/hooks/use-mutation-observer"
|
|
import { copyToClipboardWithMeta } from "@/components/copy-button"
|
|
import { Button } from "@/registry/new-york-v4/ui/button"
|
|
import {
|
|
Command,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList,
|
|
} from "@/registry/new-york-v4/ui/command"
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@/registry/new-york-v4/ui/dialog"
|
|
import { Kbd, KbdGroup } from "@/registry/new-york-v4/ui/kbd"
|
|
import { Separator } from "@/registry/new-york-v4/ui/separator"
|
|
import { Spinner } from "@/registry/new-york-v4/ui/spinner"
|
|
|
|
export function CommandMenu({
|
|
tree,
|
|
colors,
|
|
blocks,
|
|
navItems,
|
|
...props
|
|
}: DialogProps & {
|
|
tree: typeof source.pageTree
|
|
colors: ColorPalette[]
|
|
blocks?: { name: string; description: string; categories: string[] }[]
|
|
navItems?: { href: string; label: string }[]
|
|
}) {
|
|
const router = useRouter()
|
|
const [config] = useConfig()
|
|
const [open, setOpen] = React.useState(false)
|
|
const [selectedType, setSelectedType] = React.useState<
|
|
"color" | "page" | "component" | "block" | null
|
|
>(null)
|
|
const [copyPayload, setCopyPayload] = React.useState("")
|
|
|
|
const { search, setSearch, query } = useDocsSearch({
|
|
type: "fetch",
|
|
})
|
|
const packageManager = config.packageManager || "pnpm"
|
|
|
|
// Track search queries with debouncing to avoid excessive tracking.
|
|
const searchTimeoutRef = React.useRef<NodeJS.Timeout | undefined>(undefined)
|
|
const lastTrackedQueryRef = React.useRef<string>("")
|
|
|
|
const trackSearchQuery = React.useCallback((query: string) => {
|
|
const trimmedQuery = query.trim()
|
|
|
|
// Only track if the query is different from the last tracked query and has content.
|
|
if (trimmedQuery && trimmedQuery !== lastTrackedQueryRef.current) {
|
|
lastTrackedQueryRef.current = trimmedQuery
|
|
trackEvent({
|
|
name: "search_query",
|
|
properties: {
|
|
query: trimmedQuery,
|
|
query_length: trimmedQuery.length,
|
|
},
|
|
})
|
|
}
|
|
}, [])
|
|
|
|
const handleSearchChange = React.useCallback(
|
|
(value: string) => {
|
|
// Clear existing timeout.
|
|
if (searchTimeoutRef.current) {
|
|
clearTimeout(searchTimeoutRef.current)
|
|
}
|
|
|
|
// Set new timeout to debounce both search and tracking.
|
|
searchTimeoutRef.current = setTimeout(() => {
|
|
setSearch(value)
|
|
trackSearchQuery(value)
|
|
}, 500)
|
|
},
|
|
[setSearch, trackSearchQuery]
|
|
)
|
|
|
|
// Cleanup timeout on unmount.
|
|
React.useEffect(() => {
|
|
return () => {
|
|
if (searchTimeoutRef.current) {
|
|
clearTimeout(searchTimeoutRef.current)
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
const handlePageHighlight = React.useCallback(
|
|
(isComponent: boolean, item: { url: string; name?: React.ReactNode }) => {
|
|
if (isComponent) {
|
|
const componentName = item.url.split("/").pop()
|
|
setSelectedType("component")
|
|
setCopyPayload(
|
|
`${packageManager} dlx shadcn@latest add ${componentName}`
|
|
)
|
|
} else {
|
|
setSelectedType("page")
|
|
setCopyPayload("")
|
|
}
|
|
},
|
|
[packageManager, setSelectedType, setCopyPayload]
|
|
)
|
|
|
|
const handleColorHighlight = React.useCallback(
|
|
(color: Color) => {
|
|
setSelectedType("color")
|
|
setCopyPayload(color.className)
|
|
},
|
|
[setSelectedType, setCopyPayload]
|
|
)
|
|
|
|
const handleBlockHighlight = React.useCallback(
|
|
(block: { name: string; description: string; categories: string[] }) => {
|
|
setSelectedType("block")
|
|
setCopyPayload(`${packageManager} dlx shadcn@latest add ${block.name}`)
|
|
},
|
|
[setSelectedType, setCopyPayload, packageManager]
|
|
)
|
|
|
|
const runCommand = React.useCallback((command: () => unknown) => {
|
|
setOpen(false)
|
|
command()
|
|
}, [])
|
|
|
|
React.useEffect(() => {
|
|
const down = (e: KeyboardEvent) => {
|
|
if ((e.key === "k" && (e.metaKey || e.ctrlKey)) || e.key === "/") {
|
|
if (
|
|
(e.target instanceof HTMLElement && e.target.isContentEditable) ||
|
|
e.target instanceof HTMLInputElement ||
|
|
e.target instanceof HTMLTextAreaElement ||
|
|
e.target instanceof HTMLSelectElement
|
|
) {
|
|
return
|
|
}
|
|
|
|
e.preventDefault()
|
|
setOpen((open) => !open)
|
|
}
|
|
|
|
if (e.key === "c" && (e.metaKey || e.ctrlKey)) {
|
|
runCommand(() => {
|
|
if (selectedType === "color") {
|
|
copyToClipboardWithMeta(copyPayload, {
|
|
name: "copy_color",
|
|
properties: { color: copyPayload },
|
|
})
|
|
}
|
|
|
|
if (selectedType === "block") {
|
|
copyToClipboardWithMeta(copyPayload, {
|
|
name: "copy_npm_command",
|
|
properties: { command: copyPayload, pm: packageManager },
|
|
})
|
|
}
|
|
|
|
if (selectedType === "page" || selectedType === "component") {
|
|
copyToClipboardWithMeta(copyPayload, {
|
|
name: "copy_npm_command",
|
|
properties: { command: copyPayload, pm: packageManager },
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
document.addEventListener("keydown", down)
|
|
return () => document.removeEventListener("keydown", down)
|
|
}, [copyPayload, runCommand, selectedType, packageManager])
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button
|
|
variant="outline"
|
|
className={cn(
|
|
"text-foreground dark:bg-card hover:bg-muted/50 relative h-8 w-full justify-start pl-3 font-normal shadow-none sm:pr-12 md:w-48 lg:w-56 xl:w-64"
|
|
)}
|
|
onClick={() => setOpen(true)}
|
|
{...props}
|
|
>
|
|
<span className="hidden lg:inline-flex">Search documentation...</span>
|
|
<span className="inline-flex lg:hidden">Search...</span>
|
|
<div className="absolute top-1.5 right-1.5 hidden gap-1 group-has-[[data-slot=designer]]/body:hidden sm:flex">
|
|
<Kbd>⌘K</Kbd>
|
|
</div>
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent
|
|
showCloseButton={false}
|
|
className="rounded-xl border-none bg-clip-padding p-2 pb-11 shadow-2xl ring-4 ring-neutral-200/80 dark:bg-neutral-900 dark:ring-neutral-800"
|
|
>
|
|
<DialogHeader className="sr-only">
|
|
<DialogTitle>Search documentation...</DialogTitle>
|
|
<DialogDescription>Search for a command to run...</DialogDescription>
|
|
</DialogHeader>
|
|
<Command
|
|
className="**:data-[slot=command-input-wrapper]:bg-input/50 **:data-[slot=command-input-wrapper]:border-input rounded-none bg-transparent **:data-[slot=command-input]:!h-9 **:data-[slot=command-input]:py-0 **:data-[slot=command-input-wrapper]:mb-0 **:data-[slot=command-input-wrapper]:!h-9 **:data-[slot=command-input-wrapper]:rounded-md **:data-[slot=command-input-wrapper]:border"
|
|
filter={(value, search, keywords) => {
|
|
handleSearchChange(search)
|
|
const extendValue = value + " " + (keywords?.join(" ") || "")
|
|
if (extendValue.toLowerCase().includes(search.toLowerCase())) {
|
|
return 1
|
|
}
|
|
return 0
|
|
}}
|
|
>
|
|
<div className="relative">
|
|
<CommandInput placeholder="Search documentation..." />
|
|
{query.isLoading && (
|
|
<div className="pointer-events-none absolute top-1/2 right-3 z-10 flex -translate-y-1/2 items-center justify-center">
|
|
<Spinner className="text-muted-foreground size-4" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
<CommandList className="no-scrollbar min-h-80 scroll-pt-2 scroll-pb-1.5">
|
|
<CommandEmpty className="text-muted-foreground py-12 text-center text-sm">
|
|
{query.isLoading ? "Searching..." : "No results found."}
|
|
</CommandEmpty>
|
|
{navItems && navItems.length > 0 && (
|
|
<CommandGroup
|
|
heading="Pages"
|
|
className="!p-0 [&_[cmdk-group-heading]]:scroll-mt-16 [&_[cmdk-group-heading]]:!p-3 [&_[cmdk-group-heading]]:!pb-1"
|
|
>
|
|
{navItems.map((item) => (
|
|
<CommandMenuItem
|
|
key={item.href}
|
|
value={`Navigation ${item.label}`}
|
|
keywords={["nav", "navigation", item.label.toLowerCase()]}
|
|
onHighlight={() => {
|
|
setSelectedType("page")
|
|
setCopyPayload("")
|
|
}}
|
|
onSelect={() => {
|
|
runCommand(() => router.push(item.href))
|
|
}}
|
|
>
|
|
<IconArrowRight />
|
|
{item.label}
|
|
</CommandMenuItem>
|
|
))}
|
|
</CommandGroup>
|
|
)}
|
|
{tree.children.map((group) => (
|
|
<CommandGroup
|
|
key={group.$id}
|
|
heading={group.name}
|
|
className="!p-0 [&_[cmdk-group-heading]]:scroll-mt-16 [&_[cmdk-group-heading]]:!p-3 [&_[cmdk-group-heading]]:!pb-1"
|
|
>
|
|
{group.type === "folder" &&
|
|
group.children.map((item) => {
|
|
if (item.type === "page") {
|
|
const isComponent = item.url.includes("/components/")
|
|
|
|
if (!showMcpDocs && item.url.includes("/mcp")) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<CommandMenuItem
|
|
key={item.url}
|
|
value={
|
|
item.name?.toString()
|
|
? `${group.name} ${item.name}`
|
|
: ""
|
|
}
|
|
keywords={isComponent ? ["component"] : undefined}
|
|
onHighlight={() =>
|
|
handlePageHighlight(isComponent, item)
|
|
}
|
|
onSelect={() => {
|
|
runCommand(() => router.push(item.url))
|
|
}}
|
|
>
|
|
{isComponent ? (
|
|
<div className="border-muted-foreground aspect-square size-4 rounded-full border border-dashed" />
|
|
) : (
|
|
<IconArrowRight />
|
|
)}
|
|
{item.name}
|
|
</CommandMenuItem>
|
|
)
|
|
}
|
|
return null
|
|
})}
|
|
</CommandGroup>
|
|
))}
|
|
{colors.map((colorPalette) => (
|
|
<CommandGroup
|
|
key={colorPalette.name}
|
|
heading={
|
|
colorPalette.name.charAt(0).toUpperCase() +
|
|
colorPalette.name.slice(1)
|
|
}
|
|
className="!p-0 [&_[cmdk-group-heading]]:!p-3"
|
|
>
|
|
{colorPalette.colors.map((color) => (
|
|
<CommandMenuItem
|
|
key={color.hex}
|
|
value={color.className}
|
|
keywords={["color", color.name, color.className]}
|
|
onHighlight={() => handleColorHighlight(color)}
|
|
onSelect={() => {
|
|
runCommand(() =>
|
|
copyToClipboardWithMeta(color.oklch, {
|
|
name: "copy_color",
|
|
properties: { color: color.oklch },
|
|
})
|
|
)
|
|
}}
|
|
>
|
|
<div
|
|
className="border-ghost aspect-square size-4 rounded-sm bg-(--color) after:rounded-sm"
|
|
style={{ "--color": color.oklch } as React.CSSProperties}
|
|
/>
|
|
{color.className}
|
|
<span className="text-muted-foreground ml-auto font-mono text-xs font-normal tabular-nums">
|
|
{color.oklch}
|
|
</span>
|
|
</CommandMenuItem>
|
|
))}
|
|
</CommandGroup>
|
|
))}
|
|
{blocks?.length ? (
|
|
<CommandGroup
|
|
heading="Blocks"
|
|
className="!p-0 [&_[cmdk-group-heading]]:!p-3"
|
|
>
|
|
{blocks.map((block) => (
|
|
<CommandMenuItem
|
|
key={block.name}
|
|
value={block.name}
|
|
onHighlight={() => {
|
|
handleBlockHighlight(block)
|
|
}}
|
|
keywords={[
|
|
"block",
|
|
block.name,
|
|
block.description,
|
|
...block.categories,
|
|
]}
|
|
onSelect={() => {
|
|
runCommand(() =>
|
|
router.push(
|
|
`/blocks/${block.categories[0]}#${block.name}`
|
|
)
|
|
)
|
|
}}
|
|
>
|
|
<SquareDashedIcon />
|
|
{block.description}
|
|
<span className="text-muted-foreground ml-auto font-mono text-xs font-normal tabular-nums">
|
|
{block.name}
|
|
</span>
|
|
</CommandMenuItem>
|
|
))}
|
|
</CommandGroup>
|
|
) : null}
|
|
<SearchResults
|
|
open={open}
|
|
setOpen={setOpen}
|
|
query={query}
|
|
search={search}
|
|
/>
|
|
</CommandList>
|
|
</Command>
|
|
<div className="text-muted-foreground absolute inset-x-0 bottom-0 z-20 flex h-10 items-center gap-2 rounded-b-xl border-t border-t-neutral-100 bg-neutral-50 px-4 text-xs font-medium dark:border-t-neutral-700 dark:bg-neutral-800">
|
|
<div className="flex items-center gap-2">
|
|
<CommandMenuKbd>
|
|
<CornerDownLeftIcon />
|
|
</CommandMenuKbd>{" "}
|
|
{selectedType === "page" || selectedType === "component"
|
|
? "Go to Page"
|
|
: null}
|
|
{selectedType === "color" ? "Copy OKLCH" : null}
|
|
</div>
|
|
{copyPayload && (
|
|
<>
|
|
<Separator orientation="vertical" className="!h-4" />
|
|
<div className="flex items-center gap-1">
|
|
<CommandMenuKbd>⌘</CommandMenuKbd>
|
|
<CommandMenuKbd>C</CommandMenuKbd>
|
|
{copyPayload}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
function CommandMenuItem({
|
|
children,
|
|
className,
|
|
onHighlight,
|
|
...props
|
|
}: React.ComponentProps<typeof CommandItem> & {
|
|
onHighlight?: () => void
|
|
"data-selected"?: string
|
|
"aria-selected"?: string
|
|
}) {
|
|
const ref = React.useRef<HTMLDivElement>(null)
|
|
|
|
useMutationObserver(ref, (mutations) => {
|
|
mutations.forEach((mutation) => {
|
|
if (
|
|
mutation.type === "attributes" &&
|
|
mutation.attributeName === "aria-selected" &&
|
|
ref.current?.getAttribute("aria-selected") === "true"
|
|
) {
|
|
onHighlight?.()
|
|
}
|
|
})
|
|
})
|
|
|
|
return (
|
|
<CommandItem
|
|
ref={ref}
|
|
className={cn(
|
|
"data-[selected=true]:border-input data-[selected=true]:bg-input/50 h-9 rounded-md border border-transparent !px-3 font-medium",
|
|
className
|
|
)}
|
|
{...props}
|
|
>
|
|
{children}
|
|
</CommandItem>
|
|
)
|
|
}
|
|
|
|
function CommandMenuKbd({ className, ...props }: React.ComponentProps<"kbd">) {
|
|
return (
|
|
<kbd
|
|
className={cn(
|
|
"bg-background text-muted-foreground pointer-events-none flex h-5 items-center justify-center gap-1 rounded border px-1 font-sans text-[0.7rem] font-medium select-none [&_svg:not([class*='size-'])]:size-3",
|
|
className
|
|
)}
|
|
{...props}
|
|
/>
|
|
)
|
|
}
|
|
|
|
type Query = Awaited<ReturnType<typeof useDocsSearch>>["query"]
|
|
|
|
function SearchResults({
|
|
setOpen,
|
|
query,
|
|
search,
|
|
}: {
|
|
open: boolean
|
|
setOpen: (open: boolean) => void
|
|
query: Query
|
|
search: string
|
|
}) {
|
|
const router = useRouter()
|
|
|
|
const uniqueResults =
|
|
query.data && Array.isArray(query.data)
|
|
? query.data.filter(
|
|
(item, index, self) =>
|
|
!(
|
|
item.type === "text" &&
|
|
item.content.trim().split(/\s+/).length <= 1
|
|
) && index === self.findIndex((t) => t.content === item.content)
|
|
)
|
|
: []
|
|
|
|
if (!search.trim()) {
|
|
return null
|
|
}
|
|
|
|
if (!query.data || query.data === "empty") {
|
|
return null
|
|
}
|
|
|
|
if (query.data && uniqueResults.length === 0) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<CommandGroup
|
|
className="!px-0 [&_[cmdk-group-heading]]:scroll-mt-16 [&_[cmdk-group-heading]]:!p-3 [&_[cmdk-group-heading]]:!pb-1"
|
|
heading="Search Results"
|
|
>
|
|
{uniqueResults.map((item) => {
|
|
return (
|
|
<CommandItem
|
|
key={item.id}
|
|
data-type={item.type}
|
|
onSelect={() => {
|
|
router.push(item.url)
|
|
setOpen(false)
|
|
}}
|
|
className="data-[selected=true]:border-input data-[selected=true]:bg-input/50 h-9 rounded-md border border-transparent !px-3 font-normal"
|
|
keywords={[item.content]}
|
|
value={`${item.content} ${item.type}`}
|
|
>
|
|
<div className="line-clamp-1 text-sm">{item.content}</div>
|
|
</CommandItem>
|
|
)
|
|
})}
|
|
</CommandGroup>
|
|
)
|
|
}
|