From c26a7badaaafdd97e4481c6c826468c6f9b72b2a Mon Sep 17 00:00:00 2001 From: Ya-Fan Chen <20377719+Lexachoc@users.noreply.github.com> Date: Sat, 29 Aug 2026 03:47:08 +0200 Subject: [PATCH] fix: prevent Command from stealing input focus see #368 and #254 --- cmdk/src/index.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmdk/src/index.tsx b/cmdk/src/index.tsx index 3131c7b6..dcbd1cd2 100644 --- a/cmdk/src/index.tsx +++ b/cmdk/src/index.tsx @@ -205,6 +205,7 @@ const Command = React.forwardRef((props, forwarded const labelId = useId() const inputId = useId() + const rootRef = React.useRef(null) const listInnerRef = React.useRef(null) const schedule = useScheduleLayoutEffect() @@ -242,7 +243,7 @@ const Command = React.forwardRef((props, forwarded schedule(1, selectFirstItem) } else if (key === 'value') { // Force focus input or root so accessibility works - if (document.activeElement.hasAttribute('cmdk-input') || document.activeElement.hasAttribute('cmdk-root')) { + if (document.activeElement === rootRef.current) { const input = document.getElementById(inputId) if (input) input.focus() else document.getElementById(listId)?.focus() @@ -572,7 +573,7 @@ const Command = React.forwardRef((props, forwarded return (