# Overlays

> A small box above the app's pages, drawn from your components or one of your in-app views, which the person places, hides and brings back.

Page: https://songstress.app/docs/develop/overlays/

An overlay is a small box the app draws above its pages, the sidebar and the player bar: a pet that reacts to the
music, a timer, a note that stays in view while the person browses. It is your components (a
[canvas](/docs/develop/canvas/) among them) or one of your [in-app views](/docs/develop/views/). The person places it,
hides it, and brings it back; your code says what it shows, and may show or hide it, but never over the person's own
hide. Ask for `"songstress": "^0.1.13"`, or start one with `npm create songstress-extension -- --template overlay`.

<Shot name="web/ext-overlays" alt="Three overlays above an extension's page: a counter at the top end under its name chip, Pets, Counter; a notice its server entry drew at the bottom start; and Prince, napping, at the bottom end" />

## Declare it [#declare-it]

Up to two overlays an extension, under `contributes.overlays`:

```json title="songstress.json"
"engines": { "songstress": "^0.1.13" },
"entry": { "client": "dist/client.js" },
"permissions": { "storage": { "reason": "Keep the pets." } },
"contributes": {
  "commands": [{ "id": "rest", "title": "Let it rest", "run": "client" }],
  "menus": { "overlay/menu": [{ "command": "rest", "when": "overlay == 'pet'" }] },
  "overlays": [
    { "id": "pet", "title": "Pet", "render": "client", "size": { "width": 200, "height": 120 }, "anchor": "bottom-end", "interactive": true }
  ]
}
```

A floating box above the app's pages, the extension's companion: components its code builds, or one of its in-app views.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The overlay's name: code registers its renderer, and shows and hides it, by it. |
| `title` | `string` | What it is: the name on its chip and in the extension's card. |
| `render?` | `Side \| undefined` | The entry that builds its components. An overlay names render or view. |
| `view?` | `string \| undefined` | One of the extension's in-app views (views[].inApp), drawn in the box. |
| `size` | `{ width: number; height: number; }` | The box, in points. |
| `anchor?` | `OverlayAnchor \| undefined` | The corner it starts in. Left out, bottom-end. |
| `interactive?` | `boolean \| undefined` | It takes the pointer and keys. Left out, false: a decoration a click passes through. |
| `when?` | `string \| undefined` | It is drawn only while this holds. |

- **`size`** is its box in points, 96 to 360 wide and 64 to 360 high. It never grows to fit what it holds: draw for
  the box.
- **`anchor`** is the corner of the page's area it starts in. The person moves it from there.
- **`interactive`** takes the pointer and keys. Left out, it is a decoration a click passes through, whose name chip
  shows while the pointer is over it.
- **`render`** names the entry that builds its components, or **`view`** one of your in-app views
  (`OVERLAY_CONTENT`): one or the other.

## Draw it [#draw-it]

The `overlay` template's pet, drawn from components in each app, counts each time it is petted:

```tsx title="src/pet.tsx"
import { songstress } from 'songstress/client'
import { Button, Stack, Text, useStored } from 'songstress/ui'

function Pet() {
  const [petted, setPetted, loading] = useStored('petted', 0)
  return (
    <Stack gap="small" align="center">
      <Text style="title">{loading ? '…' : petted === 0 ? 'Hello' : `Petted ${petted} times`}</Text>
      <Button title="Pet" icon="heart" onPress={() => setPetted((was) => was + 1)} />
    </Stack>
  )
}

songstress.ui.overlays.register('pet', () => <Pet />)

// The chip's menu: "Let it rest" hides it on this device until code shows it again.
songstress.commands.register('rest', async () => {
  await songstress.ui.overlays.hide('pet')
})
```

