# 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`](#signal) | A value that remembers the effects that read it and reruns them when it changes. |
| [`Computed`](#computed) | A value worked out from signals. |
| [`signal`](#signal-1) | Make a signal that starts as `initial`. |
| [`effect`](#effect) | Run `fn` now, and again after any signal it read changes. |
| [`computed`](#computed-1) | A value worked out from other signals. |
| [`onCleanup`](#oncleanup) | Run `fn` when the surrounding effect reruns or stops, or its element leaves the page. |

### `Signal`

```ts
interface Signal<T> {
  value: T
}
```

A value that remembers the effects that read it and reruns them when it changes.

### `Computed`

```ts
interface Computed<T> {
  readonly value: T
}
```

A value worked out from signals.

### `signal`

```ts
function signal<T>(initial: T): Signal<T>
```

Make a signal that starts as `initial`. Read and set it through `.value`.

### `effect`

```ts
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`

```ts
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`

```ts
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`](#template) | What `html` and `svg` return: the literal text of a template and the values between it. |
| [`html`](#html) | A template of HTML. |
| [`svg`](#svg) | A template of SVG elements to place inside an `<svg>`. |
| [`raw`](#raw) | A string of HTML as a template: markup made elsewhere, such as a drawing or a page written in Markdown. |
| [`nothing`](#nothing) | A value that draws nothing. |

### `Template`

```ts
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`

```ts
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`

```ts
function svg(strings: TemplateStringsArray, ...values: unknown[]): Template
```

A template of SVG elements to place inside an `<svg>`.

### `raw`

```ts
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`

```ts
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`](#props) | A custom element's props as signals, one for each name in its `props`. |
| [`Setup`](#setup) | Runs each time the element enters the page and returns what to draw. |
| [`Options`](#options) |  |
| [`onMount`](#onmount) | Run `fn` once the element's content is in the page. |
| [`css`](#css) | CSS as a string. |
| [`HypeElement`](#hypeelement) | A custom element written as a class. |
| [`ElementClass`](#elementclass) | A class that extends `HypeElement`, as `define` takes it. |
| [`define`](#define) | Define a custom element, from a function or from a class. |

### `Props`

```ts
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`

```ts
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`

```ts
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`

```ts
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`

```ts
function css(strings: TemplateStringsArray, ...values: unknown[]): string
```

CSS as a string. A tag so editors highlight it; it does nothing else.

### `HypeElement`

```ts
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.

```ts
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`

```ts
interface ElementClass<P extends object> {
  new (): HypeElement<P>
  props: P
  styles: string
  shadow?: boolean
}
```

A class that extends `HypeElement`, as `define` takes it.

### `define`

```ts
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.

```ts
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.

```ts
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`](#render) | Draw a value inside `container`, replacing what was there. |
| [`renderToString`](#rendertostring) | A value as markup, with every defined element's own content written inside it. |

### `render`

```ts
function render(value: unknown, container: ParentNode): () => void
```

Draw a value inside `container`, replacing what was there. Returns a function
that stops its effects.

### `renderToString`

```ts
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.
