A SegmentedControl
should not be used in a form as a replacement for something like a RadioGroup or Select. See the Accessibility section of the SegmentedControl interface guidelines for more details.
<FormControl><FormControl.Label id="scLabel-horiz" as="span">File view</FormControl.Label><SegmentedControl aria-labelledby="scLabel-horiz" aria-describedby="scCaption-horiz"><SegmentedControl.Button defaultSelected>Preview</SegmentedControl.Button><SegmentedControl.Button>Raw</SegmentedControl.Button><SegmentedControl.Button>Blame</SegmentedControl.Button></SegmentedControl><FormControl.Caption id="scCaption-horiz">Change the way the file is viewed</FormControl.Caption></FormControl>
<Box display="flex"><Box flexGrow={1}><Text fontSize={2} fontWeight="bold" id="scLabel-vert" display="block">File view</Text><Text color="fg.subtle" fontSize={1} id="scCaption-vert" display="block">Change the way the file is viewed</Text></Box><SegmentedControl aria-labelledby="scLabel-vert" aria-describedby="scCaption-vert"><SegmentedControl.Button defaultSelected>Preview</SegmentedControl.Button><SegmentedControl.Button>Raw</SegmentedControl.Button><SegmentedControl.Button>Blame</SegmentedControl.Button></SegmentedControl></Box>
const Controlled = () => {const [selectedIndex, setSelectedIndex] = React.useState(0)const handleSegmentChange = selectedIndex => {setSelectedIndex(selectedIndex)}return (<SegmentedControl aria-label="File view" onChange={handleSegmentChange}><SegmentedControl.Button selected={selectedIndex === 0}>Preview</SegmentedControl.Button><SegmentedControl.Button selected={selectedIndex === 1}>Raw</SegmentedControl.Button><SegmentedControl.Button selected={selectedIndex === 2}>Blame</SegmentedControl.Button></SegmentedControl>)}render(Controlled)
<SegmentedControl aria-label="File view" size="small"><SegmentedControl.Button defaultSelected>Preview</SegmentedControl.Button><SegmentedControl.Button>Raw</SegmentedControl.Button><SegmentedControl.Button>Blame</SegmentedControl.Button></SegmentedControl>
<SegmentedControl aria-label="File view"><SegmentedControl.Button defaultSelected leadingIcon={EyeIcon}>Preview</SegmentedControl.Button><SegmentedControl.Button leadingIcon={FileCodeIcon}>Raw</SegmentedControl.Button><SegmentedControl.Button leadingIcon={PeopleIcon}>Blame</SegmentedControl.Button></SegmentedControl>
<SegmentedControl aria-label="File view"><SegmentedControl.IconButton defaultSelected icon={EyeIcon} aria-label="Preview" /><SegmentedControl.IconButton icon={FileCodeIcon} aria-label="Raw" /><SegmentedControl.IconButton icon={PeopleIcon} aria-label="Blame" /></SegmentedControl>
<SegmentedControl aria-label="File view" variant={{narrow: 'hideLabels', regular: 'default'}}><SegmentedControl.Button defaultSelected leadingIcon={EyeIcon}>Preview</SegmentedControl.Button><SegmentedControl.Button leadingIcon={FileCodeIcon}>Raw</SegmentedControl.Button><SegmentedControl.Button leadingIcon={PeopleIcon}>Blame</SegmentedControl.Button></SegmentedControl>
<SegmentedControl aria-label="File view" variant={{narrow: 'dropdown', regular: 'default'}}><SegmentedControl.Button defaultSelected leadingIcon={EyeIcon}>Preview</SegmentedControl.Button><SegmentedControl.Button leadingIcon={FileCodeIcon}>Raw</SegmentedControl.Button><SegmentedControl.Button leadingIcon={PeopleIcon}>Blame</SegmentedControl.Button></SegmentedControl>
<SegmentedControl fullWidth aria-label="File view"><SegmentedControl.Button defaultSelected>Preview</SegmentedControl.Button><SegmentedControl.Button>Raw</SegmentedControl.Button><SegmentedControl.Button>Blame</SegmentedControl.Button></SegmentedControl>
<SegmentedControl aria-label="File view"><SegmentedControl.Button>Preview</SegmentedControl.Button><SegmentedControl.Button defaultSelected>Raw</SegmentedControl.Button><SegmentedControl.Button>Blame</SegmentedControl.Button></SegmentedControl>
Name | Type | Default | Description |
---|---|---|---|
aria-label | string | ||
aria-labelledby | string | ||
aria-describedby | string | ||
fullWidth | | boolean | { narrow?: boolean regular?: boolean wide?: boolean } | Whether the control fills the width of its parent | |
onChange | (selectedIndex?: number) => void | The handler that gets called when a segment is selected | |
variant | | 'default' | { narrow?: 'hideLabels' | 'dropdown' | 'default' regular?: 'hideLabels' | 'dropdown' | 'default' wide?: 'hideLabels' | 'dropdown' | 'default' } | 'default' | Configure alternative ways to render the control when it gets rendered in tight spaces |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLDivElement> | A ref to the element rendered by this component. |
Name | Type | Default | Description |
---|---|---|---|
leadingIcon | Component | The leading icon comes before item label | |
selected | boolean | Whether the segment is selected. This is used for controlled SegmentedControls, and needs to be updated using the onChange handler on SegmentedControl. | |
defaultSelected | boolean | Whether the segment is selected. This is used for uncontrolled SegmentedControls to pick one SegmentedControlButton that is selected on the initial render. | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLButtonElement> | A ref to the element rendered by this component. |
Name | Type | Default | Description |
---|---|---|---|
aria-label Required | string | ||
icon Required | Component | The icon that represents the segmented control item | |
selected | boolean | Whether the segment is selected. This is used for controlled SegmentedControls, and needs to be updated using the onChange handler on SegmentedControl. | |
selected | 'small' | 'medium' | The size of the buttons | |
defaultSelected | boolean | Whether the segment is selected. This is used for uncontrolled SegmentedControls to pick one SegmentedControlButton that is selected on the initial render. | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLButtonElement> | A ref to the element rendered by this component. |