"use client"; import * as React from "react"; import { Command as CommandPrimitive, useCommandState } from "cmdk"; import { cn } from "./lib/utils"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "./dialog"; import { InputGroup, InputGroupAddon } from "./input-group"; import { IconSearch, IconCheck } from "@tabler/icons-react"; type CommandListElement = React.ElementRef; type CommandListProps = React.ComponentPropsWithoutRef; type RefCleanup = void | (() => void); function applyCommandListRef( ref: React.ForwardedRef, node: CommandListElement | null, ): RefCleanup { if (typeof ref === "function") return ref(node); if (ref) ref.current = node; } function Command({ className, ...props }: React.ComponentProps) { return ( ); } function CommandDialog({ title = "Command Palette", description = "Search for a command to run...", children, className, showCloseButton = false, overlayClassName, contentProps, ...props }: React.ComponentProps & { title?: string; description?: string; className?: string; showCloseButton?: boolean; overlayClassName?: string; /** Optional overrides for the owning dialog, such as an application-local confirmation step. */ contentProps?: React.ComponentProps; }) { return ( {title} {description} {children} ); } function CommandInput({ className, ...props }: React.ComponentProps) { return (
); } const CommandList = React.forwardRef(function CommandList( { className, ...props }, ref, ) { const search = useCommandState((state) => state.search); const internalRef = React.useRef(null); const latestForwardedRef = React.useRef(ref); const assignedRef = React.useRef>(null); const assignedCleanup = React.useRef<(() => void) | undefined>(undefined); latestForwardedRef.current = ref; const detachAssignedRef = React.useCallback(() => { const cleanup = assignedCleanup.current; const forwardedRef = assignedRef.current; assignedCleanup.current = undefined; assignedRef.current = null; if (cleanup) { cleanup(); return; } if (forwardedRef) applyCommandListRef(forwardedRef, null); }, []); const attachAssignedRef = React.useCallback( (node: CommandListElement, forwardedRef: typeof ref) => { assignedRef.current = forwardedRef; const cleanup = applyCommandListRef(forwardedRef, node); assignedCleanup.current = typeof cleanup === "function" ? cleanup : undefined; }, [], ); const setRefs = React.useCallback( (node: CommandListElement | null) => { if (internalRef.current === node) return; detachAssignedRef(); internalRef.current = node; if (!node) return; attachAssignedRef(node, latestForwardedRef.current); return () => { if (internalRef.current !== node) return; internalRef.current = null; detachAssignedRef(); }; }, [attachAssignedRef, detachAssignedRef], ); React.useLayoutEffect(() => { const node = internalRef.current; if (!node || assignedRef.current === ref) return; detachAssignedRef(); attachAssignedRef(node, ref); }, [attachAssignedRef, detachAssignedRef, ref]); React.useLayoutEffect(() => { if (internalRef.current) internalRef.current.scrollTop = 0; }, [search]); return ( ); }); CommandList.displayName = CommandPrimitive.List.displayName; function CommandEmpty({ className, ...props }: React.ComponentProps) { return ( ); } function CommandGroup({ className, ...props }: React.ComponentProps) { return ( ); } function CommandSeparator({ className, ...props }: React.ComponentProps) { return ( ); } function CommandItem({ className, children, ...props }: React.ComponentProps) { return ( {children} ); } function CommandShortcut({ className, ...props }: React.ComponentProps<"span">) { return ( ); } export { Command, CommandDialog, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem, CommandShortcut, CommandSeparator, };