Skip to content

Checkbox

Checkbox component for selecting multiple options with various styles and group support.

import { Checkbox } from 'asterui'

Basic Usage

Simple checkbox with label.

import { Checkbox } from 'asterui'

function App() {
  return (
      <Checkbox>Accept terms and conditions</Checkbox>
    )
}

export default App

Colors

Different checkbox colors for various contexts.

import { Checkbox, Space } from 'asterui'

function App() {
  return (
      <Space direction="vertical" size="sm">
        <Checkbox color="primary" defaultChecked>Primary</Checkbox>
        <Checkbox color="secondary" defaultChecked>Secondary</Checkbox>
        <Checkbox color="accent" defaultChecked>Accent</Checkbox>
        <Checkbox color="success" defaultChecked>Success</Checkbox>
        <Checkbox color="warning" defaultChecked>Warning</Checkbox>
        <Checkbox color="info" defaultChecked>Info</Checkbox>
        <Checkbox color="error" defaultChecked>Error</Checkbox>
      </Space>
    )
}

export default App

Sizes

Five checkbox sizes from xs to xl.

import { Checkbox, Space } from 'asterui'

function App() {
  return (
      <Space direction="horizontal" size="md" align="center">
        <Checkbox size="xs" defaultChecked>XS</Checkbox>
        <Checkbox size="sm" defaultChecked>SM</Checkbox>
        <Checkbox size="md" defaultChecked>MD</Checkbox>
        <Checkbox size="lg" defaultChecked>LG</Checkbox>
        <Checkbox size="xl" defaultChecked>XL</Checkbox>
      </Space>
    )
}

export default App

Disabled

Disabled checkbox states.

import { Checkbox, Space } from 'asterui'

function App() {
  return (
      <Space direction="horizontal" size="md">
        <Checkbox disabled>Disabled</Checkbox>
        <Checkbox disabled defaultChecked>Disabled Checked</Checkbox>
      </Space>
    )
}

export default App

Indeterminate

Indeterminate state for check-all functionality.

import { Checkbox, Space } from 'asterui'
import { useState } from 'react'

function App() {
  const [items, setItems] = useState([true, false, true])
  const allChecked = items.every(Boolean)
  const someChecked = items.some(Boolean) && !allChecked
  
  const handleSelectAll = () => {
    setItems(allChecked ? [false, false, false] : [true, true, true])
  }
  
  return (
      <Space direction="vertical" size="sm">
        <Checkbox
          checked={allChecked}
          indeterminate={someChecked}
          onChange={handleSelectAll}
          className="font-medium"
        >
          Select All
        </Checkbox>
        <div className="ml-6">
          <Space direction="vertical" size="xs">
            {['Item 1', 'Item 2', 'Item 3'].map((item, i) => (
              <Checkbox
                key={i}
                checked={items[i]}
                onChange={() => {
                  const newItems = [...items]
                  newItems[i] = !newItems[i]
                  setItems(newItems)
                }}
              >
                {item}
              </Checkbox>
            ))}
          </Space>
        </div>
      </Space>
    )
}

export default App

Checkbox Group

Group multiple checkboxes with shared state.

import { Checkbox, Modal } from 'asterui'

function App() {
  const options = [
    { label: 'Apple', value: 'apple' },
    { label: 'Banana', value: 'banana' },
    { label: 'Orange', value: 'orange' },
    { label: 'Mango', value: 'mango' },
  ]
  
  return (
      <Checkbox.Group
        options={options}
        defaultValue={['apple', 'orange']}
        onChange={(values) => Modal.info({ title: 'Selected', content: values.join(', ') })}
      />
    )
}

export default App

Group with Options

Generate checkboxes from options array with disabled support.

import { Checkbox } from 'asterui'

function App() {
  const options = [
    { label: 'Read', value: 'read' },
    { label: 'Write', value: 'write' },
    { label: 'Delete', value: 'delete', disabled: true },
  ]
  
  return (
      <Checkbox.Group
        options={options}
        defaultValue={['read']}
      />
    )
}

export default App

Group Direction

Horizontal and vertical group layouts.

