@plitzi/sdk-authoring 0.37.1 → 0.37.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ # When `authorSpace` refuses
2
+
3
+ `authorSpace` checks the whole declaration before it writes anything, and stops at the first thing that would not
4
+ render as written. The message names the element (`Element "text" (price) at shop/home/0/2`) and what to write
5
+ instead. **Do what the message says.** Never cast past a check, silence it, or move the logic into a plugin to avoid
6
+ it — the check exists because that declaration renders something other than what it says.
7
+
8
+ The one exception is a TEST fixture whose subject is the break itself — how the runtime copes with a document no author
9
+ would write. It names that break, and only that one: `authorSpace(spec, { allow: [{ code, element, why }] })`. See
10
+ [testing](testing.md#spaces-written-for-a-test). A space anybody visits never has an `allow`.
11
+
12
+ ## Refused
13
+
14
+ | The message says | What was wrong | Write instead |
15
+ | --- | --- | --- |
16
+ | `has "x", which it does not take` | a field the space, a page, an element, a binding or a step does not have | the field it suggests; an element's attribute goes inside the factory's props (`button({ content })`) |
17
+ | `sets "x", which a "button" never reads` | an attribute the element does not have | one it lists; an event (`onClick`) is a flow: `flows: [[onClick(), …]]` |
18
+ | `` `subType` is "h7" … It is one of `` | a value outside the attribute's list | one of the listed values |
19
+ | `reads it as true or false` / `as a list` | text where a flag or a list is read | `disabled: true`, not `'true'`; `items: [ … ]` |
20
+ | `holds none` | children on a type that renders only its own attributes | a `container` (for a heading made of parts, `container({ subType: 'h1', children })`) |
21
+ | `the template … cannot be read as written` | a template with an operator, filter or function that does not exist, or broken syntax | see [templates](templates.md) for what exists; `matches` never does |
22
+ | `reads "x", which nothing here answers to` | a name in a template that is not a source, a variable or a route param | the full source name (`list_rows.item`), `navigation.queryParams.x` for a query param, `source` for the bound value |
23
+ | `is not around it` | a source read by an element that is not inside the element publishing it | move the element inside it, or share the value through `state` |
24
+ | `lands on "x", which a "text" never reads` | a binding onto an attribute the element does not have — the value arrives and nothing shows it | one it lists (`content`); to follow data with a class, bind `styleSelectors.base` in `initialState` |
25
+ | `no element answers to the name "x"` | a binding whose source names an element that does not exist | the id of the element that publishes it |
26
+ | `renders text — and "items" holds a list` | a text template feeding a list | `bindTemplate('items', src, '{{ … }}', { returns: 'value' })` |
27
+ | `which a "x" never fires` / `never answers to it` | a flow on an event, or a step to an action, the element does not have | the element that fires it (the message names it); for a plugin, a name its declaration has — `declaredTrigger` / `declaredCallback` make a wrong one a compile error |
28
+ | `does not start with its trigger` | a flow whose first step is not the event that runs it | `[onClick(), setState(…)]` |
29
+ | `step "x" has the param "y"` / `"type" is "string"` | a step param that does not exist or a value outside its options | the params and values it lists |
30
+ | `step "setState" sets "x" on "y", which a "z" never reads` | an element `setState`/`toggleState` writing a field the element does not have | an attribute it lists, or for `category: 'state'` `visibility` / `styleSelectors.<selector>` |
31
+ | `names the page "x", and no page has that id` | a link or `navigate` to a page id that does not exist | an existing page id, or a path with its slash (`'/about'`) |
32
+ | `a full URL, in page mode` | a URL, `mailto:` or `tel:` in a link left in page mode | `mode: 'external'` |
33
+ | `is a controlled list with no items` | a list with nothing to render | `items: [ … ]` or `bind: { items: 'provider.data.rows' }` |
34
+ | `` has items, but its `source` is "none" `` | a list whose items nothing reads — it renders its children once | `source: 'controlled'` |
35
+ | `answers at /x for the same visitors as page` | two pages at one address | another slug — or `accessLevel` `'public'` on one and `'authenticated'` on the other |
36
+ | `is not one CSS value` | an empty CSS value, or one with `;` or `{}` | one value per property; leave a property out instead of writing it empty |
37
+ | `computed … declared after it` / `does not compute` | a computed value read before it is declared, or never declared | declare it in `computed`, above the one that reads it |
38
+
39
+ ## Warned
40
+
41
+ A warning means the space renders, and renders something you probably did not mean. Fix every one.
42
+
43
+ | Code | Meaning | Fix |
44
+ | --- | --- | --- |
45
+ | `condition-starts-visible` | a computed visibility shows until its data answers | `visible: { source, template }` |
46
+ | `template-never-resolved` | a condition in an attribute, used as written | move it into `bindTemplate` |
47
+ | `tablet-rule-skips-mobile` | a tablet rule phones never get | write it under `compact` |
48
+ | `default-content-beside-children` | a button prints "Button" beside its children | `content: ''` |
49
+ | `overlay-starts-open` | a modal or dialog is open when the page loads | `visible: false`, opened by `openModal` |
50
+ | `provider-without-source` | an `apiContainer` asks nothing | give it a `query` (or `action`, `connector`, `resource`) |
51
+ | `unknown-element-type` | a type no built-in element has | the built-in it suggests; a plugin's DECLARATION goes in `authorSpace(space, { plugins: [declaration] })`, which also checks its events, actions and attributes |
52
+ | `colour-without-dark` | a colour token with no dark value | `{ light, dark, default }` |
53
+ | `FORM_SUBMIT_UNMANAGED` | a form the browser would submit itself | `managedByInteractions: true` |
54
+ | `STYLE_WITHOUT_TAG` | style on a provider that renders no element | `subType: 'div'` on the provider, or style its parent |
55
+ | `server-data-without-rsc` | a `runtime: 'server'` provider with a `connector` or `action` in a space that does not turn server data on — it renders its mock data | `rsc: { enabled: true }` on the space |
56
+ | `route-param-undeclared` | `navigation.routeParams.x` read on a page whose slug has no `:x` — always empty | add `:x` to the slug, or read `navigation.queryParams.x` |
57
+ | `form-control-unnamed` | a control in a form with no `name` — its value never reaches `values` | `formControl({ name: 'email', … })` |
58
+ | `form-control-name-taken` | two controls in one form with one name — one overwrites the other | a name each |
59
+ | `while-running` | `whileRunning` on a step that is not the trigger, or a value other than `skip`, `parallel`, `queue` | `[whileRunning('queue', onClick()), …]` |
60
+ | `trigger-keys` | an `onKey` flow whose `keys` cannot fire: two keys in one shortcut, only modifiers, or a name that is not a key | `onKey('f')`, `onKey('shift+f')`, `onKey('mod+k, escape')` |
61
+ | `state-toggled-in-branches` | two `setState` steps of one key, each under a `when` on that key — the second flips back what the first wrote | `toggleState({ key })`; for something shown by default, a key named for hiding it |
62
+ | `form-value-compared-to-blank` | a `when` asking whether a submitted field (`….values.x`) `=` or `!=` `""` — a field nobody typed in is not sent, so it never matches | `operator: 'empty'` / `'notEmpty'` |
63
+ | `overlay-never-opened` | a modal or dialog that starts hidden and that no step opens | a flow with `openModal('id')` / `openDialog('id')` |
64
+ | `control-without-name` | a button or link with no words (an icon, a picture without `alt`, nothing), or a field nothing names — a screen reader says just "button", a browser agent cannot find it | `title` on the button, `label` on the link, `label` (with `hideLabel: true`) on the field — see [accessibility](accessibility.md) |
65
+ | `image-without-alt` | an image that is not `decorative` and has no `alt` | say what it shows, or `decorative: true` |
66
+ | `click-on-static-element` | a click flow on a container, text, heading, image or list item — no keyboard reaches it, no agent finds it | the flow on a `button` (it holds children) or a `link` |
67
+ | `heading-level-skipped` | an `h4` right after an `h2` on a page — the outline misses a level | the next level down; size it with its class |
68
+ | `label-ignored` | a `label` on a container whose tag is named by what it holds (`li`, a heading) | the words inside, or a landmark tag (`nav`, `section`, …) |
69
+ | `dropdown-without-control` | a dropdown opened from a box or an icon — no keyboard opens it | a `button` as what opens it (`title` if it is only an icon) |
70
+ | `control-in-decorative` | a control inside a `decorative` container — Tab lands on something nothing announces | move it out of the illustration |
@@ -0,0 +1,180 @@
1
+ # Data and visibility
2
+
3
+ ## Bindings
4
+
5
+ ```ts
6
+ heading({ bind: { content: 'posts.title' } }) // short form: attributes
7
+ text({ bind: [bindTemplate('content', 'stats.data.total', "{{ source|number_format(0, '.', ',') }} views")] })
8
+ container({ bind: [bindTemplate('width', 'state.xp', '{{ source / 10 }}%', { category: 'style' })] }) // a style
9
+ text({ bind: [{ to: 'content', source: 'posts.title', when: { … } }] }) // full form: anything else
10
+ ```
11
+
12
+ `bindTemplate(to, source, template, { category?, returns? })` is the computed attribute — a template over the value
13
+ at `source`. `returns: 'value'` hands over what a single `{{ expression }}` evaluates to instead of its text: a list's
14
+ `items`, a number, a flag (see [lists](lists.md)).
15
+
16
+ **A source names the id you gave the element** — `'posts.title'`, `'postList.item.cover'` — and the prefix is
17
+ completed from what the element publishes (`apiContainer_posts`, `list_postList`; a `form` publishes under
18
+ `apiContainer`). A name nothing answers to, or a prefix that does not match, is refused.
19
+
20
+ **Inside a template the name is spelled in full**: `{{ apiContainer_stats.data.total }}`, `{{ list_rows.item.id }}`.
21
+ The binding's `source` is completed for you; its template is read as written, so the short name there is refused.
22
+
23
+ An element re-renders when anything it reads changes: the first part of each binding's `source` and every source its
24
+ templates name. The globals are `variables`, `navigation` (`routeParams`, `queryParams`, `origin`, `currentPageId`),
25
+ `auth` (who is signed in, `status` while that is being found out), `state` (what flows wrote), `theme`
26
+ (`mode`, `resolved`), `host` (the `hostData` an application embedding the space hands the SDK) and `computed` (below).
27
+
28
+ ## Values computed once
29
+
30
+ A value many elements show — a score, a level, a count of what the visitor picked — is declared once on the space and
31
+ read everywhere as `computed.<name>`, instead of repeating its expression in every binding:
32
+
33
+ ```ts
34
+ export const space = {
35
+ …,
36
+ computed: {
37
+ xp: '{{ (state.favourites|length) * 10 + (state.runs ?? 0) * 5 }}',
38
+ level: '{{ computed.xp // 100 + 1 }}'
39
+ }
40
+ };
41
+
42
+ text('', { bind: [bindTemplate('content', 'computed.level', 'Level {{ source }}')] })
43
+ ```
44
+
45
+ A computed value reads the globals (`state`, `auth`, `navigation`, `variables`, `theme`, `host`) and the ones declared
46
+ above it — not an element's source: bind that on the element. One `{{ expression }}` gives its value (a number, a
47
+ list); anything else gives text. `authorSpace` refuses a name read before it is declared, or never.
48
+
49
+ ## Providers
50
+
51
+ ```ts
52
+ apiContainer({ id: 'orders', query: '{{apiUrl}}/orders', credentials: 'include', children: [ … ] })
53
+ apiContainer({ id: 'board', runtime: 'server', action: 'queue-board' }) // resolved before the HTML
54
+ ```
55
+
56
+ - A provider's source is readable by its **descendants** only. Wrap what reads it; a sibling cannot see it.
57
+ - It publishes `data` plus `isLoading`, `isEmpty`, `hasError`, `errorMessage` — bind a state to those, not to guesses.
58
+ `isEmpty` reads what arrived: a `query`'s body when it is missing, `null`, `''`, `[]` or `{}`; a connector list by
59
+ its `records`; a `singleRecord` provider by its `record`. It is also true before the first answer, so pair it with
60
+ `not isLoading` for an empty state.
61
+ - A query that depends on state is a binding on `query`; it answers `''` (and fetches nothing) until the state exists:
62
+ `"{{ source ? apiUrl ~ '/workspaces/' ~ source ~ '/stats' : '' }}"` with `source: 'state.workspace.id'`.
63
+ - A space with any `runtime: 'server'` provider needs `rsc: { enabled: true }`.
64
+
65
+ ### Data in a project with no backend
66
+
67
+ An offline project (`offlineMode`) still never invents data: the content lives in JSON files the project serves
68
+ (`public/data/games.json` with Vite) and a provider reads them like any API — `apiContainer({ id: 'catalog', query:
69
+ '/data/games.json', cache: true, children: [ … ] })`. The page binds to `catalog.data.…` exactly as it would to a live
70
+ backend, so swapping the file for a real endpoint later changes one `query`. `mockData` is what the BUILDER shows while
71
+ editing; it is not a data source for the running page. Say on the page that demo content is demo content.
72
+
73
+ ### Live, cached, refreshed
74
+
75
+ - **Live:** `refreshSeconds: 15` asks again on its own (either runtime); it pauses in a hidden tab and never
76
+ overlaps. Use it for queues, feeds, status boards — then mark the panel as live so the reader knows it moves.
77
+ - **Cached:** `cache: true` (and `staleTime` seconds, 30 by default) shares an answer between providers asking the
78
+ same thing and across page changes.
79
+ - **Refreshed by writes:** a `webHook` that is not a GET refreshes every request to its own site by default
80
+ (`invalidateQueries: 'origin'`); a `runServerAction` refreshes everything (`'all'`). Narrow it with
81
+ `'elements'` + `invalidateElements: ['orders']`, or stop it with `'none'`.
82
+ - **Say `'none'` when a refresh would move the page on.** A page that decides where to go from a provider's answer
83
+ ("profile complete → leave") will act on the refreshed answer in the middle of a multi-step form that saves as it
84
+ goes, and skip the last step.
85
+
86
+ ## Visibility
87
+
88
+ ```ts
89
+ container({ visible: 'posts.hasPosts', … }) // shown while true
90
+ container({ visible: '!posts.hasPosts', … }) // its inverse
91
+ modalContainer({ visible: false, … }) // starts hidden; a flow opens it
92
+ ```
93
+
94
+ **An element is visible by default.** Which way its condition flips decides how it is written:
95
+
96
+ | The logic… | Example | Write |
97
+ | --- | --- | --- |
98
+ | reveals it: hidden until the data says so | empty state, "get started", admin-only panel, error note | `visible: 'src'`, or `visible: false` + a computed binding |
99
+ | hides it: shown unless a flag says otherwise | sidebar labels until the sidebar is folded, a banner until dismissed | a binding with no `visible`; an absent flag must leave it shown |
100
+
101
+ `visible` starts the element **hidden**, and it appears when its data says so. A revealing condition you have to
102
+ compute is `visible: { source, template }` — it starts hidden too, and the template's value is read as a yes or a
103
+ no: `false`, `0`, an empty text, an empty list and nothing at all are a no, anything else a yes. Write the condition
104
+ itself; `? 'true' : 'false'` is not needed:
105
+
106
+ ```ts
107
+ container({
108
+ id: 'first-steps',
109
+ visible: {
110
+ source: 'stats.data.totals',
111
+ template: '{{ source and not (source.spaces > 0 and source.published > 0) }}'
112
+ }
113
+ })
114
+ ```
115
+
116
+ Rules for a condition's template:
117
+
118
+ - **A revealing condition starts hidden** — `visible: { source, template }` does that for you. A visibility binding
119
+ written by hand in `bind` keeps the element on screen until it answers — right for a hiding flag, a flash for a
120
+ condition on data. `authorSpace` warns `condition-starts-visible` when a computed one on a provider starts on screen.
121
+ - **A hidden element stays in the DOM** (`plitzi-component--hidden`), its subtree mounted. `loadStrategy: 'visible'`
122
+ mounts the subtree only while it is shown, and `'lazy'` from the first time it is shown (modals do that already).
123
+ Use it on heavy panels that are usually hidden; leave the default on what is usually on screen.
124
+ - **`'true'` shows, `'false'` hides, `''` writes nothing** and leaves the element as it is. Visibility hides only on
125
+ an explicit false.
126
+ - **Answer what "not known yet" should mean.** For a revealing condition that is `'false'` (`source ? … : 'false'`);
127
+ for a hiding flag it is `''` — nothing written, the default stays. A sidebar that answered `'false'` for a
128
+ "folded" state nobody had set yet hid every label of the menu for everybody.
129
+ - **Hide the element itself.** Never wrap it in a container that is shown while the inner one is hidden: an empty
130
+ visible wrapper still takes a slot in its parent's `gap`, and leaves a hole in the page.
131
+
132
+ ### Loading, empty, error
133
+
134
+ Four states, and each has its own element:
135
+
136
+ | State | How to tell |
137
+ | --- | --- |
138
+ | Loading | `apiContainer_x.isLoading` — or simply nothing: every condition is hidden until data arrives |
139
+ | Empty | the answer ARRIVED and is empty: `{{ source is defined and source is empty }}` over the list |
140
+ | Error | `apiContainer_x.hasError` |
141
+ | Data | the list itself |
142
+
143
+ `visible: '!provider.data.items'` is NOT an empty state: before the answer, `!undefined` is true and "Nothing here yet"
144
+ shows on every load. Test for "arrived and empty" (`is defined and … is empty`), or bind to `isEmpty` together with
145
+ `not isLoading`.
146
+
147
+ ## Never ask the data for an opposite
148
+
149
+ Both sides of one question are `visible: 'x'` and `visible: '!x'`, not an `x` and a `notX` in the server's answer.
150
+ `!` reads a boolean that travelled as text (`"false"`, `"0"`) correctly. A three-state condition
151
+ (`Boolean(post) && !canEdit`) still belongs where the data is made.
152
+
153
+ ## State that outlives a reload
154
+
155
+ `render(…, { state })` seeds `runtime.state` when the page starts — the way a host hands a space what it already knows.
156
+ `settings: { keepState: true, stateStorage: 'localStorage' }` keeps `runtime.state` across reloads, filed under
157
+ whoever is signed in (a guest's state is the browser's; another account never sees it). Keep only what a person would
158
+ expect back — favourites, a chosen theme. Everything else goes in `transientState`, which is never written and never
159
+ brought back:
160
+
161
+ ```ts
162
+ settings: { keepState: true, transientState: ['filter', 'tourStep', 'panelOpen'] }
163
+ ```
164
+
165
+ Do not reset kept state from `onPageLoad` instead: what was kept is restored late — after hydration, once auth knows
166
+ who this is — and lands in the middle of that flow, so half of it is undone. A filter restored on the next visit is a
167
+ page that looks broken. Keeping state is the SPACE's setting; a page does not take `keepState`.
168
+
169
+ That lateness shows. Kept state is restored after the server's first paint, so anything kept that changes what is
170
+ DRAWN — the tool a toolbar shows as last picked, a name in an avatar, a panel left off — is drawn with its default and
171
+ then swapped. List those keys in `paintedState`: they are kept in a cookie too, the server renders with them, and the
172
+ page starts from the same values.
173
+
174
+ ```ts
175
+ settings: { keepState: true, paintedState: ['shapesPick', 'name', 'color'], transientState: ['panelOpen'] }
176
+ ```
177
+
178
+ Only what the first paint shows, and small values: the cookie travels with every request and holds a few kilobytes
179
+ (over that, the page falls back to the defaults and the dev tools say so). Never a secret — a key, a token — and never
180
+ a key that is also in `transientState` (`authorSpace` refuses it).
@@ -0,0 +1,161 @@
1
+ # Elements and styles
2
+
3
+ ## Elements
4
+
5
+ One factory per element, named after it. Attributes and the authoring fields go in ONE flat object:
6
+
7
+ ```ts
8
+ heading({ content: 'Fieldnotes', subType: 'h2', class: title })
9
+ image({ src: '/fox.jpg', alt: 'A fox', css: { 'aspect-ratio': '3/2' } })
10
+ container({ class: card, children: [ … ] })
11
+ text('Wildlife, close up') // a string is the content
12
+ container([hero, grid]) // an array is the children
13
+ ```
14
+
15
+ | Authoring field | What it does |
16
+ | --- | --- |
17
+ | `id` | the ONE name this element answers to — its key in the document, a binding's source, a step's target. Unique across the WHOLE space |
18
+ | `class` | a shared class: a `styles()` declaration or a name from the space's `classes`; a list for several. Exclusive with `css` |
19
+ | `css` | rules of this element's own: one set, or one per breakpoint (`{ desktop, tablet, mobile }`) |
20
+ | `states` | `hover`, `focus-visible`, `hidden`… beside the element's own `css` |
21
+ | `variant` | a variant of the class it wears (when the class declares it) or of its type |
22
+ | `slots` | a class for one of the element's OTHER selectors — a form control's `input` |
23
+ | `bind` | where a value comes from |
24
+ | `visible` | a condition (`'src'`, `'!src'`, `{ source, template }`), or `false` to start hidden |
25
+ | `flows` | what happens on click, on submit, on load |
26
+ | `runtime` | `'server'` resolves this provider's data on the server |
27
+ | `loadStrategy` | when the contents mount relative to visibility: `eager` (default), `lazy`, `visible` |
28
+ | `children` | the tree |
29
+ | `meta` | `meta.label` is the element's name in the builder's tree |
30
+
31
+ Anything else in the object is an **attribute** (`authorSpace` warns `unknown-attribute` for one the element never
32
+ reads).
33
+
34
+ **A link's `href` is a page id.** `link({ href: 'reports' })` finds the page by id, folder included. `mode: 'internal'`
35
+ for a path within the space carrying `{{tokens}}` — a row's own slug (`'/games/{{ list_games.item.slug }}'`), a route
36
+ param, state; `mode: 'external'` for a full URL passed through untouched. A link is an `<a>`: never put a link inside
37
+ another link — make the card the link, or the button, not both.
38
+
39
+ **Some types hold no children.** A `heading`, `text`, `paragraph`, `image`, `formControl`… renders its own attributes
40
+ and nothing nested in it — `authorSpace` refuses children there. A heading made of parts (a word in another colour, an
41
+ icon, a badge) is a `container` with the heading's tag: `container({ subType: 'h1', class: title, children: [text('Space
42
+ '), text('Gamer', { class: accent })] })` — give the texts `display: inline` in their class.
43
+
44
+ **A `button` shows its `content` AND its children.** `content` defaults to "Button": a button whose words are children
45
+ says `content: ''` (`default-content-beside-children` warns otherwise). Its accessible name is everything it shows.
46
+
47
+ **A `formControl` works on its own.** Inside a `form` its value is the form's and it validates on submit; outside one it
48
+ keeps its own value and fires `onChange` with `{ value, name }` on every change — a search box or a select that
49
+ filters a screen needs no form. Bind `defaultValue` to the state it writes and a reset of that state reaches it.
50
+
51
+ **A `list` is a `<ul>`.** The browser indents it and puts bullets on it; a list of cards needs
52
+ `{ margin: '0px', padding: '0px', 'list-style-type': 'none' }` in its class.
53
+
54
+ **A provider has no box unless you give it a tag.** An `apiContainer` renders no element of its own by default, so its
55
+ children sit in the parent's layout and a `class` on it styles nothing (`STYLE_WITHOUT_TAG`). Put the layout on the
56
+ parent, or give it `attributes: { subType: 'div' }`.
57
+
58
+ A type this SDK does not ship — a plugin — is authored the same way: `defineElement<Props>({ type, … })`,
59
+ `element<Props>('type', props)`, or `elementsFromManifest(manifest)`. See [plugins](plugins.md).
60
+
61
+ **What a type takes, answered by the package** instead of the 7,000-line `.d.ts`: every catalogue is exported, keyed by
62
+ type — `elementCatalog` (what it is for), `elementDefaultAttributes`, `elementAttributeNames`, `elementTriggers`,
63
+ `elementCallbacks`, `elementSlots`, `elementSourceTypes`, `elementLeafTypes`, plus `BUILTIN_TRANSFORMERS`. One line
64
+ prints any of them:
65
+
66
+ ```bash
67
+ node --input-type=module -e "import * as a from '@plitzi/sdk-authoring'; const t = 'formControl'; console.log({ attributes: a.elementDefaultAttributes[t], triggers: a.elementTriggers[t], slots: a.elementSlots[t] })"
68
+ ```
69
+
70
+ ## CSS
71
+
72
+ Write CSS as you write CSS, shorthands included — they are expanded to the longhands Plitzi's editor reads. A property
73
+ outside the vocabulary is refused with the key it should have been (`paddingTop` → `padding-top`).
74
+
75
+ - `column(gap, extra?)`, `row(gap, extra?)`, `grid(columns, gap, extra?)` for the three layouts every space repeats.
76
+ - **Breakpoints are ranges, not a cascade.** `tablet` is 48–64rem, `mobile` below 48rem, and each inherits only from
77
+ `desktop` — a rule for `tablet` never reaches a phone (`authorSpace` warns `tablet-rule-skips-mobile`). A rule for
78
+ everything narrower than a desktop is `compact`, written to both: `css: { desktop: { … }, compact: { 'grid-template-columns':
79
+ '1fr' }, mobile: { gap: '8px' } }` — what `tablet` or `mobile` say for themselves wins.
80
+ - **`backdrop-filter`, `filter` and `transform` make an element the containing block of its `position: fixed`
81
+ children.** A glass header with a fixed phone dock inside puts the dock at the top of the header, not the screen.
82
+ - **Fonts are declared.** A `font-family` loads only if the space lists the face in `fonts`
83
+ (`{ source: 'google', family: 'Geist', fallback: 'system-ui, sans-serif', weights: [400, 600], styles: ['normal'] }`).
84
+
85
+ ## Classes
86
+
87
+ ```ts
88
+ const card = styles('card', {
89
+ css: { padding: '24px', 'border-radius': '12px', 'background-color': 'var(--card)' },
90
+ states: { hover: { 'border-color': 'var(--primary)' }, 'focus-visible': { outline: '2px solid var(--primary)' } },
91
+ variants: { active: { 'border-color': 'var(--primary)' } }
92
+ });
93
+
94
+ container({ class: card, children: [ … ] });
95
+ ```
96
+
97
+ - A flat object is the base rules; `{ css, states, variants, ancestors }` when there is more. Each part may be written
98
+ per breakpoint.
99
+ - **Share a rule as a class, never as a spread.** Spreading a `const` into each element's `css` shares the source and
100
+ duplicates the document: one selector per element, so restyling one card in the builder restyles one card.
101
+ - **Build a family from a plain object**, not by layering classes: `styles('button-primary', { ...buttonBase, … })`.
102
+ - One name declared twice with rules that disagree is refused. Declare a class once, in the module that owns it, and
103
+ import it.
104
+ - `ancestors: { [sidebar.name]: { variants: { collapsed: { display: 'none' } } } }` styles an element by the state or
105
+ variant of an ancestor class — the way to react to a parent's hover or collapse without a flow.
106
+ - **`hidden` is how an element looks while its `visible` says no** — where it goes as it hides, and where it comes
107
+ from as it shows. Give the class a transition that includes `display` with `allow-discrete`, and a panel fades or
108
+ slides instead of blinking; the transition on the base is the way in, the one in `hidden` the way out:
109
+
110
+ ```ts
111
+ const panel = styles('panel', {
112
+ css: {
113
+ transition: 'opacity 220ms cubic-bezier(0.2, 0.9, 0.3, 1.2), transform 220ms cubic-bezier(0.2, 0.9, 0.3, 1.2), display 220ms allow-discrete'
114
+ },
115
+ states: {
116
+ hidden: {
117
+ opacity: '0',
118
+ transform: 'translateY(-6px) scale(0.98)',
119
+ transition: 'opacity 120ms ease-in, transform 120ms ease-in, display 120ms allow-discrete'
120
+ }
121
+ }
122
+ });
123
+ ```
124
+
125
+ What is inside follows its container with `ancestors: { [panel.name]: { states: { hidden: { … } } } }` — a
126
+ different `transition-delay` per item staggers them. A container whose children mount only while it shows
127
+ (`loadStrategy: 'visible'`) empties before it can leave: use `'lazy'` on one that animates out.
128
+ - **`:hover` is part of the selector, not a rule in `customCss`.** `customCss` is for what no class can say:
129
+ keyframes, pseudo-elements, a rule across two unrelated elements, the inside of a third-party widget.
130
+ - Per element TYPE defaults go in `elements: { heading: { base, states, variants, slots } }` — a whole type at once.
131
+ - **Slots** are an element's other selectors: a `formControl`'s `input`, `label`, `error`; a `modalContainer`'s
132
+ `backgroundContainer`, `rootContainer`, `headerContainer`, `headerTitle`, `headerCloseButton`, `bodyContainer`; a
133
+ `dialogContainer`'s footer and buttons. `elementSlots` lists them all by type. Style one element's with `slots`, a
134
+ whole type's with `elements.<type>.slots`.
135
+ - A `modalContainer` / `dialogContainer` is fixed to the viewport, as tall as its content up to the screen, and 500px
136
+ wide up to the screen; theme its look through its slots.
137
+
138
+ **A variant chosen by the data:** `variantFrom(pill, 'jobs.item.status')`, or with a template when the data does not
139
+ already speak in variant names — `variantFrom(pill, 'runs.item.status', { template: "{{ source == 'completed' ?
140
+ 'ok' : 'failed' }}" })`. The binding's key names the CLASS; never write it by hand. For "the current page", see
141
+ `activeOn` in [layouts.md](layouts.md).
142
+
143
+ ## Colours and themes
144
+
145
+ Every colour is a **variable** with both values, and elements say `var(--name)`:
146
+
147
+ ```ts
148
+ variables: {
149
+ color: {
150
+ foreground: { light: '#0c0c14', dark: '#ededf3', default: '#0c0c14' },
151
+ card: { light: '#ffffff', dark: '#101019', default: '#ffffff' },
152
+ primary: { light: '#5b3df5', dark: '#6e52f7', default: '#5b3df5' }
153
+ }
154
+ }
155
+ ```
156
+
157
+ - Choose each value against its own background: a colour picked on white is not the same colour on near-black.
158
+ - Fixed colours are only for surfaces that are fixed in both themes (a brand panel that is always dark) — and then
159
+ the text on them is fixed too. Theme-following text on a fixed background is the bug.
160
+ - A tint of a token is `color-mix(in srgb, var(--primary) 12%, transparent)`, which follows the theme for free.
161
+ - `themeToggle()` is the switch; `theme.resolved` (`light`/`dark`) is a global source for anything else that needs it.
@@ -0,0 +1,171 @@
1
+ # Flows
2
+
3
+ ```ts
4
+ button({
5
+ id: 'cta',
6
+ content: 'Get a quote',
7
+ flows: [
8
+ [
9
+ onClick(),
10
+ named('quote', runServerAction({ actionId: 'shipping-quote', input: { city: 'Berlin' }, mode: 'await' })),
11
+ whenSucceeded('quote', setState({ key: 'quote', type: 'text', value: '{{ quote.output.summary }}' })),
12
+ whenFailed('quote', addNotification({ content: 'Could not reach the courier', appearance: 'danger' }))
13
+ ]
14
+ ]
15
+ });
16
+ ```
17
+
18
+ A flow is a list: a trigger, then steps in order. "Only if" is on the step (`when`, `whenSucceeded`, `whenFailed`),
19
+ never a nested tree. Use the step builders — they fill in where a step runs and what it takes:
20
+
21
+ - **Where it runs.** A global callback registers under its source MODULE (`state`, `auth`, `actions`), an element
22
+ callback under an element's id, a utility under nothing. Naming either half wrong is a control that does nothing,
23
+ with no error — which is why the builders exist.
24
+ - **Which `setState`.** `setState({ key })` writes `runtime.state.<key>` and is read as `state.<key>`; never put
25
+ `state.` in the key. `updateElement(…)` changes one element's own attribute or state.
26
+ - **Flip in one step.** `toggleState({ key })` for app state, `toggleElement({ category: 'state', key: 'visibility' },
27
+ 'panel')` to show/hide an element. Never two branches under opposite `when` guards — the second reads what the
28
+ first just wrote and flips it back. A key never set toggles to `true`, so for something shown by default name the
29
+ key for hiding it (`sidebarCollapsed`).
30
+ - **A trigger fired again while its flow runs is IGNORED** (`skip`, the default — no double submit). For a stream of
31
+ events that must each run, wrap the trigger: `whileRunning('queue', on('onArrival'))` (in order) or `'parallel'`.
32
+ - **Each step reads the page as it is when it runs.** A `when` or a `{{ state.x }}` after a `setState` sees the new
33
+ value, and one after a `delay` or a server action sees whatever changed meanwhile. To act on the value from BEFORE
34
+ a write, put the step that reads it first.
35
+ - **Keys are flat names.** A dotted key (`docsClosed.start`) is split into a path; use `docsClosedStart`.
36
+ - **`setState` types**: `text`, `number` (decimals kept), `boolean` (the word or a real boolean), and `json` for an
37
+ object or a list — `value: '{{ list_rows.item }}'` stores the row itself; JSON text is parsed, and text that is not
38
+ JSON fails the step (the dev tools log it) instead of storing the text.
39
+
40
+ ## What a trigger hands the flow
41
+
42
+ Name the trigger (`named('changed', onChange())`) and read its payload as `{{ changed.<field> }}`:
43
+
44
+ | Trigger | Payload |
45
+ | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------- |
46
+ | `onChange` (formControl) | `value` (a boolean for a checkbox), `name` |
47
+ | `onSubmit` (form) | `values` (by control `name`), `actionUrl`, `method` |
48
+ | `onPageLoad` (page) | `pageId`, `routeParams`, `queryParams` |
49
+ | `onApiSuccess` / `onApiError` (apiContainer) | `url`, `method`, `status`, `data` |
50
+ | `onModalOpen` / `onModalClose`, `onDialogOpen` / `onDialogClose` / `onDialogAccept` / `onDialogReject` | `metadata` — what `openModal` / `openDialog` was handed |
51
+ | `onPageChange` (pagination) | `page` |
52
+ | `onThemeChange` (themeToggle) | `theme` |
53
+
54
+ ## Reading what came before
55
+
56
+ `named('quote', step)` is how a later step reads an earlier one: `{{ quote.output.total }}`. The trigger too:
57
+ `named('submitted', onSubmit())` → `{{ submitted.values.email }}`. Unnamed steps get ids nothing can refer to.
58
+
59
+ A step's params are templates, evaluated in full (conditions, loops, filters). A source in them is named in full:
60
+ a row's button posts `{ jobId: '{{ list_jobRows.item.id }}' }` — the row that was clicked; the short name is refused.
61
+
62
+ **Pass objects, not JSON text.** `input: { title: '{{ form.values.title }}' }` rather than `input: '{"title": …}'`:
63
+ each value keeps its own type, and nothing depends on the quoting. (JSON text works — a value inside one of its strings
64
+ is escaped for it — but an object says the same without the punctuation.)
65
+
66
+ **A field left empty is not in `values`.** A form sends what was typed, and a field nobody typed in sends nothing —
67
+ so `when({ field: 'sent.values.code', operator: '=', value: '' }, …)` never holds for it. Ask
68
+ `operator: 'empty'` / `'notEmpty'`, which treat missing and `''` alike.
69
+
70
+ ## Writes and what they refresh
71
+
72
+ `webHook` (not GET) refreshes every request to its own site when it succeeds; `runServerAction` refreshes
73
+ everything. Say what it should refresh — `invalidateQueries: 'elements', invalidateElements: ['orders']` — or
74
+ `'none'` for a step that only reads, or when a refresh would make the page act on the new answer mid-flow.
75
+
76
+ ## Triggers belong to the element that fires them
77
+
78
+ Every element fires `onClick`, `onPointerDown` (the press, before it is a click — where a drag away from it starts),
79
+ `onLoad`, `onHover`, `onMouseEnter`/`onMouseLeave`, `onFocus`/`onBlur` and the ends of a
80
+ server action it started (`onFlowEnd`, `onFlowError`, `onFlowProgress`). A `page` fires `onPageLoad`, a `form`
81
+ `onSubmit`, a `formControl` `onChange`, an `apiContainer` `onApiSuccess`/`onApiError` (each answer, either runtime,
82
+ each refresh), a `modalContainer` `onModalOpen`/`onModalClose`, a `pagination` `onPageChange`. A flow on an element
83
+ that never fires its trigger is refused, naming the type that does.
84
+
85
+ **Keyboard shortcuts** are a trigger every element has: `onKey('f')`, `onKey('shift+f')`, `onKey('mod+k')` (⌘ on a
86
+ Mac, Ctrl elsewhere), several with commas (`onKey('plus, =')`). Heard on the whole page while the element is mounted,
87
+ so put it on the element whose flows it drives — `onKey('plus'), declaredCallback(map, 'zoomIn', { on: 'map' })` — or
88
+ on the page. A press while typing in a field is the field's, unless Ctrl/⌘/Alt is held or the key is Escape.
89
+ `{{ <step>.key }}` is the key pressed (`shift+f`). A shortcut that cannot fire (`'ctrl+shift'`, `'arrowupp'`) is refused
90
+ where it is written.
91
+
92
+ **A form's flow goes on the `form`**, which hands its submit over with `managedByInteractions: true`
93
+ (`FORM_SUBMIT_UNMANAGED` otherwise — the browser submits it natively and `onSubmit` never fires):
94
+
95
+ ```ts
96
+ form({
97
+ id: 'signup',
98
+ managedByInteractions: true,
99
+ flows: [
100
+ [named('submitted', onSubmit()), setState({ key: 'email', type: 'text', value: '{{ submitted.values.email }}' })]
101
+ ],
102
+ children: [
103
+ formControl({ id: 'email', name: 'email', label: 'Email', subType: 'email' }),
104
+ button({ content: 'Sign up', subType: 'submit' })
105
+ ]
106
+ });
107
+ ```
108
+
109
+ A `formControl` is `required` by default: an optional field says `required: false`, or an empty one stops the submit
110
+ without a word.
111
+
112
+ ## Notifications
113
+
114
+ `addNotification({ content: 'Saved', appearance: 'success' })` — `appearance` is `success`, `danger`, `warning` or
115
+ `info`. They follow the page's theme and font; their colours are the space's `notifications`:
116
+ `notifications: { background: 'var(--card)', text: 'var(--foreground)', success: 'var(--accent)', radius: '12px' }`.
117
+
118
+ ## Modals, dropdowns, tabs
119
+
120
+ - A modal or dialog is declared `visible: false` and driven with `openModal('credits')` / `closeModal('credits')`
121
+ (`openDialog` / `closeDialog`). `openModal`'s second argument is the modal's data: a JSON object is read by its
122
+ fields (`{{ modalContainer_credits.title }}`); anything else — a row's id, a number, a word — as
123
+ `{{ modalContainer_credits.content }}`. The close control is a real button, reachable by keyboard.
124
+ - A `dropdown`'s label is a child and its `dropdownPopup` sits inside it; a `tabContainer`'s header and body are held
125
+ inside it too. Outside, they are refused.
126
+
127
+ ## Realtime channels
128
+
129
+ Pages that see each other — cursors, presence, a shared board — need a channel. Declare its topic pattern on the space,
130
+ then subscribe with a `channel` element; a topic no pattern matches is refused, naming the patterns:
131
+
132
+ ```ts
133
+ channels: {
134
+ 'board:{id}': { access: { mode: 'public' }, publish: 'server' }, // only `realtime.publish` speaks
135
+ 'room:{id}': { access: { mode: 'public' }, presence: true } // pages speak directly
136
+ }
137
+
138
+ channel({ id: 'room', topic: 'room:{{ id }}', keep: 0, bind: { presence: 'computed.me' }, children: [...] })
139
+ ```
140
+
141
+ Its descendants bind `room.members`, `room.connected`, `room.last`; flows use `on('onMessage')` (`type`, `data`,
142
+ `from`), `onJoin`/`onLeave` (`from`, `user` and the `state` the member announced — `onJoin` only for who came after
143
+ this page), `publishOn('room', 'reaction', data)` and `announceOn('room', state)`. State everyone must
144
+ agree on goes through a server action whose last step is `realtime.publish` — validated and saved first, announced
145
+ after. `docs/en/realtime.md` is the whole of it.
146
+
147
+ A topic only some visitors may hear — a room behind a password, a customer's order — goes on a channel declared
148
+ `grant: true`. The action that decides they may be there ends with `realtime.grant` and answers the grant; the
149
+ element binds it, or the server refuses the topic every time (`channel-grant`). `realtime.revoke { topic, grant }`
150
+ takes one back — or every grant for the topic, naming none — and lets go whoever is on it with it:
151
+
152
+ ```ts
153
+ channels: { 'order:{id}': { access: { mode: 'session' }, grant: true } }
154
+
155
+ // steps: [ …decide…, { id: 'let', task: 'realtime.grant', params: { topic: 'order:{{ input.order }}' } } ]
156
+ channel({ id: 'order', topic: 'order:{{ id }}', bind: { grant: 'state.follow.grant' }, children: [...] })
157
+ ```
158
+
159
+ Two people writing the same thing at once: `kv.setIf` writes only if the value is still the one the flow read (empty
160
+ `expected` = only if nothing is there yet — claim a seat, a username), `list.put`/`list.range` keep an ordered list
161
+ (latest first, a leaderboard), and `flow.rateLimit` first in a public action that writes.
162
+
163
+ ## Lists as state
164
+
165
+ `toggleInState({ key: 'picks', value })` keeps a list; `when({ field: 'state.picks', operator: 'contains', value })`
166
+ asks it. `appendState({ key, value, withId: true })` stores `{ id, value }` — bind `.value`, address by `.id`.
167
+ `whenFailed` matches every outcome that is not `completed` (also `skipped`, `aborted`). A failed run gives `reason`,
168
+ and `error` only when the server or a step wrote one for the caller (a task throwing `ActionRefusal`, `flow.fail` with
169
+ `tellCaller`) — so show `{{ saved.error ? saved.error : "…" }}`, never the bare `error`.
170
+
171
+ `when(rule, step)` around a step that already has a `when` adds to it: both must hold.