Menhir

Checkbox Group

View as Markdown

Checkboxes that hold one list of values, with an optional parent checkbox that checks all or none. The parent is mixed while some are checked. It renders a native <fieldset>, named by its <legend>, and each box is a Checkbox: the form receives one entry per checked box under the group's name. Tab moves between the boxes, and Space checks one, as on a Checkbox.

Size
2.4 kB

Example

Styling
Toppings
CheckboxGroupExample.tsx
import { CheckboxGroup } from "menhir/checkbox-group";
import { For } from "solid-js";

const toppings = ["Mushrooms", "Olives", "Peppers"];

const box =
  "inline-flex size-5 cursor-pointer items-center justify-center rounded border border-stone-400 text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-checked:border-transparent data-checked:bg-stone-800 data-indeterminate:border-transparent data-indeterminate:bg-stone-800 dark:border-stone-500 dark:text-stone-900 dark:data-checked:bg-stone-300 dark:data-indeterminate:bg-stone-300";
// A check from two borders of a rotated box, a dash from one.
const mark =
  "hidden border-0 border-current data-checked:block data-checked:h-2.5 data-checked:w-1.5 data-checked:-translate-y-px data-checked:rotate-45 data-checked:border-r-2 data-checked:border-b-2 data-indeterminate:block data-indeterminate:w-2 data-indeterminate:border-b-2";

export default function CheckboxGroupExample() {
  return (
    <CheckboxGroup.Root
      name="toppings"
      defaultValue={["Olives"]}
      class="inline-flex flex-col gap-2"
    >
      <legend class="mb-2 font-semibold">Toppings</legend>
      <label class="inline-flex items-center gap-2">
        <CheckboxGroup.Parent values={toppings} class={box}>
          <CheckboxGroup.Indicator class={mark} />
        </CheckboxGroup.Parent>
        All toppings
      </label>
      <For each={toppings}>
        {(topping) => (
          <label class="ml-7 inline-flex items-center gap-2">
            <CheckboxGroup.Item value={topping} class={box}>
              <CheckboxGroup.Indicator class={mark} />
            </CheckboxGroup.Item>
            {topping}
          </label>
        )}
      </For>
    </CheckboxGroup.Root>
  );
}

Anatomy

<CheckboxGroup.Root>
  <legend />
  <CheckboxGroup.Parent>
    <CheckboxGroup.Indicator />
  </CheckboxGroup.Parent>
  <CheckboxGroup.Item>
    <CheckboxGroup.Indicator />
  </CheckboxGroup.Item>
</CheckboxGroup.Root>

API reference

Root

The group: a <fieldset>, named by a <legend> inside it, with data-disabled and data-required. A visually hidden radio after it carries required. Render it as a <div role="group"> with aria-labelledby when the group's name is outside it.

PropTypeDefaultDescription
valuestring[]—The values of the checked boxes, when controlled. Pair it with onValueChange. None checked is [].
defaultValuestring[][]The values of the boxes checked at first, when uncontrolled. A form reset goes back to it.
onValueChange(value: string[]) => void—Called with the new list when the user checks or unchecks a box, or the form is reset. A box checked is added at the end. A controlled group changes only if value does.
namestring—The name each checked box is submitted under, one entry per box: formData.getAll(name).
disabledbooleanfalseWhether every box ignores the user. The fieldset is disabled too, with every control inside it, and the form submits no box.
requiredbooleanfalseWhether the form can be submitted only while at least one box is checked. No box is required on its own. It counts the value, so a checked box that is disabled satisfies it although the form does not submit it: make a box that must stay checked readOnly instead.
formstring—The id of the form the boxes belong to, when the group is not inside it.
render(props) => JSX.Element—Renders the group as another element, such as a <div role="group">. Read once.

Item

One box: a Checkbox.Root, checked while the group's value has its value, and submitted under the group's name. It takes every prop of Checkbox.Root but the value props, which are the group's, and name, required and form.

PropTypeDefaultDescription
valuestring—Identifies the box in the group's value, and is what the form submits while it is checked.
disabledbooleanfalseWhether this box ignores the user. A disabled box keeps its place in the value, but the form does not submit it.
readOnlybooleanfalseWhether this box can be focused but not changed. Unlike a disabled box, the form submits it: use it for a box that must stay checked, such as essential cookies.

Parent

A Checkbox.Root over the boxes of values: checked while the group has all of them, aria-checked="mixed" and data-indeterminate while it has some. A click on a mixed parent checks all, on a checked one unchecks all, and on an unchecked one brings back the boxes that were checked when it was last mixed, or checks all. It submits nothing. Leave it out, and the group does not bundle it.

PropTypeDefaultDescription
valuesstring[]—The values of the boxes the parent checks and unchecks, usually every Item's. Other values in the group's value are kept.
disabledbooleanfalseWhether the parent ignores the user.

Indicator

Checkbox.Indicator: the check mark, a <span> with its box's data-checked, data-unchecked or data-indeterminate. Place it inside an Item or the Parent.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the indicator with your own component.

Keyboard

KeysAction
SpaceChecks or unchecks the focused box.
TabMoves focus to the next box, or out of the group.

In a Field

The group joins a Field as Field.Control's render: the fieldset gets the Field's description and error in aria-describedby, aria-invalid while the error shows, and the Field's name and disabled, which the group passes to its boxes. Name the group with a <legend>, not Field.Label, which cannot name a fieldset, and put the Error inside it. With required, the browser blocks a submission while no box is checked and focuses the first box; the error shows once focus has left the group, a box has changed or a submission was attempted. The browser's message asks to select one of the options; give the Error match="valueMissing" and your own words to say which.

import { CheckboxGroup } from "menhir/checkbox-group";
import { Field } from "menhir/field";
import { For } from "solid-js";

const topics = ["Releases", "Security notices", "Events"];

export default function CheckboxGroupInField() {
  return (
    <Field.Root name="topics">
      <Field.Control render={CheckboxGroup.Root} required>
        <legend>Email me about</legend>
        <Field.Description>Choose at least one.</Field.Description>
        <For each={topics}>
          {(topic) => (
            <label>
              <CheckboxGroup.Item value={topic}>
                <CheckboxGroup.Indicator />
              </CheckboxGroup.Item>
              {topic}
            </label>
          )}
        </For>
        <Field.Error match="valueMissing">Choose a topic to go on.</Field.Error>
      </Field.Control>
    </Field.Root>
  );
}