pacocoursey / cmdk

Fast, unstyled command menu React component.
https://cmdk.paco.me
MIT License
9.03k stars 258 forks source link

bug (?): scroll position inside list is not reset when clearing the list #234

Open jimniels opened 3 months ago

jimniels commented 3 months ago

I'm not sure if this is intended behavior, but when you filter down the list to an item that's near the bottom of the list, then clear your search filter, all the results return but the scroll position of the list is not reset to the top.

Take a look at the example from the project's home page:

https://github.com/pacocoursey/cmdk/assets/1316441/02e688f8-725b-4606-9506-ecadfbbddbea

This seems unexpected to me. Whenever I clear my search query, I expect to the scroll position of the results list to always be reset to the top of the list.

triptu commented 1 month ago

workaround i'm using for this -

const listRef = useRef<HTMLDivElement>(null);

const scrollUpWhenCleared = useCallback((value: string) => {
    if (value === "") {
      requestAnimationFrame(() => {
        listRef.current?.scrollTo({ top: 0 });
      });
    }
  }, []);

<Command.List ref={listRef}>

<Command.Input  onValueChange={scrollUpWhenCleared} >...

https://github.com/pacocoursey/cmdk/issues/233 looks similar.