@lipsjs/lips 0.3.1 → 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.
Files changed (45) hide show
  1. package/README.md +122 -9
  2. package/dist/lips.js +915 -669
  3. package/dist/lips.js.map +15 -12
  4. package/dist/lips.min.js +12 -12
  5. package/dist/lips.min.js.map +15 -12
  6. package/dist/lynx-background.min.js +3 -0
  7. package/dist/lynx-background.min.js.map +13 -0
  8. package/dist/lynx-main.min.js +3 -0
  9. package/dist/lynx-main.min.js.map +11 -0
  10. package/dist/lynx-plugin.mjs +1487 -0
  11. package/dist/lynx.min.js +8 -8
  12. package/dist/lynx.min.js.map +11 -10
  13. package/dist/mf2.min.js.map +1 -1
  14. package/dist/parts.min.js +19 -0
  15. package/dist/parts.min.js.map +11 -0
  16. package/dist/precompile.min.js +15 -15
  17. package/dist/precompile.min.js.map +8 -7
  18. package/dist/runtime.min.js +4 -4
  19. package/dist/runtime.min.js.map +11 -9
  20. package/dist/stylis.min.js.map +1 -1
  21. package/dist/types/component.d.ts +32 -0
  22. package/dist/types/ir/compiler.d.ts +2 -4
  23. package/dist/types/ir/facade.d.ts +63 -12
  24. package/dist/types/ir/host.d.ts +9 -1
  25. package/dist/types/ir/initial.d.ts +16 -0
  26. package/dist/types/ir/router.d.ts +2 -0
  27. package/dist/types/ir/tags.d.ts +16 -0
  28. package/dist/types/lips.d.ts +7 -7
  29. package/dist/types/lynx/a11y.d.ts +1 -0
  30. package/dist/types/lynx/background.d.ts +52 -3
  31. package/dist/types/lynx/main.d.ts +9 -5
  32. package/dist/types/lynx/plugin.d.ts +8 -0
  33. package/dist/types/lynx/protocol.d.ts +55 -4
  34. package/dist/types/lynx/style.d.ts +7 -0
  35. package/dist/types/lynx/template.d.ts +6 -0
  36. package/dist/types/parts/menu.d.ts +44 -0
  37. package/dist/types/parts/popover.d.ts +83 -0
  38. package/dist/types/parts.d.ts +16 -0
  39. package/dist/types/precompile.d.ts +8 -11
  40. package/dist/types/runtime.d.ts +1 -0
  41. package/dist/types/types.d.ts +181 -57
  42. package/dist/untrusted.min.js +4 -4
  43. package/dist/untrusted.min.js.map +7 -7
  44. package/package.json +24 -3
  45. package/dist/types/i18n/formatters.d.ts +0 -3
package/README.md CHANGED
@@ -17,12 +17,12 @@ template string ──parse──▶ AST ──compile──▶ IR ──render
17
17
  - **Zero build step** — import and go; templates render at runtime
18
18
  - **Fine-grained reactivity** — per-key signals; updates are O(bindings that changed)
19
19
  - **HTML-native syntax** — element-shaped control flow (`<if>`, `<for>`, `<switch>`, `<async>`), not an attribute DSL
20
- - **Tiny** — ~21 KB gzip full, ~12 KB gzip precompiled-only, one dependency
20
+ - **Small** — ~36 KB gzip full (compiler, styles, router, MessageFormat 2), ~16 KB precompiled-only
21
21
  - **Serializable components** — a template is a plain object; the compiled IR is JSON
22
22
  - **Precompile + CSP mode** — build templates to IR; run with no `eval`/`Function` under a strict CSP
23
23
  - **Hot-swap** — `instance.swap(newIR)` re-renders only what changed, preserving state
24
24
  - **Batteries included** — router, i18n, macros, scoped styles, slots, component events
25
- - **TypeScript** — full type definitions
25
+ - **TypeScript** — a component written in `register`/`render` is inferred: `this` typed in every handler, no wrapper
26
26
 
27
27
  ## 🚀 Quick start
28
28
 
@@ -67,13 +67,56 @@ import Lips from '@lipsjs/lips'
67
67
 
68
68
  | Import | Contents | gzip |
69
69
  |---|---|---|
