Skip to content

First item is selected by default - can this be disabled? #171

Closed as not planned

Description

@moroshko

Just wondering whether the default behaviour of selecting the first item in the list is some sort of accessibility requirement (if so, could you please point to the docs?), and whether this is something the can be disabled?
I was looking for something like selectFirstItem={false} but couldn't find such API.

Activity

  1. JamesParkinSonos commented on Aug 22, 2023

    @JamesParkinSonos

    It would be great if there was a way to specify which item should be selected first.

    Actually, this was easy once I figured out how. You can set the value prop on the Command component to the item you want to be selected first.

  2. moshest commented on Oct 23, 2023

    @moshest

    My solution is to add a hidden value to it will be automatically selected:

    <CommandItem value="-" className="hidden" />

    The hidden class make sure it will not be visible but still controlled and selected by cmdk.

  3. sunwrobert commented on Dec 1, 2023

    @sunwrobert

    +1 on this being a somewhat arbitrary requirement. We should have the flexibility to set this

  4. moroshko commented on Mar 14, 2024

    @moroshko
    Author

    @pacocoursey Do I understand correctly that in v1.0 it's still not possible to disable the auto-select of the first item? Is it some sort of accessibility concern?

  5. vinceflores commented on Jun 6, 2024

    @vinceflores

    Heres an example where only the CommandItem selected is highlighted on render

         {repos?.map((repo: string, i: number) => {
                  const pathName = usePathname();
                  const selected = pathName === `/${repo}`;
                  return (
                    <Link key={i} href={`/${repo}`}>
                      <CommandItem
                        value={selected? "-": ""}
                        className={`${
                          selected ? "bg-accent" : "bg-background"
                        } `}
                      >
                        {repo}
                      </CommandItem>
                    </Link>
                  );
                })}
    
  6. andrecasal commented on Jul 2, 2024

    @andrecasal

    If the component sits below the fold, the page will always scroll to the first selected item, which is a pain. Any way to disable any kind of selection on render would be much appreciated.

  7. AMerkuri commented on Sep 11, 2024

    @AMerkuri

    This solution preserves selected value and allows keyboard navigation.

    <Command defaultValue={value} disablePointerSelection={true}>

  8. maerzhase commented on Oct 18, 2024

    @maerzhase

    would be cool to get an API for this. selectFirstItem={false} would do the trick.
    Example where it comes in handy: Let's say you use the UI for a search input that can display some suggestions. You want to write into the input and submit the search query only OR you wanna select an item (via keyboard or mouse) and trigger the suggestion.

  9. zachlankton commented on Nov 14, 2024

    @zachlankton

    Monkey Patch to the rescue for me:

     if (typeof window !== "undefined") {
      const originalScrollIntoView = Element.prototype.scrollIntoView;
    
      Element.prototype.scrollIntoView = function () {
        if (!this.hasAttribute("cmdk-item")) {
          originalScrollIntoView.call(this);
        }
      };
    }
  10. Unsleeping commented on Feb 11, 2025

    @Unsleeping

    My solution is to add a hidden value to it will be automatically selected:

    The hidden class make sure it will not be visible but still controlled and selected by `cmdk`.

    This works fine, but <CommandEmpty> is not available with this fix.

    Btw, I found a solution for this, i just remove hidden <CommandItem/> to display <CommandEmpty/>

    {!selectFirstItem && filteredOptions.length !== 0 && (
        <CommandItem value='-' className='hidden' />
    )}
    
  11. ako-v commented on Feb 25, 2025

    @ako-v

    This solution preserves selected value and allows keyboard navigation.

    <Command defaultValue={value} disablePointerSelection={true}>

    This is the solution to the problem, is sets the data-selected and aria-selected to true on the selected one. but we do not need to disable disablePointerSelection. just set the defaultValue.
    if you want to disable the selection of the first item just set the default value to a non existing value, but not undefined

    <Command defaultValue={"-"} >

  12. ryantbrown commented on Mar 1, 2025

    @ryantbrown

    This solution preserves selected value and allows keyboard navigation.
    <Command defaultValue={value} disablePointerSelection={true}>

    This is the solution to the problem, is sets the data-selected and aria-selected to true on the selected one. but we do not need to disable disablePointerSelection. just set the defaultValue. if you want to disable the selection of the first item just set the default value to a non existing value, but not undefined

    <Command defaultValue={"-"} >

    This worked for me

  13. sirajtahra commented on Apr 14, 2025

    @sirajtahra

    +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions