|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +title: ButtonGroup |
| 4 | +description: Group a series of buttons together on a single line with the button group, and super-power them with JavaScript. |
| 5 | +group: Components |
| 6 | +--- |
| 7 | + |
| 8 | +import { ButtonGroup, Toolbar } from 'boot-cell/source/Form/ButtonGroup'; |
| 9 | +import { Button } from 'boot-cell/source/Form/Button'; |
| 10 | +import { DropMenu } from 'boot-cell/source/Navigator/DropMenu'; |
| 11 | + |
| 12 | +import { Example } from '../../../source/component/Example'; |
| 13 | + |
| 14 | +## Basic example |
| 15 | + |
| 16 | +Wrap a series of buttons with `<Button />` in `<ButtonGroup />`. |
| 17 | + |
| 18 | +<Example> |
| 19 | + <ButtonGroup> |
| 20 | + <Button kind="secondary">Left</Button> |
| 21 | + <Button kind="secondary">Middle</Button> |
| 22 | + <Button kind="secondary">Right</Button> |
| 23 | + </ButtonGroup> |
| 24 | +</Example> |
| 25 | + |
| 26 | +```TSX |
| 27 | +import { render, createCell } from 'web-cell'; |
| 28 | +import { ButtonGroup } from 'boot-cell/source/Form/ButtonGroup'; |
| 29 | +import { Button } from 'boot-cell/source/Form/Button'; |
| 30 | + |
| 31 | +render( |
| 32 | + <ButtonGroup> |
| 33 | + <Button kind="secondary">Left</Button> |
| 34 | + <Button kind="secondary">Middle</Button> |
| 35 | + <Button kind="secondary">Right</Button> |
| 36 | + </ButtonGroup> |
| 37 | +); |
| 38 | +``` |
| 39 | + |
| 40 | +> ##### Ensure correct `role` and provide a label |
| 41 | +> |
| 42 | +> In order for assistive technologies (such as screen readers) to convey that a series of buttons is grouped, |
| 43 | +> an appropriate `role` attribute needs to be provided. For button groups, this would be `role="group"`, |
| 44 | +> while toolbars should have a `role="toolbar"`. |
| 45 | +> |
| 46 | +> In addition, groups and toolbars should be given an explicit label, as most assistive technologies will otherwise not announce them, |
| 47 | +> despite the presence of the correct role attribute. In the examples provided here, we use `aria-label`, |
| 48 | +> but alternatives such as `aria-labelledby` can also be used. |
| 49 | +
|
| 50 | +## Button toolbar |
| 51 | + |
| 52 | +Combine sets of button groups into button toolbars for more complex components. |
| 53 | +Use utility classes as needed to space out groups, buttons, and more. |
| 54 | + |
| 55 | +<Example> |
| 56 | + <Toolbar aria-label="Toolbar with button groups"> |
| 57 | + <ButtonGroup className="mr-2" aria-label="First group"> |
| 58 | + <Button kind="secondary">1</Button> |
| 59 | + <Button kind="secondary">2</Button> |
| 60 | + <Button kind="secondary">3</Button> |
| 61 | + <Button kind="secondary">4</Button> |
| 62 | + </ButtonGroup> |
| 63 | + <ButtonGroup className="mr-2" aria-label="Second group"> |
| 64 | + <Button kind="secondary">5</Button> |
| 65 | + <Button kind="secondary">6</Button> |
| 66 | + <Button kind="secondary">7</Button> |
| 67 | + </ButtonGroup> |
| 68 | + <ButtonGroup aria-label="Third group"> |
| 69 | + <Button kind="secondary">8</Button> |
| 70 | + </ButtonGroup> |
| 71 | + </Toolbar> |
| 72 | +</Example> |
| 73 | + |
| 74 | +```TSX |
| 75 | +import { render, createCell } from 'web-cell'; |
| 76 | +import { ButtonGroup, Toolbar } from 'boot-cell/source/Form/ButtonGroup'; |
| 77 | +import { Button } from 'boot-cell/source/Form/Button'; |
| 78 | + |
| 79 | +render( |
| 80 | + <Toolbar aria-label="Toolbar with button groups"> |
| 81 | + <ButtonGroup className="mr-2" aria-label="First group"> |
| 82 | + <Button kind="secondary">1</Button> |
| 83 | + <Button kind="secondary">2</Button> |
| 84 | + <Button kind="secondary">3</Button> |
| 85 | + <Button kind="secondary">4</Button> |
| 86 | + </ButtonGroup> |
| 87 | + <ButtonGroup className="mr-2" aria-label="Second group"> |
| 88 | + <Button kind="secondary">5</Button> |
| 89 | + <Button kind="secondary">6</Button> |
| 90 | + <Button kind="secondary">7</Button> |
| 91 | + </ButtonGroup> |
| 92 | + <ButtonGroup aria-label="Third group"> |
| 93 | + <Button kind="secondary">8</Button> |
| 94 | + </ButtonGroup> |
| 95 | + </Toolbar> |
| 96 | +); |
| 97 | +``` |
| 98 | + |
| 99 | +## Sizing |
| 100 | + |
| 101 | +Instead of applying button sizing properties to every button in a group, |
| 102 | +just add `size` property to each `<ButtonGroup />`, including each one when nesting multiple groups. |
| 103 | + |
| 104 | +<Example> |
| 105 | + <ButtonGroup size="lg"> |
| 106 | + <Button kind="secondary">Left</Button> |
| 107 | + <Button kind="secondary">Middle</Button> |
| 108 | + <Button kind="secondary">Right</Button> |
| 109 | + </ButtonGroup> |
| 110 | + <ButtonGroup> |
| 111 | + <Button kind="secondary">Left</Button> |
| 112 | + <Button kind="secondary">Middle</Button> |
| 113 | + <Button kind="secondary">Right</Button> |
| 114 | + </ButtonGroup> |
| 115 | + <ButtonGroup size="sm"> |
| 116 | + <Button kind="secondary">Left</Button> |
| 117 | + <Button kind="secondary">Middle</Button> |
| 118 | + <Button kind="secondary">Right</Button> |
| 119 | + </ButtonGroup> |
| 120 | +</Example> |
| 121 | + |
| 122 | +```TSX |
| 123 | +import { render, createCell, Fragment } from 'web-cell'; |
| 124 | +import { ButtonGroup } from 'boot-cell/source/Form/ButtonGroup'; |
| 125 | +import { Button } from 'boot-cell/source/Form/Button'; |
| 126 | + |
| 127 | +render( |
| 128 | + <Fragment> |
| 129 | + <ButtonGroup size="lg"> |
| 130 | + <Button kind="secondary">Left</Button> |
| 131 | + <Button kind="secondary">Middle</Button> |
| 132 | + <Button kind="secondary">Right</Button> |
| 133 | + </ButtonGroup> |
| 134 | + <ButtonGroup> |
| 135 | + <Button kind="secondary">Left</Button> |
| 136 | + <Button kind="secondary">Middle</Button> |
| 137 | + <Button kind="secondary">Right</Button> |
| 138 | + </ButtonGroup> |
| 139 | + <ButtonGroup size="sm"> |
| 140 | + <Button kind="secondary">Left</Button> |
| 141 | + <Button kind="secondary">Middle</Button> |
| 142 | + <Button kind="secondary">Right</Button> |
| 143 | + </ButtonGroup> |
| 144 | + </Fragment> |
| 145 | +); |
| 146 | +``` |
| 147 | + |
| 148 | +## Nesting |
| 149 | + |
| 150 | +Place a `<ButtonGroup />` within another `<ButtonGroup />` |
| 151 | +when you want `<DropMenu />`s mixed with a series of buttons. |
| 152 | + |
| 153 | +<Example> |
| 154 | + <ButtonGroup> |
| 155 | + <Button kind="secondary">1</Button> |
| 156 | + <Button kind="secondary">2</Button> |
| 157 | + <DropMenu |
| 158 | + buttonKind="secondary" |
| 159 | + title="Dropdown" |
| 160 | + href="https://web-cell.dev/" |
| 161 | + list={[ |
| 162 | + { href: '#', title: 'Dropdown link' }, |
| 163 | + { href: '#', title: 'Dropdown link' } |
| 164 | + ]} |
| 165 | + /> |
| 166 | + </ButtonGroup> |
| 167 | +</Example> |
| 168 | + |
| 169 | +```TSX |
| 170 | +import { render, createCell } from 'web-cell'; |
| 171 | +import { ButtonGroup } from 'boot-cell/source/Form/ButtonGroup'; |
| 172 | +import { Button } from 'boot-cell/source/Form/Button'; |
| 173 | +import { DropMenu } from 'boot-cell/source/Navigator/DropMenu'; |
| 174 | + |
| 175 | +render( |
| 176 | + <ButtonGroup> |
| 177 | + <Button kind="secondary">1</Button> |
| 178 | + <Button kind="secondary">2</Button> |
| 179 | + <DropMenu |
| 180 | + buttonKind="secondary" |
| 181 | + title="Dropdown" |
| 182 | + href="https://web-cell.dev/" |
| 183 | + list={[ |
| 184 | + { href: '#', title: 'Dropdown link' }, |
| 185 | + { href: '#', title: 'Dropdown link' } |
| 186 | + ]} |
| 187 | + /> |
| 188 | + </ButtonGroup> |
| 189 | +); |
| 190 | +``` |
| 191 | + |
| 192 | +## Vertical variation |
| 193 | + |
| 194 | +Make a set of buttons appear vertically stacked rather than horizontally. |
| 195 | + |
| 196 | +<Example> |
| 197 | + <ButtonGroup vertical> |
| 198 | + <Button kind="secondary">Button</Button> |
| 199 | + <Button kind="secondary">Button</Button> |
| 200 | + <Button kind="secondary">Button</Button> |
| 201 | + <Button kind="secondary">Button</Button> |
| 202 | + <Button kind="secondary">Button</Button> |
| 203 | + <Button kind="secondary">Button</Button> |
| 204 | + </ButtonGroup> |
| 205 | +</Example> |
| 206 | + |
| 207 | +<Example> |
| 208 | + <ButtonGroup vertical> |
| 209 | + <Button kind="secondary">Button</Button> |
| 210 | + <Button kind="secondary">Button</Button> |
| 211 | + <DropMenu |
| 212 | + buttonKind="secondary" |
| 213 | + title="Dropdown" |
| 214 | + href="https://web-cell.dev/" |
| 215 | + list={[ |
| 216 | + { href: '#', title: 'Dropdown link' }, |
| 217 | + { href: '#', title: 'Dropdown link' } |
| 218 | + ]} |
| 219 | + /> |
| 220 | + <Button kind="secondary">Button</Button> |
| 221 | + <Button kind="secondary">Button</Button> |
| 222 | + <DropMenu |
| 223 | + buttonKind="secondary" |
| 224 | + title="Dropdown" |
| 225 | + href="https://web-cell.dev/" |
| 226 | + list={[ |
| 227 | + { href: '#', title: 'Dropdown link' }, |
| 228 | + { href: '#', title: 'Dropdown link' } |
| 229 | + ]} |
| 230 | + /> |
| 231 | + <DropMenu |
| 232 | + buttonKind="secondary" |
| 233 | + title="Dropdown" |
| 234 | + href="https://web-cell.dev/" |
| 235 | + list={[ |
| 236 | + { href: '#', title: 'Dropdown link' }, |
| 237 | + { href: '#', title: 'Dropdown link' } |
| 238 | + ]} |
| 239 | + /> |
| 240 | + <DropMenu |
| 241 | + buttonKind="secondary" |
| 242 | + title="Dropdown" |
| 243 | + href="https://web-cell.dev/" |
| 244 | + list={[ |
| 245 | + { href: '#', title: 'Dropdown link' }, |
| 246 | + { href: '#', title: 'Dropdown link' } |
| 247 | + ]} |
| 248 | + /> |
| 249 | + </ButtonGroup> |
| 250 | +</Example> |
| 251 | + |
| 252 | +```TSX |
| 253 | +import { render, createCell } from 'web-cell'; |
| 254 | +import { ButtonGroup } from 'boot-cell/source/Form/ButtonGroup'; |
| 255 | +import { Button } from 'boot-cell/source/Form/Button'; |
| 256 | +import { DropMenu } from 'boot-cell/source/Navigator/DropMenu'; |
| 257 | + |
| 258 | +render( |
| 259 | + <ButtonGroup vertical> |
| 260 | + <Button kind="secondary">Button</Button> |
| 261 | + <Button kind="secondary">Button</Button> |
| 262 | + <DropMenu |
| 263 | + buttonKind="secondary" |
| 264 | + title="Dropdown" |
| 265 | + href="https://web-cell.dev/" |
| 266 | + list={[ |
| 267 | + { href: '#', title: 'Dropdown link' }, |
| 268 | + { href: '#', title: 'Dropdown link' } |
| 269 | + ]} |
| 270 | + /> |
| 271 | + <Button kind="secondary">Button</Button> |
| 272 | + <Button kind="secondary">Button</Button> |
| 273 | + <DropMenu |
| 274 | + buttonKind="secondary" |
| 275 | + title="Dropdown" |
| 276 | + href="https://web-cell.dev/" |
| 277 | + list={[ |
| 278 | + { href: '#', title: 'Dropdown link' }, |
| 279 | + { href: '#', title: 'Dropdown link' } |
| 280 | + ]} |
| 281 | + /> |
| 282 | + <DropMenu |
| 283 | + buttonKind="secondary" |
| 284 | + title="Dropdown" |
| 285 | + href="https://web-cell.dev/" |
| 286 | + list={[ |
| 287 | + { href: '#', title: 'Dropdown link' }, |
| 288 | + { href: '#', title: 'Dropdown link' } |
| 289 | + ]} |
| 290 | + /> |
| 291 | + <DropMenu |
| 292 | + buttonKind="secondary" |
| 293 | + title="Dropdown" |
| 294 | + href="https://web-cell.dev/" |
| 295 | + list={[ |
| 296 | + { href: '#', title: 'Dropdown link' }, |
| 297 | + { href: '#', title: 'Dropdown link' } |
| 298 | + ]} |
| 299 | + /> |
| 300 | + </ButtonGroup> |
| 301 | +); |
| 302 | +``` |
0 commit comments