brumate 0.2.0 → 0.4.0
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.
- package/README.md +2 -2
- package/check.d.ts +49 -0
- package/dist/{boot-D-IdPy7B.d.ts → boot-DLlRmD9z.d.ts} +11 -10
- package/dist/cli.js +275 -15
- package/dist/cli.js.map +1 -1
- package/dist/client-BvDsC6Qr.js +3463 -0
- package/dist/client-BvDsC6Qr.js.map +1 -0
- package/dist/client-DBwL2wKQ.d.ts +669 -0
- package/dist/{client-routes-JTbGtukf.js → client-routes-BtGpYCef.js} +60 -30
- package/dist/client-routes-BtGpYCef.js.map +1 -0
- package/dist/{cookies-Av5WFmcf.js → cookies-Cnbf4w-S.js} +15 -7
- package/dist/cookies-Cnbf4w-S.js.map +1 -0
- package/dist/{defer-D0bo1Gqx.js → defer-CKWZLv2K.js} +8 -4
- package/dist/defer-CKWZLv2K.js.map +1 -0
- package/dist/devtools-4uTPwR_v.js +597 -0
- package/dist/devtools-4uTPwR_v.js.map +1 -0
- package/dist/devtools-panel-Be9pxYgv.js +1994 -0
- package/dist/devtools-panel-Be9pxYgv.js.map +1 -0
- package/dist/events-LgLdp1R7.js +114 -0
- package/dist/events-LgLdp1R7.js.map +1 -0
- package/dist/features-CHY5Ekku.js +30 -0
- package/dist/features-CHY5Ekku.js.map +1 -0
- package/dist/files-WW4RlajL.js +506 -0
- package/dist/files-WW4RlajL.js.map +1 -0
- package/dist/forms-j1xPAR14.d.ts +281 -0
- package/dist/forms-nXK7-Lv_.js +440 -0
- package/dist/forms-nXK7-Lv_.js.map +1 -0
- package/dist/highlight.d.ts +17 -0
- package/dist/highlight.js +278 -0
- package/dist/highlight.js.map +1 -0
- package/dist/html-BtKRyi7g.js +305 -0
- package/dist/html-BtKRyi7g.js.map +1 -0
- package/dist/html-Bw9Qwu6F.d.ts +107 -0
- package/dist/index.d.ts +142 -11
- package/dist/index.js +641 -106
- package/dist/index.js.map +1 -1
- package/dist/{inspect-BJtACnQp.js → inspect-Y6y4uNZp.js} +4333 -681
- package/dist/inspect-Y6y4uNZp.js.map +1 -0
- package/dist/{loader-B18i3_-T.js → loader-BjKGJzLN.js} +21 -14
- package/dist/loader-BjKGJzLN.js.map +1 -0
- package/dist/logo-DQ_OYhBi.js +10 -0
- package/dist/logo-DQ_OYhBi.js.map +1 -0
- package/dist/{navigate-BwuZveDF.js → navigate-CfOr-TRF.js} +130 -24
- package/dist/navigate-CfOr-TRF.js.map +1 -0
- package/dist/nesting-DA5vJw9X.js +265 -0
- package/dist/nesting-DA5vJw9X.js.map +1 -0
- package/dist/produce-B7X9ori9.js +402 -0
- package/dist/produce-B7X9ori9.js.map +1 -0
- package/dist/produce-DIgHBVwL.d.ts +148 -0
- package/dist/{rpc-B-Zkn6Ch.js → rpc-Q-XkLwFh.js} +28 -13
- package/dist/rpc-Q-XkLwFh.js.map +1 -0
- package/dist/runtime/batch.js +9 -7
- package/dist/runtime/batch.js.map +1 -1
- package/dist/runtime/client.d.ts +4 -4
- package/dist/runtime/client.js +4 -4
- package/dist/runtime/devtools.d.ts +172 -5
- package/dist/runtime/devtools.js +3 -1
- package/dist/runtime/env.js +14 -2
- package/dist/runtime/env.js.map +1 -1
- package/dist/runtime/events.d.ts +38 -1
- package/dist/runtime/events.js +1 -1
- package/dist/runtime/forms.d.ts +2 -0
- package/dist/runtime/forms.js +1 -0
- package/dist/runtime/gather.d.ts +25 -0
- package/dist/runtime/gather.js +33 -0
- package/dist/runtime/gather.js.map +1 -0
- package/dist/runtime/hot.d.ts +1 -1
- package/dist/runtime/hot.js +5 -2
- package/dist/runtime/hot.js.map +1 -1
- package/dist/runtime/html.d.ts +2 -2
- package/dist/runtime/html.js +1 -1
- package/dist/runtime/loader.d.ts +2 -2
- package/dist/runtime/loader.js +2 -2
- package/dist/runtime/routes.js +4 -1
- package/dist/runtime/routes.js.map +1 -1
- package/dist/runtime/server.d.ts +5 -4
- package/dist/runtime/server.js +5 -5
- package/dist/runtime/signals.d.ts +75 -2
- package/dist/runtime/signals.js +44 -2
- package/dist/runtime/signals.js.map +1 -1
- package/dist/runtime/static-loader.d.ts +2 -2
- package/dist/runtime/static-loader.js +1 -1
- package/dist/runtime/testing.d.ts +18 -3
- package/dist/runtime/testing.js +150 -33
- package/dist/runtime/testing.js.map +1 -1
- package/dist/runtime/values.d.ts +2 -0
- package/dist/runtime/values.js +24 -0
- package/dist/runtime/values.js.map +1 -0
- package/dist/{server-BA9SJ2Vm.d.ts → server-BvPIg9pT.d.ts} +363 -32
- package/dist/{server-BqfM95r1.js → server-DjyOVJtJ.js} +1309 -220
- package/dist/server-DjyOVJtJ.js.map +1 -0
- package/dist/{static-loader-BzE9SRaj.d.ts → static-loader-BGgdp_Y5.d.ts} +2 -2
- package/dist/static-loader-BZR17hSw.js +312 -0
- package/dist/static-loader-BZR17hSw.js.map +1 -0
- package/dist/{stream-JaMHMdBD.js → stream-BP5AVmg5.js} +182 -17
- package/dist/stream-BP5AVmg5.js.map +1 -0
- package/dist/values-B8C7CLvG.d.ts +63 -0
- package/dist/vite.d.ts +20 -20
- package/dist/vite.js +2031 -539
- package/dist/vite.js.map +1 -1
- package/elements.d.ts +951 -0
- package/forms/forms.brm.ts +141 -0
- package/package.json +41 -3
- package/skills/brumate/SKILL.md +58 -0
- package/skills/brumate/references/errors.md +2078 -0
- package/skills/brumate/references/events.md +117 -0
- package/skills/brumate/references/signals.md +61 -0
- package/skills/brumate/references/state.md +235 -0
- package/skills/brumate/references/syntax.md +250 -0
- package/types.d.ts +4 -4
- package/dist/client-C0UCNL3J.js +0 -1643
- package/dist/client-C0UCNL3J.js.map +0 -1
- package/dist/client-MD0ZmcFR.d.ts +0 -354
- package/dist/client-routes-JTbGtukf.js.map +0 -1
- package/dist/cookies-Av5WFmcf.js.map +0 -1
- package/dist/defer-D0bo1Gqx.js.map +0 -1
- package/dist/devtools-C50VH8gZ.js +0 -113
- package/dist/devtools-C50VH8gZ.js.map +0 -1
- package/dist/devtools-panel-AYjfwPhj.js +0 -435
- package/dist/devtools-panel-AYjfwPhj.js.map +0 -1
- package/dist/events-BKWRqtMg.js +0 -52
- package/dist/events-BKWRqtMg.js.map +0 -1
- package/dist/files-Bnn1JqHx.js +0 -192
- package/dist/files-Bnn1JqHx.js.map +0 -1
- package/dist/html-bHZbkK-h.js +0 -170
- package/dist/html-bHZbkK-h.js.map +0 -1
- package/dist/html-y1q7-t7D.d.ts +0 -60
- package/dist/inspect-BJtACnQp.js.map +0 -1
- package/dist/loader-B18i3_-T.js.map +0 -1
- package/dist/navigate-BwuZveDF.js.map +0 -1
- package/dist/produce-CWfJ4byV.js +0 -167
- package/dist/produce-CWfJ4byV.js.map +0 -1
- package/dist/produce-D4K7qpaL.d.ts +0 -72
- package/dist/rpc-B-Zkn6Ch.js.map +0 -1
- package/dist/server-BqfM95r1.js.map +0 -1
- package/dist/static-loader-DOygOFj5.js +0 -216
- package/dist/static-loader-DOygOFj5.js.map +0 -1
- package/dist/stream-JaMHMdBD.js.map +0 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# Events
|
|
2
|
+
|
|
3
|
+
`onEvent=handler` runs the handler when the DOM event fires. The name after `on` is the event: `onClick` is `click`, `onKeyDown` is `keydown`. The handler gets the event. It is the name of a function, an arrow function in parentheses, or a call that returns a function.
|
|
4
|
+
|
|
5
|
+
```brm
|
|
6
|
+
<script>
|
|
7
|
+
import { $signal } from 'brumate/signals';
|
|
8
|
+
import { once, prevent, stop } from 'brumate/events';
|
|
9
|
+
|
|
10
|
+
let draft = $signal('');
|
|
11
|
+
let message = $signal('');
|
|
12
|
+
let focused = $signal(0);
|
|
13
|
+
|
|
14
|
+
function send() {
|
|
15
|
+
message = `Sent: ${draft}`;
|
|
16
|
+
draft = '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function move(event: KeyboardEvent) {
|
|
20
|
+
focused += event.key === 'ArrowDown' ? 1 : -1;
|
|
21
|
+
}
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<form onSubmit=(prevent(send))>
|
|
25
|
+
<label>
|
|
26
|
+
Message
|
|
27
|
+
<input value=draft onInput=((event) => (draft = event.currentTarget.value))>
|
|
28
|
+
</label>
|
|
29
|
+
<button>Send</button>
|
|
30
|
+
</form>
|
|
31
|
+
<ul tabindex="0" onKeyDown=(prevent(move, 'ArrowDown', 'ArrowUp'))>
|
|
32
|
+
<li>Item {focused}</li>
|
|
33
|
+
</ul>
|
|
34
|
+
<button onClick=(stop(prevent(send)))>Send</button>
|
|
35
|
+
<button onClick=(once(() => (message = 'First click')))>Once</button>
|
|
36
|
+
<p>{message}</p>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Wrappers
|
|
40
|
+
|
|
41
|
+
A handler runs after the event has finished dispatching, so the browser has acted on the event by then. A wrapper from `brumate/events` applies before the code of the handler loads. The compiler reads it in the markup, so write it in the attribute.
|
|
42
|
+
|
|
43
|
+
| Wrapper | Effect |
|
|
44
|
+
| --------------------------- | ------------------------------------------------------------------------------ |
|
|
45
|
+
| `prevent(handler)` | Calls `event.preventDefault()`. |
|
|
46
|
+
| `prevent(handler, ...keys)` | On a keyboard event, prevents only these keys. The handler runs for every key. |
|
|
47
|
+
| `stop(handler)` | Stops the event at this element. |
|
|
48
|
+
| `once(handler)` | Runs the handler for the first event only. |
|
|
49
|
+
| `capture(handler)` | Runs the handler in the capture phase. |
|
|
50
|
+
| `transition(handler)` | Runs the handler in a view transition. It takes types after it. |
|
|
51
|
+
|
|
52
|
+
- Wrappers nest in any order: `stop(prevent(save))`.
|
|
53
|
+
- Keys are string literals of `event.key`: `'Enter'`, `'Escape'`, `'ArrowDown'`, `'F1'`, a letter or a number key, and `'Space'`. Only `prevent` of `onKeyDown`, `onKeyUp` and `onKeyPress` takes keys.
|
|
54
|
+
- A key alone is prevented only while Ctrl, Meta and Alt are up, so shortcuts of the browser keep working. `'Mod+ArrowDown'` names Ctrl or Meta, and `'Ctrl+…'`, `'Meta+…'` and `'Alt+…'` name one.
|
|
55
|
+
- There are no modifiers such as `onSubmit:prevent`, and none on components. A component puts the wrapper on the element that it renders.
|
|
56
|
+
- A handler that the caller passes to a component goes through `{...rest}` to the element, with its wrappers. Both run, the element's own first.
|
|
57
|
+
|
|
58
|
+
```brm wrong: Only prevent of a keyboard event takes keys, such as onKeyDown
|
|
59
|
+
<script>
|
|
60
|
+
import { prevent } from 'brumate/events';
|
|
61
|
+
|
|
62
|
+
function save() {}
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<button onClick=(prevent(save, 'Enter'))>Save</button>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```brm wrong: Name each key of prevent as a string of its event.key, such as 'ArrowDown', 'Space' or 'a', with a modifier to hold if it needs one, such as 'Mod+ArrowDown'
|
|
69
|
+
<script>
|
|
70
|
+
import { prevent } from 'brumate/events';
|
|
71
|
+
|
|
72
|
+
function move() {}
|
|
73
|
+
</script>
|
|
74
|
+
|
|
75
|
+
<ul onKeyDown=(prevent(move, 'NotAKey'))></ul>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Cancel from a part
|
|
79
|
+
|
|
80
|
+
`cancelWhen(element, 'click' | 'submit', check)` from `brumate/events` cancels the event of the element or of an element inside it while `check(event)` returns true, before any brumate handler runs. It returns a function that removes the check. It calls `preventDefault()` and does not stop the event, so plain `addEventListener` listeners still run and can read `event.defaultPrevented`. An element inside a shadow root is not reached.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const stop = cancelWhen(form, 'submit', () => form.hasAttribute('data-pending'));
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Handlers that a script makes
|
|
87
|
+
|
|
88
|
+
A function in a script is a handler only when markup uses it, so a spread leaves it out. `$handler(fn, ...args)` marks one. The arguments are JSON values, and the handler runs as `fn(...args, event)`. A value of the function around the call must be an argument.
|
|
89
|
+
|
|
90
|
+
```brm
|
|
91
|
+
<script>
|
|
92
|
+
import { $handler, $signal } from 'brumate/signals';
|
|
93
|
+
import { prevent } from 'brumate/events';
|
|
94
|
+
|
|
95
|
+
let items = $signal(['Milk', 'Bread']);
|
|
96
|
+
|
|
97
|
+
function removeAt(index: number) {
|
|
98
|
+
items = items.filter((_, at) => at !== index);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const remove = (index: number) => ({
|
|
102
|
+
type: 'button',
|
|
103
|
+
'aria-label': `Remove ${items[index]}`,
|
|
104
|
+
onClick: $handler(prevent(removeAt), index),
|
|
105
|
+
});
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<ul>
|
|
109
|
+
#for (item, index of items by item) {
|
|
110
|
+
<li>{item} <button {...remove(index)}>x</button></li>
|
|
111
|
+
}
|
|
112
|
+
</ul>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Window and document
|
|
116
|
+
|
|
117
|
+
`<window onResize=(handler) />` and `<document onKeyDown=(handler) />` listen to events of the window and the document. They take event handlers only, and close with `/>`.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# The functions of brumate/signals
|
|
2
|
+
|
|
3
|
+
The compiler replaces each call and removes the import, so none of them ship. Import each one that the file calls: `import { $memo, $signal } from 'brumate/signals';`. A call of one that the file does not import is a compile error. They work only in `.brm` files and `.brm.ts` modules.
|
|
4
|
+
|
|
5
|
+
"Top level of a script" means a statement of the component's `<script>`, or of a `<script>` at the start of a block body. It is not inside a function or another statement.
|
|
6
|
+
|
|
7
|
+
| Function | Returns | Where |
|
|
8
|
+
| ----------------------------- | -------------------------- | ----------------------------------------------------- |
|
|
9
|
+
| `$props<T>()` | `T` | `const` or `let` in the component script |
|
|
10
|
+
| `$signal<T>(value?)` | `T` | `let` at the top level of a script |
|
|
11
|
+
| `$memo<T>(value)` | the value, never a promise | `const` at the top level of a script |
|
|
12
|
+
| `$debounced(value, ms)` | the value, as a memo | `const` at the top level of a script |
|
|
13
|
+
| `$throttled(value, ms)` | the value, as a memo | `const` at the top level of a script |
|
|
14
|
+
| `$isPending(memo)` | `boolean` | Any expression |
|
|
15
|
+
| `$resolve(memo, options?)` | `Promise<T>` | A handler, an effect or a `$client` callback |
|
|
16
|
+
| `$isPassed(prop)` | `boolean` | `const` in the component script |
|
|
17
|
+
| `$isBound(prop)` | `boolean` | Any expression |
|
|
18
|
+
| `$effect(callback)` | `void` | Statement at the top level of a script |
|
|
19
|
+
| `$client(callback, trigger?)` | `void` | Statement at the top level of a script |
|
|
20
|
+
| `$untrack(value)` | the value | Any expression |
|
|
21
|
+
| `$produce(signal, recipe)` | `void` | Where a signal can be written |
|
|
22
|
+
| `$setContext(context, value)` | `void` | Statement at the top level of a script |
|
|
23
|
+
| `$getContext(context)` | the value | `const` at the top level of a script |
|
|
24
|
+
| `$route(path?)` | `{ url, search, params }` | `const` at the top level of a script |
|
|
25
|
+
| `$navigation()` | `{ pending, from, to }` | `const` at the top level of a script |
|
|
26
|
+
| `$id()` | `string` | `const` at the top level of a script |
|
|
27
|
+
| `$title(value)` | `void` | Statement at the top level of a script |
|
|
28
|
+
| `$action(fn)` | the result, or `undefined` | `const` at the top level of a script |
|
|
29
|
+
| `$optimistic(source)` | the value of `source` | `let` at the top level of a script |
|
|
30
|
+
| `$handler(fn, ...args)` | a handler | Any expression of a script or a module function |
|
|
31
|
+
| `$bind(value, set?)` | a binding | The whole value of a prop, or of `value` or `checked` |
|
|
32
|
+
|
|
33
|
+
## What each one does
|
|
34
|
+
|
|
35
|
+
- `$props<T>()`: the props of the component. Destructure it: `const { title, size = 'm', ...rest } = $props<Props>()`. With `let`, the component writes the props, and `Bindable<T>` in the type marks one that a parent can bind. Not in a block script.
|
|
36
|
+
- `$signal(value)`: writable state. Read it and assign to it as a variable. The start value is computed once, on the server.
|
|
37
|
+
- `$memo(expression)`: a read-only value that follows the values it reads. It can hold a promise, such as a server function call, and gives the settled value.
|
|
38
|
+
- `$debounced(value, ms)`: a memo that takes the value once it stops changing for `ms` milliseconds. `ms` is a number literal.
|
|
39
|
+
- `$throttled(value, ms)`: a memo that takes the value at most once every `ms` milliseconds.
|
|
40
|
+
- `$isPending(memo)`: true while a new value of the memo loads in the browser. Always false on the server.
|
|
41
|
+
- `$resolve(memo, { timeout, signal })`: waits until the memo is no longer pending, and gives its value. A handler that reads a memo that can lack a value uses `await $resolve(memo)` or checks `$isPending(memo)` first (BRM182).
|
|
42
|
+
- `$isPassed(prop)`: true when the caller passed the prop, also as `undefined` or `null`. A default value does not count. The argument is a prop that the component destructures.
|
|
43
|
+
- `$isBound(prop)`: true when the caller passed the prop with `$bind`, so a write changes the caller's value. The argument is a prop destructured with `let`.
|
|
44
|
+
- `$effect(callback)`: runs the callback in the browser when a value that it reads changes. Never on mount, never on the server. The callback takes no parameters and is not `async`. It can return a cleanup function.
|
|
45
|
+
- `$client(callback, trigger?)`: runs the callback in the browser once. The trigger is `'visible'` (default), `'idle'` or `'load'`. It can return a cleanup function.
|
|
46
|
+
- `$untrack(value)`: reads a value, or calls a function, without depending on it.
|
|
47
|
+
- `$produce(signal, recipe)`: changes a draft of an object or array in a signal, and writes the result as a new value. The recipe can return a new value instead.
|
|
48
|
+
- `$setContext(context, value)`: gives a value of a context to the components inside. Create the context with `createContext` from `brumate/context`.
|
|
49
|
+
- `$getContext(context)`: reads a context from the nearest component above that provides it, or its default.
|
|
50
|
+
- `$route(path?)`: the current route, as `{ url, search, params }`. The path is the route, such as `'/posts/[id]'`, and takes at most one argument.
|
|
51
|
+
- `$navigation()`: the state of a navigation, as `{ pending, from, to }`. It takes no arguments.
|
|
52
|
+
- `$id()`: an ID that is unique in the page and the same on the server and in the browser. It takes no arguments.
|
|
53
|
+
- `$title(value)`: sets the title of the page. The compiler warns about it, and it will be removed: use `<title>` in `#head`. It takes one argument.
|
|
54
|
+
- `$action(fn)`: the latest result of the server function `fn` of the same file, which a form of this component posts to. It is `undefined` before the first post.
|
|
55
|
+
- `$optimistic(source)`: a value that follows `source`, a memo, an action or a prop, and that handlers can write to show a guess. Write it with `=`, never `++` or `--`. `source` is not a signal.
|
|
56
|
+
- `$handler(fn, ...args)`: marks a function that a script makes as an event handler, so a spread passes it on. The arguments are JSON values, and the handler runs as `fn(...args, event)`.
|
|
57
|
+
- `$bind(value, set?)`: passes a value that the receiver writes back. The value is the name of a signal, or of a prop destructured with `let`. With `set`, a write calls `set(next)` instead of writing the value.
|
|
58
|
+
|
|
59
|
+
## Not supported
|
|
60
|
+
|
|
61
|
+
Calling one of these is a compile error that names the replacement: `$ref` (use a `$signal` with `ref=$bind(name)`), `$watch` (use `$effect`), `$settled` (use `$client`), `$cleanup` (return a function from `$effect` or `$client`), `$store` (assign a new value to a `$signal`, or use `$produce`), `$get` and `$set` (signals cannot leave the file).
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# State and browser code
|
|
2
|
+
|
|
3
|
+
## Signals and memos
|
|
4
|
+
|
|
5
|
+
```brm
|
|
6
|
+
<script>
|
|
7
|
+
import { $memo, $produce, $signal } from 'brumate/signals';
|
|
8
|
+
|
|
9
|
+
let todos = $signal([{ id: 1, title: 'Write the docs', done: false }]);
|
|
10
|
+
let query = $signal('');
|
|
11
|
+
let selected = $signal<number>();
|
|
12
|
+
const left = $memo(todos.filter((todo) => !todo.done).length);
|
|
13
|
+
const shown = $memo(todos.filter((todo) => todo.title.includes(query)));
|
|
14
|
+
|
|
15
|
+
function toggle(id: number) {
|
|
16
|
+
$produce(todos, (draft) => {
|
|
17
|
+
const todo = draft.find((item) => item.id === id);
|
|
18
|
+
if (todo) {
|
|
19
|
+
todo.done = !todo.done;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
selected = id;
|
|
23
|
+
}
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<input value=query onInput=((event) => (query = event.currentTarget.value))>
|
|
27
|
+
<p>{left} left, selected {selected}</p>
|
|
28
|
+
<ul>
|
|
29
|
+
#for (todo of shown by todo.id) {
|
|
30
|
+
<li><button onClick=(() => toggle(todo.id))>{todo.title}</button></li>
|
|
31
|
+
}
|
|
32
|
+
</ul>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- `let name = $signal(value)` declares state that you write. `const name = $memo(expression)` declares a value that follows other values. A memo can read other memos.
|
|
36
|
+
- The start value of a signal is computed once, on the server, and does not track what it reads. Use a memo for a value that follows others.
|
|
37
|
+
- Without an argument, `$signal<number>()` starts as `undefined`.
|
|
38
|
+
- `$produce(signal, (draft) => …)` changes a draft in place and writes the result as a new value. Without it, assign a new array or object: `todos = todos.map(…)`.
|
|
39
|
+
- `$untrack(expression)` reads without depending on the values in it.
|
|
40
|
+
- A signal or a memo holds data that the server can send: strings, numbers, booleans, `null`, `undefined`, `bigint`, plain objects and arrays, `Date`, `Map`, `Set`, `URL`, typed arrays and promises. It never holds a function, a class instance or a regular expression. Keep a function as a plain function in the script, or pass it as a function prop. Send the source and the flags of a regular expression, and make it where you use it. An app that sends a class registers a plugin in `src/serial.ts`.
|
|
41
|
+
- Declare signals and memos at the top level of a script, or of the script of a block. A signal never leaves its file: pass its value, or a function that writes it.
|
|
42
|
+
|
|
43
|
+
```brm wrong: Cannot assign to memo "double"
|
|
44
|
+
<script>
|
|
45
|
+
import { $memo, $signal } from 'brumate/signals';
|
|
46
|
+
|
|
47
|
+
let count = $signal(0);
|
|
48
|
+
const double = $memo(count * 2);
|
|
49
|
+
|
|
50
|
+
function reset() {
|
|
51
|
+
double = 0;
|
|
52
|
+
}
|
|
53
|
+
</script>
|
|
54
|
+
|
|
55
|
+
<button onClick=reset>{double}</button>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```brm wrong: Signal "count" can only be written in event handlers, effects, $client callbacks and functions
|
|
59
|
+
<script>
|
|
60
|
+
import { $signal } from 'brumate/signals';
|
|
61
|
+
|
|
62
|
+
let count = $signal(0);
|
|
63
|
+
count = 5;
|
|
64
|
+
</script>
|
|
65
|
+
|
|
66
|
+
<p>{count}</p>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```brm wrong: Declare $signal with let
|
|
70
|
+
<script>
|
|
71
|
+
import { $signal } from 'brumate/signals';
|
|
72
|
+
|
|
73
|
+
const count = $signal(0);
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<p>{count}</p>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Other derived values
|
|
80
|
+
|
|
81
|
+
- `const settled = $debounced(query, 300)` takes the value once it stops changing for 300 ms. `$throttled(value, ms)` takes it at most once in `ms`. The wait is a number literal.
|
|
82
|
+
- `$isPending(memo)` is true while an async memo loads.
|
|
83
|
+
- `let shown = $optimistic(source)` follows a memo, an action or a prop, and takes a value in the meantime.
|
|
84
|
+
- `const field = $id()` is an ID that is the same on the server and in the browser.
|
|
85
|
+
|
|
86
|
+
## Async data
|
|
87
|
+
|
|
88
|
+
A memo can hold a promise, such as the result of a server function. Markup and other memos see the value, never the promise.
|
|
89
|
+
|
|
90
|
+
```brm
|
|
91
|
+
<script>
|
|
92
|
+
import { $debounced, $isPending, $memo, $signal } from 'brumate/signals';
|
|
93
|
+
|
|
94
|
+
async function search(query: string): Promise<string[]> {
|
|
95
|
+
'use server';
|
|
96
|
+
return ['apple', 'banana'].filter((fruit) => fruit.includes(query));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let query = $signal('');
|
|
100
|
+
const settled = $debounced(query, 300);
|
|
101
|
+
const results = $memo(search(settled));
|
|
102
|
+
</script>
|
|
103
|
+
|
|
104
|
+
<input type="search" value=query onInput=((event) => (query = event.currentTarget.value))>
|
|
105
|
+
#if ($isPending(results)) {
|
|
106
|
+
<p>Searching…</p>
|
|
107
|
+
}
|
|
108
|
+
#loading {
|
|
109
|
+
<ul>
|
|
110
|
+
#for (result of results by result) {
|
|
111
|
+
<li>{result}</li>
|
|
112
|
+
}
|
|
113
|
+
</ul>
|
|
114
|
+
} #else {
|
|
115
|
+
<p>Loading…</p>
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
- A server function is `async` with `'use server'` on its first line, in any script. It runs on the server and the browser calls it over the network. It reads no variable of the instance script: pass values as arguments. Its arguments and result are serializable data.
|
|
120
|
+
- A `.ts` file that starts with `'use server'` makes every export a server function.
|
|
121
|
+
- `#loading { … } #else { … }` renders the fallback first, and the body when the values it reads have loaded.
|
|
122
|
+
- `#defer { … }` renders its body on the server later, in a request of its own. It cannot be in a `#client` body or in markup that the browser renders.
|
|
123
|
+
- `const result = $action(save)` gives the result of a form that posts to the server function `save` of the same file.
|
|
124
|
+
|
|
125
|
+
```brm wrong: Server function "save" must be async
|
|
126
|
+
<script>
|
|
127
|
+
function save(list: string[]) {
|
|
128
|
+
'use server';
|
|
129
|
+
return list.length;
|
|
130
|
+
}
|
|
131
|
+
</script>
|
|
132
|
+
|
|
133
|
+
<p>Saving</p>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```brm wrong: Server function "save" captures "user". Pass it as an argument
|
|
137
|
+
<script>
|
|
138
|
+
let user = 'Ada';
|
|
139
|
+
|
|
140
|
+
async function save() {
|
|
141
|
+
'use server';
|
|
142
|
+
return user;
|
|
143
|
+
}
|
|
144
|
+
</script>
|
|
145
|
+
|
|
146
|
+
<p>Saving</p>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Effects and code for the browser
|
|
150
|
+
|
|
151
|
+
```brm
|
|
152
|
+
<script>
|
|
153
|
+
import { $client, $effect, $signal } from 'brumate/signals';
|
|
154
|
+
|
|
155
|
+
let count = $signal(0);
|
|
156
|
+
let width = $signal<number>();
|
|
157
|
+
let box = $signal<HTMLDivElement>();
|
|
158
|
+
|
|
159
|
+
$effect(() => {
|
|
160
|
+
document.title = `Clicked ${count} times`;
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
$client(() => {
|
|
164
|
+
const measure = () => {
|
|
165
|
+
width = window.innerWidth;
|
|
166
|
+
};
|
|
167
|
+
measure();
|
|
168
|
+
window.addEventListener('resize', measure);
|
|
169
|
+
return () => window.removeEventListener('resize', measure);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
$client(() => {
|
|
173
|
+
box!.focus();
|
|
174
|
+
}, 'idle');
|
|
175
|
+
</script>
|
|
176
|
+
|
|
177
|
+
<button onClick=(() => count++)>Add</button>
|
|
178
|
+
<div ref=$bind(box) tabindex="-1">The window is {width ?? 'not measured yet'} pixels wide.</div>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
| You want to | Use |
|
|
182
|
+
| ----------------------------------------------- | ----------------- |
|
|
183
|
+
| Respond to something the user does | A handler |
|
|
184
|
+
| Run code when a value changes | `$effect` |
|
|
185
|
+
| Run code when the page loads or comes into view | `$client` |
|
|
186
|
+
| Render markup that needs the browser | `#client` |
|
|
187
|
+
| Reach an element from browser code | `ref=$bind(name)` |
|
|
188
|
+
|
|
189
|
+
- `$effect` runs after the first change of a value that it reads. It never runs on mount and never on the server. Use `$client` for code on mount.
|
|
190
|
+
- `$client(callback, trigger)` runs once. The trigger is `'visible'` (the default), `'idle'` or `'load'`. Prefer `'visible'` or `'idle'`.
|
|
191
|
+
- A callback returns a cleanup function if it needs one. It runs before the next run of an effect, and when the component leaves the page.
|
|
192
|
+
- An effect depends on the values read by the functions that it calls. Wrap a call that reads and writes the same signal in `$untrack(save())`.
|
|
193
|
+
- The callback of `$effect` and `$client` takes no parameters and is not `async`.
|
|
194
|
+
- `ref=$bind(name)` writes the element into the signal `name`. On the server it is `undefined`, so read it in handlers, effects and `$client` callbacks.
|
|
195
|
+
- A handler that only writes signals needs no effect: the page updates by itself.
|
|
196
|
+
- An effect that only sets a signal from other signals should be a `$memo`, or a write in the handler that changes them. Keep `$effect` for work outside the page's state: element methods, cookies, `document.title`, timers and libraries.
|
|
197
|
+
|
|
198
|
+
```brm wrong: The trigger of $client must be "visible", "idle" or "load"
|
|
199
|
+
<script>
|
|
200
|
+
import { $client } from 'brumate/signals';
|
|
201
|
+
|
|
202
|
+
$client(() => {
|
|
203
|
+
document.title = 'Ready';
|
|
204
|
+
}, 'mount');
|
|
205
|
+
</script>
|
|
206
|
+
|
|
207
|
+
<p>Ready</p>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```brm wrong: "field" is not declared
|
|
211
|
+
<script>
|
|
212
|
+
import { $signal } from 'brumate/signals';
|
|
213
|
+
|
|
214
|
+
let input = $signal<HTMLInputElement>();
|
|
215
|
+
</script>
|
|
216
|
+
|
|
217
|
+
<input ref=$bind(field)>
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Context
|
|
221
|
+
|
|
222
|
+
```brm
|
|
223
|
+
<script>
|
|
224
|
+
import { $getContext, $setContext, $signal } from 'brumate/signals';
|
|
225
|
+
import { Theme } from './theme.ts';
|
|
226
|
+
|
|
227
|
+
let theme = $signal<'light' | 'dark'>('light');
|
|
228
|
+
$setContext(Theme, theme);
|
|
229
|
+
const parent = $getContext(Theme);
|
|
230
|
+
</script>
|
|
231
|
+
|
|
232
|
+
<p>{parent}</p>
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
`createContext` comes from `brumate/context`: `export const Theme = createContext<'light' | 'dark'>('light');` in a `.ts` file. A component reads the context of the nearest component above it, children included.
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# Syntax
|
|
2
|
+
|
|
3
|
+
## The parts of a file
|
|
4
|
+
|
|
5
|
+
```brm
|
|
6
|
+
<script module>
|
|
7
|
+
export const MAX = 10;
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<script>
|
|
11
|
+
import { $signal } from 'brumate/signals';
|
|
12
|
+
|
|
13
|
+
let count = $signal(0);
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<style>
|
|
17
|
+
button {
|
|
18
|
+
font-weight: bold;
|
|
19
|
+
}
|
|
20
|
+
</style>
|
|
21
|
+
|
|
22
|
+
<button onClick=(() => count++)>{count} of {MAX}</button>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- `<script>` runs once for each instance, on the server. `<script module>` runs once per module, and its exports can be imported.
|
|
26
|
+
- `<style>` is scoped to the component. `lang="scss"` and `lang="less"` work.
|
|
27
|
+
- A file has at most one of each script. Markup can come before or after them.
|
|
28
|
+
- Markup in a script, an expression or an attribute value is an error. Use a `#template`.
|
|
29
|
+
|
|
30
|
+
## Expressions and attributes
|
|
31
|
+
|
|
32
|
+
`{expression}` inserts text. It is escaped, and `null`, `undefined`, `true` and `false` insert nothing. Use `raw(html)` from `brumate/html` for HTML that you trust.
|
|
33
|
+
|
|
34
|
+
```brm
|
|
35
|
+
<script>
|
|
36
|
+
import { $signal } from 'brumate/signals';
|
|
37
|
+
|
|
38
|
+
let name = $signal('Ada');
|
|
39
|
+
let on = $signal(false);
|
|
40
|
+
const rest = { id: 'main' };
|
|
41
|
+
const href = '/about';
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<input type="text" value=name maxlength=(20 - 1) disabled=(!name)>
|
|
45
|
+
<a {href} {...rest} class={ on, big: name.length > 3 }>About</a>
|
|
46
|
+
<p style={ color: on ? 'crimson' : undefined }>{name.toUpperCase()}</p>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- Quoted values are strings. A name, a property path, a call, a number, an object, an array, a template string and a prefix such as `!closed` or `-1` can stand without quotes. A value with an operator between parts, such as `a ? b : c`, `a + b`, `a > b` or `a && b`, and an arrow function, goes in parentheses: `(count > 3)`.
|
|
50
|
+
- `null` and `undefined` leave the attribute out. `false` leaves out a plain attribute such as `disabled`, and says `"false"` for `aria-*` and `data-*`.
|
|
51
|
+
- `class` takes a string, an array or an object of class names. `style` takes a string or an object in camel case.
|
|
52
|
+
- `<{tag}>` renders the element or component that `tag` names. Close it with `</>`.
|
|
53
|
+
|
|
54
|
+
```brm wrong: Wrap this attribute value in parentheses
|
|
55
|
+
<script>
|
|
56
|
+
import { $signal } from 'brumate/signals';
|
|
57
|
+
|
|
58
|
+
let count = $signal(0);
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<input value=count+1>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Props
|
|
65
|
+
|
|
66
|
+
```brm
|
|
67
|
+
<script>
|
|
68
|
+
import { $props } from 'brumate/signals';
|
|
69
|
+
|
|
70
|
+
const { title, size = 'medium', children, ...rest } = $props<{
|
|
71
|
+
title: string;
|
|
72
|
+
size?: 'small' | 'medium' | 'large';
|
|
73
|
+
children?: unknown;
|
|
74
|
+
}>();
|
|
75
|
+
</script>
|
|
76
|
+
|
|
77
|
+
<section data-size=size {...rest}>
|
|
78
|
+
<h2>{title}</h2>
|
|
79
|
+
{children}
|
|
80
|
+
</section>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
- Destructure `$props()`. Do not keep the object.
|
|
84
|
+
- Markup between the tags of a component is `children`.
|
|
85
|
+
- Props are read-only with `const`. Writing one is an error.
|
|
86
|
+
- A prop that a child writes is declared with `let` and typed `Bindable<T>`:
|
|
87
|
+
|
|
88
|
+
```brm
|
|
89
|
+
<script>
|
|
90
|
+
import { $props } from 'brumate/signals';
|
|
91
|
+
import type { Bindable } from 'brumate/signals';
|
|
92
|
+
|
|
93
|
+
let { on = false } = $props<{ on?: Bindable<boolean> }>();
|
|
94
|
+
</script>
|
|
95
|
+
|
|
96
|
+
<button aria-pressed=on onClick=(() => (on = !on))>Toggle</button>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The parent passes `on=$bind(open)` to write its signal, or `on=open` to give a start value that the child copies. `$bind` also works on `value` and `checked` of `<input>`, `<select>` and `<textarea>`.
|
|
100
|
+
|
|
101
|
+
A component that renders an element for its caller forwards the element with a bindable `ref` prop:
|
|
102
|
+
|
|
103
|
+
```brm
|
|
104
|
+
<script>
|
|
105
|
+
import { $props } from 'brumate/signals';
|
|
106
|
+
import type { Bindable } from 'brumate/signals';
|
|
107
|
+
|
|
108
|
+
let { ref } = $props<{ ref?: Bindable<HTMLButtonElement> }>();
|
|
109
|
+
const { children } = $props();
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<button ref=$bind(ref)>{children}</button>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
A file that has generic props declares them on the script: `<script generics="T">`.
|
|
116
|
+
|
|
117
|
+
A file uses another component by importing it, and renders it with a capitalized tag: `import Card from './Card.brm';`, then `<Card title="Hello" />`.
|
|
118
|
+
|
|
119
|
+
## Control flow
|
|
120
|
+
|
|
121
|
+
```brm
|
|
122
|
+
<script>
|
|
123
|
+
import { $signal } from 'brumate/signals';
|
|
124
|
+
|
|
125
|
+
let people = $signal([{ id: 1, name: 'Ada' }]);
|
|
126
|
+
let tab = $signal<'a' | 'b'>('a');
|
|
127
|
+
let open = $signal(false);
|
|
128
|
+
</script>
|
|
129
|
+
|
|
130
|
+
#if (people.length > 3) {
|
|
131
|
+
<p>Many</p>
|
|
132
|
+
} #else if (people.length > 0) {
|
|
133
|
+
<p>Some</p>
|
|
134
|
+
} #else {
|
|
135
|
+
<p>None</p>
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
<ol>
|
|
139
|
+
#for (person, index of people by person.id) {
|
|
140
|
+
<li>{index + 1}. {person.name}</li>
|
|
141
|
+
} #else {
|
|
142
|
+
<li>Nobody</li>
|
|
143
|
+
}
|
|
144
|
+
</ol>
|
|
145
|
+
|
|
146
|
+
#repeat (star of 5) {
|
|
147
|
+
<i>{star}</i>
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#switch {
|
|
151
|
+
#case (tab === 'a') {
|
|
152
|
+
<p>A</p>
|
|
153
|
+
}
|
|
154
|
+
#default {
|
|
155
|
+
<p>Other</p>
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
#key (tab) {
|
|
160
|
+
<p>Starts over when the tab changes.</p>
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#try {
|
|
164
|
+
<p>{people[0].name}</p>
|
|
165
|
+
} #catch (error, reset) {
|
|
166
|
+
<p>{String(error)}</p>
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#portal {
|
|
170
|
+
<p>At the end of the page.</p>
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
#offscreen (open) {
|
|
174
|
+
<p>Hidden, and kept, while open is false.</p>
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#client ('idle') {
|
|
178
|
+
<p>Rendered in the browser.</p>
|
|
179
|
+
} #else {
|
|
180
|
+
<p>Shown until then.</p>
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
- The body of a block can start with a `<script>` of its own, which runs for each instance, such as each item of a `#for`. It can declare signals and functions.
|
|
185
|
+
- `#for (item of list) { … }` has no `by` for a list that needs no key: each item is its own key. Use `by item.id` when items are objects that change.
|
|
186
|
+
- Write `} #else if (…) {` and `} #else {` on the line of the closing brace, as the style. The compiler accepts `#else` on the next line too.
|
|
187
|
+
- `#for (item of list) window(32) { … }` renders only the items in view, for a long list. `32` is the height of an item.
|
|
188
|
+
- `#loading { … } #else { … }` and `#defer { … }` are for async data. See state.md.
|
|
189
|
+
|
|
190
|
+
```brm wrong: #else must follow its block
|
|
191
|
+
<p>One</p>
|
|
192
|
+
#else {
|
|
193
|
+
<p>Two</p>
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Templates
|
|
198
|
+
|
|
199
|
+
`#template name(parameters) { … }` is markup as a value. Declare it at the top level of the markup or at the start of a block body, and call it in an expression. A component can take one as a prop and call it for its own data.
|
|
200
|
+
|
|
201
|
+
```brm
|
|
202
|
+
<script>
|
|
203
|
+
const total = 42;
|
|
204
|
+
</script>
|
|
205
|
+
|
|
206
|
+
#template price(label: string, amount: number) {
|
|
207
|
+
<p><strong>{label}:</strong> {amount.toFixed(2)}</p>
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
{price('Subtotal', total)}
|
|
211
|
+
{price('Tax', total * 0.1)}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Other parts of the markup
|
|
215
|
+
|
|
216
|
+
- `<window onKeyDown=(handler) />` and `<document onClick=(handler) />` listen to events of the window and the document. They close with `/>`.
|
|
217
|
+
- `$id()` gives an ID that is the same on the server and in the browser: `const field = $id();`, then `<label for=field>` and `<input id=field>`.
|
|
218
|
+
- `<script>` can export nothing. A value that other files import goes in `<script module>`.
|
|
219
|
+
- `#head { <title>{title}</title> <meta name="description" content="…"> }` sets the head of the page. A `<title>` outside `#head` is an error.
|
|
220
|
+
- A value that changes in a `<textarea>` or `<title>` must be its whole content: `<textarea>{text}</textarea>`.
|
|
221
|
+
|
|
222
|
+
## Modules of reactive logic
|
|
223
|
+
|
|
224
|
+
A file that ends in `.brm.ts` holds functions that use `$signal` and `$memo` for several components. The compiler copies the body into the component that calls it.
|
|
225
|
+
|
|
226
|
+
```ts module="counter.brm.ts"
|
|
227
|
+
import { $memo, $signal } from 'brumate/signals';
|
|
228
|
+
|
|
229
|
+
export function counter(start: number) {
|
|
230
|
+
let count = $signal(start);
|
|
231
|
+
const double = $memo(count * 2);
|
|
232
|
+
function increment() {
|
|
233
|
+
count++;
|
|
234
|
+
}
|
|
235
|
+
return { count, double, increment };
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
```brm uses
|
|
240
|
+
<script>
|
|
241
|
+
import { counter } from './counter.brm.ts';
|
|
242
|
+
|
|
243
|
+
const { count, increment } = counter(1);
|
|
244
|
+
</script>
|
|
245
|
+
|
|
246
|
+
<button onClick=increment>{count}</button>
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
- The function ends with `return { … }`, an object of plain keys, and returns nowhere else.
|
|
250
|
+
- A component calls it at the top level of its script, and destructures the result with plain names.
|