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

Close the Combobox, Dialog, Listbox, Menu and Popover components when the trigger disappears #3075

Merged
merged 4 commits into from
Apr 3, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Apr 2, 2024

This PR will ensure that we don't have any floating panels when the trigger elements disappear. We already had this implementation for the Dialog component, but now this is implemented for all components that have an anchor prop.

The big problem is that with the anchor prop, each panel (e.g.: MenuItems, ListboxOptions, …) is portalled and positioned relative to the anchor element. If the anchor element disappears, the panel will still be rendered and strangely positioned in the DOM, which is not the desired behavior.

This PR solves that by:

  • Closing the Combobox, when the ComboboxInput disappears
  • Closing the Dialog, when the Dialog itself disappears (this was already implemented, but now it's using the new useOnDisappear hook)
  • Closing the Listbox, when the ListboxButton disappears
  • Closing the Menu, when the MenuButton disappears
  • Closing the Popover, when the PopoverPanel disappears

Copy link

vercel bot commented Apr 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2024 1:10pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2024 1:10pm

@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Accept optional `strategy` for the `anchor` prop ([#3034](https://github.com/tailwindlabs/headlessui/pull/3034))
- Expose `--input-width` and `--button-width` CSS variables on the `ComboboxOptions` component ([#3057](https://github.com/tailwindlabs/headlessui/pull/3057))
- Expose the `--button-width` CSS variable on the `PopoverPanel` component ([#3058](https://github.com/tailwindlabs/headlessui/pull/3058))
- Close the `Menu`, `Listbox`, `Popover` and `Dialog` components when the trigger disappears ([#3075](https://github.com/tailwindlabs/headlessui/pull/3075))
Copy link
Member

Choose a reason for hiding this comment

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

Alphabetical 🤷‍♂️

Suggested change
- Close the `Menu`, `Listbox`, `Popover` and `Dialog` components when the trigger disappears ([#3075](https://github.com/tailwindlabs/headlessui/pull/3075))
- Close the `Dialog`, `Listbox`, `Menu`, and `Popover` components when the trigger disappears ([#3075](https://github.com/tailwindlabs/headlessui/pull/3075))

Copy link
Member

Choose a reason for hiding this comment

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

Just thinking — should this behavior not be added to the Combobox as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

I completely missed this one, 100% should be added there as well!

Copy link
Member

@reinink reinink left a comment

Choose a reason for hiding this comment

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

Looks good! Mostly just wondering if we need this behavior on the Combobox as well?

@RobinMalfait RobinMalfait changed the title Close the Menu, Listbox, Popover and Dialog components when the trigger disappears Close the Combobox, Dialog, Listbox, Menu and Popover components when the trigger disappears Apr 3, 2024
@RobinMalfait RobinMalfait force-pushed the feat/close-components-on-disappear branch from 9a6e17e to c9f9182 Compare April 3, 2024 12:39
This hook allows us to trigger a callback if the element becomes
"hidden". We use the bounding client rect and check the dimensions to
know wether we are "hidden" or not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants