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
