'use client' import { MagnifyingGlassIcon as MagnifyingGlassIconSmall } from '@heroicons/react/20/solid' import { MagnifyingGlassIcon as MagnifyingGlassIconLarge, XMarkIcon, } from '@heroicons/react/24/solid' import { usePathname } from 'next/navigation' import { useEffect, useState } from 'react' import { twJoin } from 'tailwind-merge' import { cn } from '@/utils/cn' import { Search } from '.' function isEditableElement(target: EventTarget | null) { if (!(target instanceof HTMLElement)) return false return ( target.isContentEditable || target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT' ) } export function SearchButton({ type, layout, className, }: { type: 'docs' | 'blog' layout: 'mobile' | 'desktop' | 'keyboard-shortcut-only' className?: string }) { const [open, setOpen] = useState(false) const pathname = usePathname() useEffect(() => { if (layout !== 'desktop' && layout !== 'keyboard-shortcut-only') return const onKeyDown = (e: KeyboardEvent) => { if (e.key === '/') { // A bare "/" is only a shortcut outside text fields, otherwise typing a path into // the feedback textarea opens the dialog and steals focus mid-word. if (isEditableElement(e.target)) return e.preventDefault() setOpen(true) } else if (e.key === 'k' && (e.metaKey || e.ctrlKey)) { setOpen((open) => !open) } else if (e.key === 'Escape') { setOpen(false) } } document.addEventListener('keydown', onKeyDown) return () => document.removeEventListener('keydown', onKeyDown) }, [layout]) useEffect(() => { setOpen(false) }, [pathname]) if (layout === 'keyboard-shortcut-only') { return open ? setOpen(false)} /> : null } return (
{open && setOpen(false)} />}
) }