# The interface kit

`framescript/kit` holds parts for showing an interface in a scene: a pointer that clicks, plates and windows, a phone, a tablet and a laptop, comment pins and threads, code on a bar, and connectors. Every part is a group with named objects inside, so it is added, moved and animated like anything else.

```ts
import { add, camera, timeline, tween, video, wait } from "framescript"
import { click, cursor, windowFrame } from "framescript/kit"
import { panel } from "framescript/shapes"
import { MeshBasicMaterial, PerspectiveCamera } from "three"

export default video(
  { width: 1920, height: 1080, fps: 60 },
  timeline(function* () {
    const lens = yield* add(new PerspectiveCamera(32, 16 / 9, 0.1, 100), {
      position: [0, 0, 14],
    })
    yield* camera(lens)

    const frame = yield* add(
      windowFrame({ title: "release.ts", width: 5, height: 3.2 })
    )
    // What a window shows is added under its content.
    yield* add(
      panel({
        width: 1.5,
        height: 0.5,
        radius: 0.08,
        material: new MeshBasicMaterial({ color: "#ff6467" }),
      }),
      { position: [1.3, -0.9, 0] },
      { parent: frame.content }
    )

    const pointer = yield* add(cursor(0.4), { position: [-5, -3, 0.1] })
    yield* tween(pointer.position, { x: 1.3, y: -1.1 }, 0.9)
    yield* click(pointer)
    yield* wait(0.6)
  })
)
```

Parts with text inside are laid out while `add` waits, which takes no scene time. Sizes are in world units and suit a scene a few units high.

## Looks

A look is everything a piece of the library reads to be drawn in a video's style: its colours, a palette for series of data, the faces and sizes of its type, its corners and lines, how it moves and the finish of the whole frame. Every kit part and chart takes one as `look`: an option of those that take options (`plate`, `windowFrame`, `deviceFrame`, `listing`, `codeBar`, the charts), the last argument of the others (`cursor`, `pin`, `clusterPin`, `threadCard`, `wire`). Left out, it is `KIT`, the kit's dark look.

- `LOOKS` holds the looks a video starts from: `dark` (which is `KIT`), `light`, and seven directions: `editorial`, `kinetic`, `gallery`, `swiss`, `spatial`, `signal` and `warm`.
- `look(base, changes)` changes some of a look and keeps the rest: `look(LOOKS.swiss, { colors: { accent: "#1f4fff" } })`. New colours bring their series and code colours with them, unless the look was given a series or code colours of its own. A finish changes effect by effect, and `null` takes one away: `look(LOOKS.spatial, { finish: { bloom: null } })`.
- `fromBrand({ accent, background, ink, faces })` makes a look from a brand: the greys are mixed between its background and its ink (black or white, whichever reads, when left out), on the light or dark look's sizes and motion.
- `textOf(look, role, color)` is a text's face, size, weight, spacing and colour for a role (`display`, `title`, `body`, `code`, `small`, `tiny`), to spread into a text's options.
- `finishOf(look)` is the look's stage effects (grain, grade, bloom, chromatic aberration, vignette), to `add` once for the whole frame.
- `inCase(look, role, words)` sets words in the case of a role's face: in capitals in the kinetic look's display and the signal look's labels.
- `onColor(look, fill)` is the look's ink or background, whichever reads better on a fill: the colour of a label on an accent button or a status badge.
- `contrast(a, b)` measures how well two colours read on each other, as WCAG does (4.5 and more for text), and `mixColor(a, b, amount)` mixes two. Colours are given as `#rgb` or `#rrggbb`; anything else is refused.

A look's parts:

- `colors`: the greys `void`, `card`, `raised`, `lifted`, `border`, `ink`, `muted` and `faint`, one `accent`, and the status colours `positive`, `warning`, `info` and `negative`. Every look's accent and status colours read on its background and cards at 3:1 or more.
- `series`: colours for series of data, the accent first and then the ink's greys, so the first series reads as the one that matters.
- `faces`: `display`, `text` and `mono`, each a font, weight, tracking and leading.
- `type`: font sizes in world units: `display`, `title`, `body`, `code`, `small` and `tiny`.
- `radius` (`small`, `medium`, `large`), `stroke` and `elevation` (`shadow`, `glow`, `line` or `none`).
- `ui`, how large interface pieces are drawn against the type (1 for the kit, smaller in a dense look), and `space`, one step of space between parts in world units.
- `motion`: the look's `beat` in seconds, how long an entrance, an exit and an emphasis take, the time between parts, and the easing of each.
- `code`: a theme for [code blocks](/docs/framework/objects/code.md) in greys of the ink, so that the accent can mean something.

