ButtonGroup
ButtonGroup handles overflow for a group of Buttons. When there isn't enough horizontal space for every Button, then it switches to a vertical layout. Anytime there are two or more Buttons next to each other, a ButtonGroup should be used.
| install | yarn add @react-spectrum/buttongroup |
|---|---|
| version | 3.0.0-alpha.1 |
| usage | import {ButtonGroup} from '@react-spectrum/buttongroup' |
Example#
To see the orientation switch, reduce the width of your window.
<ButtonGroup width="50%">
<Button variant="primary">Rate Now</Button>
<Button variant="secondary">No, thanks</Button>
<Button variant="secondary">Remind me later</Button>
</ButtonGroup>Content#
ButtonGroup expects Buttons as children.
Props#
| Name | Type | Default | Description |
isDisabled | boolean | — | Whether the Buttons in the ButtonGroup are all disabled. |
orientation | Orientation | "horizontal" | The axis the ButtonGroup should align with. Setting this to 'vertical' will prevent any switching behaviors between 'vertical' and 'horizontal'. |
children | ReactNode | — | The Buttons contained within the ButtonGroup. |
align | Alignment | 'center' | "start" | The alignment of the buttons within the ButtonGroup. |
UNSAFE_className | string | — | |
UNSAFE_style | CSSProperties | — |
Layout
| Name | Type | Default | Description |
flex | string | number | boolean | — | |
flexGrow | number | — | |
flexShrink | number | — | |
flexBasis | number | string | — | |
alignSelf | 'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'stretch' | — | |
justifySelf | 'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'left'
| 'right'
| 'stretch' | — | |
flexOrder | number | — | |
gridArea | string | — | |
gridColumn | string | — | |
gridRow | string | — | |
gridColumnStart | string | — | |
gridColumnEnd | string | — | |
gridRowStart | string | — | |
gridRowEnd | string | — |
Spacing
| Name | Type | Default | Description |
margin | DimensionValue | — | |
marginTop | DimensionValue | — | |
marginLeft | DimensionValue | — | |
marginRight | DimensionValue | — | |
marginBottom | DimensionValue | — | |
marginStart | DimensionValue | — | |
marginEnd | DimensionValue | — | |
marginX | DimensionValue | — | |
marginY | DimensionValue | — |
Sizing
| Name | Type | Default | Description |
width | DimensionValue | — | |
minWidth | DimensionValue | — | |
maxWidth | DimensionValue | — | |
height | DimensionValue | — | |
minHeight | DimensionValue | — | |
maxHeight | DimensionValue | — |
Positioning
| Name | Type | Default | Description |
position | 'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky' | — | |
top | DimensionValue | — | |
bottom | DimensionValue | — | |
left | DimensionValue | — | |
right | DimensionValue | — | |
start | DimensionValue | — | |
end | DimensionValue | — | |
zIndex | number | — | |
isHidden | boolean | — |
Accessibility
| Name | Type | Default | Description |
role | string | — | |
id | string | — | |
tabIndex | number | — | |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-controls | string | — | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-owns | string | — | Identifies an element (or elements) in order to define a visual, functional, or contextual parent/child relationship between DOM elements where the DOM hierarchy cannot be used to represent the relationship. |
aria-hidden | boolean | 'false' | 'true' | — | Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable. |
Visual Options#
Orientation#
Setting this to 'vertical' will prevent any dynamic orientation changes, the ButtonGroup will remain in vertical regardless of the available width.
<ButtonGroup orientation="vertical">
<Button variant="secondary">No, thanks</Button>
<Button variant="secondary">Remind me later</Button>
<Button variant="primary">Rate Now</Button>
</ButtonGroup>Alignment#
Button groups are aligned contextually. Generally (in LTR languages), button groups should be left-aligned to follow content. However, they should be right-aligned inside container components, such as in dialogs, popovers, or cards. They should be center-aligned in the context of an empty state. In RTL, left and right should be flipped. To see the orientation switch, reduce the width of your window.
<View width="50%">
<ButtonGroup align="end" width="100%">
<Button variant="secondary">No, thanks</Button>
<Button variant="secondary">Remind me later</Button>
<Button variant="primary">Rate Now</Button>
</ButtonGroup>
</View><View width="50%">
<ButtonGroup align="center" width="100%">
<Button variant="secondary">No, thanks</Button>
<Button variant="secondary">Remind me later</Button>
<Button variant="primary">Rate Now</Button>
</ButtonGroup>
</View>