Feedback
Snackbar
A snackbar is a brief message near the bottom of the screen that confirms what the person just did and, at most, offers one way to undo it.
In kanso-ui as Snackbar
Web and native
Snackbar in kanso-ui 0.59.0, for React.
import { Snackbar } from '@kanso-labs/kanso-ui'A brief message at the bottom of the window, with an optional action and close button, shown from a queue you can reach from anywhere.
It follows Material Design's snackbar page: an inverse surface strip at a 4dp corner and elevation 3, body medium text in inverse on surface, the action in inverse primary, 8dp from the window's edges, and between 320 and 576dp wide above the 600dp breakpoint (full width below it). It shows one message at a time: a new one replaces the one on screen, and nothing waits behind it.
What you provide
- A
Snackbar.Queue, made once where the whole app can reach it. - One
<Snackbar queue={…} />, mounted once near the root. It is portalled to the end of<body>and renders nothing while the queue is empty. - Messages, through
queue.add(message, options)from anywhere.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | — | What a screen reader calls the region holding the snackbar. Left out, React Aria names the region itself: a count of the messages it holds, in the reader's own locale — "1 notification." in English, and one of some thirty languages otherwise. Pass a name only to replace that, and pass a translated one, since whatever is passed is read as written. |
className | ToastRegionProps<SnackbarMessage>['className'] | — | A function may compute the class from the region's render state. |
closeLabel | string | — | What a screen reader calls the button that dismisses a snackbar. It sits here rather than on a message's own options because it does not vary by message, so putting it there would make every `add` call repeat it. Left out, React Aria names it in the reader's locale — "Close" in English. |
queue required | SnackbarQueue | — | The queue the region shows messages from. |
style | ToastRegionProps<SnackbarMessage>['style'] | — | A function may compute the style from the region's render state. |
Also takes the props of Omit<ToastRegionProps<SnackbarMessage>, 'aria-label' | 'children' | 'className' | 'queue' | 'style'>.
Parts
Snackbar.Queue
add(message, options)— shows a message in place of the one on screen and returns its key. Options:action:{ label, onPress }— one action after the message. Pressing it runsonPressand closes the snackbar.tone:'failure'— leads the message with a warning glyph in the inverse error, so a failure does not look like a confirmation. Left out, the message is a confirmation. The glyph is not announced, so the words must say what failed.showCloseButton:boolean, defaultfalse— a close button at the end.timeout: milliseconds, held to at least four seconds so a message can be read. Five seconds for a message alone; a message with an action stays until it is taken or closed.0keeps any message up until something closes it. Timers pause while the region is hovered or focused.onClose: runs however the snackbar closes, including being replaced.
close(key)closes one message;clear()closes the one on screen. A snackbar fades out as it leaves.
Example
import { Snackbar } from '@kanso-labs/kanso-ui'
const messages = new Snackbar.Queue()
<Snackbar queue={messages} />
messages.add('First item saved', {
action: { label: 'Undo', onPress: undo },
})The design’s classes
k-snackbar, k-snackbar__text, with k-btn k-btn--text and k-iconbtn inside. The accent-on-ink focus ring is written in the preview until bundle.css has it.
Maps to Snackbar in kanso-ui and react-native-kanso-ui.