rasti 4.1.4 → 4.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  <p align="center">
2
2
  <picture>
3
- <source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.1.4/docs/logo-dark.svg">
4
- <img alt="Rasti.js" src="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.1.4/docs/logo.svg" height="120">
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.2.0/docs/logo-dark.svg">
4
+ <img alt="Rasti.js" src="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.2.0/docs/logo.svg" height="120">
5
5
  </picture>
6
6
  </p>
7
7
 
@@ -115,10 +115,11 @@ const Link = Component.create`
115
115
  `;
116
116
 
117
117
  // Create a Navigation component that renders Link components for each route.
118
+ // `html` builds a partial: a sub-template for lists and conditional blocks.
118
119
  const Navigation = Component.create`
119
120
  <nav>
120
- ${({ props, partial }) => props.routes.map(
121
- ({ label, href }) => partial`<${Link} href="${href}">${label}</${Link}>`
121
+ ${({ props, html }) => props.routes.map(
122
+ ({ label, href }) => html`<${Link} href="${href}">${label}</${Link}>`
122
123
  )}
123
124
  </nav>
124
125
  `;
@@ -376,7 +377,11 @@ const Toggle = Component.create<ToggleProps, ToggleState>`
376
377
 
377
378
  Inside its own template, a component can't use `InstanceType<typeof Toggle>`: the type of `Toggle` depends on the template itself, so TypeScript reports a circular reference (TS7022).
378
379
 
379
- **Content and quoted attributes** receive the component as their argument, so annotating the parameter types everything they read. A `function` that reads `this` instead needs `satisfies RenderExpression<C>`, which types `this` as well.
380
+ **Content and quoted attributes** receive the component as their argument, so annotating the parameter types everything they read. A `function` that reads `this` instead needs `satisfies RenderExpression<C>`, which types `this` as well:
381
+
382
+ ```ts
383
+ ${(function() { return this.props.label; }) satisfies RenderExpression<ToggleComponent>}
384
+ ```
380
385
 
381
386
  **Unquoted handlers** receive the event first, not the component. An annotated parameter still compiles there, since the template's expressions are `any`, and fails at runtime. `satisfies` checks the function against what rasti passes it:
382
387
 
@@ -397,7 +402,7 @@ interface ListProps { items: string[]; handleSelect: (item: string) => void; }
397
402
 
398
403
  class ListBase extends Component<ListProps> {
399
404
  renderItems() {
400
- return this.props.items.map((item) => this.partial`<li>${item}</li>`);
405
+ return this.props.items.map((item) => this.html`<li>${item}</li>`);
401
406
  }
402
407
  select(ev: MouseEvent) {
403
408
  const li = (ev.target as HTMLElement).closest('li');
@@ -423,6 +428,15 @@ For a function passed to a child, neither helper fits — its type comes from th
423
428
  handleChange=${((checked) => model.toggleAll(checked)) satisfies ToggleAllProps['handleChange']}
424
429
  ```
425
430
 
431
+ A quoted value is the result of a function the parent runs, so what reaches the child is what that function returns. Annotate its return type with the child's prop: that checks the value, and it types the parameters of a callback returned by a thunk, which would otherwise be an implicit `any` (TS7006):
432
+
433
+ ```ts
434
+ // where the child was created with Component.create<HeaderProps>`...`
435
+ handleAddTodo="${({ model }: AppComponent): HeaderProps['handleAddTodo'] => (title) => model!.addTodo(title)}"
436
+ ```
437
+
438
+ `satisfies` on the returned callback does the same, `({ model }: AppComponent) => ((title) => model!.addTodo(title)) satisfies HeaderProps['handleAddTodo']`; the return type keeps the whole contract in the signature.
439
+
426
440
  ### Known limitations
427
441
 
428
442
  - **Template interpolation callbacks are `any`**. Functions in ``Component.create`...` `` templates can't be inferred from the surrounding string — type them by where they sit in the template (see [Typing template interpolations](#typing-template-interpolations)).
package/dist/rasti.js CHANGED
@@ -2445,6 +2445,8 @@
2445
2445
  this.options = options;
2446
2446
  // Bind `partial` method to `this`.
2447
2447
  this.partial = this.partial.bind(this);
2448
+ // Expose `partial` as `html`, unless a subclass defines its own `html`.
2449
+ if (!('html' in this)) this.html = this.partial;
2448
2450
  // Bind `onChange` method to `this`.
2449
2451
  this.onChange = this.onChange.bind(this);
2450
2452
  // Call lifecycle method.
@@ -2637,7 +2639,7 @@
2637
2639
  * It will return a Partial object that preserves structure for position-based recycling.
2638
2640
  * Components will be added as children by the parent component. Template strings literals
2639
2641
  * will be marked as safe HTML to be rendered.
2640
- * This method is bound to the component instance by default.
2642
+ * This method is bound to the component instance by default, and is also available as `this.html`.
2641
2643
  * @param {TemplateStringsArray} strings - Template strings.
2642
2644
  * @param {...any} expressions - Template expressions.
2643
2645
  * @return {Partial} Partial object containing strings and expressions.
@@ -3192,8 +3194,8 @@
3192
3194
  * // Create a navigation component. Add buttons as children. Iterate over items.
3193
3195
  * const Navigation = Component.create`
3194
3196
  * <nav>
3195
- * ${({ props, partial }) => props.items.map(
3196
- * item => partial`<${Button}>${item.label}</${Button}>`
3197
+ * ${({ props, html }) => props.items.map(
3198
+ * item => html`<${Button}>${item.label}</${Button}>`
3197
3199
  * )}
3198
3200
  * </nav>
3199
3201
  * `;
@@ -3226,7 +3228,7 @@
3226
3228
  * ```javascript
3227
3229
  * class ListBase extends Component {
3228
3230
  * renderItems() {
3229
- * return this.props.items.map(item => this.partial`<li>${item}</li>`);
3231
+ * return this.props.items.map(item => this.html`<li>${item}</li>`);
3230
3232
  * }
3231
3233
  * }
3232
3234
  * const List = ListBase.create`
@@ -3325,6 +3327,7 @@
3325
3327
  * @property {Model} [model] A `Model` or any emitter object containing data and business logic. The component will listen to `change` events and call `onChange` lifecycle method.
3326
3328
  * @property {Model} [state] A `Model` or any emitter object containing data and business logic, to be used as internal state. The component will listen to `change` events and call `onChange` lifecycle method.
3327
3329
  * @property {Model} [props] Automatically created from any options not merged to the component instance. Contains props passed from parent component as a `Model`. The component will listen to `change` events on props and call `onChange` lifecycle method. When a component with a `key` is recycled during parent re-render, new props are automatically updated and any changes trigger a re-render.
3330
+ * @property {Function} html Alias of {@link #module_component__partial partial}, bound to the component instance. Not set when a subclass defines its own `html`.
3328
3331
  * @see {@link #module_component_create Component.create}
3329
3332
  * @example
3330
3333
  * import { Component, Model } from 'rasti';