Fieldset
Groups related form controls under a legend.
There is no WAI-ARIA APG pattern for fieldsets. Fieldset renders the native <fieldset> and <legend> elements, so the group gets role="group" and is named by its legend.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Fieldset } from '@leve-ui/fieldset';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Fieldset.Root class="fieldset-default">
<Fieldset.Legend class="fieldset-default-legend">Shipping method</Fieldset.Legend>
<label class="fieldset-default-option">
<input class="fieldset-default-radio" type="radio" name="shipping" value="standard" />{' '}
Standard
</label>
<label class="fieldset-default-option">
<input class="fieldset-default-radio" type="radio" name="shipping" value="express" />{' '}
Express
</label>
</Fieldset.Root>
);
});.fieldset-default {
display: flex;
flex-direction: column;
gap: 10px;
width: 280px;
margin: 0;
padding: 0;
border: none;
font-family: inherit;
}
.fieldset-default-legend {
padding: 0;
margin-bottom: 10px;
font-size: 14px;
font-weight: 600;
color: light-dark(#0f172a, #ffffff);
}
/* Each option is a selectable card */
.fieldset-default-option {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.fieldset-default-option:has(input:checked) {
border-color: #009dfd;
background: light-dark(#daf0ff, #009dfd26);
}
.fieldset-default-radio {
width: 16px;
height: 16px;
margin: 0;
accent-color: #009dfd;
}
.fieldset-default-radio:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/fieldsetnpm install @leve-ui/fieldsetyarn add @leve-ui/fieldsetbun add @leve-ui/fieldsetpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Fieldset } from '@leve-ui/fieldset'; // or '@leve-ui/core'
<Fieldset.Root>
<Fieldset.Legend />
</Fieldset.Root>
| Part | Element | Description |
|---|---|---|
Fieldset.Root | <fieldset> | Groups the form controls. |
Fieldset.Legend | <legend> | The label of the group. Place it as the first child of the Root. |
Examples#
Disabled#
disabled on the Root disables every form control inside it, like the native <fieldset disabled>. The leve-ui controls inside it (Checkbox, Switch, Radio Group, Toggle, Toggle Group and Field) are disabled too: they render data-disabled and ignore clicks. As in HTML, controls inside the Legend are not disabled by its own Fieldset, and a nested Fieldset is disabled when an ancestor is.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Fieldset } from '@leve-ui/fieldset';
import { Switch } from '@leve-ui/switch';
import styles from './disabled.css?inline';
export default component$(() => {
useStyles$(styles);
// A disabled Fieldset also disables the leve-ui controls inside it
return (
<Fieldset.Root disabled class="fieldset-disabled">
<Fieldset.Legend class="fieldset-disabled-legend">Notifications</Fieldset.Legend>
<Switch.Root defaultChecked class="fieldset-disabled-switch">
<Switch.Label class="fieldset-disabled-label">Email</Switch.Label>
<Switch.Control class="fieldset-disabled-control">
<Switch.Thumb class="fieldset-disabled-thumb" />
</Switch.Control>
</Switch.Root>
<Switch.Root class="fieldset-disabled-switch">
<Switch.Label class="fieldset-disabled-label">Push</Switch.Label>
<Switch.Control class="fieldset-disabled-control">
<Switch.Thumb class="fieldset-disabled-thumb" />
</Switch.Control>
</Switch.Root>
</Fieldset.Root>
);
});.fieldset-disabled {
font-family: inherit;
display: flex;
flex-direction: column;
gap: 12px;
width: 240px;
margin: 0;
padding: 0;
border: none;
}
.fieldset-disabled-legend {
padding: 0;
margin-bottom: 4px;
font-size: 14px;
font-weight: 600;
color: light-dark(#0f172a, #ffffff);
}
.fieldset-disabled-legend[data-disabled] {
opacity: 0.5;
}
.fieldset-disabled-switch {
/* HiddenInput is absolutely positioned inside Root */
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.fieldset-disabled-label {
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.fieldset-disabled-control {
display: inline-flex;
align-items: center;
flex-shrink: 0;
width: 36px;
height: 20px;
padding: 2px;
border: none;
border-radius: 9999px;
background: light-dark(#cbd5e1, #ffffff3d);
cursor: pointer;
}
.fieldset-disabled-control[data-state="checked"] {
background: #009dfd;
}
.fieldset-disabled-control:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.fieldset-disabled-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #ffffff;
box-shadow: 0 1px 2px rgb(15 23 42 / 0.2);
transition: transform 150ms ease;
}
.fieldset-disabled-thumb[data-state="checked"] {
transform: translateX(16px);
}
/* The Fieldset passes its disabled state to the switches */
.fieldset-disabled-label[data-disabled],
.fieldset-disabled-control[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}API Reference#
Root#
No props of its own. Accepts all <fieldset> attributes.
| Data attribute | Value |
|---|---|
data-leve-fieldset | Always present |
data-disabled | Present when disabled |
All props, including disabled, are passed to the native <fieldset>.
Legend#
No props of its own. Accepts all <legend> attributes.
| Data attribute | Value |
|---|---|
data-leve-fieldset-legend | Always present |
data-disabled | Present when the Root is disabled |
Accessibility#
- The native
<fieldset>has the implicitrole="group", andFieldset.Legendgives the group its accessible name. - When the Root is disabled, the controls inside it are disabled and skipped by Tab: native controls by the browser, leve-ui controls through the Fieldset.
- Fieldset has no keyboard interaction of its own.