70
- | `@lipsjs/lips` | full: runtime + parser/compiler + styles + router | ~21 KB |
71
- | `@lipsjs/lips/runtime` | precompiled-only: no parser/compiler (CSP-friendly) | ~12 KB |
70
+ | `@lipsjs/lips` | full: runtime + parser/compiler + styles + router + i18n | ~36 KB |
71
+ | `@lipsjs/lips/runtime` | precompiled-only: no parser/compiler (CSP-friendly) | ~16 KB |
72
72
  | `@lipsjs/lips/precompile` | build-time helpers + Vite/Rollup plugin | — |
73
73
  | `@lipsjs/lips/untrusted` | opt-in policy for templates you did not write ([RFC-007](design/untrusted-templates.md)) | ~9 KB |
74
74
  | `@lipsjs/lips/lynx` | build-time Lynx pass: a template and a stylesheet held to what Lynx draws (RFC-006) | — |
75
+ | `@lipsjs/lips/lynx/main` | Lynx main thread: the applier over Element PAPI | ~2.8 KB |
76
+ | `@lipsjs/lips/lynx/background` | Lynx background thread: the host Lips renders into | ~5 KB |
77
+ | `@lipsjs/lips/lynx-plugin` | Rspeedy plugin: `*.lips.css` sheets and the page config Lips needs | — |
75
78
  | `@lipsjs/lips/dev` | unminified full build | — |
76
79
 
