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

Prefer incoming data-* attributes, over the ones set by Headless UI #3035

Merged
merged 2 commits into from
Mar 14, 2024

Conversation

RobinMalfait
Copy link
Collaborator

@RobinMalfait RobinMalfait commented Mar 14, 2024

Typically when Headless UI components set certain props on a component, then they are there to make sure that the component behaves as expected. E.g.: aria-* attributes, role attributes, tabIndex, etc.

However, we also provide helpful data-focus or data-hover attributes that make styling components easier. These attributes are not necessary to make the component behave as expected.

This brings us to the problem that this PR solves. Sometimes when you use one of Headless UI's components, and you provide your own component that already contains the same data-* attributes, then your attributes will be overridden.

This PR solves that and prefers the existing data-* attributes over the ones we set in Headless UI.

This is currently only implemented when you can't really control the props when using as={Fragment}.

<MenuButton as={Fragment}>
  <button data-hover={hovered}>My menu button</button>
</MenuButton>

In this scenario the data-hover based on the hovered variable that can come from anywhere will be preferred compared to what MenuButton provides.

If the component already had `data-foo`, and we set `data-foo` then the
`data-foo` that was already there should stay.
Copy link

vercel bot commented Mar 14, 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 Mar 14, 2024 3:31pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 3:31pm

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.

None yet

2 participants