# Switch

A switch allows users to toggle between two states: on and off. It provides immediate feedback and is commonly used for enabling/disabling features, settings, or binary choices.

## When to Use

- Enable or disable features, settings, or options
- Toggle between two mutually exclusive states
- Control real-time changes that take effect immediately
- Provide on/off controls in forms and preference panels
- Create compact binary controls where space is limited

## Component

---

### Properties

- **`children`** `string` — The content to display as the switch label
- **`isSelected`** `boolean` (default: `false`) — Whether the switch is selected (controlled)
- **`defaultSelected`** `boolean` (default: `false`) — Whether the switch is selected by default (uncontrolled)
- **`size`** `'small' | 'medium' | 'large'` (default: `medium`) — Switch size
- **`tooltip`** `string | TooltipProviderProps` — Tooltip shown on hover over the switch and its inline label, including while disabled. A string, or `TooltipProvider` props such as `title` and `placement`. It also opens when the switch takes keyboard focus, and describes its input to screen readers. A disabled switch with a tooltip stays in the Tab order, marked `aria-disabled` rather than natively disabled, so the keyboard reaches the tooltip too, and a plain `<form>` submits it when it is on. The info badge next to a field label is `labelTooltip`
- **`onChange`** — Callback fired when the switch value changes
- **`onFocus`** — Callback fired when the switch receives focus
- **`onBlur`** — Callback fired when the switch loses focus

### Base Properties

Supports [Base properties](../../BaseProperties.md)

`qa` (default: `Switch`) identifies the hidden interactive input only. The visual switch track uses `data-element="Input"`; customize it through `inputStyles`.

### Field Properties

Supports all [Field properties](../../FieldProperties.md)

### Styling Properties

#### styles

Customizes the root wrapper element of the component.

#### inputStyles

Customizes the switch toggle element specifically.

**Sub-elements:**

- `Thumb` - The movable indicator inside the switch

#### fieldStyles

Customizes the field wrapper element when the switch is used with labels or validation.

### Style Properties

These properties allow direct style application without using the `styles` prop:

- **Position:** `gridArea`, `order`, `gridColumn`, `gridRow`, `placeSelf`, `alignSelf`, `justifySelf`, `zIndex`, `margin`, `blockMargin`, `inlineMargin`, `inset`, `blockInset`, `inlineInset`, `position`, `scrollMargin`, `blockScrollMargin`, `inlineScrollMargin`, `scrollPadding`, `blockScrollPadding`, `inlineScrollPadding`
- **Dimension:** `width`, `height`, `blockSize`, `inlineSize`, `flexBasis`, `flexGrow`, `flexShrink`, `flex`

### Modifiers

The `mods` property accepts the following modifiers you can override:

- **`checked`** `boolean` — Whether the switch is in the "on" state
- **`disabled`** `boolean` — Whether the switch is disabled
- **`readonly`** `boolean` — Whether the switch is read-only: a recessed track with a dark thumb instead of the brand fill, and no hover state
- **`invalid`** `boolean` — Whether the switch has validation errors
- **`valid`** `boolean` — Whether the switch is valid
- **`focused`** `boolean` — Whether the switch has focus
- **`hovered`** `boolean` — Whether the switch is being hovered
- **`side-label`** `boolean` — Whether the label is positioned to the side
- **`inside-form`** `boolean` — Whether the switch is inside a form field

## Variants

### Sizes

- `small` - Compact size for dense interfaces
- `medium` - Standard size (default)
- `large` - Larger size for emphasis

## Examples

### Basic Usage

```jsx
<Switch>Enable notifications</Switch>
```

### Controlled State

```jsx
const [isEnabled, setIsEnabled] = useState(false);

<Switch isSelected={isEnabled} onChange={setIsEnabled}>
  Feature enabled
</Switch>
```

Keep the label the same in both states. The switch announces its own on/off state, so a label that also states it makes screen readers announce the state twice.

### With a Tooltip

A disabled switch still shows its tooltip, which makes it a good place to say why it can't be changed. While it has a tooltip, it stays focusable and is marked `aria-disabled` instead of natively disabled, so keyboard users can read the reason too; `Space` and clicks still change nothing. Without a tooltip, a disabled switch is natively disabled and `Tab` skips it.

```jsx
<Switch isDisabled tooltip="Add a schedule to turn this on">
  Run on schedule
</Switch>
```

Prefer `tooltip` to wrapping the switch in `TooltipProvider`: while disabled, a switch takes no trigger props from a wrapper, so that tooltip never opens.

## Accessibility

### Keyboard Navigation

- `Tab` - Moves focus to the switch. A disabled switch takes focus only while it has a `tooltip`, which then opens
- `Space` - Toggles the switch state
- `Enter` does not toggle the switch. Inside a form with a submit button, the browser submits the form instead, unless the switch is disabled.

### Screen Reader Support

- Component announces as "switch" to screen readers
- Current state is announced (e.g., "on", "off")
- State changes are immediately announced
- `isLoading` only shows a spinner. The spinner is hidden from screen readers, and the switch stays enabled, so pass `isDisabled` as well if it should not change while loading.

### ARIA Properties

- `aria-label` - Provides accessible label when no visible label exists
- `aria-labelledby` - References external label elements
- `aria-describedby` - References additional descriptive text
- `aria-required` - Indicates if toggling is required
- `aria-invalid` - Indicates validation state

The control is a native `<input type="checkbox" role="switch">`, so its state is the input's `checked` property. No `aria-checked` is written.

## Best Practices

1. **Do**: Use clear, descriptive labels that indicate what the switch controls

   ```jsx
   <Switch>Enable email notifications</Switch>
   ```

2. **Don't**: Use switches for actions that require additional steps

   ```jsx
   <Switch>Delete account</Switch> {/* Use Button instead */}
   ```

3. **Immediate Effect**: Use switches for settings that take effect immediately
4. **State Clarity**: Make it clear what "on" and "off" states mean
5. **Grouping**: Group related switches together in settings panels
6. **Labels**: Always provide meaningful labels for screen readers

## Integration with Forms

This component supports all [Field properties](../../FieldProperties.md) when used within a Form.

## Suggested Improvements

- Add support for custom thumb icons or content
- Implement animation customization options
- Add support for intermediate/indeterminate states
- Consider adding color customization for different switch types
- Implement grouped switch controls for related settings

## Related Components

- [Checkbox](./Checkbox.md) - For selection that doesn't take immediate effect
- [RadioGroup](./RadioGroup.md) - For selecting one option from multiple choices
- [Button](../actions/Button.md) - For actions that require confirmation
