diff --git a/CHANGELOG.md b/CHANGELOG.md index b4e5030..4394566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve build files ([#1078](https://github.com/tailwindlabs/headlessui/pull/1078)) - Ensure typeahead stays on same item if it still matches ([#1098](https://github.com/tailwindlabs/headlessui/pull/1098)) - Fix off-by-one frame issue causing flicker ([#1111](https://github.com/tailwindlabs/headlessui/pull/1111)) +- Trigger scrollIntoView effect when position changes ([#1113](https://github.com/tailwindlabs/headlessui/pull/1113)) ### Added diff --git a/packages/@headlessui-react/src/components/combobox/combobox.tsx b/packages/@headlessui-react/src/components/combobox/combobox.tsx index e99a3e3..3c6c114 100644 --- a/packages/@headlessui-react/src/components/combobox/combobox.tsx +++ b/packages/@headlessui-react/src/components/combobox/combobox.tsx @@ -860,7 +860,12 @@ function Option< document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' }) }) return d.dispose - }, [id, active, state.comboboxState]) + }, [ + id, + active, + state.comboboxState, + /* We also want to trigger this when the position of the active item changes so that we can re-trigger the scrollIntoView */ state.activeOptionIndex, + ]) let handleClick = useCallback( (event: { preventDefault: Function }) => { diff --git a/packages/@headlessui-react/src/components/listbox/listbox.tsx b/packages/@headlessui-react/src/components/listbox/listbox.tsx index fce0f5f..e4abbd3 100644 --- a/packages/@headlessui-react/src/components/listbox/listbox.tsx +++ b/packages/@headlessui-react/src/components/listbox/listbox.tsx @@ -670,7 +670,12 @@ function Option< document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' }) }) return d.dispose - }, [id, active, state.listboxState]) + }, [ + id, + active, + state.listboxState, + /* We also want to trigger this when the position of the active item changes so that we can re-trigger the scrollIntoView */ state.activeOptionIndex, + ]) let handleClick = useCallback( (event: { preventDefault: Function }) => { diff --git a/packages/@headlessui-react/src/components/menu/menu.tsx b/packages/@headlessui-react/src/components/menu/menu.tsx index 6df8751..edb6932 100644 --- a/packages/@headlessui-react/src/components/menu/menu.tsx +++ b/packages/@headlessui-react/src/components/menu/menu.tsx @@ -539,7 +539,12 @@ function Item( document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' }) }) return d.dispose - }, [id, active, state.menuState]) + }, [ + id, + active, + state.menuState, + /* We also want to trigger this when the position of the active item changes so that we can re-trigger the scrollIntoView */ state.activeItemIndex, + ]) let bag = useRef({ disabled })