---
title: "Checkbox"
description: "Select individual options or a group of items."
---

# Checkbox

Select individual options or a group of items.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Checkbox, CheckboxField, CheckboxControl, CheckboxMixed from "@rightui/ui"
```

## Usage

Import Checkbox for a labeled boolean control, CheckboxControl for a caller-composed label, or CheckboxMixed for checked="true", "false" or "mixed". Keep state in the parent and update it through change(bool). Use CheckboxField to associate helper text or validation with the control, or make the entire option card selectable.

## Example

```contract
use Checkbox, CheckboxField, CheckboxMixed, FontWeights, uiFontFamily from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
    uiWeights: FontWeights
  state terms = false
  state described = true
  state alerts = false
  state required = false
  state desktop = ["External disks"]
  state members = ["Sam Lee"]
  state arabic = false
  derive names = ["Sam Lee", "Alex Morgan", "Priya Shah", "Jules Roy"]
  derive selection = members.length == 0 ? "false" : members.length == names.length ? "true" : "mixed"
  action setTerms(value: bool)
    terms = value
  action setDescribed(value: bool)
    described = value
  action setAlerts(value: bool)
    alerts = value
  action setRequired(value: bool)
    required = value
  action setArabic(value: bool)
    arabic = value
  action selectDesktop(name: string, value: bool)
    desktop = value ? desktop.concat([name]) : desktop.filter(item => item != name)
  action selectMember(name: string, value: bool)
    members = value ? members.concat([name]) : members.filter(item => item != name)
  action selectAll(value: bool)
    members = value ? names : []
  view
    column width="100%" max-width="28rem" min-width=0 gap="ui.space-32"
      column gap="ui.space-12"
        Checkbox(id="checkbox-basic", label="Accept terms and conditions", checked=terms, disabled=false, change=setTerms)
        CheckboxField(id="checkbox-description", label="Accept terms and conditions", description="I agree to the terms for this workspace.", checked=described, disabled=false, change=setDescribed, showLabel=true, card=false, invalid=false)
        Checkbox(id="checkbox-disabled", label="Enable notifications", checked=false, disabled=true, change=setAlerts)
        CheckboxField(id="checkbox-card", label="Enable notifications", description="Receive an alert when a teammate shares an update.", checked=alerts, disabled=false, change=setAlerts, showLabel=true, card=true, invalid=false)
      column gap="ui.space-8" role="group" aria-label="Desktop items"
        text "Show on the desktop" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
        for name, i in ["Hard disks", "External disks", "CDs and DVDs", "Connected servers"] key=name
          Checkbox(id="checkbox-list-" + i.toString(), label=name, checked=desktop.includes(name), disabled=false, change=selectDesktop(name))
      column gap="ui.space-4" role="group" aria-label="Required field"
        CheckboxField(id="checkbox-invalid", label="Accept the workspace policy", description=(required ? "" : "Required to continue."), checked=required, disabled=false, change=setRequired, showLabel=true, card=false, invalid=not required)
      column role="table" aria-label="Workspace members" min-width=0
        row role="row" align-items="center" gap="ui.space-12" border-bottom="1px solid ui.border"
          view role="columnheader"
            CheckboxMixed(id="advanced-mixed", label="Select all members", labelVisible=false, checked=selection, disabled=false, change=selectAll)
          text "Name" role="columnheader" flex=1 font-weight=uiWeights.medium
          text "Role" role="columnheader" width=68 font-weight=uiWeights.medium
        for name, i in names key=name
          row role="row" align-items="center" gap="ui.space-12" border-bottom="1px solid ui.border" background-color=(members.includes(name) ? "ui.muted" : "transparent")
            view role="cell"
              CheckboxMixed(id="checkbox-member-" + i.toString(), label="Select " + name, labelVisible=false, checked=(members.includes(name) ? "true" : "false"), disabled=false, change=selectMember(name))
            text name role="cell" flex=1 font-size="ui.text-sm"
            text (i == 0 ? "Admin" : i == 3 ? "Editor" : "Member") role="cell" width=68 font-size="ui.text-sm" color="ui.muted-foreground"
      text (members.length.toString() + " of " + names.length.toString() + " selected") testId="checkbox-selection" role="status" color="ui.muted-foreground" font-size="ui.text-sm"
      column direction="rtl" lang="ar" align-items="flex-start"
        Checkbox(id="checkbox-rtl", label="قبول الشروط والأحكام", checked=arabic, disabled=false, change=setArabic)

