liveBook · Manning

Wrec

As explained in Web Component Development with Modern Libraries and Tooling

Wrec is a library for creating web components with automatic reactivity and concise component code.

wrec (“Web REactive Components”) is a library for building web components with less code. Its main convenience is automatic reactivity: component properties can drive rendered HTML, CSS, and computed values without a lifecycle method for each update. It does not use JavaScript decorators. section 8.0 section 8.7

A greeting component

Import `css`, `html`, and `Wrec`; extend `Wrec`; then declare static `properties`, `css`, and `html`. A `name` property defaulting to `World` can appear in the template as `this.name`. Call `HelloWorld.define('hello-world')`, then use `<hello-world></hello-world>` or `<hello-world name="Mark"></hello-world>` in a page. Property references in the template need no surrounding braces. section 8.2

Listing 8.1 hello-world.ts
import {css, html, Wrec} from 'wrec';

class HelloWorld extends Wrec {
  static properties = {
    name: {type: String, value: 'World'},
  };

  static css = css`
    :host {
      color: blue
    }
  `;

  static html = html`
    <p>Hello, <span>this.name</span>!</p>
  `;
}

HelloWorld.define('hello-world');

The complete greeting component shows the property declaration, CSS and HTML templates, and custom-element registration together. section 8.2

Reactive properties connect component state to common UI tasks:

  • A calculated `area` can use `computed: "this.width * this.height"`; range inputs bound to width and height update those properties as their values change. section 8.7
  • An `onClick` attribute can name a handler or contain an expression. Setting `dispatch: true` on a property emits change events when it changes. section 8.8
  • Passing a parent property to a wrec child creates a two-way binding. A radio group can therefore change a shared color property and update a greeting component. section 9.8
  • A sortable table can declare computed sorted data and use `batchSet` to change sort criteria before updating the DOM. section 8.12

For a new project, install `wrec` with npm; Vite is recommended for development but is not required. Run `npx wrec-lint file-path` to check a component source file for common issues. wrec also supports form-associated components and shared state through `WrecState`. These conveniences reduce code compared with equivalent vanilla components while retaining web-component usage in framework-based apps. section 8.2 section 8.5 section 8.10 section 8.14

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works through property declarations, bindings, events, and complete components; leaves several additional features as highlights.
Examples
Uses a greeting, area calculator, counter, radio group, and sortable table as component examples.
What it assumes
Introduces wrec project creation from npm setup while using JavaScript classes, HTML, and CSS in its starter component.
What you can do after
Build it from scratch with wrec: define reactive components, connect inputs and events, and bind parent and child properties.
How it connects
Connects web components to computed properties, two-way binding, change events, form association, and shared state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.