# ctx.ui

Draw the panel with blocks, or take your own element under the header for what blocks can’t draw. Park it in a corner, fold it to a pill, or let people make it bigger. The header is always Fave1’s, so nobody can fake it.

card mode · app mode · no permission

## Render

`src/index.ts`

```ts
Fave1.defineApp({
  onLaunch(ctx) {
    const ui = ctx.ui;
    ui.render([
      ui.heading('Save this page?'),
      ui.button('Save', {
        variant: 'purple',
        onClick: () => ui.toast('Saved', 'success'),
      }),
    ]);
  },
});
```

## Blocks

| Block | What it draws | Takes |
| --- | --- | --- |
| `heading(t) · text(t) · muted(t)` | Lines of text, big to quiet | text |
| `button(label, opts)` | A button. `opts.variant` is `primary` (the default), `purple`, `ghost` or `danger`; `opts.onClick` runs on a tap | label, opts |
| `input(opts)` | A text field — `placeholder`, `value`, and `onChange(value)` on every keystroke | opts |
| `list(items) · item(t, opts)` | Rows. An item takes `sub` (a second line), `icon` (an emoji or a letter) and `onClick` | blocks |
| `card · row · stack` | Group other blocks — a box, side by side, one above the other | blocks |
| `badge(t) · divider() · spinner()` | Small pieces | — |
| `image(src, opts)` | A picture — `opts.alt` for the text people hear | src, opts |
| `empty(icon, text)` | An icon and a sentence for nothing-yet | icon, text |

Text goes in as text, never as HTML — a `<b>` in a string shows up as the characters. A block the shell doesn’t know is skipped.

## A form

`render()` draws the whole panel again, so keep what someone typed in a variable and pass it back as `value`. Re-render only when something outside the field changes — not on every keystroke, or the field loses its cursor.

`src/index.ts`

```ts
Fave1.defineApp({
  onLaunch(ctx) {
    const ui = ctx.ui;
    let note = '';

    function draw(saved = false) {
      ui.render([
        ui.heading('Leave a note'),
        ui.input({ placeholder: 'Write something', value: note, onChange: (v) => { note = v; } }),
        ui.button(saved ? 'Saved' : 'Save', {
          variant: 'purple',
          onClick: () => {
            if (!note.trim()) return ui.toast('Write something first', 'error');
            draw(true);
          },
        }),
      ]);
    }
    draw();
  },
});
```

## Your own DOM — ctx.ui.host()

Blocks cover panels that read like a list. For what they can’t — a drawing canvas, a gallery grid, a board with avatars — take an element and draw it yourself.

`src/index.ts`

```ts
const el = ctx.ui.host();          // yours, under the shell header
el.append(canvas, controls);        // any DOM, any CSS — the page's styles stay out

ctx.ui.render([ctx.ui.text('Saved')]);   // blocks again
ctx.ui.host();                           // your element comes back, untouched
```

It sits inside the panel but outside the shell’s own DOM: walk up from it and you reach the page, never the header or the permission chip. Same element every time until the app closes.

Skip drop shadows in there. The panel body scrolls, and a scroll box clips anything that spills past an element’s edge — a blurred shadow under a full-width button shows up as a band underneath with the sides cut off. Let color do the lifting: a gradient fill, a touch brighter on hover, a slight press. Focus rings are fine; the panel leaves 8px of room for those.

## Getting the panel out of the way

Games and full-page tools need the page, not a 320px card in the middle of it. Three calls move the panel; the person can always close it, and the shell puts everything back when your app closes. All three are no-ops in app mode.

`src/index.ts`

```ts
ctx.ui.corner('bottom-right');            // panel stays open, parked in a corner, dim gone
ctx.ui.minimize({ text: 'Shooting 12s' });  // a small pill — icon · text · × — the page is theirs
ctx.ui.minimize({ text: 'Shooting 3s' });   // call again to update the text
ctx.ui.minimize({ text: 'Pause', corner: 'top-right' }); // pill in another corner
ctx.ui.restore();                           // back to the panel (tapping the pill does the same)
ctx.ui.onFold((folded) => {                 // true the moment it becomes a pill
  pause(folded);                            // false when the panel is back
});
ctx.ui.folded();                            // is it a pill right now?
ctx.ui.corner('center');                    // back to the middle, dim back
ctx.ui.close();                             // done — closes your app, same as tapping ×
```

