# Checkbox Input

## Usage

Use `CheckboxInput` for a boolean option or a list of independent choices.
The component includes a label, description and status message.

```vue
<script setup lang="ts">
import { CheckboxInput } from '@astryx-vue/core/CheckboxInput'
import { shallowRef } from 'vue'

const selected = shallowRef(false)
</script>

<template>
  <CheckboxInput v-model="selected" label="Email updates" />
</template>
```

Import the theme once in the application entry:

```ts
import '@astryx-vue/themes/neutral.css'
```

### Anatomy

- **Checkbox**: The check box itself: unchecked, checked, or indeterminate.
- **Label**: Text describing what the checkbox controls. Always present for accessibility.
- **Description**: Helper text below the label with additional context.
- **Status message**: An error, warning, or success message below the checkbox.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Always provide a visible label so the user knows what they are toggling. Use isLabelHidden only when surrounding context makes it obvious. |
| Do | Add a description for choices that need extra context, like explaining what "Share usage data" actually shares. |
| Do | Use the indeterminate state for "select all" checkboxes when only some items in a group are selected. |
| Don't | Use a checkbox for mutually exclusive choices; use RadioList when only one option can be selected. |
| Don't | Use a checkbox for actions that take effect immediately; use a toggle switch or button instead. |
| Don't | Wrap a disabled checkbox in Tooltip to explain why it is disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead. |

## Examples

### States

Checked, unchecked, disabled, and indeterminate, each with a description.

```vue
<script setup lang="ts">
import type { CheckboxValue } from '@astryx-vue/core/CheckboxInput'
import { CheckboxInput } from '@astryx-vue/core/CheckboxInput'
import { VStack } from '@astryx-vue/core/Stack'
import { ref } from 'vue'

const checked = ref<CheckboxValue>(true)
const unchecked = ref<CheckboxValue>(false)
const disabled = ref<CheckboxValue>(false)
const indeterminate = ref<CheckboxValue>('indeterminate')
</script>

<template>
  <VStack :gap="4">
    <CheckboxInput v-model="checked" label="Checked" description="This checkbox is currently on." />
    <CheckboxInput v-model="unchecked" label="Unchecked" description="This checkbox is currently off." />
    <CheckboxInput v-model="disabled" label="Disabled" description="This checkbox cannot be changed." is-disabled />
    <CheckboxInput v-model="indeterminate" label="Indeterminate" description="This checkbox represents a partial selection." />
  </VStack>
</template>
```

### Indeterminate

Select all shows a dash when only some notifications are on.

```vue
<script setup lang="ts">
import type { CheckboxValue } from '@astryx-vue/core/CheckboxInput'
import { CheckboxInput } from '@astryx-vue/core/CheckboxInput'
import { Divider } from '@astryx-vue/core/Divider'
import { VStack } from '@astryx-vue/core/Stack'
import { computed, ref } from 'vue'

const items = ref({
  email: true,
  push: false,
  sms: true,
  slack: false,
})

const checkedCount = computed(() => Object.values(items.value).filter(Boolean).length)
const totalCount = computed(() => Object.keys(items.value).length)
const selectAll = computed<CheckboxValue>({
  get() {
    if (checkedCount.value === 0)
      return false
    if (checkedCount.value === totalCount.value)
      return true
    return 'indeterminate'
  },
  set(checked: CheckboxValue) {
    const next = checked === true
    items.value = { email: next, push: next, sms: next, slack: next }
  },
})
</script>

<template>
  <VStack :gap="3">
    <CheckboxInput
      v-model="selectAll"
      label="Select all notifications"
      :description="`${checkedCount} of ${totalCount} enabled`"
    />
    <Divider />
    <VStack :gap="3">
      <CheckboxInput v-model="items.email" label="Email notifications" />
      <CheckboxInput v-model="items.push" label="Push notifications" />
      <CheckboxInput v-model="items.sms" label="SMS alerts" />
      <CheckboxInput v-model="items.slack" label="Slack messages" />
    </VStack>
  </VStack>
</template>
```

### Status

Error, warning, and success messages under the checkbox.

```vue
<script setup lang="ts">
import type { CheckboxValue } from '@astryx-vue/core/CheckboxInput'
import { CheckboxInput } from '@astryx-vue/core/CheckboxInput'
import { VStack } from '@astryx-vue/core/Stack'
import { ref } from 'vue'

const error = ref<CheckboxValue>(false)
const warning = ref<CheckboxValue>(true)
const success = ref<CheckboxValue>(true)
</script>

<template>
  <VStack :gap="4">
    <CheckboxInput
      v-model="error"
      label="Error"
      description="Required field that has not been accepted."
      :status="{ type: 'error', message: 'You must accept the terms to continue' }"
    />
    <CheckboxInput
      v-model="warning"
      label="Warning"
      description="Enabled setting with a side effect to be aware of."
      :status="{ type: 'warning', message: 'This data may be shared with partners' }"
    />
    <CheckboxInput
      v-model="success"
      label="Success"
      description="Confirmed setting that has been verified."
      :status="{ type: 'success', message: 'Your email has been verified' }"
    />
  </VStack>
</template>
```

## Keyboard and accessibility

`CheckboxInput` renders a native `<input type="checkbox">`, so keyboard behaviour follows the browser defaults: press Tab to focus the checkbox, and press Space to toggle the selection.

The component wires the label, description and status message together through ARIA attributes, so assistive technology reads them as one unit:

