> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thehuxdesign.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Toggle

> Two-state toggle button for formatting controls and feature toggles

## Overview

`HuxToggle` is a two-state button component commonly used for formatting controls (like bold, italic) or feature toggles. It supports both icon-only and icon-with-text configurations, with multiple variants and sizes.

## Basic Usage

```dart theme={null}
HuxToggle(
  value: isBold,
  onChanged: (value) => setState(() => isBold = value),
  icon: FeatherIcons.bold,
  label: 'Bold', // Optional visual label
  semanticLabel: 'Bold', // Optional when label is provided
)
```

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-primary.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=1f2aa6cd62e13572889a3b255985b2c1" alt="Basic toggle usage" width="1400" height="300" data-path="images/toggle/toggle-primary.png" />

## Icon-Only Toggle

```dart theme={null}
HuxToggle(
  value: isEditing,
  onChanged: (value) => setState(() => isEditing = value),
  icon: FeatherIcons.edit2,
  semanticLabel: 'Edit', // Required when label is null
)
```

## Variants

HuxToggle supports four visual variants through the `HuxButtonVariant` enum:

### Primary Toggle

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-primary.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=1f2aa6cd62e13572889a3b255985b2c1" alt="Primary toggle variants" width="1400" height="300" data-path="images/toggle/toggle-primary.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.primary, // Default
)
```

### Secondary Toggle

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-secondary.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=da5c4c910e7aa0a70c2a787081d5e976" alt="Secondary toggle variants" width="1400" height="300" data-path="images/toggle/toggle-secondary.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.secondary,
)
```

### Outline Toggle

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-outline.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=d8bb983586d58dd290895c632635fc8b" alt="Outline toggle variants" width="1400" height="300" data-path="images/toggle/toggle-outline.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.outline,
)
```

### Ghost Toggle

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-ghost.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=5a6e40f4f9fe4f792450e86c249ae709" alt="Ghost toggle variants" width="1400" height="300" data-path="images/toggle/toggle-ghost.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.ghost,
)
```

## Sizes

Control toggle dimensions with three size options:

### Small Toggle

Compact size for tight spaces (32px height).

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-small.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=f2385c2cdf1894bc49b694b9bd0c0a58" alt="Small toggle" width="1400" height="300" data-path="images/toggle/toggle-small.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  size: HuxToggleSize.small,
)
```

### Medium Toggle (Default)

Default size for most use cases (40px height).

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-primary.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=1f2aa6cd62e13572889a3b255985b2c1" alt="Medium toggle" width="1400" height="300" data-path="images/toggle/toggle-primary.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  size: HuxToggleSize.medium, // Default
)
```

### Large Toggle

Prominent size for important actions (48px height).

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-large.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=a37f76c11036600d8cfae087a8856b38" alt="Large toggle" width="1400" height="300" data-path="images/toggle/toggle-large.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  size: HuxToggleSize.large,
)
```

## States

### Disabled State

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-disabled.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=e4463cec82e87ed735be84bcd9ae5939" alt="Disabled toggle states" width="1400" height="300" data-path="images/toggle/toggle-disabled.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: null, // Disable the toggle
  icon: FeatherIcons.edit2,
  isDisabled: true,
)
```

### Custom Primary Color

<img src="https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-custom.png?fit=max&auto=format&n=klklbwMu82vQdxDk&q=85&s=d31a3d0bd26676f62f649376c1010a6d" alt="Custom color toggle" width="1400" height="300" data-path="images/toggle/toggle-custom.png" />

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  primaryColor: Colors.deepPurple,
)
```

## API Reference

### HuxToggle Properties

| Property        | Type                  | Default      | Description                                           |
| --------------- | --------------------- | ------------ | ----------------------------------------------------- |
| `value`         | `bool`                | **required** | The current toggle state (on/off)                     |
| `onChanged`     | `ValueChanged<bool>?` | `null`       | Callback triggered when toggle state changes          |
| `icon`          | `IconData`            | **required** | The icon to display in the toggle                     |
| `label`         | `String?`             | `null`       | Optional text label to display next to the icon       |
| `semanticLabel` | `String?`             | `null`       | Accessibility label used when `label` is not provided |
| `size`          | `HuxToggleSize`       | `medium`     | Size variant of the toggle                            |
| `variant`       | `HuxButtonVariant`    | `primary`    | Visual variant of the toggle                          |
| `isDisabled`    | `bool`                | `false`      | Whether the toggle is disabled                        |
| `primaryColor`  | `Color?`              | `null`       | Optional custom primary color                         |

> If `label` is omitted (icon-only toggle), provide `semanticLabel` for accessibility.

### HuxToggleSize Enum

* `HuxToggleSize.small` - 32px height, compact padding
* `HuxToggleSize.medium` - 40px height, standard padding
* `HuxToggleSize.large` - 48px height, generous padding

## Accessibility

HuxToggle includes several accessibility features:

### Automatic Contrast

Text and icon colors are automatically calculated to ensure WCAG AA compliance (4.5:1 contrast ratio) against any background color.

### Semantic Roles

Toggles include proper semantic roles for screen readers and announce `label` or `semanticLabel`.

### Focus States

Keyboard navigation support with visible focus indicators.

### Disabled States

Proper disabled state handling for assistive technologies.

## Best Practices

<AccordionGroup>
  <Accordion title="Choose the Right Variant" icon="eye">
    * Use **Primary** for important toggles that need emphasis
    * Use **Secondary** for standard toggles in content areas
    * Use **Outline** for toggles that need visual separation
    * Use **Ghost** for subtle toggles in toolbars
  </Accordion>

  <Accordion title="Size Appropriately" icon="ruler">
    * Use **Large** for important feature toggles
    * Use **Medium** for most standard interactions
    * Use **Small** for compact toolbars or formatting controls
  </Accordion>

  <Accordion title="Icon Selection" icon="image">
    * Choose clear, meaningful icons that represent the action
    * Use standard icons (bold, italic) for formatting controls
    * Add labels for actions that aren't immediately clear
    * For icon-only toggles, always provide `semanticLabel`
  </Accordion>

  <Accordion title="Theme Integration" icon="palette">
    * Let toggles adapt to your app's theme
    * Use `primaryColor` only for specific emphasis
    * Maintain consistent styling within toggle groups
  </Accordion>
</AccordionGroup>

## Examples

<CardGroup cols={2}>
  <Card title="Form Building" icon="edit" href="/examples/form-building">
    See toggles in action within forms
  </Card>

  <Card title="Text Editor" icon="type" href="/examples/text-editor">
    Learn how to build a formatting toolbar
  </Card>

  <Card title="Custom Theming" icon="palette" href="/examples/custom-theming">
    Apply custom colors and themes
  </Card>

  <Card title="Accessibility" icon="universal-access" href="/advanced/accessibility">
    Ensure your toggles are accessible
  </Card>
</CardGroup>
