# Breakpoints

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

Material's five window sizes, by width: compact, medium, expanded, large and extra-large. The template changes its navigation at 840px, and the Material channel reports each time the window enters a size. A model does not ask for any of it.

## In an app model

Nothing. The five are declared once, in `src/index.js`, as media queries for the window channel.

## In a view

A view that changes with the window’s size listens to the Material channel, not to the window. A whole view and its trait:

```js
import { SpyneTrait, ViewStream } from 'spyne';

class MyPanelTraits extends SpyneTrait {
  constructor(context) {
    let traitPrefix = 'myPanel$';
    super(context, traitPrefix);
  }

  static myPanel$OnBreakpoint(e) {
    const { breakpoint } = e.payload;
    this.props.el$.toggleClass('is-compact', breakpoint === 'compact');
  }
}

export class MyPanelView extends ViewStream {
  constructor(props = {}) {
    props.traits = [MyPanelTraits];
    props.channels = ['CHANNEL_MATERIAL'];
    super(props);
  }

  addActionListeners() {
    return [['CHANNEL_MATERIAL_BREAKPOINT_EVENT', 'myPanel$OnBreakpoint']];
  }
}
```

| Action of `CHANNEL_MATERIAL` | Direction | Payload | Means |
| --- | --- | --- | --- |
| `CHANNEL_MATERIAL_BREAKPOINT_EVENT` | out | `{ breakpoint }` | the window entered that size. `breakpoint` is `compact`, `medium`, `expanded`, `large` or `extra-large`. |

- The listener is a method of a trait, named in `addActionListeners`. What the channel sent is `e.payload`.
- It reports a change. It does not say which size the window started in, so a view hears nothing until the window is resized across a line.
- What must be right from the start belongs in styles, which always know: write a media query with the same widths, such as `@media (width < 600px)` for compact. There is no way to ask the channel for the size the window started in.
- The widths: compact under 600px; medium 600 to 839px; expanded 840 to 1199px; large 1200 to 1599px; extra-large 1600px and over.

## Where it is in the template

| File | What it holds | By name |
| --- | --- | --- |
| `src/index.js` | the five media queries, under names that start with `material` | `materialCompact`, `materialMedium`, `materialExpanded`, `materialLarge`, `materialExtraLarge` |
| `src/app/traits/channel/channel-material-traits.js` | turns a media query starting to match into the size’s name | `channelMaterial$OnBreakpointEntered` |
| `src/app/channels/channel-material.js` | the Material channel and its actions |  |

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`: `op:window-event-via-channel`, `record:responsive-behavior-on-media-query`.

## 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.

Material sorts a window into one of five sizes by its width. The one this window is in is marked. What the template does at each is beside it.

| Size | Width | What the template does |
| --- | --- | --- |
| compact | under 600px | The app bar shows a menu button; the drawer opens over the page. |
| medium | 600 to 839px | The app bar shows a menu button; the drawer opens over the page. |
| expanded | 840 to 1199px | The app bar shows the links; there is no drawer. |
| large | 1200 to 1599px | The app bar shows the links; there is no drawer. |
| extra-large | 1600px and over | The app bar shows the links; there is no drawer. |

That is the template. The gallery differs from 840px: it keeps the drawer open as its sidebar and hides the app bar’s links. An open drawer closes whenever the window passes from one size to another.

### The last thing the channel said

```
Nothing yet. Resize the window across one of the lines.
```

### What the channel says, and what it does not

The Material channel reports each time the window enters one of the five (CHANNEL_MATERIAL_BREAKPOINT_EVENT, with the size’s name). It reports a change. It does not say which size the window started in, so nothing is heard until the window is resized across a line. The mark on the list above comes from the styles, which always know.

Nothing to ask for in a model. The five are declared once, in index.js, as media queries for the window channel.
