# Toggle
Package: @mantine/core
Import: import { Toggle } from '@mantine/core';
Description: A toggleable button with active/inactive states

## Usage

`Toggle` is a button that switches between active and inactive states:

```tsx
import { TextBIcon, TextItalicIcon, TextUnderlineIcon } from '@phosphor-icons/react';
import { Group, Toggle } from '@mantine/core';

function Demo() {
  return (
    <Group>
      <Toggle aria-label="Bold">
        <TextBIcon weight="bold" size="100%" />
      </Toggle>
      <Toggle aria-label="Italic">
        <TextItalicIcon weight="bold" size="100%" />
      </Toggle>
      <Toggle aria-label="Underline">
        <TextUnderlineIcon weight="bold" size="100%" />
      </Toggle>
    </Group>
  );
}
```


## Controlled and uncontrolled

Set `defaultActive` for an uncontrolled `Toggle`, `active` and `onActiveChange` props to control it:

```tsx
import { useState } from 'react';
import { Toggle } from '@mantine/core';

// Uncontrolled
function UncontrolledDemo() {
  return <Toggle defaultActive />;
}

// Controlled
function ControlledDemo() {
  const [active, setActive] = useState(false);
  return <Toggle active={active} onActiveChange={setActive} />;
}
```

## Auto width

By default, `Toggle` is a square button. Set `autoWidth` to fit the width to the content:

```tsx
import { Group, Toggle } from '@mantine/core';

function Demo() {
  return (
    <Group>
      <Toggle autoWidth>Save</Toggle>
      <Toggle autoWidth active>Active</Toggle>
      <Toggle autoWidth disabled>Disabled</Toggle>
    </Group>
  );
}
```


## Configurator

```tsx
import { TextBIcon } from '@phosphor-icons/react';
import { Toggle } from '@mantine/core';

function Demo() {
  return (
    <Toggle variant="filled" color="blue" size="md" radius="md" disabled={false} aria-label="Bold">
      <TextBIcon weight="bold" size="100%" />
    </Toggle>
  );
}
```


## Accessibility

Give an icon-only toggle a label that stays the same when its state changes:

```tsx
import { IconBold } from '@tabler/icons-react';
import { Toggle } from '@mantine/core';

function Demo() {
  return (
    <Toggle aria-label="Bold">
      <IconBold />
    </Toggle>
  );
}
```


#### Props

**Toggle props**

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| active | boolean | - | Controlled active state |
| autoContrast | boolean | - | Determines whether icon color with filled variant should be changed based on the given `color` prop |
| autoWidth | boolean | - | If set, the toggle width adjusts to content instead of being square, `false` by default |
| color | MantineColor | - | Key of `theme.colors` or any valid CSS color, `theme.primaryColor` by default |
| defaultActive | boolean | - | Uncontrolled default active state |
| disabled | boolean | - | If set, the toggle is disabled |
| onActiveChange | (active: boolean) => void | - | Called when active state changes |
| radius | MantineRadius \| number | - | Key of `theme.radius` or any valid CSS value to set `border-radius`, `theme.defaultRadius` by default |
| size | MantineSize | - | Controls toggle height and min-width, `'md'` by default |


#### Styles API

Toggle component supports Styles API. With Styles API, you can customize styles of any inner element. Follow the documentation to learn how to use CSS modules, CSS variables and inline styles to get full control over component styles.

**Toggle selectors**

| Selector | Static selector | Description |
|----------|----------------|-------------|
| root | .mantine-Toggle-root | Root element |

**Toggle CSS variables**

| Selector | Variable | Description |
|----------|----------|-------------|
| root | --toggle-size | Controls toggle `height` and `min-width` |
| root | --toggle-radius | Controls `border-radius` |
| root | --toggle-active-bg | Controls active state `background-color` |
| root | --toggle-active-hover | Controls active state hover `background-color` |
| root | --toggle-active-color | Controls active state text `color` |

**Toggle data attributes**

| Selector | Attribute | Condition | Value |
|----------|-----------|-----------|-------|
| root | data-active | Toggle is in active state | - |
| root | data-disabled | `disabled` prop is set | - |
| root | data-auto-width | `autoWidth` prop is set | - |
