# Components

> Reuse a composition as a layer with `use` and `ref`, pass it props, and understand its default size, duration and clipping — with a reusable lower third as the worked example.

Any composition in a project can be drawn inside another one with a `use` layer. Give the inner
composition `props` with defaults, reference them as `{{name}}`, and every `use` can pass different
values. This is vidmake's version of a React component (or Remotion's nested `<Composition>`).

```json
{ "id": "ada", "type": "use", "ref": "lowerThird", "start": "2s",
  "props": { "name": "Ada Lovelace", "role": "Analyst" } }
```

| field | default | notes |
|---|---|---|
| `ref` | required | The `id` of a composition in the same project. |
| `props` | `{}` | Values for the referenced composition's `{{...}}` placeholders. Missing ones use its defaults. |
| `width`, `height` | the referenced composition's size | The box the component is drawn and clipped in. |
| `duration` | the referenced composition's duration | |
| `clip` | `true` | Content outside the box is hidden; set `false` to let it spill out. |

All [common fields](https://vidmake.app/docs/layers.md) work on a `use` layer, exactly as on a [group](https://vidmake.app/docs/groups.md):
position, anchor, rotation, scale, opacity, shadow, filters, masks and `enter`/`exit` presets.

## A reusable lower third

The component is an ordinary composition sized to its content (here 640×140), with its own `props`
defaults. The main composition places two instances in the bottom-left corner, each with its own name,
role and accent color:

**Demo: One lowerThird composition, used twice with different props** (rendered video: https://vidmake.app/media/components-lower-third.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [
    {
      "id": "main", "description": "One lowerThird composition, used twice with different props",
      "width": 1280, "height": 720, "fps": 30, "duration": "5s",
      "background": { "type": "radial", "center": [0.6, 0.35], "radius": 0.9, "stops": ["#f4f1ea", "#e9e4d8"] },
      "layers": [
        { "id": "ada", "type": "use", "ref": "lowerThird", "anchor": "bottom-left", "x": 60, "y": 660, "start": "0.2s",
          "props": { "name": "Ada Lovelace", "role": "Analyst, Analytical Engine", "color": "#c92a26" } },
        { "id": "grace", "type": "use", "ref": "lowerThird", "anchor": "bottom-left", "x": 60, "y": 660, "start": "2.6s",
          "props": { "name": "Grace Hopper", "role": "Compiler pioneer", "color": "#1f5fb0" } }
      ]
    },
    {
      "id": "lowerThird", "description": "Lower third. Props: name, role, color",
      "width": 640, "height": 140, "fps": 30, "duration": "2.2s",
      "props": { "name": "Name", "role": "Role", "color": "#161412" },
      "layers": [
        { "id": "plate", "type": "rect", "cornerRadius": 2, "fill": "#fbf9f4", "stroke": "#161412", "strokeWidth": 2,
          "enter": { "type": "wipeRight", "duration": "0.5s" }, "exit": { "type": "fadeDown", "duration": "0.4s" } },
        { "id": "accent", "type": "rect", "anchor": "left", "x": 0, "width": 8, "height": "100%", "fill": "{{color}}",
          "enter": "fade", "exit": "fade" },
        { "id": "name", "type": "text", "text": "{{name}}", "anchor": "left", "x": 40, "y": 56, "font": "Schibsted Grotesk",
          "fontSize": 44, "fontWeight": 860, "letterSpacing": -1.8, "fill": "#161412", "start": "0.2s", "enter": "fadeRight", "exit": "fade" },
        { "id": "role", "type": "text", "text": "{{role}}", "anchor": "left", "x": 42, "y": 100, "font": "JetBrains Mono", "fontSize": 17,
          "letterSpacing": 2, "uppercase": true, "fill": "{{color}}", "start": "0.35s", "enter": "fadeRight", "exit": "fade" }
      ]
    }
  ]
}
```

What to notice in the JSON:

- `lowerThird` declares `"props": { "name": "Name", "role": "Role", "color": "#161412" }`. Those are
  the defaults, used when a `use` layer leaves a prop out (and when you render the component alone).
- `{{color}}` is the whole string of `fill`, so it is replaced by the raw prop value; a paint object or a
  number would work as well. `{{name}}` inside a longer string is spliced in as text. See
  [Props](https://vidmake.app/docs/props.md) for the exact rules.
- The `use` layers are anchored `bottom-left`, so the component's box is placed by its corner, like any
  layer.
- The component's own presets (`wipeRight` in, `fadeDown` out) play at its start and end, so each
  instance animates in and out without the main composition knowing how.

## Size, time and clipping

**Size.** A `use` layer's box defaults to the referenced composition's `width`×`height`. The children
keep the coordinates they have in their own composition. Setting `width`/`height` on the `use` layer
changes the box it is clipped to (and what percentages inside resolve against), but does not scale the
content. To draw a component bigger or smaller, use `scale`.

**Duration.** A `use` layer lasts as long as the referenced composition (`"duration": "2.2s"` for the
lower third), so it has a fixed length that works in [series](https://vidmake.app/docs/series.md) and in `"duration": "auto"`.
Set `duration` on the `use` layer to cut it shorter. Times inside follow the component's clock: its
layers start at the `use` layer's `start`. If the component has a different `fps`, its timing is
converted and `validate` warns, since expressions that count frames may behave differently.

**Clipping.** Components are clipped to their box by default, like a picture-in-picture. Make the
component composition large enough for its shadows and animations (a `slide*` exit leaves the box entirely, a `fadeDown` moves 32 px, or 0.3em on text), or
set `"clip": false` on the `use` layer.

`vidmake timeline` lists each instance with its children, which is a quick way to check when each
one plays:

```console
$ vidmake timeline components-lower-third.json
Composition `main` — 1280x720 @ 30fps, 150 frames (5.00s)

