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
