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

Use native fieldset instead of div by default for <Fieldset /> component #3237

Merged
merged 11 commits into from
May 24, 2024

Conversation

RobinMalfait
Copy link
Collaborator

@RobinMalfait RobinMalfait commented May 24, 2024

This PR improves the default behavior of our <Fieldset /> component.

Initially we used a div because fieldset / legend is hard to style otherwise. However, a fieldset on its own is still useful and has some benefits (such as disabling all native form elements if the fieldset itself is disabled).

We still setup an internal DisabledProvider such that our own components can read from this state and disable themselves accordingly.

This is technically a breaking change, but I doubt that anyone runs into this issue.

If so, you can apply the following diff to fix it:

- <Fieldset>
+ <Fieldset as="div">
    {/* … */}
  </Fieldset>

Note: if you were already using disabled on the <Fieldset disabled>, and you use as="div" then you won't have the default behavior where it disables these form elements.


Implementation looks a bit more complex, but essentially added new functionality to compute what the final tagName is of a component and based on this information we know which props to render. If a native fieldset is used, we can use the disabled prop, if another element is used we can render aria-disabled and a role="group".

The new internal useResolveTag will compute the tag name, and it defaults to the props.as value.

Copy link

vercel bot commented May 24, 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 May 24, 2024 2:59pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 24, 2024 2:59pm

This allows us to compute the `tag` name of a component. We can use a
shortcut based on the `props.as` and/or the `DEFAULT_XXX_TAG` of a
component. If this is not known/passed, then we compute it based on the
`ref` instead which requires an actual re-render.
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

1 participant