Checkbox
Capture boolean input from user
Import
Source
Docs
Package
Usage
Label position
Color
Radius
xs
sm
md
lg
xl
Size
xs
sm
md
lg
xl
import { Checkbox } from '@mantine/core';
function Demo() {
return (
<Checkbox
defaultChecked
label="I agree to sell my privacy"
/>
);
}
States
import { Checkbox } from '@mantine/core';
function Demo() {
return (
<>
<Checkbox />
<Checkbox indeterminate />
<Checkbox checked indeterminate />
<Checkbox checked />
<Checkbox disabled />
<Checkbox disabled checked />
<Checkbox disabled indeterminate />
</>
);
}
Replace icon
import { Checkbox, CheckboxProps } from '@mantine/core';
import { IconBiohazard, IconRadioactive } from '@tabler/icons-react';
const CheckboxIcon: CheckboxProps['icon'] = ({ indeterminate, ...others }) =>
indeterminate ? <IconRadioactive {...others} /> : <IconBiohazard {...others} />;
function Demo() {
return (
<>
<Checkbox icon={CheckboxIcon} label="Custom icon" defaultChecked />
<Checkbox icon={CheckboxIcon} label="Custom icon: indeterminate" indeterminate mt="sm" />
</>
);
}
Indeterminate state
Checkbox
supports indeterminate state. When indeterminate
prop is true,
checked
prop is ignored:
import { useListState, randomId } from '@mantine/hooks';
import { Checkbox } from '@mantine/core';
const initialValues = [
{ label: 'Receive email notifications', checked: false, key: randomId() },
{ label: 'Receive sms notifications', checked: false, key: randomId() },
{ label: 'Receive push notifications', checked: false, key: randomId() },
];
export function IndeterminateCheckbox() {
const [values, handlers] = useListState(initialValues);
const allChecked = values.every((value) => value.checked);
const indeterminate = values.some((value) => value.checked) && !allChecked;
const items = values.map((value, index) => (
<Checkbox
mt="xs"
ml={33}
label={value.label}
key={value.key}
checked={value.checked}
onChange={(event) => handlers.setItemProp(index, 'checked', event.currentTarget.checked)}
/>
));
return (
<>
<Checkbox
checked={allChecked}
indeterminate={indeterminate}
label="Receive all notifications"
onChange={() =>
handlers.setState((current) =>
current.map((value) => ({ ...value, checked: !allChecked }))
)
}
/>
{items}
</>
);
}
Controlled
import { useState } from 'react';
import { Checkbox } from '@mantine/core';
function Demo() {
const [checked, setChecked] = useState(false);
return (
<Checkbox checked={checked} onChange={(event) => setChecked(event.currentTarget.checked)} />
);
}
Label with link
import { Checkbox, Anchor } from '@mantine/core';
function Demo() {
return (
<Checkbox
label={
<>
I accept{' '}
<Anchor href="https://mantine.dev" target="_blank" inherit>
terms and conditions
</Anchor>
</>
}
/>
);
}
Checkbox.Group
Select your favorite frameworks/libraries *
This is anonymous
import { Checkbox, Group } from '@mantine/core';
function Demo() {
return (
<Checkbox.Group
defaultValue={['react']}
label="Select your favorite frameworks/libraries"
description="This is anonymous"
withAsterisk
>
<Group mt="xs">
<Checkbox value="react" label="React" />
<Checkbox value="svelte" label="Svelte" />
<Checkbox value="ng" label="Angular" />
<Checkbox value="vue" label="Vue" />
</Group>
</Checkbox.Group>
);
}
Controlled Checkbox.Group
import { useState } from 'react';
import { Checkbox } from '@mantine/core';
function Demo() {
const [value, setValue] = useState<string[]>([]);
return (
<Checkbox.Group value={value} onChange={setValue}>
<Checkbox value="react" label="React" />
<Checkbox value="svelte" label="Svelte" />
</Checkbox.Group>
);
}
Get element ref
import { useRef } from 'react';
import { Checkbox } from '@mantine/core';
function Demo() {
const ref = useRef<HTMLInputElement>(null);
return <Checkbox ref={ref} />;
}
Accessibility
Set aria-label
or label
prop to make checkbox accessible:
import { Checkbox } from '@mantine/core';
// Not ok, input is not labeled
function Bad() {
return <Checkbox />;
}
// Ok, input is labelled by aria-label
function GoodAriaLabel() {
return <Checkbox aria-label="My checkbox" />;
}
// Ok, input is labelled by label element
function GoodLabel() {
return <Checkbox label="My checkbox" />;
}
Welcome to Mantine, React components library that you always wished for
Build fully functional accessible web applications faster than ever