Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop propagation on the Popover Button #1263

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `multi` value support for Listbox & Combobox ([#1243](https://github.com/tailwindlabs/headlessui/pull/1243))
- Improve Combobox Input value ([#1248](https://github.com/tailwindlabs/headlessui/pull/1248))
- Fix Tree-shaking support ([#1247](https://github.com/tailwindlabs/headlessui/pull/1247))
- Stop propagation on the Popover Button ([#1263](https://github.com/tailwindlabs/headlessui/pull/1263))

### Added

Expand Down Expand Up @@ -60,6 +61,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `multi` value support for Listbox & Combobox ([#1243](https://github.com/tailwindlabs/headlessui/pull/1243))
- Improve Combobox Input value ([#1248](https://github.com/tailwindlabs/headlessui/pull/1248))
- Fix Tree-shaking support ([#1247](https://github.com/tailwindlabs/headlessui/pull/1247))
- Stop propagation on the Popover Button ([#1263](https://github.com/tailwindlabs/headlessui/pull/1263))

### Added

Expand Down
2 changes: 2 additions & 0 deletions packages/@headlessui-react/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ let Button = forwardRefWithAs(function Button<TTag extends ElementType = typeof
dispatch({ type: ActionTypes.ClosePopover })
state.button?.focus() // Re-focus the original opening Button
} else {
event.preventDefault()
event.stopPropagation()
Comment on lines +464 to +465

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change totally borks us from utilizing events.
A <Popover.Button as="div"> wrapping an element now blocks any event bubbling and we cannot actually react to these events for an item wrapped in a tooltip popover.

This is a clear inversion of control problem. I do not believe that the decision to preventDefault and stopPropagation on an event is the sole responsibility of headlessui to decide.

A user could supply an onClick function that calls preventDefault or stopPropagation if they so choose to. But with this code change the end users no longer have a choice in this.

We were forced to roll back to 1.5.0

if (state.popoverState === PopoverStates.Closed) closeOthers?.(state.buttonId)
state.button?.focus()
dispatch({ type: ActionTypes.TogglePopover })
Expand Down
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/src/components/popover/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,14 @@ export let PopoverButton = defineComponent({
}
}

function handleClick() {
function handleClick(event: MouseEvent) {
if (props.disabled) return
if (isWithinPanel) {
api.closePopover()
dom(api.button)?.focus() // Re-focus the original opening Button
} else {
event.preventDefault()
event.stopPropagation()
if (api.popoverState.value === PopoverStates.Closed) closeOthers?.(api.buttonId)
dom(api.button)?.focus()
api.togglePopover()
Expand Down