80
+ ## 🔷 TypeScript
81
+
82
+ A component is typed where it is written. Written in a `register`,
83
+ `render` or `root` call, Lips infers it: `state` and `static` from their
84
+ values, each handler from itself, and `this` in every handler from all
85
+ three, sibling handlers included. No annotation, no wrapper.
86
+
87
+ ```ts
88
+ lips.register('counter', {
89
+ state: { count: 0 },
90
+ handler: {
91
+ step( by: number ){ this.state.count += by },
92
+ reset(){ this.step( -this.state.count ) } // a typo here is an error
93
+ },
94
+ default: `<button on-click(step, 1)>{state.count}</button>`
95
+ })
96
+
97
+ lips.render('greeting', { default: `<b>{input.name}</b>` }, { name: 'Ada' }).state // typed
98
+ ```
99
+
100
+ Written apart, in a file of its own, its `this` is fixed where it is
101
+ written, so its handler names the parts it reads:
102
+
103
+ ```ts
104
+ export const handler: Handlers<Shape<{ input: Input, state: typeof state }>> = { … }
105
+ ```
106
+
107
+ or the object is wrapped in `component( … )`, which infers it as the calls
108
+ do. The app's context is typed once, for every component and for
109
+ `new Lips()`:
110
+
111
+ ```ts
112
+ declare module '@lipsjs/lips' {
113
+ interface SharedContext { theme: 'dark' | 'light' }
114
+ }
115
+ ```
116
+
117
+ The types, and what the template strings do not check, are in the
118
+ [reference](docs/reference.md#types).
119
+
77
120
  ## 🧩 Template syntax
78
121
 
79
122
  ```html
@@ -84,6 +127,14 @@ import Lips from '@lipsjs/lips'
84
127
  <button on-click(select, item.id)>pick</button>
85
128
  <button on-click(() => state.count++)>+</button>
86
129
 
130
+ <!-- event modifiers, the same on the DOM and on Lynx -->
131
+ <div on-click(openRow)><button on-click.stop(remove)>×</button></div> <!-- the row does not open -->
132
+ <div on-keydown.capture(onKey)>…</div> <!-- before its children -->
133
+ <div class="menu" on-click.outside(dismiss)>…</div> <!-- a tap anywhere else -->
134
+
135
+ <!-- element references: this.refs.search.focus(), .scrollIntoView(), .animate(…) -->
136
+ <input @ref="search">
137
+
87
138
  <!-- conditionals -->
88
139
  <if(state.ready)>…</if>
89
140
  <else-if(state.loading)>…</else-if>
@@ -102,13 +153,26 @@ import Lips from '@lipsjs/lips'
102
153
  ```
103
154
 
104
155
  Components compose with **slots** (`<{input.renderer}/>`) and **events**
105
- (`this.emit('picked', …)` → parent `on-picked(...)`). Full lifecycle:
156
+ (`this.emit('picked', …)` → parent `on-picked(...)`). A slot's expressions
157
+ are where it was written; its context is where it is placed. Full lifecycle:
106
158
  `onCreate · onInput · onMount · onRender · onUpdate · onAttach · onDetach · onContext ·
107
- onError · onDestroy`.
159
+ onError · onDestroy`. `onInput` runs at creation and again each time an
160
+ input changes; `onUpdate` and `onRender` after any change the template
161
+ shows, a branch or a row as much as a text. Whatever a template evaluates,
162
+ and whatever a handler throws, reaches `onError`, and the rest renders.
108
163
 
109
- ### Constants
164
+ A library of components installs as a plugin, with what it requires:
110
165
 
111
- `static` holds values that never change — reading them creates no subscription:
166
+ ```js
167
+ import { menu } from '@lipsjs/lips/parts'
168
+ lips.use( menu ) // and the popover it is built on
169
+ ```
170
+
171
+ ### Variables that never re-render
172
+
173
+ `static` holds what a component keeps without re-rendering: reading it
174
+ creates no subscription, and writing it redraws nothing. Each instance has
175
+ its own copy, as it has of `state`:
112
176
 
113
177
  ```js
114
178
  {
@@ -195,6 +259,52 @@ not script. What it may not do is reported as a diagnostic with `line`/`col`/`co
195
259
  and refused again at runtime. What Lips leaves to CSP and to the page is listed in
196
260
  [RFC-007](design/untrusted-templates.md).
197
261
 
262
+ ## 📱 Native, on Lynx
263
+
264
+ The same templates render natively through [Lynx](https://lynxjs.org)
265
+ (SDK 4.0 or later), on its two threads: Lips runs in the background and a
266
+ small applier on the main thread is the only code that touches elements
267
+ ([RFC-006](design/lynx-binding.md)). With Rspeedy and
268
+ `@lynx-js/vanilla-rsbuild-plugin`:
269
+
270
+ ```ts
271
+ // lynx.config.ts
272
+ import { defineConfig } from '@lynx-js/rspeedy'
273
+ import { pluginVanillaLynx } from '@lynx-js/vanilla-rsbuild-plugin'
274
+ import { pluginLips } from '@lipsjs/lips/lynx-plugin'
275
+
276
+ export default defineConfig({
277
+ source: { entry: './src/main-thread.ts' },
278
+ plugins: [ pluginVanillaLynx(), pluginLips() ]
279
+ })
280
+ ```
281
+
282
+ ```ts
283
+ // src/main-thread.ts
284
+ import { connectMain } from '@lipsjs/lips/lynx/main'
285
+ import './app.lips.css' // a Lips sheet: its name is its scope
286
+
287
+ connectMain( __CreatePage( '0', 0 ) )
288
+ ```
289
+
290
+ ```ts
291
+ // src/background.ts
292
+ import { compileLynxTemplate } from '@lipsjs/lips/lynx'
293
+ import { renderIR } from '@lipsjs/lips/runtime'
294
+ import { connectBackground } from '@lipsjs/lips/lynx/background'
295
+ import sheet from './app.lips.css?raw' // the sheet's binds: its CSS is in the bundle
296
+
297
+ const { ir } = compileLynxTemplate( template ) // or precompile it at build time
298
+ const bg = connectBackground()
299
+ renderIR( ir, { state, handlers, stylesheet: JSON.parse( sheet ) }, { host: bg.host, mode: 'interpreted' }).mount( bg.root )
300
+ ```
301
+
302
+ `pluginLips` compiles `*.lips.css` files (a lint error fails the build), sets
303
+ the page config Lips relies on, and lowers Lips's own files to the app's target,
304
+ since Lynx's engines parse less than browsers. Import a sheet from a script, not
305
+ with `@import`. [`demos/lynx`](demos/lynx) is a complete app, run in the real
306
+ engine on every change.
307
+
198
308
  ## 🔥 Hot-swap
199
309
 
200
310
  Re-render a live component against a revised template, keeping component state:
@@ -206,7 +316,10 @@ const { changes } = app.swap(newIR) // patches only what differs
206
316
 
207
317
  ## 📖 Documentation
208
318
 
209
- [Full Lips documentation](https://lips-js.github.io)
319
+ [**The reference**](docs/reference.md): every part of Lips, when to use it
320
+ and when not, with an example that `tests/reference.spec.ts` runs. The
321
+ design behind each part is in the RFCs under [`design/`](design).
322
+ A guided tour is at [lips-js.github.io](https://lips-js.github.io).
210
323
 
211
324
  ## 🏗️ Building from source
212
325