liveBook · Manning

Adaptable component

As explained in Web Components in Action

An adaptable component is a reusable interface component whose presentation can vary according to its usage context.

An adaptable component is a reusable Web Component whose presentation changes with its usage context. The exercise component renders in both the exercise library and workout plan, while :host selectors respond to context classes such as cardio and strength. section 9.4

Build the component around a small, reusable data and styling interface:

  • Define getters and setters so attributes and JavaScript properties can represent the same data. section 9.4
  • Serialize exercise data into attributes, omitting undefined values for different exercise shapes. section 9.4

One component, two visual variants

Render <wkout-exercise> with class="cardio" for a timed jog or class="strength" for pushups. Both instances share the component while their host classes select different border colors.

How Web Components in Action covers it

How deep it goes
Builds the component through reflected properties, serialization, shadow rendering, and host-class styling, but defers the fully different workout-plan design.
Examples
Uses cardio jog and strength pushup elements, exercise-library rendering, reflected attributes, and host-class border colors.
What it assumes
Assumes familiarity with Web Components and attribute-property reflection while demonstrating the exercise component incrementally.
What you can do after
Build it from scratch as a reusable exercise Web Component with reflected data and context-selected Shadow CSS variants.
How it connects
Connects Web Components with Shadow DOM, CSS host selectors, HTML attributes, JavaScript properties, and serialization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.