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.
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.
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 |
Source: Compose FloatingToolbarTokens, DockedToolbarTokens, StandardToolbarTokens and
VibrantToolbarTokens. The Compose code draws the floating toolbar at elevation level 0 and with
no space between the items, 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.
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. 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. |
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. |
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. |