apps/mantine.dev/src/pages/core/segmented-control.mdx
import { SegmentedControlDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.SegmentedControl);
import { useState } from 'react';
import { SegmentedControl } from '@mantine/core';
function Demo() {
const [value, setValue] = useState('react');
return (
<SegmentedControl
value={value}
onChange={setValue}
data={[
{ label: 'React', value: 'react' },
{ label: 'Angular', value: 'ng' },
{ label: 'Vue', value: 'vue' },
{ label: 'Svelte', value: 'svelte' },
]}
/>
);
}
SegmentedControl can be used with uncontrolled forms the same way as a native input element.
Set the name attribute to include segmented control value in FormData object on form submission.
To control the initial value in uncontrolled forms, use the defaultValue prop.
Example usage of uncontrolled SegmentedControl with FormData:
import { SegmentedControl } from '@mantine/core';
function Demo() {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
console.log('Segmented control value:', formData.get('framework'));
}}
>
<SegmentedControl
name="framework"
defaultValue="react"
data={[
{ label: 'React', value: 'react' },
{ label: 'Angular', value: 'ng' },
{ label: 'Vue', value: 'vue' },
{ label: 'Svelte', value: 'svelte' },
]}
/>
<button type="submit">Submit</button>
</form>
);
}
SegmentedControl supports two different data formats:
value and label are the samevalue and label are differentimport { SegmentedControl } from '@mantine/core';
function ArrayOfStrings() {
return (
<SegmentedControl data={['React', 'Angular', 'Svelte', 'Vue']} />
);
}
function ArrayOfObjects() {
return (
<SegmentedControl
data={[
{ value: 'React', label: 'React' },
{ value: 'Angular', label: 'Angular' },
{ value: 'Svelte', label: 'Svelte' },
{ value: 'Vue', label: 'Vue' },
]}
/>
);
}
SegmentedControl supports generic value type. You can pass primitive values (numbers, strings, boolean, null)
as the type argument. The generic type is used for value, defaultValue, onChange and data props.
Example with strings union:
import { SegmentedControl } from '@mantine/core';
function Demo() {
return (
<SegmentedControl<'orange' | 'grape' | 'apple'>
data={[
{ value: 'orange', label: 'Orange' },
{ value: 'grape', label: 'Grape' },
{ value: 'apple', label: 'Apple' },
]}
/>
);
}
To disable a SegmentedControl item, use the array of objects data format and set disabled: true
on the item that you want to disable. To disable the entire component, use the disabled prop.
You can use any React node as a label:
<Demo data={SegmentedControlDemos.labels} />By default, SegmentedControl uses theme.white with shadow in the light color scheme and var(--mantine-color-dark-6) background color for the indicator.
Set the color prop to change the indicator background-color:
SegmentedControl supports autoContrast prop. If set to true, the label text color will automatically adjust
to ensure optimal contrast against the indicator background color:
Change transition properties with:
transitionDuration – all transitions duration in ms, 200 by defaulttransitionTimingFunction – all transitions timing function, ease by defaultSet the readOnly prop to prevent the value from being changed:
SegmentedControl uses radio inputs under the hood, so it is accessible by default with no extra steps required if you have text in labels.
The component supports the same keyboard events as a regular radio group.
If you do not have text in labels (for example, when you want to use SegmentedControl with icons only),
use VisuallyHidden to make the component accessible: