Skip to content

Commit be4a84c

Browse files
authored
Toolbar docs updates (#5363)
1 parent faae654 commit be4a84c

File tree

4 files changed

+122
-64
lines changed

4 files changed

+122
-64
lines changed

packages/@react-aria/toolbar/docs/useToolbar.mdx

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,39 @@ keywords: [toolbar, aria]
4040

4141
## Features
4242

43-
There is no native element to implement a toolbar in HTML without Javascript. `useToolbar`
43+
There is no native element to implement a toolbar in HTML. `useToolbar`
4444
helps achieve accessible toolbar components that can be styled as needed.
4545

46-
* Exposed to assistive technology as a `toolbar` or `group` element via ARIA
47-
* Support for keyboard navigation
46+
* Exposed to assistive technology as a `toolbar` element via ARIA
47+
* Support for arrow key navigation
4848
* Support for both horizontal and vertical orientations
49-
* Support for React Aria based children: button, togglebutton, menu, checkbox, and link
49+
* Support for interactive children including button, toggle button, menu, checkbox, and link
5050
* Automatic scrolling support during keyboard navigation
5151

5252
## Anatomy
5353

5454
<Anatomy />
5555

56-
A toolbar consists of a wrapper element with role `toolbar` and groups of interactive children.
56+
A toolbar consists of a container element for a set of interactive controls.
5757
`useToolbar` handles exposing this to assistive technology using ARIA, along with
58-
handling keyboard, mouse, and interactions to support navigation behavior.
58+
handling arrow key navigation between children.
5959

6060
`useToolbar` returns props that you should spread onto the appropriate element:
6161

6262
<TypeContext.Provider value={docs.links}>
6363
<InterfaceType properties={docs.links[docs.exports.useToolbar.return.id].properties} />
6464
</TypeContext.Provider>
6565

66-
If a toolbar does not have a visible label, an `aria-label` or `aria-labelledby`
67-
prop must be passed instead to identify the element to assistive technology.
66+
An `aria-label` or `aria-labelledby` prop must be provided to identify the toolbar to assistive technology.
6867

6968
## Example
7069

7170
### Toolbar
7271

73-
This uses the <TypeLink links={docs.links} type={docs.exports.useToolbar} /> hook, spread on a container to handle
72+
This example uses the <TypeLink links={docs.links} type={docs.exports.useToolbar} /> hook, spread on a container to handle
7473
navigation of components inside it.
7574

76-
```tsx example export=true render=false
75+
```tsx example
7776
import {useToolbar} from '@react-aria/toolbar';
7877
import {useRef} from 'react';
7978

@@ -91,11 +90,7 @@ function Toolbar(props) {
9190
</div>
9291
);
9392
}
94-
```
9593

96-
Now we can render a simple toolbar with actionable items:
97-
98-
```tsx example
9994
<Toolbar aria-label="Actions">
10095
<Button>Copy</Button>
10196
<Button>Cut</Button>
@@ -112,8 +107,6 @@ Now we can render a simple toolbar with actionable items:
112107
display: flex;
113108
flex-wrap: wrap;
114109
gap: 5px;
115-
padding: 15px;
116-
border: 1px solid var(--separator-color);
117110
}
118111
```
119112

@@ -141,12 +134,10 @@ function Button(props) {
141134
<span
142135
{...buttonProps}
143136
style={{
144-
background: isPressed ? 'darkgreen' : 'green',
145-
color: 'white',
146-
padding: 10,
147-
cursor: 'pointer',
148-
userSelect: 'none',
149-
WebkitUserSelect: 'none'
137+
background: isPressed ? '#bbb' : '#aaa',
138+
color: 'black',
139+
cursor: 'default',
140+
padding: '5px 10px'
150141
}}
151142
ref={ref}>
152143
{children}

packages/@react-aria/toolbar/src/useToolbar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export interface ToolbarAria {
3232
}
3333

3434
/**
35-
* Handles interactions for toolbar elements, such as keyboard navigation between elements.
35+
* Provides the behavior and accessibility implementation for a toolbar.
36+
* A toolbar is a container for a set of interactive controls with arrow key navigation.
3637
* @param props - Props to be applied to the toolbar.
3738
* @param ref - A ref to a DOM element for the toolbar.
3839
*/

packages/react-aria-components/docs/Toolbar.mdx

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default Layout;
1212

1313
import docs from 'docs:react-aria-components';
1414
import typesDocs from 'docs:@react-types/shared/src/events.d.ts';
15-
import {PropTable, HeaderInfo, TypeLink, PageDescription, ContextTable} from '@react-spectrum/docs';
15+
import {PropTable, HeaderInfo, TypeLink, PageDescription, ContextTable, StateTable} from '@react-spectrum/docs';
1616
import styles from '@react-spectrum/docs/src/docs.css';
1717
import packageData from 'react-aria-components/package.json';
1818
import Anatomy from '@react-aria/toolbar/docs/toolbar-anatomy.svg';
@@ -38,27 +38,20 @@ type: component
3838
## Example
3939

4040
```tsx example
41-
import {Toolbar, Button, ToggleButton, Separator, Popover, MenuTrigger, Menu, MenuItem, Checkbox} from 'react-aria-components';
42-
43-
<Toolbar>
44-
<ToggleButton><strong>B</strong></ToggleButton>
45-
<ToggleButton><div style={{textDecoration: 'underline'}}>U</div></ToggleButton>
46-
<ToggleButton><i>I</i></ToggleButton>
41+
import {Toolbar, Button, ToggleButton, Separator, Checkbox, Group} from 'react-aria-components';
42+
43+
<Toolbar aria-label="Text formatting">
44+
<Group aria-label="Style">
45+
<ToggleButton aria-label="Bold"><b>B</b></ToggleButton>
46+
<ToggleButton aria-label="Italic"><i>I</i></ToggleButton>
47+
<ToggleButton aria-label="Underline"><u>U</u></ToggleButton>
48+
</Group>
4749
<Separator orientation="vertical" />
48-
<Button>Copy</Button>
49-
<Button>Paste</Button>
50-
<Button>Cut</Button>
51-
<Separator orientation="vertical" />
52-
<MenuTrigger>
53-
<Button>Alignment</Button>
54-
<Popover>
55-
<Menu>
56-
<MenuItem>Left</MenuItem>
57-
<MenuItem>Center</MenuItem>
58-
<MenuItem>Right</MenuItem>
59-
</Menu>
60-
</Popover>
61-
</MenuTrigger>
50+
<Group aria-label="Clipboard">
51+
<Button>Copy</Button>
52+
<Button>Paste</Button>
53+
<Button>Cut</Button>
54+
</Group>
6255
<Separator orientation="vertical" />
6356
<Checkbox>
6457
<div className="checkbox">
@@ -77,8 +70,6 @@ import {Toolbar, Button, ToggleButton, Separator, Popover, MenuTrigger, Menu, Me
7770
@import './Checkbox.mdx' layer(checkbox);
7871
@import './Button.mdx' layer(button);
7972
@import './ToggleButton.mdx' layer(togglebutton);
80-
@import './Popover.mdx' layer(popover);
81-
@import './Menu.mdx' layer(menu);
8273
```
8374

8475
```css
@@ -87,46 +78,104 @@ import {Toolbar, Button, ToggleButton, Separator, Popover, MenuTrigger, Menu, Me
8778
display: flex;
8879
flex-wrap: wrap;
8980
gap: 5px;
90-
padding: 15px;
91-
border: 1px solid var(--separator-color);
92-
93-
&[data-orientation=vertical] {
94-
flex-direction: column;
95-
}
9681

9782
&[data-orientation=horizontal] {
9883
flex-direction: row;
9984
}
100-
}
10185

102-
.react-aria-ToggleButton {
103-
width: 32px;
86+
.react-aria-Group {
87+
display: contents;
88+
}
89+
90+
.react-aria-ToggleButton {
91+
width: 32px;
92+
}
10493
}
10594

95+
10696
.react-aria-Separator {
107-
width: 2px;
108-
margin: 0px 10px;
10997
align-self: stretch;
11098
background-color: var(--separator-color);
99+
100+
&[aria-orientation=vertical] {
101+
width: 1px;
102+
margin: 0px 10px;
103+
}
111104
}
112105
```
113106

114107
</details>
115108

116109
## Features
117110

118-
Toolbar provides keyboard navigation and announcements for groups of interactive elements.
119-
`Toolbar` helps implement these in an accessible way.
111+
There is no built-in HTML toolbar element. `Toolbar` helps achieve accessible toolbars with arrow key navigation that can be styled as needed.
120112

121-
* **Flexible** – Support for various RAC or Aria Hooks implemented components.
122-
* **Accessible** – Follows the [ARIA toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/) with support for keyboard arrow key navigation and is a single tab stop.
123-
* **Styleable** – Can be styled in any custom way, including extra DOM structure between the Toolbar and the interactive children.
113+
* **Flexible** – Support for interactive children such as buttons, checkboxes, dropdown menus, etc. in a horizontal or vertical orientation.
114+
* **Accessible** – Follows the [ARIA toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/) with support for arrow key navigation as a single tab stop.
124115

125116
## Anatomy
126117

127118
<Anatomy />
128119

129-
Toolbar consists of a wrapping element which controls arrow key navigation depending on the orientation and children.
120+
A toolbar consists of a container element for a set of interactive controls. It provides arrow key navigation between its children, in either horizontal or vertical orientation.
121+
122+
```tsx
123+
import {Toolbar} from 'react-aria-components';
124+
125+
<Toolbar>
126+
{/* ... */}
127+
</Toolbar>
128+
```
129+
130+
## Orientation
131+
132+
By default, toolbars are horizontally oriented. The `orientation` prop can be set to `"vertical"` to change the arrow key navigation behavior.
133+
134+
```tsx example
135+
import Move from '@spectrum-icons/workflow/Move';
136+
import Select from '@spectrum-icons/workflow/Select';
137+
import Polygon from '@spectrum-icons/workflow/PolygonSelect';
138+
import Brush from '@spectrum-icons/workflow/Brush';
139+
import Pencil from '@spectrum-icons/workflow/Edit';
140+
141+
<Toolbar aria-label="Tools" orientation="vertical">
142+
<Group aria-label="Select">
143+
<Button aria-label="Move"><Move size="S" /></Button>
144+
<Button aria-label="Rectangle"><Select size="S" /></Button>
145+
<Button aria-label="Polygon"><Polygon size="S" /></Button>
146+
</Group>
147+
<Separator orientation="horizontal" />
148+
<Group aria-label="Draw">
149+
<Button aria-label="Brush"><Brush size="S" /></Button>
150+
<Button aria-label="Pencil"><Pencil size="S" /></Button>
151+
</Group>
152+
</Toolbar>
153+
```
154+
155+
<details>
156+
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show CSS</summary>
157+
158+
```css
159+
.react-aria-Toolbar {
160+
width: fit-content;
161+
162+
&[data-orientation=vertical] {
163+
flex-direction: column;
164+
align-items: start;
165+
}
166+
}
167+
168+
.react-aria-Separator {
169+
&:not([aria-orientation=vertical]) {
170+
border: none;
171+
height: 1px;
172+
width: 100%;
173+
margin: 10px 0;
174+
}
175+
}
176+
```
177+
178+
</details>
130179

131180
## Props
132181

@@ -163,6 +212,22 @@ A custom `className` can also be specified on any component. This overrides the
163212
</Toolbar>
164213
```
165214

215+
The `className` and `style` props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like [Tailwind](https://tailwindcss.com/).
216+
217+
```jsx
218+
<Toolbar className={({orientation}) => orientation === 'vertical' ? 'flex-col' : 'flex-row'}>
219+
{/* ... */}
220+
</Toolbar>
221+
```
222+
223+
The selectors and render props for each component used in a `Toolbar` are documented below.
224+
225+
### Toolbar
226+
227+
A `Toolbar` can be targeted with the `.react-aria-Toolbar` CSS selector, or by overriding with a custom `className`. It supports the following states and render props:
228+
229+
<StateTable properties={docs.exports.ToolbarRenderProps.properties} />
230+
166231
### Separator
167232

168233
A `Separator` can be targeted with the `.react-aria-Separator` CSS selector, or by overriding with a custom `className`.

packages/react-aria-components/src/Toolbar.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function Toolbar(props: ToolbarProps, ref: ForwardedRef<HTMLDivElement>) {
5353
}
5454

5555
/**
56-
* A toolbar is a container for grouping a set of controls, such as buttons, menubuttons, or checkboxes.
56+
* A toolbar is a container for a set of interactive controls, such as buttons, dropdown menus, or checkboxes,
57+
* with arrow key navigation.
5758
*/
5859
const _Toolbar = /*#__PURE__*/ (forwardRef as forwardRefType)(Toolbar);
5960
export {_Toolbar as Toolbar};

0 commit comments

Comments
 (0)