Switch

Capture boolean input from user
Import

Usage

Size
xs
sm
md
lg
xl
Radius
xs
sm
md
lg
xl
Color
import { Switch } from '@mantine/core';
function Demo() {
return (
<Switch
label="I agree to sell my privacy"
/>
);
}

Controlled

import { useState } from 'react';
import { Switch } from '@mantine/core';
function Demo() {
const [checked, setChecked] = useState(false);
return <Switch checked={checked} onChange={(event) => setChecked(event.currentTarget.checked)} />;
}

Inner Labels

import { Switch } from '@mantine/core';
function Demo() {
return <Switch onLabel="ON" offLabel="OFF" />;
}

Get input ref

import { useRef } from 'react';
import { Switch } from '@mantine/core';
function Demo() {
const ref = useRef<HTMLInputElement>();
return <Switch ref={ref} />;
}

Accessibility

Switch is a regular input with checkbox type. Provide aria-label if Switch is used without label:

<Switch /> // -> not ok, input is not labeled
<Switch label="I agree to everything" /> // -> ok, input and label is connected
<Switch aria-label="I agree to everything" /> // -> ok, label is not visible but will be announced by screen reader