Skip to content

Commit 7267cc4

Browse files
Keep <Combobox /> open when clicking in scrollbar on <ComboboxOptions> (#3249)
* keep `<Combobox />` open when clicking in scrollbar on `<ComboboxOptions>` * update changelog * Update packages/@headlessui-react/CHANGELOG.md Co-authored-by: Adam Wathan <[email protected]> * Update packages/@headlessui-react/src/components/combobox/combobox.tsx Co-authored-by: Adam Wathan <[email protected]> * format comment --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent e8c7661 commit 7267cc4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Add ability to render multiple `<Dialog />` components at once (without nesting them) ([#3242](https://github.com/tailwindlabs/headlessui/pull/3242))
1313

14+
### Fixed
15+
16+
- Keep `<Combobox />` open when clicking scrollbar in `<ComboboxOptions>` ([#3249](https://github.com/tailwindlabs/headlessui/pull/3249))
17+
1418
## [2.0.4] - 2024-05-25
1519

1620
### Fixed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,18 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
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.
1687+
let handleMouseDown = useEvent((event: ReactMouseEvent) => {
1688+
event.preventDefault()
1689+
})
1690+
16791691
let ourProps = mergeProps(anchor ? getFloatingPanelProps() : {}, {
16801692
'aria-labelledby': labelledBy,
16811693
role: 'listbox',
@@ -1688,6 +1700,7 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
16881700
'--button-width': useElementSize(data.buttonRef, true).width,
16891701
} as CSSProperties,
16901702
onWheel: handleWheel,
1703+
onMouseDown: handleMouseDown,
16911704
})
16921705

16931706
// Map the children in a scrollable container when virtualization is enabled

0 commit comments

Comments
 (0)