`onFold` fires at the moment the panel folds or opens — `true` when it becomes a pill, `false` when the panel is back, including when your own `restore()` opens it. Calling `minimize()` again while it is already a pill (new text or corner) does not fire. It returns a function that stops listening. People can fold any app themselves with − in the header, and drag the panel or the pill, so `onFold` can fire even if you never call `minimize()`. In app mode the panel never folds, so `onFold` never fires and `folded()` is false. Pause on `onFold`. While an account prompt has the panel open, `folded()` is false and `onFold` stays quiet — the person didn’t tap the pill. If it was a pill, `folded()` is true again once the prompt is gone.

The pill sits bottom-right unless you pass `corner` — any of the four. Move it when your own controls take that edge: a game whose gravity points up can put its toolbar at the bottom and send the pill to `'top-right'`, then back again. Calling `minimize()` again with a new corner moves the pill that’s already there.

When the job is done — the person saved the result, downloaded it, or tapped your own **Done** — call `close()` instead of going back to your first screen. On a site it’s the same as tapping ×: Fave1 closes, your `onDestroy` runs, and your layer and page listeners come off — anything else you changed on the page is yours to undo there ([Put it back](https://developers.fave1.com/docs/ctx-page.md#put-it-back)). In app mode it closes your app and the person is back on My Fave1.

> **Keys in the panel stay in the panel.** What people type into a field in the panel — an `input` block, or an input in your own `host()` — never reaches the page underneath — Fave1 stops those key and text events at the card, so a site that reacts to any keystroke (jumping to its search box, say) stays quiet. Keys pressed with focus on a button still reach the page and your `ctx.page.on('keydown')`. **Esc closes your app** — Fave1 takes it in the capture phase on `document`, even inside your inputs. To handle Esc yourself first (close a popover you drew, then let the next Esc close the app), listen on `window` in the capture phase and call `stopPropagation()` for the cases you handled.

## A bigger panel — resizable() and resize()

The panel is 320px wide and as tall as your content. If your app reads better bigger — a chat, an editor, a table — let people resize it. Only apps that ask can be resized; everyone else keeps the 320px card.

`src/index.ts`

```ts
ctx.ui.resizable();                          // drag any edge or corner to resize, double-click an edge to reset
ctx.ui.resizable({ minWidth: 360, maxWidth: 900 }); // optional limits, in CSS pixels

const saved = (await ctx.storage.get('size')) as { width: number; height: number } | undefined;
if (saved) ctx.ui.resize(saved);             // open at the size they left it

ctx.ui.onResize((size) => {                  // { width, height } when they let go, null after a reset
  if (size) ctx.storage.set('size', size);
  else ctx.storage.delete('size');
});

ctx.ui.resize({ width: 520, height: 640 });  // or set it yourself — the whole panel, header included
ctx.ui.resize(null);                         // back to 320 and your content's height
```

People drag any of the panel’s four edges or four corners. Nothing is drawn on the panel; the cursor changes when they reach an edge. The opposite edge stays put. `onResize` fires once when they let go, not while they drag. Your own `resize()` calls don't fire it.

The limits are `minWidth`, `minHeight`, `maxWidth` and `maxHeight`. Without them people can drag down to 320 wide and 200 tall, and up to the window margin. A `minHeight` below 120 counts as 120. The two max limits also cap your own `resize()` calls while `resizable()` is on. `ctx.ui.resizable(false)` takes the edges away again and keeps the size the panel has.

The size in `onResize` always has both numbers. When someone drags only a side edge, `height` is the height the panel had at that moment, and the panel still follows your content. If you pass that size back to `resize()` later, the height stays fixed. Save only `width` if you want the height to keep following your content.

`resizable()` throws `invalid` for anything that isn’t `true`, `false` or an options object. `resize()` throws `invalid` unless you give it `null` or at least one of `width` and `height` as a positive number.

**The panel always keeps a 16px margin from the browser window.** However big you ask for, it stops 16px short of every edge. It never covers the whole window and never spills past it. On a phone that means it grows only to the margin it already has. When the window gets bigger again, the panel grows back to the size you asked for. It never gets narrower than 320px.

While a height is set, your `host()` is the full height of the panel body. Give your root `height: 100%` and let one inner area scroll, so your input or toolbar stays at the bottom. Without a set height the panel follows your content as before. `resize()` works without `resizable()`, since it's your own choice. All three calls do nothing in app mode, where your app already has the whole window.

## Out of the shot — hide() and show()

An app that captures or records the screen needs Fave1 out of the frame. `hide()` takes it off the screen for a moment — the panel or the pill, and a permission line if one is up. Your layer ([ctx.page.layer()](https://developers.fave1.com/docs/ctx-page.md#layer)) is yours and stays. Hidden means gone, not just see-through: clicks land on the page underneath.

`src/index.ts`

```ts
ctx.ui.hide();              // the panel, the pill, any permission line — off screen
try {
  shot = await capture();   // your capture: Fave1 isn't in it
} finally {
  ctx.ui.show();            // back as it was — same spot, same state, same focus
}
```

`show()` brings everything back exactly where it was, focus included — unless your app or the page moved focus while Fave1 was hidden; it won’t take that back. It never stays hidden by accident: closing your app brings it back, and if a call needs the person’s OK while it’s hidden — the first save on someone else’s site — the panel comes back before the permission line does, because people have to see what they tap. In app mode (`surface === 'app'`) both are no-ops.

Fave1 leaves the screen from the next frame the browser paints. If you read pixels from a screen stream, the stream can run behind the page — take a frame painted after `hide()`, not one already on its way.

## Paint the card — setBackground()

Your app area is yours to paint, but the card around it — the header, the edge, the rounded corners — belongs to Fave1. `setBackground()` paints all of it in one color, so your screen reaches the edge of the card. Call it again whenever the mood changes: a night scene, a win, a different person’s reading. The color fades over in .4 seconds.

`src/index.ts`

```ts
ctx.ui.setBackground('#14102A');         // the whole card, edge to edge
ctx.ui.setBackground('oklch(0.92 0.06 80)'); // any CSS color the browser knows
ctx.ui.setBackground(null);              // back to white
```

Fave1 picks the ink for you. On a dark color the header — your name, −, × — and every block turn white; on a light one they stay black. You can’t make the header disappear, and the purple edge stays, because that edge is how people know the card is Fave1’s.

Your `host()` inherits two CSS variables so your own drawing can match: `--f1-bg` (the card color) and `--f1-ink` (`#111` or `#fff`). Text inside `host()` already takes `--f1-ink`.

`src/panel.css`

```
.row { border-color: color-mix(in srgb, var(--f1-ink) 14%, transparent); }
.fade { background: linear-gradient(transparent, var(--f1-bg)); }
```

- **Solid, not a picture** (one color) — Any CSS color — hex, `rgb()`, `hsl()`, `oklch()`, a name — written out, up to 80 characters. No gradients, images, `var()` or `calc()` (`invalid`); draw those in `host()`. A see-through color is laid on white, because the card stays solid.
- **Gone when you are** (your app only) — Back to the apps, another app, or closing Fave1 — the card is white again. The next app starts white until it calls this itself.
- **The whole screen** (app mode) — In app mode (`surface === 'app'`) the same call paints your app’s screen below the top bar.

Permission prompts always show on white with black ink, even while your color is on — people should see the same question the same way in every app. Your color comes back when the prompt is gone. A string that isn’t a color throws `invalid`.

## Panel helpers

`ctx.ui`

```ts
ctx.ui.render(blocks);                   // replace the panel — null · false · undefined in the list are skipped
ctx.ui.setLoading('Working…');           // a spinner with a line
ctx.ui.toast('Saved');                   // a quiet black pill
ctx.ui.toast("Couldn't save", 'error');  // the coral one
ctx.ui.setTitle('3 found');              // the title in the header
ctx.ui.setBackground('#14102A');         // paint the whole card
ctx.ui.error("Couldn't load", { retry }); // the standard failure view
ctx.ui.error("Couldn't load", { retry, retryLabel: 'Load again' }); // your own word on the button
```
