Toolbar
A toolbar shows a row of actions for the current screen. The component follows the
Material Design 3 Toolbars specification,
from MD3 Expressive. It has two types, which variant sets:
variant | What it is |
|---|---|
floating (default) | A 64dp pill at elevation level 3 that floats over the content. It sizes to its content, with 8dp of padding on each side and 4dp between the items. It's horizontal or vertical. |
docked | A full-width 64dp bar at the bottom edge, with no corner and no shadow. See Docked toolbar. |
Toolbar holds actions for the current screen. For top-level destinations, use
NavigationBar.
Usage
Put the actions in the toolbar as children. A standard IconButton takes the toolbar colors
with no prop. Each icon button needs an accessibilityLabel.
import { IconButton, Toolbar } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { useState } from 'react'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'
export default function App() {
const [bold, setBold] = useState(true)
const [italic, setItalic] = useState(false)
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Toolbar>
<IconButton
variant="standard"
icon="format-bold"
accessibilityLabel="Bold"
selected={bold}
onSelectedChange={setBold}
/>
<IconButton
variant="standard"
icon="format-italic"
accessibilityLabel="Italic"
selected={italic}
onSelectedChange={setItalic}
/>
<IconButton
variant="standard"
icon="link-variant"
accessibilityLabel="Insert link"
onPress={() => {}}
/>
</Toolbar>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}
The toolbar doesn't change the default variant of IconButton, which is filled. Pass
variant="standard" for the MD3 toolbar look.
Color styles
colorStyle sets the MD3 color style.
colorStyle | Container | Icons | Selected toggle |
|---|---|---|---|
standard (default) | surfaceContainer | onSurfaceVariant | secondaryContainer, onSecondaryContainer icon |
vibrant | primaryContainer | onPrimaryContainer | surfaceContainer, onSurface icon |
Use vibrant when the toolbar needs more emphasis than the content around it.
<Toolbar colorStyle="vibrant">
<IconButton variant="standard" icon="format-bold" accessibilityLabel="Bold" selected={bold} onSelectedChange={setBold} />
</Toolbar>
Selected toggles
A standard IconButton with selected or defaultSelected is a toggle. In a toolbar, a
selected toggle fills with the selected container of the color style, and its icon takes the
selected icon color. Outside a toolbar, a selected standard toggle has no fill and a primary
icon.
A filled, tonal or outlined IconButton keeps its own colors in a toolbar.
Place a floating toolbar
A floating toolbar doesn't place itself. Put it over the content with style. MD3 puts a
horizontal floating toolbar 16dp from the edge of the screen:
<View style={{ flex: 1 }}>
<ScrollView contentContainerStyle={{ paddingBottom: 80 }}>...</ScrollView>
<Toolbar style={{ position: 'absolute', bottom: 16, alignSelf: 'center' }}>...</Toolbar>
</View>
The content doesn't make room for the toolbar. Pad the end of the content by the toolbar height and its offset, 80dp, so the last item can scroll clear of it.
Vertical toolbar
orientation="vertical" lays the items out in a column, in a pill 64dp wide. Use it for a
palette of tools at the side of the screen. MD3 puts it 24dp from the side.
const [tool, setTool] = useState('draw')
<View style={{ flex: 1 }}>
<ScrollView>...</ScrollView>
<View style={[StyleSheet.absoluteFill, styles.toolbarLayer]}>
<Toolbar orientation="vertical" style={styles.toolbar}>
<IconButton variant="standard" icon="pencil-outline" accessibilityLabel="Draw" selected={tool === 'draw'} onSelectedChange={() => setTool('draw')} />
<IconButton variant="standard" icon="eraser" accessibilityLabel="Erase" selected={tool === 'erase'} onSelectedChange={() => setTool('erase')} />
<IconButton variant="standard" icon="shape-outline" accessibilityLabel="Shapes" selected={tool === 'shapes'} onSelectedChange={() => setTool('shapes')} />
</Toolbar>
</View>
</View>
const styles = StyleSheet.create({
// A layer over the content that puts the toolbar at the end edge. It takes
// no touches itself, so the content under it still scrolls.
toolbarLayer: {
flexDirection: 'row',
justifyContent: 'flex-end',
paddingHorizontal: 24,
pointerEvents: 'box-none',
},
toolbar: { alignSelf: 'center' },
})
The layer uses justifyContent: 'flex-end', so the toolbar moves to the left edge in a
right-to-left layout. The toolbar sets alignSelf: 'flex-start' by default, so pass
alignSelf: 'center' to centre it on the side.
orientation is floating only. A docked toolbar is always horizontal, and orientation on it
is a type error.
Expand and collapse
A floating toolbar can hold content that shows only while expanded is true:
leadingsits before the middle content: at the start, or at the top of a vertical toolbar.trailingsits after it: at the end, or at the bottom of a vertical toolbar.- The children are the middle content. It always shows.
const [expanded, setExpanded] = useState(true)
<Toolbar
expanded={expanded}
leading={<IconButton variant="standard" icon="undo" accessibilityLabel="Undo" onPress={undo} />}
trailing={<IconButton variant="standard" icon="dots-vertical" accessibilityLabel="More" onPress={openMenu} />}
>
<IconButton variant="standard" icon="pencil" accessibilityLabel="Edit" onPress={edit} />
</Toolbar>
A change of expanded opens or closes the two slots with the MD3 fast spatial spring. Each slot
opens as a clip from its outer edge, as in Compose, with no fade. A closed slot leaves the
screen-reader tree and the tab order, so no one can reach a control that isn't in view.
While a screen reader is on (iOS and Android), the toolbar stays expanded. expanded,
leading and trailing are floating only. On a docked toolbar, each one is a type error.
With a FAB
fab puts a FAB beside a floating toolbar, 8dp away. The toolbar draws the FAB itself, in the FAB
colors of its color style: secondaryContainer when standard, tertiaryContainer when vibrant.
const [expanded, setExpanded] = useState(true)
<Toolbar
expanded={expanded}
fab={{ icon: 'plus', accessibilityLabel: 'New message', onPress: compose }}
>
<IconButton variant="standard" icon="reply" accessibilityLabel="Reply" onPress={reply} />
<IconButton variant="standard" icon="archive-outline" accessibilityLabel="Archive" onPress={archive} />
</Toolbar>
With a FAB, expanded decides whether the toolbar shows at all:
| Toolbar | FAB | |
|---|---|---|
| Expanded | Shows, at elevation level 1 | 56dp, 16dp corner, 24dp icon, elevation level 1 |
| Collapsed | Closes toward the FAB, and leaves the screen-reader tree and the tab order | 80dp, 20dp corner, 28dp icon, elevation level 2 |
A change animates with the MD3 fast spatial spring. The FAB keeps its place: it grows toward the toolbar, and the space of the toolbar stays reserved. While a screen reader is on (iOS and Android), the toolbar stays expanded.
fabPosition sets the side: end (the default) puts the FAB after the toolbar, at the end or at
the bottom of a vertical toolbar, and start puts it before. fab and fabPosition are floating
only, and fab with leading or trailing is a type error: Compose has no slots beside a FAB.
Docked toolbar
variant="docked" gives a full-width bar for the bottom of a screen. MD3 recommends it in place
of the bottom app bar. It's 64dp tall, on the same colors as the floating
toolbar, with 16dp of padding at each end. It has no corner and no shadow.
<View style={{ flex: 1 }}>
<ScrollView style={{ flex: 1 }}>...</ScrollView>
<Toolbar variant="docked" insetBottom>
<IconButton variant="standard" icon="arrow-left" accessibilityLabel="Back" onPress={goBack} />
<IconButton variant="standard" icon="share-variant" accessibilityLabel="Share" onPress={share} />
<IconButton variant="standard" icon="bookmark-outline" accessibilityLabel="Save" onPress={save} />
</Toolbar>
</View>
arrangement sets how the items are spaced:
arrangement | Layout |
|---|---|
space-between (default) | The first item at the start, the last at the end, and the others evenly between, at least 4dp apart |
fixed | The items centred, 32dp apart |
Safe area
The docked toolbar pads the safe area the same way as BottomAppBar:
insetBottomadds the bottom inset below the 64dp row, so the toolbar clears the home indicator. It's off by default. Leave it off when an ancestor, such asLayoutwith abottomedge, already pads that edge.insetSidespads the row by the left and right insets, so the items stay clear of the notch in landscape. It's on by default. Inside aLayoutthat pads the sides, the background reaches the screen edge. Set it tofalsefor a toolbar inside content.
arrangement, insetBottom and insetSides are docked only. On a floating toolbar, each one is a
type error.
Hide on scroll
Pass the vertical scroll offset of the content as scrollOffset, and the toolbar moves out of
view while the content scrolls down. It comes back while the content scrolls up. Take the offset
from useScroll() of @rootnative/inertia, and give its onScroll to your scroll view:
import { Motion, useScroll } from '@rootnative/inertia'
const { scrollY, onScroll } = useScroll()
<View style={{ flex: 1 }}>
<Motion.ScrollView onScroll={onScroll} scrollEventThrottle={16}>...</Motion.ScrollView>
<Toolbar scrollOffset={scrollY} style={{ position: 'absolute', bottom: 16, alignSelf: 'center' }}>
...
</Toolbar>
</View>
- A docked toolbar moves down by its own height, which includes the bottom inset with
insetBottom. - A floating toolbar leaves by
hideEdge:bottomby default, orendfor a vertical toolbar.topandstartare the other edges, andstartandendfollow the layout direction. - A floating toolbar moves by its own size, the MD3 distance to the edge (16dp, or 24dp for a vertical toolbar) and the safe-area inset of that edge. Place it that far from the edge, or a strip stays in view.
The toolbar moves and the content doesn't, so place the toolbar over the content. On iOS, keep
contentInsetAdjustmentBehavior of the scroll view at 'never', as for
BottomAppBar.
hideEdge is floating only. On a docked toolbar it's a type error.
Other content
A toolbar can hold buttons and other controls too. To color custom content, read the icon color
with useAppBarContentColor() from @rootnative/components/appbar.
import { useAppBarContentColor } from '@rootnative/components/appbar'
function WordCount({ count }: { count: number }) {
const color = useAppBarContentColor()
return <Text style={{ color }}>{count} words</Text>
}
Overrides
| Prop | Target |
|---|---|
containerColor | The toolbar background |
contentColor | The icons of the standard icon buttons that aren't selected, and the color that useAppBarContentColor() returns |
style | The toolbar container |
A selected toggle keeps the selected colors of the color style. Pass containerColor to the
IconButton to change its fill.
Tokens
| Token | Floating | Docked |
|---|---|---|
| Container | surfaceContainer or primaryContainer | surfaceContainer or primaryContainer |
| Elevation | level 3 | none |
| Corner | full | none |
| Size | 64dp tall, or 64dp wide when vertical | 64dp tall (+ bottom inset with insetBottom) |
| Padding | 8dp on each side | 16dp at each end |
| Between the items | 4dp | 4dp at least, or 32dp with arrangement="fixed" |
| Distance to the screen edge | 16dp, or 24dp when vertical | 0 |
| FAB | 56dp, or 80dp collapsed; 8dp from the toolbar | — |
Source: Compose FloatingToolbarTokens, FloatingToolbarFabTokens, DockedToolbarTokens,
StandardToolbarTokens and VibrantToolbarTokens. The Compose code draws the floating toolbar at
elevation level 0, with no space between the items, and the standard FAB on primaryContainer,
with a TODO to read the tokens. This library follows the tokens.
Accessibility
- Both types report
role="toolbar". Passroleto replace it. - On the web, a vertical toolbar sets
aria-orientation="vertical". ARIA reads a toolbar with no orientation as horizontal. - Each icon button is a
buttonnamed by itsaccessibilityLabel. - A toggle reports its state:
aria-pressedon the web, and selected on iOS and Android. - A disabled icon button exposes
accessibilityState.disabled. - With
scrollOffset, the toolbar stays in place while a screen reader is on (iOS and Android), so the reader never moves to a control off screen. - With
leadingortrailing, the toolbar stays expanded while a screen reader is on (iOS and Android). A closed slot is out of the screen-reader tree and the tab order. - The FAB is a
buttonnamed byaccessibilityLabel. With a FAB, the toolbar stays expanded while a screen reader is on, and a collapsed toolbar is out of the screen-reader tree and the tab order. - On the web, focus on a control in a hidden toolbar brings the toolbar back, so Tab never reaches a control off screen.
Props
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
children | ReactNode | - | No | The content of the toolbar, in one row or one column: icon buttons, buttons, or a text field. With `leading` or `trailing`, it is the middle content, which always shows. A `'standard'` `IconButton` takes the toolbar colors with no prop, also as a selected toggle. Other content reads the icon color with `useAppBarContentColor()`. |
colorStyle | enum | standard | No | The MD3 color style. |
containerColor | string | surfaceContainer, or primaryContainer when vibrant | No | Override the toolbar background. |
contentColor | string | onSurfaceVariant, or onPrimaryContainer when vibrant | No | Override the icon color of the content. It reaches each `'standard'` `IconButton` that isn't selected. A selected toggle keeps the selected colors of the color style. |
scrollOffset | SharedValue<number> | - | No | Vertical scroll offset (in px) of the content behind the toolbar, as a Reanimated shared value. Pass `scrollY` from `@rootnative/inertia`'s `useScroll()` and give the matching `onScroll` to your scroll view. Drives the MD3 hide-on-scroll behavior: the toolbar moves out of view while the content scrolls down, and comes back while the content scrolls up. A docked toolbar moves down by its own height. A floating toolbar leaves by `hideEdge`. The toolbar moves and the content does not, so place the toolbar over the content. While a screen reader is on (iOS and Android), the toolbar stays in place. On web, focus on a control in the toolbar brings it back. |
style | StyleProp<ViewStyle> | - | No | Style applied to the toolbar container. |
variant | enum | floating | No | The MD3 toolbar type. |
orientation | enum | horizontal | No | The direction of the toolbar. |
hideEdge | enum | 'bottom', or 'end' when vertical | No | The screen edge the toolbar leaves by on scroll, with `scrollOffset`. It moves by its own size, the MD3 distance to the edge (16dp, or 24dp when vertical) and the safe-area inset of that edge. Place it that far from the edge, or a strip stays in view. |
expanded | boolean | | No | Whether the toolbar is expanded. Without a FAB, it decides whether `leading` and `trailing` show; the middle content always shows. With a FAB, it decides whether the toolbar shows at all: collapsed, only the FAB stays, at 80dp. A change animates with the MD3 fast spatial spring. While a screen reader is on (iOS and Android), the toolbar stays expanded, so no control goes out of reach. |
arrangement | enum | space-between | No | How the items are spaced. |
insetBottom | boolean | | No | When `true`, adds the bottom safe-area inset below the 64dp row, so the toolbar clears the home indicator. Leave it off when an ancestor (for example `Layout` with a `bottom` edge) already handles it. |
insetSides | boolean | | No | When `true`, pads the row by the left and right safe-area insets, and inside a `Layout` that pads the sides pulls the background back through that padding. In landscape the toolbar stays full-bleed and its items stay clear of the notch. Set it to `false` for a toolbar nested inside content. |
leading | ReactNode | - | No | Content before the middle content: at the start when horizontal, at the top when vertical. It shows only while `expanded` is `true`. |
trailing | ReactNode | - | No | Content after the middle content: at the end when horizontal, at the bottom when vertical. It shows only while `expanded` is `true`. |
fab | ToolbarFab | - | No | A FAB beside the toolbar, 8dp away. While the toolbar is expanded, the FAB is 56dp; collapsed, the toolbar closes and the FAB grows to 80dp. |
fabPosition | enum | end | No | Where the FAB sits. |