Skip to main content

Checkbox

Checkbox is a form control that lets people select one or more options, such as accepting terms or choosing notification preferences.

Import

import { Checkbox } from '@vortexlabs/vortex-native';
import type { CheckboxProps } from '@vortexlabs/vortex-native';

Other links

import { Checkbox } from "@vortexlabs/vortex-native";

<Checkbox
  label="Email updates"
  size="md"
  layout="default"
  error={false}
  disabled={false}
/>

Features

  • Supports checked, unchecked, and mixed states.
  • Groups multiple selections in one value array.
  • Supports list rows and cards.

Anatomy

<Checkbox.Group>
  <Checkbox.GroupLabel>Notifications</Checkbox.GroupLabel>
  <Checkbox.GroupDescription>Choose all that apply.</Checkbox.GroupDescription>
  <Checkbox.Item value="email" label="Email" />
  <Checkbox.Item value="push" label="Push" />
</Checkbox.Group>
Component Part
Description
Holds the selected values.
Displays a heading for the options.
Displays instructions shared by the options.
Adds or removes its value from the selection.

Initial State and Helper Text

Use defaultChecked to set the initial state of an uncontrolled checkbox. Use helperText for supporting information.

<Checkbox
  label="Product updates"
  helperText="Email me when a new version is available."
  defaultChecked
/>

Sizes

Use size in the default layout. Pass a breakpoint object to change size with the window width.

<Stack gap={2}>
  <Checkbox size="md" label="Email reminders" />
  <Checkbox size={{ xs: "md", md: "lg" }} label="Calendar reminders" />
</Stack>

Checkbox.Group

Use a distinct string value for each item. Set defaultValue on the group to choose the initial selections.

<Checkbox.Group name="alerts" defaultValue={["email"]}>
  <Checkbox.GroupLabel>Notify me by</Checkbox.GroupLabel>
  <Checkbox.GroupDescription>Choose all that apply.</Checkbox.GroupDescription>
  <Checkbox.Item value="email" label="Email" />
  <Checkbox.Item value="push" label="Push notification" />
</Checkbox.Group>

List Rows and Cards

Use layout="list" for full-width rows with dividers. Use horizontal cards for short options that share the available width.

<Stack gap={4}>
  <Checkbox.Group layout="list" defaultValue={["email"]}>
    <Checkbox.GroupLabel>Send updates by</Checkbox.GroupLabel>
    <Checkbox.Item value="email" label="Email" />
    <Checkbox.Item value="push" label="Push notification" />
  </Checkbox.Group>
  <Checkbox.Group layout="card" direction="horizontal" defaultValue={["pdf"]}>
    <Checkbox.GroupLabel>Export formats</Checkbox.GroupLabel>
    <Checkbox.Item value="pdf" label="PDF" control={false} />
    <Checkbox.Item value="csv" label="CSV" control={false} />
  </Checkbox.Group>
</Stack>

Mixed State

Set indeterminate on a parent checkbox when only some options are selected. Update the group’s value to select or clear them together.

import { Checkbox, Stack } from "@vortexlabs/vortex-native";
import { useState } from "react";

export const ReminderChannels = () => {
  const options = ["Email", "Push"];
  const [selected, setSelected] = useState<string[]>(["Email"]);

  return (
    <Stack gap={2}>
      <Checkbox
        label="All reminders"
        checked={selected.length === options.length}
        indeterminate={selected.length > 0 && selected.length < options.length}
        onChange={({ checked }) => setSelected(checked ? options : [])}
      />
      <Checkbox.Group ariaLabel="Reminder channels" value={selected} onChange={({ value }) => setSelected(value)}>
        {options.map((channel) => (
          <Checkbox.Item key={channel} value={channel} label={channel} />
        ))}
      </Checkbox.Group>
    </Stack>
  );
};

Read the selection from a descendant with useCheckboxGroup.

Disabled and Error States

Set disabled on an item to prevent changes. Set error on a group and use errorText to explain the fix.

import { Checkbox } from "@vortexlabs/vortex-native";
import { useState } from "react";

export const ContactPreferences = () => {
  const [channels, setChannels] = useState<string[]>([]);

  return (
    <Checkbox.Group
      value={channels}
      error={channels.length === 0}
      errorText="Choose at least one channel."
      onChange={({ value }) => setChannels(value)}
    >
      <Checkbox.GroupLabel>Contact preferences</Checkbox.GroupLabel>
      <Checkbox.Item value="email" label="Email" />
      <Checkbox.Item value="sms" label="SMS" disabled helperText="Add a phone number to enable SMS." />
    </Checkbox.Group>
  );
};

Accessibility

Description
Each checkbox exposes its checked, unchecked, or mixed state. Use ariaLabel on a group when it needs an explicit accessible name.
Provide label or, for a checkbox without visible text, ariaLabel.