-
-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filtering & scrolling into view dysfunction 馃 #233
Comments
Hey @najdic, I've encountered the same issue, it seems to occurs whenever the list trigger the overflow. I haven't looked into it more but I'll share the workaround i'm using for now. // 1. create a ref
const listRef = useRef<HTMLDivElement>(null);
const scrollId = useRef<ReturnType<typeof setTimeout>>();
// 2. attach it to your list
<Command.List ref={listRef}>
// 3. listen for value changes on the input
<Command.Input onValueChange={() => {
// clear pending scroll
clearTimeout(scrollId.current);
// the setTimeout is used to create a new task
// this is to make sure that we don't scroll until the user is done typing
// you can tweak the timeout duration ofc
scrollId.current = setTimeout(() => {
// inside your list select the first group and scroll to the top
listRef.current?.querySelector("[cmdk-group]")?.scrollTo({ top: 0 });
}, 0);
}}
/> |
For anybody using shadcn/ui, you can use this onValueChange function instead: // clear pending scroll
clearTimeout(scrollId.current);
// the setTimeout is used to create a new task
// this is to make sure that we don't scroll until the user is done typing
// you can tweak the timeout duration ofc
scrollId.current = setTimeout(() => {
// inside your list select the first group and scroll to the top
const div = listRef.current;
div?.scrollTo({ top: 0 });
}, 0); |
@dphuang2 could you share a component with this implemented? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
First and foremost, I want to express my appreciation for the tremendous work on cmdk 馃殌
However, I've encountered a potential issue involving filtering and/or scrolling into view functionality in my recent use case. While the library generally performs admirably, I've noticed some unexpected behavior when filtering "large" datasets, seems like the selected element is not always in the view 馃
Sandbox: https://codesandbox.io/p/sandbox/beautiful-khayyam-chp8y3
CMDK Version:
^1.0.0
Screen.Recording.2024-03-11.at.15.17.02.mov
The text was updated successfully, but these errors were encountered: