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 (