Checkbox Group
Checkboxes sharing one value.
Anatomy
A Checkbox Group wraps a set of Checkbox parts. Import both and nest the checkboxes inside the group:
<script>
import { Checkbox } from '@shardsui/svelte/checkbox'
import { CheckboxGroup } from '@shardsui/svelte/checkbox-group'
</script>
<CheckboxGroup>
<Checkbox.Root />
</CheckboxGroup>Usage guidelines
- Form controls must have an accessible name: name them with
<label>elements, or with theFieldandFieldsetcomponents.
Examples
Labeling a checkbox group
Point the group at a sibling label with aria-labelledby:
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">
<!-- checkboxes -->
</CheckboxGroup>For the individual checkboxes, the simplest option is to wrap each one in a <label>:
<label>
<Checkbox.Root value="http" />
HTTP
</label>Rendering as a native button
Checkbox.Root renders a <span> by default so it can live inside a <label>. When each checkbox has its own label tied to it with for/id, render it as a native button with as="button":
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">
<div>
<label for="protocol-http">HTTP</label>
<Checkbox.Root id="protocol-http" value="http" as="button">
<Checkbox.Indicator />
</Checkbox.Root>
</div>
</CheckboxGroup>Form integration
Combine Field and Fieldset to label the group and hook it into a form:
<Field.Root name="allowedNetworkProtocols">
<Fieldset.Root>
<Fieldset.Legend>Allowed network protocols</Fieldset.Legend>
<CheckboxGroup>
<label>
<Checkbox.Root value="http" />
HTTP
</label>
<label>
<Checkbox.Root value="https" />
HTTPS
</label>
<label>
<Checkbox.Root value="ssh" />
SSH
</label>
</CheckboxGroup>
</Fieldset.Root>
</Field.Root>