ID        TYPE         START      END      DUR  DETAILS
ada       use          0.20s    2.40s    2.20s  "4 children"
  plate   rect         0.20s    2.40s    2.20s  enter:wipeRight exit:fadeDown
  accent  rect         0.20s    2.40s    2.20s  enter:fade exit:fade
  name    text         0.40s    2.40s    2.00s  enter:fadeRight exit:fade "Ada Lovelace"
  role    text         0.55s    2.40s    1.85s  enter:fadeRight exit:fade "Analyst, Analytical Engine"
grace     use          2.60s    4.80s    2.20s  "4 children"
  plate   rect         2.60s    4.80s    2.20s  enter:wipeRight exit:fadeDown
  accent  rect         2.60s    4.80s    2.20s  enter:fade exit:fade
  name    text         2.80s    4.80s    2.00s  enter:fadeRight exit:fade "Grace Hopper"
  role    text         2.95s    4.80s    1.85s  enter:fadeRight exit:fade "Compiler pioneer"

          |0s          1s          2s          3s          4s          |
ada       |··███████████████████████████·······························|
  plate   |··███████████████████████████·······························|
  accent  |··███████████████████████████·······························|
  name    |····█████████████████████████·······························|
  role    |······███████████████████████·······························|
grace     |·······························███████████████████████████··|
  plate   |·······························███████████████████████████··|
  accent  |·······························███████████████████████████··|
  name    |·································█████████████████████████··|
  role    |···································███████████████████████··|
```

## Props in action

Props can be numbers and can feed expressions, because placeholders are filled in before the project is
compiled. This `stat` component counts up to its `value` prop; three instances sit in a
[row layout](https://vidmake.app/docs/groups.md#layout), which measures each one by its box:

**Demo: A stat component used three times in a row layout, each with its own props** (rendered video: https://vidmake.app/media/components-stats.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [
    {
      "id": "main", "description": "A stat component used three times in a row layout, each with its own props",
      "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
      "layers": [
        { "id": "row", "type": "group", "layout": { "direction": "row", "gap": 40 }, "children": [
          { "id": "users", "type": "use", "ref": "stat", "props": { "value": 1280, "label": "users", "color": "#161412" } },
          { "id": "uptime", "type": "use", "ref": "stat", "props": { "value": 99, "label": "% uptime", "color": "#1f5fb0" } },
          { "id": "deploys", "type": "use", "ref": "stat", "props": { "value": 342, "label": "deploys", "color": "#c92a26" } }
        ] }
      ]
    },
    {
      "id": "stat", "description": "Stat card. Props: value (number), label, color",
      "width": 340, "height": 280, "fps": 30, "duration": "4s",
      "props": { "value": 100, "label": "label", "color": "#161412" },
      "layers": [
        { "id": "card", "type": "rect", "cornerRadius": 3, "fill": "#fbf9f4", "stroke": "#161412", "strokeWidth": 2, "enter": "pop" },
        { "id": "bar", "type": "rect", "anchor": "top-left", "x": 32, "y": 32, "height": 8, "fill": "{{color}}",
          "width": { "from": 0, "to": 276, "duration": "1.2s", "ease": "easeOutCubic" } },
        { "id": "value", "type": "text", "y": 135, "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 96,
          "letterSpacing": -4.5, "fill": "#161412", "text": "${fmt(round({{value}} * easeOutCubic(clamp(t / 1.5, 0, 1))))}" },
        { "id": "label", "type": "text", "text": "{{label}}", "y": 212, "font": "JetBrains Mono", "fontSize": 18, "letterSpacing": 2.5,
          "uppercase": true, "fill": "{{color}}" }
      ]
    }
  ]
}
```

```json title="inside the stat component"
"text": "${fmt(round({{value}} * easeOutCubic(clamp(t / 1.5, 0, 1))))}"
```

With `"value": 1280`, that becomes `${fmt(round(1280 * ...))}`.

Props flow one level at a time. A `use` layer's `props` can themselves contain the outer composition's
placeholders, e.g. `"props": { "color": "{{accent}}" }` passes the main composition's `accent` prop
down. A `{{name}}` with no value anywhere is reported by `validate` as an unknown prop; extra props that
the component never uses are ignored.

## Working on a component alone

A component is a normal composition, so every command can target it with `--comp`:

```bash
vidmake storyboard project.json --comp lowerThird -o lt.png -n 6
vidmake still project.json --comp lowerThird -t 1s -o lt.png --props '{"name":"Grace Hopper"}'
vidmake render project.json --comp lowerThird -o lower-third.mov   # transparent ProRes 4444
```

`vidmake list project.json` shows every composition with its size, duration and props. Without
`--comp`, commands use the first composition, so keep the main one first in the `compositions` array.

> [!TIP]
> Size a component's composition to its content, not to the full frame: the box then means something
> (anchors, layouts and `inspect` bounding boxes all use it), and the transparent margins don't hide
> other layers through clipping.

## Rules and limits

- `ref` must name a composition in the same file. A `use` can reference a composition that itself
  contains `use` layers, up to 12 levels deep; a composition that (directly or indirectly) uses itself is
  an error.
- Instances share the component's layer ids: both lower thirds have a `name` layer, and `inspect` shows
  each nested under its own `use` id. Editing commands (`vidmake set name.fontSize 44`) change the
  component itself, and with it every instance; keep ids unique across the file so such paths are
  unambiguous (they resolve to the first match).
- A component's `background` is ignored when it is used as a layer. Draw a background `rect` inside it
  if it needs one.
