liveBook · Manning

Expression

As explained in Web Component Development with Modern Libraries and Tooling

A JavaScript syntactic construct that evaluates to a value.

An expression is a JavaScript construct that evaluates to a value. In wrec, expressions can supply element text, attribute values, CSS property values, and computed properties. section 8.3

When a property used by an expression changes, wrec recomputes the expression and updates the DOM. Duplicate expressions in one component are recomputed only once. section 8.3

Reactive rectangle

A component can bind sliders to this.height and this.width, display this.width * this.height as area, and use this.color, this.height + "px", and this.width + "px" in CSS. Adding :input updates a property while a slider is dragged rather than after its change event. section 8.3

Listing 8.2 expressions-demo.ts class definition
import { css, html, Wrec } from "wrec";

class ExpressionsDemo extends Wrec {
  static properties = {
    color: { type: String, value: "red" },
    height: { type: Number, value: 100 },
    width: { type: Number, value: 200 },
    max: { type: Number, value: 800 },
  };

  static css = css`
    #1
  `;

  static html = html`
    #2
  `;
}

ExpressionsDemo.define("expressions-demo");

Browse the component definition behind the expression examples. section 8.3

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Explains reactive recomputation, duplicate-expression sharing, input timing, and several ways to calculate derived values.
Examples
Demonstrates sliders, a color picker, a reactive rectangle, CSS dimensions, and a computed rectangle area.
What you can do after
Apply expressions via wrec to create reactive text, attributes, CSS values, and computed component properties.
How it connects
Connects expressions with computed properties, reactive CSS, DOM updates, component properties, and input events.

Also explained in

Compare how 14 books explain expression →

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.