Skip to content

Commit fd0575b

Browse files
Fix off-by-one frame issue causing flicker (#1111)
* Fix active item flicker * apply `nextFrame` -> `requestAnimationFrame` fix to other components * update changelog Co-authored-by: Robin Malfait <[email protected]>
1 parent 53af7fa commit fd0575b

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Improve overal codebase, use modern tech like `esbuild` and TypeScript 4! ([#1055](https://github.com/tailwindlabs/headlessui/pull/1055))
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))
18+
- Fix off-by-one frame issue causing flicker ([#1111](https://github.com/tailwindlabs/headlessui/pull/1111))
1819

1920
### Added
2021

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,9 @@ function Option<
856856
if (state.comboboxState !== ComboboxStates.Open) return
857857
if (!active) return
858858
let d = disposables()
859-
d.nextFrame(() => document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' }))
859+
d.requestAnimationFrame(() => {
860+
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })
861+
})
860862
return d.dispose
861863
}, [id, active, state.comboboxState])
862864

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,9 @@ function Option<
666666
if (state.listboxState !== ListboxStates.Open) return
667667
if (!active) return
668668
let d = disposables()
669-
d.nextFrame(() => document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' }))
669+
d.requestAnimationFrame(() => {
670+
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })
671+
})
670672
return d.dispose
671673
}, [id, active, state.listboxState])
672674

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ function Item<TTag extends ElementType = typeof DEFAULT_ITEM_TAG>(
535535
if (state.menuState !== MenuStates.Open) return
536536
if (!active) return
537537
let d = disposables()
538-
d.nextFrame(() => document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' }))
538+
d.requestAnimationFrame(() => {
539+
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })
540+
})
539541
return d.dispose
540542
}, [id, active, state.menuState])
541543

0 commit comments

Comments
 (0)