Skip to content

Commit 5cb8849

Browse files
committed
fix: cmd+k focuses cursor on search input
1 parent fb621c3 commit 5cb8849

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/components/SearchModal.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,22 @@ export function SearchModal() {
782782
}
783783
}, [isOpen])
784784

785+
React.useEffect(() => {
786+
if (!isOpen) return
787+
788+
const focusSearchInput = () => {
789+
const input = containerRef.current?.querySelector<HTMLInputElement>(
790+
'input[type="search"]',
791+
)
792+
input?.focus()
793+
}
794+
795+
const id = window.requestAnimationFrame(() => {
796+
window.requestAnimationFrame(focusSearchInput)
797+
})
798+
return () => window.cancelAnimationFrame(id)
799+
}, [isOpen])
800+
785801
const focusedIndexRef = React.useRef(focusedIndex)
786802

787803
React.useEffect(() => {
@@ -835,6 +851,7 @@ export function SearchModal() {
835851
className="fixed z-[1000] top-8 left-1/2 -translate-x-1/2 w-[98%] xl:w-full max-w-3xl text-left bg-white/80 dark:bg-black/80 shadow-lg rounded-lg xl:rounded-xl divide-y divide-gray-500/20 backdrop-blur-lg dark:border dark:border-white/20 outline-none"
836852
ref={containerRef}
837853
onKeyDown={handleKeyDown}
854+
onOpenAutoFocus={(event) => event.preventDefault()}
838855
>
839856
<DialogPrimitive.Title className="sr-only">
840857
Search TanStack docs
@@ -859,8 +876,6 @@ export function SearchModal() {
859876
reset: 'p-1 opacity-50 hover:opacity-100',
860877
}}
861878
resetIconComponent={resetIconComponent}
862-
// eslint-disable-next-line jsx-a11y/no-autofocus
863-
autoFocus
864879
/>
865880
</div>
866881
<SearchResults focusedIndex={focusedIndex} />

0 commit comments

Comments
 (0)