```

## API Reference

| Property | Type |
| --- | --- |
| CheckboxMixed.id | string |
| CheckboxMixed.label | string |
| CheckboxMixed.checked | string |
| CheckboxMixed.labelVisible | bool |
| CheckboxMixed.disabled | bool |
| CheckboxMixed.change | action |
| CheckboxField.id | string |
| CheckboxField.label | string |
| CheckboxField.checked | bool |
| CheckboxField.disabled | bool |
| CheckboxField.change | action |
| CheckboxField.showLabel | bool |
| CheckboxField.description | string |
| CheckboxField.card | bool |
| CheckboxField.invalid | bool |
| Checkbox.id | string |
| Checkbox.label | string |
| Checkbox.checked | bool |
| Checkbox.disabled | bool |
| Checkbox.change | action |
| CheckboxControl.id | string |
| CheckboxControl.label | string |
| CheckboxControl.checked | bool |
| CheckboxControl.disabled | bool |
| CheckboxControl.change | action |

## Source

File: packages/ui/checkbox.contract

```contract
use uiFontFamily from "./typography-context.contract"
use Pressable from "./styles.contract"
component CheckboxFrame
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    change: action
    showLabel: bool
  view
    CheckboxField(id=id, label=label, checked=checked, disabled=disabled, change=change, showLabel=showLabel, description="", card=false, invalid=false)

component CheckboxField
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    change: action
    showLabel: bool
    description: string
    card: bool
    invalid: bool
  inject
    uiFont: string
  action toggle
    if not disabled
      change(not checked)
  view
    button id=id testId=id appearance="none" class=Pressable press=toggle disabled=disabled
      role="checkbox" aria-checked=checked aria-label=label aria-invalid=invalid aria-describedby=(description != "" ? id + "-description" : "")
      display="flex" align-items=(description == "" ? "center" : "flex-start") justify-content="flex-start" gap="ui.space-8"
      min-height="ui.touch-target" width=(showLabel ? "auto" : "ui.touch-target") padding=(card ? "ui.space-16" : "0px") border-width=(card ? "ui.border-width" : "0px") border-style="solid" border-color=(card and checked ? "ui.primary" : "ui.border") border-radius=(card ? "ui.radius-panel" : "ui.radius-control") background-color=(card and checked ? "ui.muted" : "transparent") color="ui.foreground"
      :hover
        background-color="transparent"
      :active
        background-color="transparent"
      :focus-visible
        box-shadow="ui.shadow-focus"
      :disabled
        opacity="ui.opacity-disabled"
      view flex-shrink=0 width="ui.space-16" height="ui.space-16" border-radius="ui.radius-checkbox" box-sizing="border-box"
        margin-top=(description == "" ? 0 : 2) border-width="ui.border-width" border-style="solid" border-color=(invalid ? "ui.destructive" : checked ? "ui.primary" : "ui.input")
        background-color=(checked ? "ui.primary" : "ui.surface")
        display="flex" align-items="center" justify-content="center" aria-hidden=true
        if checked
          svg width="ui.space-12" height="ui.space-12" viewBox="0 0 24 24" fill="none" stroke="ui.primary-foreground" stroke-width="ui.stroke-standard"
            path d="M5 12l4 4L19 6"
      if showLabel
        column flex=1 min-width=0 gap="ui.space-4" align-items="flex-start" text-align="start"
          text label font-size="ui.text-sm" font-family=uiFontFamily(uiFont) data-rightui-font="body"
          if description != ""
            text description id=(id + "-description") testId=(id + "-description") color="ui.muted-foreground" font-size="ui.text-sm" line-height="ui.leading-normal" font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Checkbox
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    change: action
  view
    CheckboxFrame(id=id, label=label, checked=checked, disabled=disabled, change=change, showLabel=true)

component CheckboxControl
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    change: action
  view
    CheckboxFrame(id=id, label=label, checked=checked, disabled=disabled, change=change, showLabel=false)

```
