Skip to main content

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​

durationMeaning
'short'4s, the MD3 default
'long'10s
'indefinite'Stays until the action, the close button, or hide()
numberMilliseconds

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​

TokenValue
ContainerinverseSurface, corner 4dp (cornerExtraSmall), elevation level 3
MessagebodyMedium / inverseOnSurface
Action labellabelLarge / inversePrimary
Close icon24dp / inverseOnSurface
Height48dp single-line, 68dp two-line
WidthFull 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​

PropTypeDefaultRequiredDescription
bottomOffsetnumber0NoExtra space below the snackbar, on top of the safe-area bottom inset. Set this to clear a FAB or a bottom navigation bar.
styleStyleProp<ViewStyle>-NoStyle 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.

OptionTypeDefaultDescription
messagestring—The message. Required.
actionLabelstring—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 actionLabelVisible duration.
showCloseIconbooleanfalseShow a trailing close button.
closeIconIconSource'close'The icon of the close button.
closeAccessibilityLabelstring'Dismiss'The screen-reader label of the close button.
onDismiss(reason) => void—Called with 'timeout', 'action', 'close', 'replaced', or 'manual'.
containerColorstringinverseSurfaceOverride the container color.
contentColorstringinverseOnSurfaceOverride the message color.
actionColorstringinversePrimaryOverride the action label color.
replacebooleanfalseReplaces the visible snackbar instead of a place in the queue.

useSnackbar()​

MethodSignatureDescription
show(options: SnackbarOptions) => SnackbarIdQueues a snackbar and returns its id.
hide(id?: SnackbarId) => voidDismisses the visible snackbar, or a specific one, visible or queued.
clear() => voidDismisses the visible snackbar and drops the queue.

The returned object keeps the same reference, so it is safe in a dependency array.