Snackbar
A snackbar reports the outcome of an action in a brief message at the bottom of the screen. It follows the Material Design 3 Snackbar specification.
Snackbar is imperative only. There is no <Snackbar visible> component. The hard part of a
snackbar is the queue: one message at a time, in order, each with its own timer. A declarative
API would push that onto you. SnackbarProvider owns the queue, and useSnackbar() gives you
show, hide, and clear.
Setup
Mount SnackbarProvider once, inside ThemeProvider and inside the PortalHost of your app.
Snackbars render into the PORTAL_LAYERS.snackbar layer, which puts them above dialogs and bottom sheets.
import { Button, PortalHost, SnackbarProvider, useSnackbar } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'
function Screen() {
const snackbar = useSnackbar()
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => snackbar.show({ message: 'Photo saved' })}>
Save photo
</Button>
</View>
)
}
export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<PortalHost>
<SnackbarProvider>
<Screen />
</SnackbarProvider>
</PortalHost>
</ThemeProvider>
</SafeAreaProvider>
)
}
useSnackbar() throws if there is no provider above it. A silent no-op would look like a bug in your own code.
Actions
A snackbar takes one action. A press runs onAction and then dismisses the snackbar with the reason 'action'.
import { Button, PortalHost, SnackbarProvider, useSnackbar } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'
function Screen() {
const snackbar = useSnackbar()
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
variant="tonal"
onPress={() =>
snackbar.show({
message: 'Message deleted',
actionLabel: 'Undo',
onAction: () => console.log('undo'),
})
}
>
Delete message
</Button>
</View>
)
}
export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<PortalHost>
<SnackbarProvider>
<Screen />
</SnackbarProvider>
</PortalHost>
</ThemeProvider>
</SafeAreaProvider>
)
}
Duration
duration | Meaning |
|---|---|
'short' | 4s, the MD3 default |
'long' | 10s |
'indefinite' | Stays until the action, the close button, or hide() |
number | Milliseconds |
A snackbar with an actionLabel defaults to 'indefinite', not 'short'. An undo the
user never sees is worse than one that waits. A snackbar without an action defaults to 'short'.
The clock pauses while a pointer is over the snackbar. It continues from where it stopped when the pointer leaves, so a message doesn't disappear from a reader who moved to it.
Set showCloseIcon to add a trailing close button. Root Native warns in development if an
indefinite snackbar has neither an action nor a close button. That combination can't be
dismissed, and it stalls everything queued behind it.
Queueing
One snackbar is visible at a time. A show() while one is up queues the new message behind it
and returns an id. The queue drains in FIFO order as the timer of each snackbar expires.
const first = snackbar.show({ message: 'First' })
snackbar.show({ message: 'Second' }) // waits its turn
snackbar.hide(first) // dismiss a specific one, visible or queued
snackbar.hide() // dismiss whatever is visible
snackbar.clear() // dismiss the visible one and drop the queue
Pass replace: true to jump ahead of the visible snackbar instead of a place in the queue. The
replaced snackbar dismisses with the reason 'replaced'.
onDismiss receives the reason the snackbar went away: 'timeout', 'action', 'close',
'replaced', or 'manual'.
Positioning
The snackbar anchors to the bottom of the screen with 16dp margins, and it obeys the safe-area bottom inset. The layer itself applies both, so an offset you pass covers only what sits on top of them: a FAB or a bottom navigation bar.
snackbarOffsetFor(height) does that arithmetic, and FAB_SIZES gives the height:
import { FAB_SIZES, snackbarOffsetFor } from '@rootnative/components'
snackbarOffsetFor(FAB_SIZES.medium) // 56 + 16 = 72
A hard-coded number is the trap this replaces. A literal 88 counts the 16dp margin
twice, because the layer already added it, and pushes the snackbar 32dp too high.
App-wide vs per-screen
Use the bottomOffset prop of the provider when every screen has the same controls at the
bottom:
<SnackbarProvider bottomOffset={snackbarOffsetFor(FAB_SIZES.medium)}>
{children}
</SnackbarProvider>
When only some screens have a FAB, raise the offset from the screen that owns it.
useSnackbarOffset applies while its component is mounted, and reverts on unmount.
So the constant lives next to the component that decides it, not in the app root two
files away:
import { FAB, FAB_SIZES, snackbarOffsetFor, useSnackbarOffset } from '@rootnative/components'
function ComposeScreen() {
useSnackbarOffset(snackbarOffsetFor(FAB_SIZES.medium))
return <FAB icon="plus" accessibilityLabel="Compose" onPress={compose} />
}
A mounted useSnackbarOffset wins over the prop of the provider. With several mounted,
which happens during a navigation transition while the outgoing screen has not unmounted
yet, the largest applies, so the snackbar clears every FAB on screen. 0 has a meaning:
it overrides the prop down to zero for that screen.
The layer is pointerEvents="box-none", so a visible snackbar never blocks the UI behind it.
Tokens
| Token | Value |
|---|---|
| Container | inverseSurface, corner 4dp (cornerExtraSmall), elevation level 3 |
| Message | bodyMedium / inverseOnSurface |
| Action label | labelLarge / inversePrimary |
| Close icon | 24dp / inverseOnSurface |
| Height | 48dp single-line, 68dp two-line |
| Width | Full width minus 16dp margins, capped at 600dp |
containerColor, contentColor, and actionColor on one snackbar override the defaults,
for example for an error-styled message.
Motion
A snackbar rises 24dp and fades in on springDefaultSpatial, and reverses on the way out.
Under reduced motion, both go to their end values at once; see Motion.
Accessibility
The surface reports role="alert" with accessibilityLiveRegion="polite", so
TalkBack and web screen readers announce the message and don't steal focus.
iOS has no live region, so the snackbar also calls
AccessibilityInfo.announceForAccessibility for VoiceOver when it mounts and
when a replacement arrives. The close button is labelled "Dismiss". Override it
with closeAccessibilityLabel.
SnackbarProvider Props
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
bottomOffset | number | 0 | No | Extra space below the snackbar, on top of the safe-area bottom inset. Set this to clear a FAB or a bottom navigation bar. |
style | StyleProp<ViewStyle> | - | No | Style applied to the snackbar surface. |
show() options
SnackbarOptions is a plain object, not a component, so this page documents it by hand. It is
not generated from the source.
| Option | Type | Default | Description |
|---|---|---|---|
message | string | — | The message. Required. |
actionLabel | string | — | The label of the one trailing action. |
onAction | () => void | — | Runs on a press of the action. The snackbar then dismisses. |
duration | 'short' | 'long' | 'indefinite' | number | 'short', or 'indefinite' with an actionLabel | Visible duration. |
showCloseIcon | boolean | false | Show a trailing close button. |
closeIcon | IconSource | 'close' | The icon of the close button. |
closeAccessibilityLabel | string | 'Dismiss' | The screen-reader label of the close button. |
onDismiss | (reason) => void | — | Called with 'timeout', 'action', 'close', 'replaced', or 'manual'. |
containerColor | string | inverseSurface | Override the container color. |
contentColor | string | inverseOnSurface | Override the message color. |
actionColor | string | inversePrimary | Override the action label color. |
replace | boolean | false | Replaces the visible snackbar instead of a place in the queue. |
useSnackbar()
| Method | Signature | Description |
|---|---|---|
show | (options: SnackbarOptions) => SnackbarId | Queues a snackbar and returns its id. |
hide | (id?: SnackbarId) => void | Dismisses the visible snackbar, or a specific one, visible or queued. |
clear | () => void | Dismisses the visible snackbar and drops the queue. |
The returned object keeps the same reference, so it is safe in a dependency array.