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: none. Undo pressed:0. Retry pressed:0.

A modal

A snackbar shown now is placed inside this dialog, so its Undo can be 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: none. Save: none

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

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 */
}
TokenDefault
--sf-color-danger#b91c1c
--sf-color-success#15803d
--sf-snackbar-max-widthmin(28rem, 100vw - 2 * var(--sf-overlay-gutter))
--sf-snackbar-offsetvar(--sf-space-4)
--sf-snackbar-bg#18181b
--sf-snackbar-color#fafafa
--sf-radius-snackbarvar(--sf-radius-control)
--sf-fixed-bottom0px

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.