- Always provide `label`; screen readers use it to describe what this option means. `isLabelHidden` only hides the visual label, and the label text still provides the accessible name.
- `aria-describedby` references the description and the status message, so both are announced on focus.
- The mixed state comes from the native `indeterminate` property, which browsers and assistive technology recognise directly.
- `isRequired` enables the browser's native required validation.
- When the status is an error, the component sets `aria-invalid`.

For a decorative selection mark on its own, use [CheckboxIndicator](/en/components/checkbox-indicator).

## Props

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `boolean \| 'indeterminate'` | — | Required. The current selection. |
| `label` | `string` | — | Required accessible name. The label carries both `astryx-field-label` and `astryx-checkbox-label`, so a theme can style the checkbox label apart from a field label. |
| `id` | `string` | — | Native input ID. Generated when omitted. |
| `size` | `'sm' \| 'md'` | `'md'` | sm is 20px. md is 24px. |
| `width` | `number \| string` | — | Component width. Numbers use pixels. |
| `description` | `string` | — | Description below the label. |
| `isLabelHidden` | `boolean` | false | Visually hides the label. Preserves its accessible name. |
| `isDisabled` | `boolean` | false | Prevents changes. Excludes the field from form submission. |
| `disabledMessage` | `string` | — | Disabled reason. Keeps the disabled control focusable. |
| `isReadOnly` | `boolean` | false | Prevents changes. Checked fields still submit. |
| `isLoading` | `boolean` | false | Shows loading. Blocks repeat activation. |
| `loadingLabel` | `string` | `'Loading'` | Accessible loading text. |
| `isRequired` | `boolean` | false | Shows the required marker. Enables native required validation. |
| `isOptional` | `boolean` | false | Shows the optional marker. Takes precedence over isRequired. |
| `optionalLabel` | `string` | `'Optional'` | Optional marker text. |
| `requiredLabel` | `string` | `'Required'` | Required marker text. |
| `htmlName` | `string` | — | Form field name. Overrides native name. |
| `status` | `FieldStatusInput` | — | Status type, message and message ID. |
| `labelIcon` | `Component` | — | Decorative icon before the label. |
| `labelTooltip` | `string` | — | Tooltip for the information button beside the label. |
| `changeAction` | `CheckboxInputChangeAction` | — | Action after a selection change. Supports a Promise. |
| `hasAutoFocus` | `boolean` | false | Focuses the input after mounting. |

## Status messages

`status` contains these fields:

- `type`: `'error'`, `'warning'` or `'success'`.
- `message`: optional message text.
- `messageId`: optional message ID.

Messages appear below the checkbox. Errors use assertive announcements. Warnings and successes use polite announcements.

## Events

- `update:modelValue(value)`: emits a boolean after activation.
- `change(value, event)`: fires after the model update.
- `actionError(error, value, event)`: fires when an action rejects or throws.

`event` is a cancelable `CheckboxInputChangeEvent`.
`event.detail.originalEvent` contains the native event.
Call `event.preventDefault()` in `change` to skip `changeAction`.
This does not undo the model update.

## Async actions

`changeAction(value, event)` receives the new value and component event.

- Returning a Promise shows the loading state.
- Pending actions block repeat activation.
- Explicit parent updates override the temporary selection.
- Loading clears after success or failure.
- Failures do not revert values written to `v-model`.
- Use `@action-error` to show failure information.
- Update the bound value in application code if you need a rollback.

## Native forms

- `htmlName` or `name` sets the field name.
- Native `value` sets the submitted value. The default is `'on'`.
- `value` does not control selection. Use `v-model` for selection.
- Unchecked and indeterminate fields do not submit.
- Checked read-only fields submit.
- Disabled fields do not submit.
- Fields with a disabled reason use `aria-disabled` and have no native form owner.
- `isOptional` overrides `isRequired`. An explicit native `required` attribute still applies.

After `form.reset()`, the component restores the current `v-model`.
To clear the selection, update the bound value in the form's `reset` handler.

## Template references and attributes

- `element`: `HTMLInputElement | null`.
- `focus(options?: FocusOptions)`: focuses the input.
- `blur()`: removes input focus.
- `class` and `style` apply to the outer component.
- Undeclared native attributes and events pass to `input`.
- `aria-labelledby` and `aria-describedby` merge with internal IDs.
- The component owns `checked`, `indeterminate`, `type` and core ARIA states.

## Slots

- `labelIcon`: a decorative icon before the label. Overrides the `labelIcon` prop.

## Theme

```css
.my-checkbox {
  --astryx-checkbox-accent: #047857;
  --astryx-checkbox-on-accent: #fff;
  --astryx-checkbox-focus: #047857;
}
```

- `--astryx-checkbox-background`: unchecked background.
- `--astryx-checkbox-border`: unchecked border.
- `--astryx-checkbox-accent`: checked and indeterminate background.
- `--astryx-checkbox-on-accent`: selection mark color.
- `--astryx-checkbox-focus`: keyboard focus color.
- `--astryx-checkbox-disabled-background`: disabled unchecked background.
- `--astryx-radius-inner`: indicator corner radius.

Neutral follows `color-scheme`.
Set `data-astryx-color-mode="light"` or `"dark"` to choose a mode.

## Accessibility

### Checkbox box

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Rest, Hover, Pointer down, Checked

The box edge (unchecked) and fill (checked) must have at least 3:1 contrast with the surface behind them. For Hover and Pointer down, measure the final colors after the tint and the pressed overlay are applied.
