Skip to content

Commit 025e115

Browse files
Fix visual jitter in Combobox component when using native scrollbar (#3190)
* Avoid `scrollToIndex` when using native scrollbar in `ComboBox` * format comment * set `ActivationTrigger` in mousedown If you use the scrollbar, a `mousedown` and `pointerdown` event is fired before you start interacting with the scrollbar. If you use the `scroll` event, then the callback is fired while scrolling. To improve performance a bit more, we will set the activation trigger in the `mousedown` event because we only need to set it once. If you are done scrolling, we don't reset it (we didn't do that before either), but instead we rely on other events to override the trigger (e.g.: you start using the keyboard). The big benefit is that this now only calls the `setActivationTrigger` once, instead of `n` times with the same value. * optimize `onWheel` to only trigger once This is a similar performance trick as before. We only need to set the activationTrigger once, so there is no need to keep calling this function for no reason. * update changelog --------- Co-authored-by: Robin Malfait <[email protected]>
1 parent b3a508b commit 025e115

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/@headlessui-react/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
- Keep `<Combobox />` open when clicking scrollbar in `<ComboboxOptions>` ([#3249](https://github.com/tailwindlabs/headlessui/pull/3249))
1717
- Merge incoming `style` prop on `ComboboxOptions`, `ListboxOptions`, `MenuItems`, and `PopoverPanel` components ([#3250](https://github.com/tailwindlabs/headlessui/pull/3250))
1818
- Prevent focus on `<Checkbox />` when it is `disabled` ([#3251](https://github.com/tailwindlabs/headlessui/pull/3251))
19+
- Fix visual jitter in `Combobox` component when using native scrollbar ([#3190](https://github.com/tailwindlabs/headlessui/pull/3190))
1920

2021
## [2.0.4] - 2024-05-25
2122

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,21 +1671,26 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
16711671
}, [data])
16721672

16731673
// When the user scrolls **using the mouse** (so scroll event isn't appropriate)
1674-
// we want to make sure that the current activation trigger is set to pointer
1674+
// we want to make sure that the current activation trigger is set to pointer.
16751675
let handleWheel = useEvent(() => {
16761676
actions.setActivationTrigger(ActivationTrigger.Pointer)
16771677
})
16781678

1679-
// When clicking inside of the scrollbar, a `click` event will be triggered on
1680-
// the focusable element _below_ the scrollbar. If you use a `<Combobox>`
1681-
// inside of a `<Dialog>`, clicking the scrollbar of the `<ComboboxOptions>`
1682-
// will move focus to the `<Dialog>` which blurs the `<ComboboxInput>` and
1683-
// closes the `<Combobox>`.
1684-
//
1685-
// Preventing the default behavior in the `mousedown` event (which happens
1686-
// before `click`) will prevent this issue because the `click` never fires.
16871679
let handleMouseDown = useEvent((event: ReactMouseEvent) => {
1680+
// When clicking inside of the scrollbar, a `click` event will be triggered
1681+
// on the focusable element _below_ the scrollbar. If you use a `<Combobox>`
1682+
// inside of a `<Dialog>`, clicking the scrollbar of the `<ComboboxOptions>`
1683+
// will move focus to the `<Dialog>` which blurs the `<ComboboxInput>` and
1684+
// closes the `<Combobox>`.
1685+
//
1686+
// Preventing the default behavior in the `mousedown` event (which happens
1687+
// before `click`) will prevent this issue because the `click` never fires.
16881688
event.preventDefault()
1689+
1690+
// When the user clicks in the `<Options/>`, we want to make sure that we
1691+
// set the activation trigger to `pointer` to prevent auto scrolling to the
1692+
// active option while the user is scrolling.
1693+
actions.setActivationTrigger(ActivationTrigger.Pointer)
16891694
})
16901695

16911696
let ourProps = mergeProps(anchor ? getFloatingPanelProps() : {}, {
@@ -1700,7 +1705,7 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
17001705
'--input-width': useElementSize(data.inputRef, true).width,
17011706
'--button-width': useElementSize(data.buttonRef, true).width,
17021707
} as CSSProperties,
1703-
onWheel: handleWheel,
1708+
onWheel: data.activationTrigger === ActivationTrigger.Pointer ? undefined : handleWheel,
17041709
onMouseDown: handleMouseDown,
17051710
})
17061711

0 commit comments

Comments
 (0)