liveBook · Manning

StencilJS

As explained in Web Component Development with Modern Libraries and Tooling

A compiler for generating standards-based web components and component-based user interfaces.

StencilJS is a library and compiler for building standards-based web components. It offers a component-authoring style with TypeScript, JSX, decorators, and reactive state, while producing components that can be used across frameworks. section 6.0

A radio-group component

A <radio-group> element can display Red, Green, and Blue choices inside a form. Its component class declares properties such as labels and value; a JSX render method creates the radio inputs and labels. When a choice changes, the component updates its value and emits a valueChanged event that other page elements can listen for. section 6.7

Listing 6.6 radio-group.tsx
  render() {
    const labelArray = this.labels.split(',').map(value => value.trim());
    const valueArray = this.values.split(',').map(value => value.trim());
    return (
      <fieldset>
        <legend>{this.legend}</legend>
        <slot name="before"></slot>
        <div>
          {valueArray.map((v, index) => (
            <div>
              <input
                type="radio"
                id={v}
                name={this.name}
                value={v} checked={v === this.value}
                onChange={this.handleChange}
              />
              <label htmlFor={v}>{labelArray[index]}</label>
            </div>
          ))}
        </div>
        <slot name="after"></slot>
      </fieldset>
    );
  }

The render method shows how JSX turns the component’s values into radio inputs, labels, and slots for additional content. section 6.7

A typical workflow is:

  • Create a component project with npm init stencil, install its dependencies, and run npm start. section 6.2
  • Put component .tsx and .css files under src/components. Run stencil build to compile them into optimized web components; stencil.config.ts controls the output target. section 6.1
  • Test rendering and interactions with Vitest unit tests and Playwright end-to-end tests; a coverage command identifies untested code. section 6.9

The required build step transforms JSX into browser-ready code. Stencil also bundles a runtime for tasks including lazy loading, hydration, CSS encapsulation, and rendering updates. Decorators reduce boilerplate for properties, state, and events; @stencil/store can share state between components. This fuller toolset can mean a steeper learning curve and longer builds than more focused web-component libraries. Choose it when those authoring and tooling features justify a structured project and build process. section 6.1 section 6.16

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Develops components through project setup, decorators, JSX rendering, events, sorting behavior, and tests; describes runtime services without implementing them.
Examples
Uses a radio group that changes other elements’ colors, a sortable table, and rendering and click tests.
What it assumes
Requires Node.js and uses TypeScript, CSS, and JSX; walks through creating and running a starter project.
What you can do after
Build it from scratch with the Stencil CLI, then test component rendering and interactions with Vitest and Playwright.
How it connects
Connects JSX, custom elements, virtual DOM rendering, hydration, reactive state, and event handling.

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.