Stencil.js Guide

Component Example

import { Component, Prop, State, Event, EventEmitter, h } from '@stencil/core'; @Component({ tag: 'my-button', styleUrl: 'my-button.css', shadow: true, // Shadow DOM encapsulation }) export class MyButton { @Prop() label: string = 'Click me'; @Prop() disabled: boolean = false; @State() clickCount: number = 0; @Event() buttonClick: EventEmitter<number>; handleClick() { this.clickCount++; this.buttonClick.emit(this.clickCount); } render() { return ( <button disabled={this.disabled} onClick={() => this.handleClick()}> {this.label} ({this.clickCount}) </button> ); } }

Decorators Reference

DecoratorPurpose
@ComponentDefine web component tag, styles
@PropPublic attribute/property
@StateInternal reactive state
@EventCustom DOM event emitter
@ListenListen to DOM events
@MethodExpose public method
@ElementReference to host element
@WatchWatch prop/state for changes

Quick Start

npm init stencil component my-components cd my-components npm install npm start # dev server npm run build # production build