# Checkbox

> A Checkbox widget to allow user to toggle boolean values

Checkbox widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) that allow the user to toggle boolean (true/false) values. They are usually used to represent an "on/off" state.


```ts title="checkbox.ts"

export default [
  gui.inputs.checkbox('createUser', {
    label: 'Create a new user?',
  }),
];
```

_Live demo on the docs page. Form definition: [checkbox.ts](https://golemui.com/template/widgets-dx/checkbox/checkbox.ts)._

## Props

Use these `props` to customize your Checkbox widget.

| prop               | type   | description                                                                                      |
| ------------------ | ------ | ------------------------------------------------------------------------------------------------ |
| `checkboxPosition` | string | Position of the checkbox relative to its label. Possible values are `left` (default) or `right`. |
| `hint`             | string | A description to display below the checkbox input                                                |

### Hint

<Aside>A `label` is required for the `hint` to be displayed.</Aside>

Use the property `hint` to add a description.


```ts title="checkbox.ts"

export default [
  gui.inputs.checkbox('newsletter', {
    hint: 'We promise not to spam your inbox.',
    label: 'Subscribe to newsletter',
  }),
];
```

_Live demo on the docs page. Form definition: [checkbox-hint.ts](https://golemui.com/template/widgets-dx/checkbox/checkbox-hint.ts)._

### Position

Use the property `checkboxPosition` to put the checkbox to the `left` (default) or `right` of its label.


```ts title="checkbox.ts"

export default [
  gui.inputs.checkbox('acceptConditions', {
    checkboxPosition: 'right',
    label: 'Yes, I have read terms and conditions',
  }),
];
```

_Live demo on the docs page. Form definition: [checkbox-position.ts](https://golemui.com/template/widgets-dx/checkbox/checkbox-position.ts)._

## Mandatory checkbox

`validator: { required: true }` alone does not force a checkbox checked: an unchecked box holds `false`, which is a valid boolean and passes validation. Add `const: true` to reject `false`, and keep `required: true` so the pristine `undefined` fails too. See [Handling empty values](https://golemui.com/dx/features/validators.md#handling-empty-values) for the full recipe with custom messages.

## Styling

Checkboxes can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md).

### CSS Variables

Following you will find a list with the CSS Variables and a quick description of what you will style.

| CSS Variable           | Description                                |
| ---------------------- | ------------------------------------------ |
| `--gui-radius-sm`      | Checkbox box border radius                 |
| `--gui-intent-primary` | Checkbox checked background and tick color |

### Anatomy

This is the anatomy of the Checkbox Widget in case you want to use your CSS styles.


```html title="checkbox-anatomy.html"
<gui-checkbox-input class="gui-checkbox gui-field">
  <gui-checkbox class="gui-field">
    <label class="gui-label" for="fieldUid" id="fieldUid_label">
      <div class="gui-widget gui-widget--horizontal">
        <input type="checkbox" id="fieldUid" aria-describedby="fieldUid_hint" />
      </div>

      <span class="gui-label__container">
        <span class="gui-label__text">Label</span>
      </span>
    </label>

    <div class="gui-widget-hint">
      <span id="fieldUid_hint">Hint</span>
      <div class="gui-validator gui-validator--empty" id="fieldUid_errors" role="alert"></div>
    </div>
  </gui-checkbox>
</gui-checkbox-input>
```
