Skip to main content

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:

variantWhat 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.
dockedA full-width 64dp bar at the bottom edge, with no corner and no shadow. See Docked toolbar.
A toolbar is not a navigator

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.

colorStyleContainerIconsSelected toggle
standard (default)surfaceContaineronSurfaceVariantsecondaryContainer, onSecondaryContainer icon
vibrantprimaryContaineronPrimaryContainersurfaceContainer, 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:

arrangementLayout
space-between (default)The first item at the start, the last at the end, and the others evenly between, at least 4dp apart
fixedThe items centred, 32dp apart

Safe area​

The docked toolbar pads the safe area the same way as BottomAppBar:

  • insetBottom adds 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 as Layout with a bottom edge, already pads that edge.
  • insetSides pads the row by the left and right insets, so the items stay clear of the notch in landscape. It's on by default. Inside a Layout that pads the sides, the background reaches the screen edge. Set it to false for 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​

PropTarget
containerColorThe toolbar background
contentColorThe icons of the standard icon buttons that aren't selected, and the color that useAppBarContentColor() returns
styleThe toolbar container

A selected toggle keeps the selected colors of the color style. Pass containerColor to the IconButton to change its fill.

Tokens​

TokenFloatingDocked
ContainersurfaceContainer or primaryContainersurfaceContainer or primaryContainer
Elevationlevel 3none
Cornerfullnone
Size64dp tall, or 64dp wide when vertical64dp tall (+ bottom inset with insetBottom)
Padding8dp on each side16dp at each end
Between the items4dp4dp at least, or 32dp with arrangement="fixed"
Distance to the screen edge16dp, or 24dp when vertical0

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". Pass role to 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 button named by its accessibilityLabel.
  • A toggle reports its state: aria-pressed on the web, and selected on iOS and Android.
  • A disabled icon button exposes accessibilityState.disabled.

Props​

PropTypeDefaultRequiredDescription
childrenReactNode-NoThe 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()`.
colorStyleenumstandardNoThe MD3 color style.
containerColorstringsurfaceContainer, or primaryContainer when vibrantNoOverride the toolbar background.
contentColorstringonSurfaceVariant, or onPrimaryContainer when vibrantNoOverride 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.
styleStyleProp<ViewStyle>-NoStyle applied to the toolbar container.
variantenumfloatingNoThe MD3 toolbar type.
orientationenumhorizontalNoThe direction of the toolbar.
arrangementenumspace-betweenNoHow the items are spaced.
insetBottombooleanNoWhen `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.
insetSidesbooleanNoWhen `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.