# Toggle

> A Toggle widget to allow user to switch between two states

Toggle widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) that act as visual switches, allowing the user to turn a setting on or off. They represent a boolean value.


```ts title="toggle.ts"

export default [
  gui.inputs.booleanInput('notificationsEnabled', {
    label: 'Enable push notifications',
  }),
];
```

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

## Props

Use these `props` to customize your Toggle widget.

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

### Hint

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

Use the property `hint` to add a description.


```ts title="toggle.ts"

export default [
  gui.inputs.booleanInput('marketingEmails', {
    hint: 'We\'ll only send you relevant product updates.',
    label: 'Receive marketing emails',
  }),
];
```

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

### Position

Use the property `togglePosition` to align the toggle switch to the `left` (default) or `right` of its label.


```ts title="toggle.ts"

export default [
  gui.inputs.booleanInput('darkMode', {
    togglePosition: 'right',
    label: 'Dark mode',
  }),
];
```

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

## Styling

Toggles 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-toggle-width`            | Total width of the toggle switch                          |
| `--gui-toggle-height`           | Total height of the toggle switch                         |
| `--gui-toggle-slider-width`     | Width of the round slider knob thumb                      |
| `--gui-toggle-slider-height`    | Height of the round slider knob thumb                     |
| `--gui-toggle-slider-transform` | X-axis transform value distance for the knob when checked |
| `--gui-bg-default`              | Background color of slider container when off             |
| `--gui-bg-disabled`             | Background color of slider container when disabled        |
| `--gui-border-default`          | Default border of the off-state slider background         |
| `--gui-intent-primary`          | Slider background and border color when checked / on      |
| `--gui-text-default`            | Border color when focused                                 |
| `--gui-intent-error`            | Border and shadow color when invalid                      |

### Anatomy

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


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

        <span class="gui-toggle--slider" role="presentation"></span>
      </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-toggle>
</gui-toggle-input>
```
