Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ function CommandSearchInput({
isFullHeight: boolean
onToggleFullHeight: () => void
}) {
const { closeSearch } = useSearchContext()
const { isOpen, closeSearch } = useSearchContext()
const { refine } = useSearchBox()
const { searchQuery, setSearchQuery } = useSearchFilters()
const hasQuery = searchQuery.trim().length > 0
const inputRef = React.useRef<HTMLInputElement>(null)

React.useEffect(() => {
if (!isOpen) return

const frame = requestAnimationFrame(() => {
inputRef.current?.focus({ preventScroll: true })
})

return () => cancelAnimationFrame(frame)
}, [])
}, [isOpen])

const clearSearch = React.useCallback(() => {
setSearchQuery('')
Expand Down Expand Up @@ -295,7 +297,7 @@ const searchModalTransitionMs = 140

export function SearchModal() {
const { isOpen, closeSearch } = useSearchContext()
const contentRef = React.useRef<HTMLDivElement>(null)
const returnFocusRef = React.useRef<HTMLElement | null>(null)
const bodyPointerEventsRef = React.useRef('')
const [shouldRenderSearch, setShouldRenderSearch] = React.useState(isOpen)
const [isFullHeight, setIsFullHeight] = React.useState(() => {
Expand Down Expand Up @@ -327,20 +329,6 @@ export function SearchModal() {
return () => window.clearTimeout(timeout)
}, [isOpen])

React.useEffect(() => {
if (!isOpen) {
return
}

const frame = requestAnimationFrame(() => {
contentRef.current
?.querySelector<HTMLInputElement>('input[type="search"]')
?.focus({ preventScroll: true })
})

return () => cancelAnimationFrame(frame)
}, [isOpen])

React.useEffect(() => {
if (typeof document === 'undefined') {
return
Expand Down Expand Up @@ -398,11 +386,27 @@ export function SearchModal() {
/>
<DialogPrimitive.Content
forceMount
ref={contentRef}
className={twMerge(
'search-modal-content fixed z-[calc(var(--z-overlay)+2)] inset-0 sm:inset-auto sm:top-4 sm:left-1/2 sm:-translate-x-1/2 sm:w-[96%] xl:w-full sm:max-w-4xl text-left outline-none',
isFullHeight && 'sm:bottom-4',
)}
onOpenAutoFocus={(event) => {
// The input owns focus; the dialog has no Radix Trigger to restore.
returnFocusRef.current =
document.activeElement instanceof HTMLElement
? document.activeElement
: null
event.preventDefault()
}}
onCloseAutoFocus={(event) => {
event.preventDefault()
if (
document.activeElement === document.body &&
returnFocusRef.current?.isConnected
) {
returnFocusRef.current.focus({ preventScroll: true })
}
}}
onInteractOutside={(event) => {
if (isSearchModalPortalTarget(event.target)) {
event.preventDefault()
Expand Down