Checkbox

A binary choice. A real native checkbox under a styled box, so labels, forms, keyboard and the indeterminate state all work.

src/components/ui/checkbox.tsx

About

From the component's own source, where it was written.

A real <input type="checkbox"> visually replaced by a sibling box.

Keeping the native input (screen-reader-only, not hidden) means form submission, the label association, keyboard toggling and the indeterminate state all come for free — a div with role="checkbox" would have to reimplement every one of them.

Install

npx astralyx-ui add checkbox

Usage

import { Checkbox } from '@/components/ui/checkbox'

<Checkbox label="Email me updates" defaultChecked />

Composer

Change the props and the source below follows.

Props
tsx
<Checkbox
  label="Email me updates"
/>

Examples

Sizes

With description

States

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

Checkbox

Extends Omit<ComponentProps<'input'>, 'size' | 'type'>.

PropTypeDefaultDescription
errorbooleanfalseRed border. Also sets aria-invalid.
size'sm' | 'default' | 'lg''default'Box and tick size.
labelReactNode—Text beside the box. Also makes the whole row clickable.
descriptionReactNode—Secondary line under the label.
indeterminatebooleanfalseShows a dash and reports indeterminate to assistive tech. A DOM property, not an attribute — set through a ref internally.
labelPosition'start' | 'end''end'start puts the label before the box, for a settings row.
containerClassNamestring——

Functions

FunctionSignatureDescription
checkboxVariants(props?: { size?: 'sm' | 'default' | 'lg'; error?: boolean }) => stringA real <input type="checkbox"> visually replaced by a sibling box.