hype @diso.io/hype
Reference
Signals, html templates and custom elements, drawn in the page or written
to a string.
signals
Values that remember what read them. A signal holds a value; an effect, a computed value or a template that reads it is run again when it changes.
Signal |
A value that remembers the effects that read it and reruns them when it changes. |
Computed |
A value worked out from signals. |
signal |
Make a signal that starts as initial. |
effect |
Run fn now, and again after any signal it read changes. |
computed |
A value worked out from other signals. |
onCleanup |
Run fn when the surrounding effect reruns or stops, or its element leaves the page. |
Signal
interface Signal<T> {
value: T
}
A value that remembers the effects that read it and reruns them when it changes.
Computed
interface Computed<T> {
readonly value: T
}
A value worked out from signals.
signal
function signal<T>(initial: T): Signal<T>
Make a signal that starts as initial. Read and set it through .value.
effect
function effect(fn: () => void): () => void
Run fn now, and again after any signal it read changes. Returns a function
that stops it. An effect made inside another effect or an element stops with it.
computed
function computed<T>(fn: () => T): Computed<T>
A value worked out from other signals. Its readers rerun only when the result changes, and a reader never sees a result older than the signals it came from.
onCleanup
function onCleanup(fn: () => void): void
Run fn when the surrounding effect reruns or stops, or its element leaves the page.
templates
HTML written as a tagged template, with values in it. A value that is a function is live: only its place in the page is updated when a signal it reads changes.
Template |
What html and svg return: the literal text of a template and the values between it. |
html |
A template of HTML. |
svg |
A template of SVG elements to place inside an <svg>. |
raw |
A string of HTML as a template: markup made elsewhere, such as a drawing or a page written in Markdown. |
nothing |
A value that draws nothing. |
Template
interface Template {
strings: TemplateStringsArray
values: unknown[]
kind: "html" | "svg"
}
What html and svg return: the literal text of a template and the values between it.
html
function html(strings: TemplateStringsArray, ...values: unknown[]): Template
A template of HTML. A value that is a function is called again whenever a signal it reads changes.
svg
function svg(strings: TemplateStringsArray, ...values: unknown[]): Template
A template of SVG elements to place inside an <svg>.
raw
function raw(markup: string): Template
A string of HTML as a template: markup made elsewhere, such as a drawing or a page written in Markdown. The string is placed as it is, so it must come from a source that is trusted.
nothing
const nothing = null
A value that draws nothing.
elements
Custom elements. define names an element, its props and what it draws;
the props are signals, so the element follows them. An element is written
as a function, or as a class when other code calls its methods.
Props |
A custom element's props as signals, one for each name in its props. |
Setup |
Runs each time the element enters the page and returns what to draw. |
Options |
|
onMount |
Run fn once the element's content is in the page. |
css |
CSS as a string. |
HypeElement |
A custom element written as a class. |
ElementClass |
A class that extends HypeElement, as define takes it. |
define |
Define a custom element, from a function or from a class. |
Props
type Props<P> = { readonly [K in keyof P]: Signal<P[K]> };
A custom element's props as signals, one for each name in its props.
Setup
type Setup<P> = (props: Props<P>, host: HTMLElement) => unknown;
Runs each time the element enters the page and returns what to draw. host
is the element itself; it does not exist when rendering to a string, so use
it only inside onMount and event handlers.
Options
interface Options<P>
props?: P |
The element's props and their defaults. Each can be set as a property, or as an attribute named in kebab-case (videoId is video-id). An attribute is read as the type of its default: a boolean is whether the attribute is present, a number is parsed, anything else is the attribute's text. |
styles?: string |
CSS for the element alone. Giving it draws the element in a shadow root. |
shadow?: boolean |
Draw in a shadow root, so content placed inside the element shows through <slot>. |
onMount
function onMount(fn: () => void): void
Run fn once the element's content is in the page. Call it inside define's
setup. It never runs when rendering to a string.
css
function css(strings: TemplateStringsArray, ...values: unknown[]): string
CSS as a string. A tag so editors highlight it; it does nothing else.
HypeElement
abstract class HypeElement<P extends object = Record<never, never>> extends HTMLElement
A custom element written as a class. Extend it, give draw, and pass the
class to define. The class is the element itself, so its methods are
called on the element, and it can implement an interface other code relies
on.
interface Counter {
reset(): void;
}
class XCount extends HypeElement<typeof XCount.props> implements Counter {
static props = { start: 0 };
count = signal(0);
reset() {
this.count.value = this.props.start.value;
}
draw() {
return html`<button @click=${() => this.count.value++}>${() => this.count.value}</button>`;
}
}
define("x-count", XCount);
document.querySelector<XCount>("x-count")?.reset();
When a page is written to a string there is no page for the element to be
in, so reach for the page (this.querySelector, this.dispatchEvent) only
inside onMount, event handlers and methods, never in draw itself.
static props: object |
The element's props and their defaults. Each can be set as a property, or as an attribute named in kebab-case (videoId is video-id). An attribute is read as the type of its default: a boolean is whether the attribute is present, a number is parsed, anything else is the attribute's text. |
static styles = "" |
CSS for the element alone. Giving it draws the element in a shadow root. |
static shadow?: boolean |
Draw in a shadow root, so content placed inside the element shows through <slot>. |
static get observedAttributes(): string[] |
|
readonly props: Props<P> |
The element's props as signals, one for each name in static props. HypeElement<typeof XCount.props> types them from the defaults. |
constructor() |
|
abstract draw(): unknown |
Runs each time the element enters the page and returns what to draw. |
attributeChangedCallback(attribute: string, _: string | null, value: string | null): void |
|
connectedCallback(): void |
|
disconnectedCallback(): void |
ElementClass
interface ElementClass<P extends object> {
new (): HypeElement<P>
props: P
styles: string
shadow?: boolean
}
A class that extends HypeElement, as define takes it.
define
function define(name: string, setup: Setup<Record<never, never>>): void
function define<P extends object>(name: string, element: ElementClass<P>): void
function define<P extends Record<string, unknown>>(name: string, options: Options<P>, setup: Setup<P>): void
Define a custom element, from a function or from a class.
As a function, setup returns what to draw, and reads the element's props
as signals.
define("x-count", { props: { start: 0 } }, ({ start }) => {
const count = signal(start.value);
return html`<button @click=${() => count.value++}>${() => count.value}</button>`;
});
As a class, it extends HypeElement: see there.
define("x-count", XCount);
rendering
Putting a template somewhere: into the page, where it stays live, or into a string of markup, on a server or in a build.
render |
Draw a value inside container, replacing what was there. |
renderToString |
A value as markup, with every defined element's own content written inside it. |
render
function render(value: unknown, container: ParentNode): () => void
Draw a value inside container, replacing what was there. Returns a function
that stops its effects.
renderToString
function renderToString(value: unknown): string
A value as markup, with every defined element's own content written inside it. Needs no DOM. When the page loads, each element draws over what was written for it.