# HTML templates for the built-in screens

## Checking templates

`lim check`, the Studio check, the VS Code extension, and MCP's `check` and `check_text` check a game's `ui.templates` the same way (`projectDiagnostics` in `@liminalia/sdk`; the list of diagnostics is "Screen templates" in [format.md](../format.md)).

| Reports | code | Severity | Where |
|---|---|---|---|
| A registered file is missing or can't be read | `template-missing` | Error | The screen's line in `ui.templates` of liminalia.json |
| A screen name that can't be built from a template | `template-unknown-screen` | Warning | Same |
| A plugin's `screens.*` replaces the same screen | `template-overridden` | Warning | Same |
| An unknown part name, a `<template>` without `data-part`, or a file with no parts and no `<style>` | `template-unknown-part` | Warning | That `<template>`'s line (line 1 for a file) |
| A `data-role`, `data-field`, `data-text` or `data-setting` that doesn't work on that screen | `template-unknown-value` | Warning | That element's line |
| A `data-role` the screen needs is missing | `template-missing-role` | Error | The line of the part `screen` |
| A `data-list` without `data-item`, or a missing `data-item` part | `template-list-item` | Error | That element's line |
| Things the player removes (the list in "Handling a game's HTML safely") | `template-unsafe` | Warning | That element's line, or the line inside `<style>` |
| The copied version is older than the built-in one, or has no version | `template-outdated` | Warning | That `<template>`'s line |

- Each screen's rules aren't written on the checking side. The owner of the built-in template (`packages/screens/src/<screen>.ts`) holds them as `StandardScreen` (`@liminalia/lang`), listed in `STANDARD_SCREENS` in `packages/screens/src/rules.ts`. The rules are the working `data-role` values (`roles`), the `data-field` names (`fields`), the required `data-role` values (`required`; per group, any one is enough), the plugin hooks that replace the screen (`replacedBy`: `screens.confirm`, `system` and so on), and the setting names tied to controls (`settings`; settings screen only; names starting with `custom.` always pass). Part names and versions are read from the built-in template text. A screen rewritten as a template gets checks just by writing and listing its rules.
- Keys that can go in `data-text` are all text keys of the built-in wording (`TEXT` in `strings.ts`).
- Required `data-role` values are checked on the whole, with the game's parts laid over the built-in ones (a template that copied only one part counts the rest from the built-in parts).
- Checks read the text without a DOM (`screen-templates.ts` in `@liminalia/lang`). The reading follows the player (the outermost `<template data-part>` is a part, and the list of removed things is the same).
- The rules live on the rendering side (`@liminalia/web/screen-rules`), and `@liminalia/lang` and `@liminalia/sdk` don't read them. `lim check` and the VS Code extension read them and pass them in, and for Studio, the side that starts the dev server (`vite.config.ts` of `web`, Studio Mobile's Service Worker, `lim mcp`) passes them with `setScreenTemplateRules` (like `setScreenTable` for the list of built-in screens).
- Screens a plugin replaces are read from the text of the plugin's `./web` (`pluginScreens` in `@liminalia/project/screen-templates`; the rendering code can't run in Node). It looks the same way as `replacedScreens` in the player (`ui-probe.ts`): the `system`, `title` and `screens` entries of an object that has only `name` and `WebPlugin` entries, plus `screens` and `title` of the theme chosen in `ui.system.preset`. Contents of `screens` held in another variable can't be read, so it leans toward not reporting.

## Things to watch when rewriting the remaining screens (learned in the prototype)

- Write the screen's rules (`StandardScreen`) in `packages/screens/src/<screen>.ts` and list them in `packages/screens/src/rules.ts` ("Checking templates" above). The `roles` of the menu and title include system action names (`SYSTEM_ACTIONS`).

- First add tests of the screen's current behaviour (shape, class names, keys, focus, text-to-speech, results, closing), and confirm they pass unchanged after the rewrite. The confirmation kept the same DOM as a template (class names, button order, `type`, `role`).
- Indentation whitespace is dropped, so templates can be indented for readability. Spaces within text (between `<b>name</b> <a>…</a>`) stay.
- Write initial focus with `data-autofocus`. What screens decide in code today (a saved position, the current page, the previously chosen slot) can't be written as attributes, so each screen's code decides it (stage f1's choices decide "the first pressable choice" in code).
- Screens whose contents change while open rebuild lists with `view.list` (added in stage f1: the controls help's tabs, save and load pages, backlog search). To rewrite only values, use `view.update(values)` (added in stage f2: the backlog search count). Take care to keep the focus and reading position.
- Learned in stage f2: where the engine builds the contents of a list row (the backlog text's ruby, color and images), code replaces the contents of the element with `data-field` (the game decides the element's place and look, and the engine puts in the contents). Template checks look at `data-field` names, so no new attribute is needed. An input (backlog search) would become a pressable button with `data-role`, so it's marked with `data-input` and the screen's code watches it.
- Learned in stage f1: a screen laid out inside an existing element (the choice layer) lays the template's outer frame over the layer. To keep CSS that assumes DOM children (`.choices > button`), use `<template data-list>`. Older attributes (`data-kind`, `data-action`, `aria-pressed`) are written the same with `data-attr-*` or in code.
- Learned in stage f3: the sliders, switches and choices of the settings screen use an attribute separate from `data-role` buttons (`data-setting`), and tying them to values calls the UI parts (`slider`, `toggle`, `choice` in `ui-parts.ts`). The unit of showing, hiding and reordering is the row, so `data-setting` goes on the row rather than the control, and the controls inside are told apart by element type (`<input>`) and attributes (`data-value`, `data-toggle`). For things whose action depends on the button pressed, like tabs, the pressed element is passed to the `data-role` action and the button's attribute (`data-page`) is read.
- If the screen's heading is `.lm-screen-title`, `h1` or `h2`, `aria-labelledby` is added (`pushScreen` looks for it).
- The sound is decided by the `data-role` value (back-type actions are cancel, pages are page, choices' choose is choice, others select). Where today's screens play a different sound, match it with `data-sound` (stage f1's menu "Return to game", the ending name).
- Measure the export size per screen ("When the built-in templates are loaded" above). Little is left under the total budget, so revisit the budget before the next screen.
- Menu and title actions work by writing a system action name in `data-role` (the same names as `SYSTEM_ACTIONS`). Conditions for showing and hiding (removed in settings, stopped in a trial version) are written by the engine with `data-lm-disabled` or `hidden`.
- When a game's template misnames a part (a part not in the built-in one), the player silently doesn't use it. `lim check` and the Studio check report it (`template-unknown-part`).

## Not built yet

- Rewriting the built-in screens that can't be built from templates (extras, flowchart, items, content warning and so on). Not rewritten in 0.4.0.
- Theme templates (`PluginTheme.templates`), and building template parts from the `api` of a plugin's `screens.*` (screen JS's `api.render` went in first).
- Reading screen JS in Studio Mobile.
