cmdk icon indicating copy to clipboard operation
cmdk copied to clipboard

Accessible without Input

Open zeroliu opened this issue 11 months ago • 5 comments

Hi! I'm building Select/MultiSelect components using cmdk (coming from shadcn/ui). I wonder whether the input is considered a required component in the command tree.

Here's an example without Input. The component cannot be focused any more without input, thus, the list cannot be controlled by keyboard any more. https://codesandbox.io/p/sandbox/eloquent-ishizaka-2tlr9l?file=/src/App.tsx:9,30

zeroliu avatar Feb 28 '24 06:02 zeroliu

+1

Encountering a similar issue. Keyboard navigation with arrow keys does not work when input is not selected, so would make sense that it also wouldn't work when the component doesn't exist

Only8Bytes avatar Mar 01 '24 14:03 Only8Bytes

turns out if you have the #cmdk-root (or any element inside it) focused the navigation will work, so if you dont put a Input component you'll have to manually focus the div.

if youre using @radix-ui/react-popover as shown in documentation you should try adding asChild prop to Popover.Content. Radix behavior is to auto focus the popover content.

i believe it would be useful to have this info in the readme.

athenacfr avatar Mar 03 '24 21:03 athenacfr

turns out if you have the #cmdk-root (or any element inside it) focused the navigation will work, so if you dont put a Input component you'll have to manually focus the div.

if youre using @radix-ui/react-popover as shown in documentation you should try adding asChild prop to Popover.Content. Radix behavior is to auto focus the popover content.

i believe it would be useful to have this info in the readme.

Could you please provide some simplified example? I am having trouble making it work according to your way.

skelawsky avatar Mar 06 '24 23:03 skelawsky

turns out if you have the #cmdk-root (or any element inside it) focused the navigation will work, so if you dont put a Input component you'll have to manually focus the div. if youre using @radix-ui/react-popover as shown in documentation you should try adding asChild prop to Popover.Content. Radix behavior is to auto focus the popover content. i believe it would be useful to have this info in the readme.

Could you please provide some simplified example? I am having trouble making it work according to your way.

@skelawsky I added tabIndex={0} to the <Command /> component, that allows the command to be focused, and the keyboard nav to work.

        <Command
          tabIndex={0}
          className="outline-none" //hides focused outline
        >

callumbooth avatar Mar 19 '24 15:03 callumbooth

turns out if you have the #cmdk-root (or any element inside it) focused the navigation will work, so if you dont put a Input component you'll have to manually focus the div. if youre using @radix-ui/react-popover as shown in documentation you should try adding asChild prop to Popover.Content. Radix behavior is to auto focus the popover content. i believe it would be useful to have this info in the readme.

Could you please provide some simplified example? I am having trouble making it work according to your way.

@skelawsky I added tabIndex={0} to the <Command /> component, that allows the command to be focused, and the keyboard nav to work.

        <Command
          tabIndex={0}
          className="outline-none" //hides focused outline
        >

Thanks for this solution worked really well 🥇

joaopedrodcf avatar Apr 16 '24 16:04 joaopedrodcf