Web Components

Component Structure
Stencil components are plain ES6/TypeScript classes with some decorator metadata:
import { Component, Prop, h } from "@stencil/core";
@Component({
tag: "core-component",
styleUrl: "core-component.css",
})
export class Component {
@Prop() first: string;
@Prop() last: string;
render() {
return (
<div>
Hello world, my name is {this.first} {this.last}
</div>
);
}
}
Use it just like any other HTML element:
<core-component first="Stencil" last="JS"></core-component>
codesandbox.io example
Naming Components
When creating new components, use the core-
prefix. e.g. core-button
, core-icon
, etc are valid names.
Custom Elements must contain kebab-case names so they do not collide with existing html elements.
API
The API for Stencil closely mirrors the API for Custom Elements v1.
Components
Decorator | Description |
---|
@Component() | Indicate a class is a Stencil component. |
| |
@Prop() | Creates a property that will exist on the element and be data-bound to this component. |
@State() | Creates a local state variable that will not be placed on the element. |
@Method() | Expose specific methods to be publicly accessible. |
Component Example
Expand a detailed component example
@Component({
tag: "core-component",
styleUrls: {
ios: "component.ios.css",
md: "component.md.css",
wp: "component.wp.css",
},
})
export class Component {
num: number;
someText = "default";
@Element() el: HTMLElement;
@State() isValidated: boolean;
@State() status = 0;
@Prop() content: string;
@Prop() enabled: boolean;
@Prop() menuId: string;
@Prop() type = "overlay";
@Prop() swipeEnabled = true;
@Watch("swipeEnabled")
swipeEnabledChanged(newSwipeEnabled: boolean, oldSwipeEnabled: boolean) {
this.updateState();
}
@Event() coreClose: EventEmitter;
@Event() coreDrag: EventEmitter;
@Event() coreOpen: EventEmitter;
componentWillLoad() {}
componentDidLoad() {}
componentWillUpdate() {}
componentDidUpdate() {}
componentDidUnload() {}
@Listen("click", { enabled: false })
onClick(ev: UIEvent) {
console.log("hi!");
}
@Method()
async open(): Promise<boolean> {
return true;
}
@Method()
async close(): Promise<void> {
}
prepareAnimation(): Promise<void> {
}
updateState() {
}
render() {
return (
<Host
attribute="navigation"
side={this.isRightSide ? "right" : "left"}
type={this.type}
class={{
"core-is-animating": this.isAnimating,
}}
>
<div class="menu-inner page-inner">
<slot />
</div>
</Host>
);
}
}
Browser Support
Web Components, specifically Custom Elements, are natively supported in Chrome and Safari and are coming to both Edge and Firefox. A dynamic polyfill loader is already included in order to only load the polyfills for the browsers that are missing specific features.
- Chrome (and all Chromium based browsers)
- Safari
- Edge
- Firefox
- IE 11
Polyfills
Stencil includes a subset of the core-js
polyfills for old browsers like IE 11, fetch
and conditionally downloads the Custom Elements v1 only when it's needed.
Internet Explorer 11
Browsers that do not support native ESM (at the moment, only IE11 and older) will download a subset of core-js
.
Expand IE11 polyfill details
This subset is generated using the core-js-builder
tool with the following configuration:
require("core-js-builder")({
targets: "ie 11",
modules: [
"es",
"web.url",
"web.url.to-json",
"web.url-search-params",
"web.dom-collections.for-each",
],
blacklist: [
"es.math",
"es.date",
"es.symbol",
"es.array-buffer",
"es.data-view",
"es.typed-array",
"es.reflect",
"es.promise",
],
});
In addition, the following set of polyfills are also included:
All browsers
Some modern browsers like Edge do not include native support for Web Components. In that case, we conditionally load the Custom Elements v1 polyfill.