Skip to content

Commit bd8e88d

Browse files
authored
Trigger scrollIntoView effect when position changes (#1113)
* trigger scrollIntoView effect when position changes This is important otherwise it could happen that the current active item is still the active item even if we inserted X items before the current one. This will result in the active item being out of the current viewport. To fix this, we will also make sure to trigger the effect if the position of the active item changes. * update changelog
1 parent fd0575b commit bd8e88d

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Improve build files ([#1078](https://github.com/tailwindlabs/headlessui/pull/1078))
1717
- Ensure typeahead stays on same item if it still matches ([#1098](https://github.com/tailwindlabs/headlessui/pull/1098))
1818
- Fix off-by-one frame issue causing flicker ([#1111](https://github.com/tailwindlabs/headlessui/pull/1111))
19+
- Trigger scrollIntoView effect when position changes ([#1113](https://github.com/tailwindlabs/headlessui/pull/1113))
1920

2021
### Added
2122

packages/@headlessui-react/src/components/combobox/combobox.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,12 @@ function Option<
860860
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })
861861
})
862862
return d.dispose
863-
}, [id, active, state.comboboxState])
863+
}, [
864+
id,
865+
active,
866+
state.comboboxState,
867+
/* We also want to trigger this when the position of the active item changes so that we can re-trigger the scrollIntoView */ state.activeOptionIndex,
868+
])
864869

865870
let handleClick = useCallback(
866871
(event: { preventDefault: Function }) => {

packages/@headlessui-react/src/components/listbox/listbox.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,12 @@ function Option<
670670
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })
671671
})
672672
return d.dispose
673-
}, [id, active, state.listboxState])
673+
}, [
674+
id,
675+
active,
676+
state.listboxState,
677+
/* We also want to trigger this when the position of the active item changes so that we can re-trigger the scrollIntoView */ state.activeOptionIndex,
678+
])
674679

675680
let handleClick = useCallback(
676681
(event: { preventDefault: Function }) => {

packages/@headlessui-react/src/components/menu/menu.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,12 @@ function Item<TTag extends ElementType = typeof DEFAULT_ITEM_TAG>(
539539
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })
540540
})
541541
return d.dispose
542-
}, [id, active, state.menuState])
542+
}, [
543+
id,
544+
active,
545+
state.menuState,
546+
/* We also want to trigger this when the position of the active item changes so that we can re-trigger the scrollIntoView */ state.activeItemIndex,
547+
])
543548

544549
let bag = useRef<MenuItemDataRef['current']>({ disabled })
545550

0 commit comments

Comments
 (0)