Horizontal (default)

Vertical

import { Checkbox, Space } from 'asterui'

function App() {
  const options = ['Option A', 'Option B', 'Option C']
  
  return (
      <Space direction="vertical" size="lg">
        <div>
          <p className="text-sm font-medium mb-2">Horizontal (default)</p>
          <Checkbox.Group options={options} direction="horizontal" defaultValue={['Option A']} />
        </div>
        <div>
          <p className="text-sm font-medium mb-2">Vertical</p>
          <Checkbox.Group options={options} direction="vertical" defaultValue={['Option B']} />
        </div>
      </Space>
    )
}

export default App

Controlled

Controlled checkbox with external state management.

Checked: No

import { Checkbox, Space } from 'asterui'
import { useState } from 'react'

function App() {
  const [checked, setChecked] = useState(false)
  
  return (
      <Space direction="vertical" size="sm">
        <Checkbox
          checked={checked}
          onChange={(e) => setChecked(e.target.checked)}
        >
          Controlled checkbox
        </Checkbox>
        <p className="text-sm text-base-content/70">
          Checked: {checked ? 'Yes' : 'No'}
        </p>
      </Space>
    )
}

export default App

Swap Mode

Toggle between two visual states with animation effects.

import { Checkbox, Space } from 'asterui'
import { SpeakerWaveIcon, SpeakerXMarkIcon } from '@aster-ui/icons/solid'
import { useState } from 'react'

function App() {
  const [volume, setVolume] = useState(true)
  
  return (
      <Space size="lg">
        <Checkbox
          checked={volume}
          onChange={(e) => setVolume(e.target.checked)}
          swap={{
            on: <SpeakerWaveIcon size={32} />,
            off: <SpeakerXMarkIcon size={32} />,
          }}
        />
        <Checkbox
          swap={{
            on: <span className="text-2xl">😀</span>,
            off: <span className="text-2xl">😴</span>,
            effect: 'rotate',
          }}
        />
        <Checkbox
          swap={{
            on: <span className="text-xl font-bold text-success">ON</span>,
            off: <span className="text-xl font-bold text-error">OFF</span>,
            effect: 'flip',
          }}
        />
      </Space>
    )
}

export default App
PropertyDescriptionTypeDefault
checkedControlled checked stateboolean-
defaultCheckedDefault checked stateboolean-
disabledDisable the checkboxbooleanfalse
indeterminateIndeterminate state (partial selection)booleanfalse
sizeCheckbox size'xs' | 'sm' | 'md' | 'lg' | 'xl'-
colorCheckbox color'primary' | 'secondary' | 'accent' | 'neutral' | 'success' | 'warning' | 'info' | 'error'-
swapSwap mode configurationCheckboxSwapConfig-
valueValue when used in a groupstring | number-
onChangeChange event handler(e: ChangeEvent) => void-
nameHTML name attribute for the inputstring-
autoFocusAuto focus on mountbooleanfalse
classNameAdditional CSS classesstring-
childrenLabel content (automatically wrapped in label element)ReactNode-
PropertyDescriptionTypeDefault
valueControlled selected values(string | number)[]-
defaultValueDefault selected values(string | number)[]-
onChangeSelection change callback(values: (string | number)[]) => void-
disabledDisable all checkboxesbooleanfalse
optionsCheckbox options(string | number | CheckboxOptionType)[]-
directionLayout direction'horizontal' | 'vertical''vertical'
nameHTML name attribute for all checkboxes (for form submission)string-
childrenManual checkbox elementsReactNode-
PropertyDescriptionTypeDefault
onContent shown when checkedReactNode-
offContent shown when uncheckedReactNode-
effectAnimation effect'rotate' | 'flip'-
PropertyDescriptionTypeDefault
labelOption labelReactNode-
valueOption valuestring | number-
disabledDisable this optionbooleanfalse
  • Uses native checkbox input for keyboard and screen reader support
  • Properly associates labels with checkboxes
  • Indeterminate state is communicated via the native indeterminate property
  • Disabled state prevents interaction and is communicated to assistive technologies
  • Focus states are visible for keyboard navigation