Component · top layer
Snackbar
A short floating message that takes no space: above bars and dialogs, read out without moving focus, and gone after a few seconds unless it reports a problem.
Closed: . Undo pressed:. Retry pressed:.
Script
import { showSnackbar } from '@subfloor/core'
showSnackbar('Changes saved')
showSnackbar('Already being collected', { tone: 'info' })
showSnackbar('Could not save. Try again.', { tone: 'problem' }) // stays until closed
showSnackbar('Saved', { timeout: Infinity }) // stays too, as a status: for users who need time
const snackbar = showSnackbar('Project archived', {
action: { label: 'Undo', onClick: () => restore() },
timeout: 8000,
})
// After Undo: 'stay' takes the action away and keeps focus in the snackbar; 'close' closes it.
const archived = showSnackbar('Project archived', {
timeout: Infinity,
action: {
label: 'Undo',
onClick: () => {
restore()
archived.message.textContent = 'Project restored.'
},
then: 'stay',
},
})
// Disabled while its work runs: it keeps focus, and presses do nothing meanwhile.
const sync = showSnackbar('Could not sync.', {
tone: 'problem',
action: {
label: 'Retry',
onClick: async () => {
sync.setActionDisabled(true)
sync.message.textContent = 'Trying again…'
sync.message.textContent = (await retry()) ? 'Synced.' : 'Still offline.'
sync.setActionDisabled(false)
},
},
})
snackbar.element.addEventListener('sf:snackbar-close', (event) => {
console.log(event.detail.reason) // timeout | button | escape | replaced | api
})Astro (from the server)
For a message that comes with the page, such as after a form sent you here: see the example page.
---
import Snackbar from '@subfloor/astro/Snackbar.astro'
const sent = Astro.url.searchParams.has('sent')
---
{sent && <Snackbar>Message sent.</Snackbar>}React
'use client'
import { Snackbar } from '@subfloor/react'
{saved && (
<Snackbar
key={saved.id}
// disabled follows the prop while it is shown: it keeps focus (R-101).
action={{ label: 'Undo', onClick: undo, disabled: undoing }}
onClose={() => setSaved(null)}
>
{saved.text}
</Snackbar>
)}onClose: . Save:
Markup
<div class="sf-snackbar" data-sf-snackbar data-tone="done" role="status">
<span class="sf-snackbar__message">Changes saved</span>
<button type="button" class="sf-snackbar__action">Undo</button>
<button type="button" class="sf-snackbar__close" aria-label="Dismiss">…</button>
</div>Behaviour
- In the top layer (
popover="manual") at the bottom centre, above a fixed bottom bar (--sf-fixed-bottom) and the home indicator. - While a modal dialog is open, the snackbar is placed inside it: outside, the dialog would make it inert even though it is drawn on top. It moves back when the dialog closes.
role="status"fordoneandinfo,role="alert"for aproblem; the words follow the snackbar by a moment so screen readers read them. Focus never moves to it.doneandinfoclose after 6 s (timeout), paused while the pointer is over it or focus is in it (the time left is kept, not started over). Aproblemstays until closed.- The × closes it; Escape closes it while focus is in it, not the dialog around it. Focus goes back to where it came from.
- An action can be disabled while its work runs (
disabled,setActionDisabled(), or a change ofaction.disabledin React). It getsaria-disabled, notdisabled, so it keeps focus; presses do nothing until it is enabled again. The timeout runs as before. - One at a time: a new snackbar replaces the current one.
sf:snackbar-closereports why each one closed.
Without JavaScript
A snackbar rendered on the server still shows, fixed at the bottom. Its close button and timeout need the script; script snackbars need it anyway.
Customisation
:root {
--sf-snackbar-bg: #0f172a;
--sf-snackbar-max-width: 32rem;
--sf-fixed-bottom: 4rem; /* a fixed bottom bar: the snackbar keeps above it */
}| Token | Default |
|---|---|
--sf-color-danger | #b91c1c |
--sf-color-success | #15803d |
--sf-snackbar-max-width | min(28rem, 100vw - 2 * var(--sf-overlay-gutter)) |
--sf-snackbar-offset | var(--sf-space-4) |
--sf-snackbar-bg | #18181b |
--sf-snackbar-color | #fafafa |
--sf-radius-snackbar | var(--sf-radius-control) |
--sf-fixed-bottom | 0px |
Background
Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.