Toggle

A toggleable button with active/inactive states

Usage

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

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:

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:

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

Variant
Color
Size
Radius
import { TextBIcon } from '@phosphor-icons/react';
import { Toggle } from '@mantine/core';

function Demo() {
  return (
    <Toggle color="blue" 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:

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

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