## Pieces

A piece of the library is one object you `add`, drawn in a look, with steps to play: `enter()`, `hold(seconds)` while it stays, and `exit()`. Its origin is its centre, in world units, unless its section says otherwise.

- `enter(options)` and `exit(options)` make a new step each time, so a piece can come in again after it went. An exit hides the piece at its end and never removes it.
- `options` is `{ duration, ease }`, each the look's when left out: an entrance takes the look's `motion.enter` with its entrance easing, an exit `motion.exit` with its exit easing.
- `hold(seconds)` is the piece's motion while it stays, such as a slow drift, or a plain wait.
- A piece's own steps, such as a chart's change of data, are methods beside these.

`buildLine` in [text](/docs/framework/objects/text.md#a-line-that-builds) is one. To make your own, extend `PieceObject`: say how it arrives (`arrive`), how it goes (`depart`) and where its parts stand before it arrives (`reset`), and `moveOf(look, kind, options)` resolves a move's length and easing. Transitions between scenes, stage effects, sounds and layouts are not pieces and keep their own calls.

## Cursor and click

`cursor(size)` makes the pointer. Its origin is its tip, so its position is where it points. The default height is `0.34`. Move it with `tween` on its `position`.

`click(pointer)` is a step of 0.45 seconds: the pointer dips and a ring runs out from its tip. It does nothing to what is under the pointer; animate that yourself.

```ts
import { add, sequence, tween } from "framescript"
import { click, cursor, plate } from "framescript/kit"

const button = yield* add(plate({ width: 1.6, height: 0.6 }))
const pointer = yield* add(cursor(), { position: [-3, -2, 0.1] })
yield* tween(pointer.position, { x: 0.2, y: -0.1 }, 0.8)
yield* click(pointer)
yield* sequence(
  tween(button.scale, { x: 0.94, y: 0.94 }, 0.08),
  tween(button.scale, { x: 1, y: 1 }, 0.2)
)
```

## Plates, windows and devices

`plate(options)` is a flat card with a thin line round it. Its origin is its centre. Options: `width` (default `3.2`), `height` (`1.8`), `radius` (`0.1`), `color`, and `label`, a name written small in its top left corner. Its parts are `surface`, `rim` and `label`.

`windowFrame(options)` is a window with a title bar, three dots and a title. Options: `width` (default `4.4`), `height` (`2.8`, title bar included), `title`, `radius` (`0.14`), `color` and `barColor`. Add what it shows under `frame.content`, whose origin is the middle of the area below the bar. `frame.inside` has that area's `width` and `height`.

`deviceFrame(options)` is a phone, a tablet or a laptop, drawn flat from the front. `kind` is `"phone"` (the default), `"tablet"` or `"laptop"`. `width` is the width of its screen: `2` for a phone, `4` for a tablet and `5.6` for a laptop by default. `color` and `screenColor` set its body and its empty screen. Add what it shows under `device.content`, whose origin is the middle of the screen. `device.screen.width` and `device.screen.height` say how large the screen is.

```ts
import { add } from "framescript"
import { deviceFrame, KIT } from "framescript/kit"
import { text } from "framescript/text"

const phone = yield* add(deviceFrame({ kind: "phone", width: 1.7 }))
yield* add(
  text("Hello", {
    fontSize: KIT.type.body,
    color: KIT.colors.ink,
    anchorX: "center",
    anchorY: "middle",
  }),
  {},
  { parent: phone.content }
)
```

Neither a window nor a device clips its content. Keep what you add inside `inside` or the screen's size.

## Comment pins and threads

`pin(author, status, size)` is one comment: a disc with the author's initials and a dot in the colour of its status. The status is `"open"` (the default), `"review"` or `"resolved"`. The default size is `0.3`. `clusterPin(count, size)` stands for several comments at one place: a disc with their number. `initials(name)` is how a pin shortens a name: the first letters of its first two words.

`threadCard(thread, status, width)` is a thread as a card, one message under the other. Each message is `{ author, body }`. The default width is `3.7`. Its origin is its top left corner, so it unfolds down and to the right.

A card is whole when it is added. `fold(card)` folds it away at once. `unfold(card, upTo, duration)` grows it from its corner down to message number `upTo`, counted from 1, in `duration` seconds (default `0.5`).

```ts
import { add, wait } from "framescript"
import { fold, pin, threadCard, unfold } from "framescript/kit"

yield* add(pin("Jo Wu", "review", 0.36), { position: [-0.4, 0.2, 0] })
const thread = yield* add(
  threadCard(
    [
      { author: "Jo Wu", body: "Can the first tile be the accent?" },
      { author: "Sam Lee", body: "Done, have a look." },
    ],
    "review",
    2.9
  )
)
yield* fold(thread)
yield* unfold(thread, 1)
yield* wait(0.4)
yield* unfold(thread, 2)
```

## Code on a plate or a bar

`listing(source, options)` is a plate with a [code block](/docs/framework/objects/code.md) on it. It takes a plate's options and a code block's. The code is TypeScript in the kit's greys at `KIT.type.small`, without a caret, unless the options say otherwise. The block is `listing.block`. The plate does not size itself to the code: give it a `width` and `height` that fit. Code that runs past the plate's right or bottom edge is warned of at the line that adds the listing, with the size it needs ([`listing-overflow`](/docs/framework/agents/rules.md#objects)).

`codeBar(source, options)` is one line of code on a bar that fits it. Its origin is the middle of its left edge, so it grows to the right. `pad` sets the room between the bar's edge and the code (default `0.16`) and `color` the bar's colour. Its parts are `block`, `plate` (the bar) and `rim`; `bar.fit` is the bar's width and `bar.height` its height.

`underlineToken(block, token)` makes a line under one token of a block, for the value being talked about. Call it once the block was added. It returns `{ line, from, full }`: add `line` with the block as its parent, then tween its `width` to `full`. The line is a panel, which grows about its middle, so move its middle with it:

```ts
import { add, all, tween } from "framescript"
import { listing, underlineToken } from "framescript/kit"

const program = yield* add(
  listing("yield* tween(dot.position, { x: 3 }, 0.6)", {
    width: 5,
    height: 0.8,
  })
)
const mark = underlineToken(program.block, "0.6")
yield* add(mark.line, {}, { parent: program.block })
yield* all(
  tween(mark.line, { width: mark.full }, 0.4),
  tween(mark.line.position, { x: mark.from + mark.full / 2 }, 0.4)
)
```

`slot(bar, left, width)` says where a code bar's bar goes when it leaves its code: it returns the values to tween for a bar whose left edge is at `left`, measured from the code bar's origin, and that is `width` wide. The code stays where it is.

```ts
import { add, all, tween } from "framescript"
import { codeBar, slot } from "framescript/kit"

const row = yield* add(codeBar("yield* wait(0.3)"))
const to = slot(row, 4, 1.2)
yield* all(
  tween(row.plate, to.plate, 0.7),
  tween(row.plate.position, { x: to.x }, 0.7),
  tween(row.rim, to.rim, 0.7),
  tween(row.rim.position, { x: to.x }, 0.7)
)
```

## Connectors

`wire(route, color)` is a thin line from one thing to another, through the points of `route`, with rounded corners and a dot at each end. The points are `[x, y]` in the wire's own space. The colour defaults to the accent. Nothing of a wire shows when it is added. `connect(wire, duration)` draws it on: the first dot appears, the line runs to the other end in `duration` seconds (default `0.5`), and the second dot lands.

```ts
import { add } from "framescript"
import { connect, wire } from "framescript/kit"

const link = yield* add(
  wire([
    [-2.6, -2.5],
    [-1.35, -2.5],
    [-1.35, -0.75],
  ])
)
yield* connect(link, 0.6)
```

A wire does not follow the things it connects. When they move, the route does not.

## Parts of your own

`Composite` is the class the kit's pieces extend: a group whose `add` waits until every text inside is laid out. `STATUS` maps each status to its colour.

## See also

- Examples: kit/interface, kit/pins, kit/code-parts
- [Code blocks](/docs/framework/objects/code.md) for what a listing and a code bar hold
- [Charts](/docs/framework/objects/charts.md), which use the kit's colours
- [Shapes](/docs/framework/objects/shapes.md) for the panels the parts are made of
- [Reference: framescript/kit](/docs/framework/reference/kit.md)