Its renderer is told who looks, the app, the song playing and its `size`, and answers components, a panel's blocks, or
`null` for nothing now. The song playing is what the member plays: it is there only when your extension holds
`listening.read` and that member said yes to it, and never while they listen privately; with `library.read` alone the
overlay still draws, with no `song` ([what changed in 0.1.14](/docs/develop/versions/#0114)). An overlay's tree holds 60 nodes at
most. In client code it is mounted and keeps its state as a page does ([State and handlers](/docs/develop/state/));
the server entry may render one too, once each time it is drawn.

## Follow the player [#follow-the-player]

An overlay that reacts to the music reads what plays when it appears: the app may start your code again at any
time, in the middle of a song, and the overlay is mounted afresh then. `usePlayer()` reads the player as it
appears and follows it after; `useStored()` keeps what the person chose. A hook brings only a moment no state holds,
such as a like, heard while the overlay shows:

```tsx title="src/dancer.tsx"
import { songstress } from 'songstress/client'
import { Text, useEffect, usePlayer, useState } from 'songstress/ui'

// The manifest asks for listening.read and lists onPlay, onPause, onTrackChange and onFavorite under hooks.client.
function Dancer() {
  const player = usePlayer()
  const [loved, setLoved] = useState<string | null>(null)
  useEffect(() => {
    const listener = songstress.hooks.onFavorite((item, event) => {
      if (item.type === 'song' && event.favorite) setLoved(item.song.id)
    })
    return () => listener.dispose()
  }, [])
  if (!player?.song) return <Text>Resting</Text>
  const { song } = player
  if (loved === song.id) return <Text style="title">{`Loves ${song.title}`}</Text>
  return <Text>{player.playing ? `Dancing to ${song.title}` : 'Waiting for the music'}</Text>
}

songstress.ui.overlays.register('dancer', () => <Dancer />)
```

Keep what plays out of a variable your hooks fill: code started again holds none of it, and a hook that came first
may not say the song. Nothing needs `songstress.ui.refresh` at its start either: the app draws the overlay each
time it shows.

## Show it and hide it from code [#show-it-and-hide-it-from-code]

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

// The pet comes out when its member plays something, and rests when they pause for the night.
songstress.hooks.onPlay(async (_song, play) => {
  await songstress.ui.overlays.show('pet', { userId: play.userId })
})

songstress.jobs.register('bedtime', async () => {
  await songstress.ui.overlays.hide('pet', { everyone: true })
})
```

Code's word decides whether an overlay is drawn, where its `when` holds. The server entry says it for one member or
for every member, in words (`{ userId }` or `{ everyone: true }`), and the server keeps it; client code says it on its
own device. **A person's hide is theirs alone:** code never shows an overlay the person hid, and only that person
brings it back.

## What the person does [#what-the-person-does]

- **Moves it** by its grip, anywhere in the window. Its place is kept on each device, and held inside the window
  when the window changes size.
- **Hides it** from its name chip, with an Undo. The chip starts with your extension's name, in the app's own type,
  before the overlay's title, so a person always knows whose box it is. The chip's menu holds your `overlay/menu`
  commands too.
- **Brings it back** from your extension's **This device** card (Settings ▸ Extensions), where each of your
  overlays has its switch, and **All overlays** hides or shows every one of them on this device at once, those an
  update adds among them.

## Where it shows [#where-it-shows]

- On the web and the desktop, from a phone's width up: none at a phone's width, and none in the phone apps yet.
  `client.capabilities.overlays` says whether this app draws them.
- Above the pages, the sidebar and the player bar; under every dialog, sheet, menu, tooltip and toast, and never over
  the full-screen player. At most six from every extension at once.
- **Never while Songstress asks a person something that grants or consents:** an install's review, "Allow and
  update", a member's consent card, a sign-in the app draws. Overlays are taken off while one shows, and come back
  after.
- An overlay whose content is a view is drawn only where in-app views are: Chromium browsers on a computer, never a
  phone's or a tablet's browser.

## Good overlays [#good-overlays]

- Small and quiet. A decorative overlay is still and silent under reduced motion; a sprite shows one frame.
- Useful while the person does something else: a timer, what plays, a pet. A form or a list belongs on a page.
- One at a time is plenty. Two is the limit for an extension, and six for the whole app.

## Test it [#test-it]

`host.mount('overlay', 'pet')` mounts it as an app does: `press('Pet')`, then read `tree`. What code showed and hid is
in `host.surface(userId).overlays`. See [Test and debug](/docs/develop/testing/#mount-a-component).
