Skip to content

Angular

Use staticview components in Angular templates.

TypeScript Configuration

Add the component types to your TypeScript configuration:

tsconfig.app.json
{
"compilerOptions": {
"types": [
// Recommended: types what `querySelector` and `createElement` return for each tag
// For one component: "@staticview/ui/types/dom/[component-name]"
"@staticview/ui/types/dom",
// Recommended: declares each component type globally, with no import
// For one component: "@staticview/ui/types/global/[component-name]"
"@staticview/ui/types/global",
],
},
}
Note

Angular cannot type-check custom elements in templates, because it has no JSX type augmentation, and CUSTOM_ELEMENTS_SCHEMA accepts unknown elements without checking their bindings. Class code gets typed access through element references.

Enabling Custom Elements

Add CUSTOM_ELEMENTS_SCHEMA to every component or NgModule whose template uses web components:

import { Component, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.html",
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class App {}

Importing Web Components

Import each component once, in your entry point before bootstrapApplication, to define the elements before Angular renders them and assigns their properties:

main.ts
import { bootstrapApplication } from "@angular/platform-browser";
import "@staticview/ui/[component-name]";
import { App } from "./app/app";
bootstrapApplication(App);

Setting Properties

Set a property with Angular's property binding, [propertyName]:

<component-name [opened]="isOpen" [disabled]="isDisabled"></component-name>

Setting Attributes

Use plain attributes for static values, and an [attr.attribute-name] binding for dynamic ones:

<component-name label="Title" [attr.placement]="placement"></component-name>

Boolean Attributes

A boolean attribute is on whenever it is present, and close-button="false" turns the close button on. Since Angular removes only an attribute bound to null or undefined, [attr.close-button]="showCloseButton" renders close-button="false", and you bind the property instead:

<component-name [closeButton]="showCloseButton"></component-name>

Otherwise pass "" to turn it on and null to turn it off:

<component-name [attr.close-button]="showCloseButton ? '' : null"></component-name>

Handling Events

Listen to component events with Angular's event binding, (eventName):

<component-name (openedchange)="handleOpenedChange($event)"></component-name>

Since the template is not type-checked, $event is untyped, and you type the handler parameter as CustomEvent yourself:

handleOpenedChange(event: CustomEvent) { ... }

Typed Element References

With @staticview/ui/types/global, every component's instance type is global and pairs with ElementRef for typed access to properties and methods:

<component-name #panel></component-name>
import { Component, CUSTOM_ELEMENTS_SCHEMA, viewChild, type ElementRef } from "@angular/core";
@Component({
/* ... */
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class Example {
panel = viewChild<ElementRef<ComponentName>>("panel");
open() {
this.panel()?.nativeElement.opened; // Typed
}
}

Without the global types, import the same type from the component's module:

import type { ComponentName } from "@staticview/ui/[component-name]";

With @staticview/ui/types/dom, DOM queries return the same type:

const element = document.querySelector("component-name"); // ComponentName | null

Using Slots

Put the content between the tags, with a slot attribute for a named slot:

<component-name>
<span slot="title">Title</span>
<span>Content</span>
</component-name>