Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only scroll into view on first render and via keyboard #135

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
if (value !== undefined) {
const v = value.trim().toLowerCase()
state.current.value = v
schedule(6, scrollSelectedIntoView)
store.emit()
}
}, [value])

useLayoutEffect(() => {
schedule(6, scrollSelectedIntoView)
}, [])

const store: Store = React.useMemo(() => {
return {
subscribe: (cb) => {
Expand All @@ -197,15 +200,16 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
sort()
schedule(1, selectFirstItem)
} else if (key === 'value') {
// opts is a boolean referring to whether it should NOT be scrolled into view
if (!opts) {
// Scroll the selected item into view
schedule(5, scrollSelectedIntoView)
}
if (propsRef.current?.value !== undefined) {
// If controlled, just call the callback instead of updating state internally
const newValue = (value ?? '') as string
propsRef.current.onValueChange?.(newValue)
return
// opts is a boolean referring to whether it should NOT be scrolled into view
} else if (!opts) {
// Scroll the selected item into view
schedule(5, scrollSelectedIntoView)
}
}

Expand Down