# Snackbar

Source: https://material.spynejs.com/components/snackbar
Part of: Material shell gallery, Google's Material Design 3 for SpyneJS. Index: https://material.spynejs.com/llms.txt

Material's snackbar: a brief report of something the app has done, at the bottom of the window, one at a time, gone by itself after five seconds. A view asks the Material channel for one, in code. A model does not ask for one.

## In an app model

Nothing directly. A snackbar is asked for by a view, in code. A model reaches one only through a page item’s own words: the contact form shows its `successMessage` in one when a message has been sent. `successMessage` is in the page item’s `props`, beside `data`, not inside it.

```json
{
  "viewClass": "ContactUsView",
  "props": {
    "successMessage": "Your message has been sent."
  }
}
```

## In a view

A view asks by sending the request to the Material channel, with the words to show:

```js
this.sendInfoToChannel(
  'CHANNEL_MATERIAL',
  { message: 'Your message has been sent.' },
  'CHANNEL_MATERIAL_SNACKBAR_REQUEST',
);
```

| Action of `CHANNEL_MATERIAL` | Direction | Payload | Means |
| --- | --- | --- | --- |
| `CHANNEL_MATERIAL_SNACKBAR_REQUEST` | in, from any view | `{ message }` | show this |
| `CHANNEL_MATERIAL_SNACKBAR_SHOW_EVENT` | out | `{ snackbarId, message }` | one is being shown |
| `CHANNEL_MATERIAL_SNACKBAR_HIDE_EVENT` | out | `{ snackbarId }` | its time is up |

- That is all a view does. It does not make the snackbar, place it or time it: the channel and the snackbar’s own views do.
- A newer snackbar replaces the one on screen.
- Its words are announced to assistive technology without moving focus.
- It has no action button. The message is plain words.

## Where it is in the template

| File | What it holds | By name |
| --- | --- | --- |
| `src/app/channels/channel-material.js` | the Material channel: its actions, and how long a snackbar stays (`snackbarDuration`, in milliseconds) | `CHANNEL_MATERIAL_SNACKBAR_REQUEST`, `snackbarDuration` |
| `src/app/traits/channel/channel-material-traits.js` | takes the request, shows the snackbar, and hides it when its time is up | `channelMaterial$OnSnackbarRequest`, `channelMaterial$OnSnackbarTimeUp` |
| `src/app/components/material/material-snackbar-host-view.js` | the region at the bottom of the app where snackbars appear |  |
| `src/app/components/material/material-snackbar-view.js` | one snackbar, which removes itself |  |
| `src/scss/03-components/material/_snackbar.scss` | the styles |  |

The template is public: https://github.com/spynejs/application-shell-material. An app made from it has these same files.

What it rests on in `@spynejs/grammar`: `record:toast-notification-system`, `op:transmit-into-channel`, `op:register-channel-action-vocabulary`.

## As the page shows it

The page’s own words, in its own order, read from the page as it is drawn in the light colour scheme. They were written for someone looking at the page: “above”, “below” and “marked” mean the drawn page. A thing the page draws is named here in its place (`Button:`, `Field:`, `Switch:`), and a box that reports what the reader does is as it starts, empty. What depends on the reader’s own window is left out.

A brief report of something the app has done. It appears at the bottom of the window, one at a time, and leaves by itself after five seconds.

Buttons: Show a short one, Show a longer one

A newer snackbar replaces the one on screen. Its words are announced to assistive technology without moving focus.

### How a view asks

```js
this.sendInfoToChannel(
  'CHANNEL_MATERIAL',
  { message: "Your message has been sent." },
  'CHANNEL_MATERIAL_SNACKBAR_REQUEST',
);
```

### In the app model

```json
{
  "viewClass": "ContactUsView",
  "props": {
    "successMessage": "Your message has been sent."
  }
}
```

Nothing new is proposed here. A snackbar is asked for by a view, in code, not by a model. A model reaches one only through a page item’s own words, as the contact form’s successMessage does.
