tailwindlabs / headlessui

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
https://headlessui.com
MIT License
26.21k stars 1.09k forks source link

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

Closed RobinMalfait closed 5 months ago

RobinMalfait commented 5 months ago

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.

vercel[bot] commented 5 months ago

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