# Widget packs

Source: https://docs.lightair.app/developers/extend/widget-packs

> Put several widgets in one Lightair package with a shared look, and arrange them with layout.json so Add all places them as your preview shows them.

In short:
- A widget pack is a package with several `widget` content entries.
- Each widget's page is built from its own folder only, so shared settings, code and fonts go into every widget's files.
- A `layout.json` at the package's root places each widget on a canvas; **Add all** follows it.

A widget pack is one package that holds a matching set of widgets: alerts, a chat box, a goal, a
ticker. People install it once and add every widget at once with **Add all**.

## Make a pack

1. Make each widget as its own folder, as on [Widgets](/developers/extend/widgets).
2. Put the folders under one package folder, `widgets/<name>/` by convention.
3. List each folder in `lightair-package.json`, in the order you want them shown.
4. Optionally add `layout.json` at the package's root to arrange them.

```text
open-air/
├── lightair-package.json
├── layout.json
├── LICENSE
├── licences/
│   └── outfit-OFL.txt
└── widgets/
    ├── alerts/
    ├── chat/
    ├── goal/
    └── recent/
```

```json title="lightair-package.json"
{
  "id": "com.example.open-air",
  "name": "Open Air",
  "version": "1.0.0",
  "lightair": ">=1.1 <2",
  "licence": "MIT AND OFL-1.1",
  "description": "Alerts, chat, recent events and a goal in soft plates with one accent colour.",
  "platforms": ["macos", "windows", "ios", "android"],
  "contents": [
    { "kind": "widget", "path": "widgets/alerts" },
    { "kind": "widget", "path": "widgets/chat" },
    { "kind": "widget", "path": "widgets/recent" },
    { "kind": "widget", "path": "widgets/goal" }
  ]
}
```

## Share a look across widgets

Lightair builds each widget's page from that widget's folder alone, and serves files only from
inside it. Each widget also keeps its own settings. So what the widgets share has to be in each
of them:

- **Look settings.** Give every widget the same settings for the look, under one group, in its
  `fields.json`. People then find the same **Look** controls on each widget.
- **Shared code and style.** Repeat it at the top of every widget's `main.js` and `style.css`.
- **Fonts and pictures.** Put a copy in each widget's folder, and load them by relative path
  (`url(fonts/outfit-600.woff)`).

```json title="widgets/goal/fields.json"
{
  "title": { "type": "text", "label": "Title", "value": "Follower goal", "group": "Goal" },
  "target": { "type": "number", "label": "Target", "value": 500, "min": 1, "group": "Goal" },
  "theme": { "type": "dropdown", "label": "Plates", "value": "dark", "options": { "dark": "Dark", "light": "Light" }, "group": "Look" },
  "accent": { "type": "colorpicker", "label": "Accent", "value": "#3AA6F5", "group": "Look" },
  "radius": { "type": "slider", "label": "Corner radius", "value": 12, "min": 0, "max": 28, "step": 1, "group": "Look" }
}
```

Copying by hand gets out of step quickly, so build the package from a source folder with a small
script. Lightair's own packs are written this way:

- `look.json` holds the look settings once. The build adds each to every widget's `fields.json`,
  after the widget's own settings, unless the widget already has a setting of that name.
- `shared.js` and `shared.css` hold the shared code and style. The build puts them in front of
  each widget's `main.js` and `style.css`.
- The build copies the fonts into each widget's `fonts/` folder, writes `layout.json` and
  `lightair-package.json`, then runs `lightair-pack build` (see
  [Publish](/developers/extend/publish)).

`look.json`, `shared.js` and `shared.css` are files of that build, not of the package: Lightair
doesn't read them.

## Arrange the widgets: layout.json

`layout.json` at the package's root places each widget on a canvas, by its folder's name:

```json title="layout.json"
{
  "canvas": [1920, 1080],
  "widgets": {
    "goal": [610, 40],
    "recent": [1460, 40],
    "alerts": [440, 230],
    "chat": [40, 340]
  }
}
```

- `canvas`: the size, in pixels, the places are given on. Both at least 1.
- `widgets`: each widget's top-left corner, `[x, y]`, keyed by the name of its folder (the last
  part of its content path). Widgets it leaves out have no place.

Lightair reads it only when it has a canvas and at least one place.

## How Add all reads it

**Add all** (on the package's page in the Packages panel, and on its row when you point at it) adds
every widget to the selected scene:

- The widgets go in one group named after the package, and each gets a mixer track.
- Each widget is its page's size, from its `manifest.json` (800 × 600 when it doesn't say).
- With a layout, the arrangement is scaled to fit each canvas of the show, keeping its shape, and
  centred on it. Each widget sits at its place, scaled the same, and kept inside the canvas.
  Widgets later in `contents` are in front.
- A widget the layout doesn't place is added hidden, in the middle of the canvas.
- Without a layout, the widgets are laid out side by side at their own sizes so none overlaps,
  scaled down together to fit each canvas, and the first in `contents` is in front.

Adding some of the widgets follows the layout only when it places every one chosen. Adding one
widget adds it as **Add Object › Widgets** does.

## Related

- [Widgets](/developers/extend/widgets)
- [Packages](/developers/extend/packages)
- [Publish a package](/developers/extend/publish)
