Guide

Views

The return of a component is its view, written in TSX. A {...} in the view is a hole. Each hole is an expression in a closed grammar, so the compiler knows which model paths it reads. When a handler writes one of those paths, the page patches that hole and nothing else.

#Holes

A hole fills text, an attribute or a property:

import type { JSX } from 'idlip/jsx-runtime';

function double(n: number): number {
  return n * 2;
}

export default function Profile(props: { url: string }): JSX.Element {
  let name = 'Ada';
  let count = 1;

  function rename(text: string) {
    name = text;
    count = count + 1;
  }

  return (
    <section>
      <h2 title={`Profile of ${name}`}>{name.toUpperCase()}</h2>
      <p>
        Renamed {count} {count === 1 ? 'time' : 'times'}, doubled {double(count)}
      </p>
      <input value={name} on:input={(e) => rename(e.currentTarget.value)} />
      <a href={props.url}>Home</a>
    </section>
  );
}

{
  "url": "https://example.com/ada"
}

A hole may hold:

Anything else, such as JSON.stringify(items) or window.innerWidth, goes in a module-level helper.

#Class and style

class takes a string or an object of booleans. style takes a string or an object of values:

import type { JSX } from 'idlip/jsx-runtime';

export default function Panel(): JSX.Element {
  let open = false;

  function toggle() {
    open = !open;
  }

  return (
    <div class={{ panel: true, open }} style={{ height: open ? '20rem' : '3rem' }}>
      <button on:click={toggle}>{open ? 'Close' : 'Open'}</button>
    </div>
  );
}

#URLs and raw HTML

import { trusted } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

export default function Article(props: { body: string }): JSX.Element {
  return <article html={trusted(props.body)} />;
}

{
  "body": "<p>An article with <em>markup</em>.</p>"
}

#Conditions

Write a condition with && or ? :. A branch can be markup, text or null:

import type { JSX } from 'idlip/jsx-runtime';

export default function Notice(): JSX.Element {
  let shown = true;
  let count = 0;

  function hide() {
    shown = false;
  }
  function add() {
    count = count + 1;
  }

  return (
    <div>
      {shown && <p on:click={hide}>Click to hide</p>}
      {count === 0 ? <p>Nothing yet</p> : <p>{count} added</p>}
      <button on:click={add}>Add</button>
    </div>
  );
}

#Events

An event attribute is on: and the event name: on:click, on:input, on:submit. It takes a handler, or an arrow that sends messages to handlers.

The counter and the todo app use all three forms:

Attribute Message
on:click={increment} increment with no arguments.
on:click={() => toggle(t.id)} toggle with t.id, read from the row when the event runs.
on:input={(e) => setDraft(e.currentTarget.value)} setDraft with the value of the field when the event runs.

#Forms

A submit is always prevented, so the page does not navigate. Read the fields by name from e.currentTarget.elements:

import type { JSX } from 'idlip/jsx-runtime';

export default function Signup(): JSX.Element {
  let name = '';

  function save(text: string) {
    name = text;
  }

  return (
    <form on:submit={(e) => save(e.currentTarget.elements.name.value)}>
      <input name="name" />
      <button>Save</button>
      <p>Saved: {name}</p>
    </form>
  );
}

A field name may be computed: e.currentTarget.elements[field].value reads the field when the event runs.

#Block components

Four components from idlip mark blocks in the view. The compiler replaces them, so they never run.

#For

<For> renders a list. each is the array, and key is a callback of the item that returns a string or number, unique among the items: key={(step) => step.id}. The child is a function of the item and its index:

import { For } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

export default function Steps(props: { steps: { id: string; label: string }[] }): JSX.Element {
  return (
    <ol>
      <For each={props.steps} key={(step) => step.id}>
        {(step, index) => <li data-index={index}>{step.label}</li>}
      </For>
    </ol>
  );
}

{
  "steps": [
    {
      "id": "s1",
      "label": "Install"
    },
    {
      "id": "s2",
      "label": "Write"
    },
    {
      "id": "s3",
      "label": "Ship"
    }
  ]
}

A row moves with its key when the list changes. The key body is one expression that reads the item and nothing else. key="id" is the error for-key, which names the callback form.

Only a key that is one field of the item, step.id, keeps keyed writes: a write set can name one row (steps[id=selected].done). A computed key moves the rows by key all the same. Computed keys are a nested field (step.meta.id), a template literal (`${step.kind}:${step.id}`) or another expression of the closed grammar. Their write sets treat the row as any row (steps.*.done), so a change to the list loads the list chunk and runs the row bindings of every row. That patches more than needed and never less. .map() in a view is the error view-map.

#Try

<Try> shows catch when a hole inside it throws. catch gets the error and a reset function to try again:

import { Try } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

function check(value: number): number {
  if (value < 0) {
    throw new Error(`bad value ${value}`);
  }
  return value;
}

export default function Meter(): JSX.Element {
  let value = -1;

  function increment() {
    value = value + 1;
  }

  return (
    <main>
      <Try
        catch={(error, reset) => (
          <p>
            Failed: {error} <button on:click={reset}>Retry</button>
          </p>
        )}
      >
        <output>{check(value)}</output>
      </Try>
      <button on:click={increment}>+1</button>
    </main>
  );
}

<Head> puts elements in the document <head>. It holds elements and at most one <title>. A hole in a <title> must be its only content:

import { Head } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

export default function Inbox(): JSX.Element {
  let unread = 3;

  const label = `Inbox (${unread})`;

  function read() {
    unread = 0;
  }

  return (
    <main>
      <Head>
        <title>{label}</title>
        <meta name="description" content="Your messages" />
      </Head>
      <button on:click={read}>Mark all read</button>
    </main>
  );
}

renderToResponse() and renderToStream() of idlip/server write the head elements into <head>. See Document head.

#Subscribe

<Subscribe> reads a stream of values while it is in the view and sends each value to a handler. See Subscriptions and Effects.

#Common errors

Each rule is a compile error with this id:

Rule id Cause
view-map .map() in a view. Use <For each key>.
hole-global A global such as JSON or window in a hole. Wrap it in a helper.
hole-method A method outside the list above, such as date.getTime().
jsx-spread A spread attribute, <div {...attrs} />.
event-closure An async event arrow, or a statement that is not a handler call.
event-statement A statement other than a call, if, else or const in an arrow.
event-no-call An event arrow that calls no handler.
event-placeholder The whole event passed as an argument, such as save(e).
attr-event An onclick attribute. Use on:click.
html-trusted html={value} without trusted(...).
srcdoc-trusted A bound srcdoc without trusted(...).
hole-dynamic A hole whose model paths the compiler cannot find and cannot record.

A helper that takes the whole props, or an object of state fields, is not an error. At the top level of a root component, the server records the paths the helper reads and sends them with the state. Inside <For>, a condition, a <Try> or a child component, the hole uses the paths the compiler finds.

tsx
<button onclick="save()">Save</button>                // attr-event
<div html={props.body} />                             // html-trusted
<input on:input={(e) => setDraft(e)} />               // event-placeholder
<ul>{todos.map((t) => <li>{t.text}</li>)}</ul>        // view-map