Reference

Component syntax

An idlip component is a TSX function in a *.tsx file. The source is plain TypeScript, so tsc checks it with the types of idlip/jsx-runtime. The compiler accepts a closed subset of it and reports anything else as a diagnostic with a rule id, listed at the end of this page.

#Modules

A module has one default export, the root component. Other capitalized function declarations are components too. Lowercase function declarations and module-level const arrows are helpers or subscription sources. Imports and module-level constants have no model paths, so a hole that reads only them never patches.

import { For } from 'idlip';

interface Todo {
  id: string;
  text: string;
  done: boolean;
}

function countLeft(todos: readonly Todo[]): number {
  let left = 0;
  for (const todo of todos) {
    if (!todo.done) {
      left++;
    }
  }
  return left;
}

export default function TodoApp(props: { initial: Todo[] }) {
  let todos = props.initial;

  const left = countLeft(todos);

  function toggle(id: string) {
    todos = todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t));
  }

  return (
    <main>
      <p>{left} left</p>
      <ul>
        <For each={todos} key={(t) => t.id}>
          {(t) => (
            <li class={{ done: t.done }}>
              <input type="checkbox" checked={t.done} on:click={() => toggle(t.id)} />
              {t.text}
            </li>
          )}
        </For>
      </ul>
    </main>
  );
}

{
  "initial": [
    {
      "id": "t1",
      "text": "Read the guide",
      "done": true
    },
    {
      "id": "t2",
      "text": "Write a component",
      "done": false
    }
  ]
}

#Component body

The body holds let and const declarations, function declarations, type declarations and one final return (body-statement, body-return).

Props are one parameter, props or a destructuring pattern of plain fields (props-count, props-pattern).

#Holes

A hole is an expression in the view: a text child or an attribute value. The compiler resolves the model paths each hole reads, such as todos.*.done, and a binding patches only when one of its paths changes. Holes use a closed grammar:

Spreads, closures, other globals and other methods are errors (hole-spread, hole-closure, hole-global, hole-method). Impure globals (Math.random, Date.now, performance.now, crypto.randomUUID) are allowed in holes only, and give the warning hole-impure when the hole also reads paths, since the value is computed again at each patch.

#Views

export default function Status(props: { count: number }) {
  let count = props.count;

  function reset() {
    count = 0;
  }

  return (
    <section>
      {count === 0 ? <p>Nothing yet</p> : <p>{count} items</p>}
      {count > 0 && <button on:click={reset}>Reset</button>}
    </section>
  );
}

{
  "count": 3
}

#Attributes

#Events

on:<event> dispatches a message. Its value is a handler, or an arrow that calls handlers. The arrow compiles to a function in the handler chunk. It reads the event, the state, the props and the row when it runs, after the event has dispatched, and returns the messages. The page holds only its name.

export default function Editor() {
  let draft = '';
  let saved = '';

  function setDraft(text: string) {
    draft = text;
  }
  function save() {
    saved = draft;
  }
  function clear() {
    draft = '';
  }

  return (
    <form on:submit={save}>
      <input
        value={draft}
        on:input={(e) => setDraft(e.currentTarget.value)}
        on:keydown={(e) => {
          if (e.key === 'Escape') {
            clear();
          }
        }}
      />
      <output>{saved}</output>
    </form>
  );
}

#Handlers and effects

A handler may await a call. The handler is split at each await. The part before it runs on the click. The loader then runs the call and records its outcome. The part after it runs as a continuation, with the state at that moment.

import { Time } from 'idlip';

export default function Saver() {
  let status = 'idle';

  async function save() {
    status = 'saving';
    try {
      await Time.sleep(500);
      status = 'saved';
    } catch {
      status = 'failed';
    }
  }

  return (
    <button on:click={save} disabled={status === 'saving'}>
      {status}
    </button>
  );
}

#Children

A component used in JSX is inlined when it is stateless: no let and no handler. Its props bind to the caller's expressions, and props.children places the caller's children.

A stateful component is a child with its own model slot. It must be the default export of its own *.tsx file, imported by that name. Its onX props are message props: the parent passes () => handler(args), and the child dispatches it with on:click={props.onRemove}.

tsx
import { For } from 'idlip';
import Counter from './counter.tsx';

interface Item {
  id: string;
  label: string;
}

export default function Board(props: { items: Item[] }) {
  let items = props.items;

  function remove(id: string) {
    items = items.filter((item) => item.id !== id);
  }

  return (
    <main>
      <For each={items} key={(c) => c.id}>
        {(c) => <Counter label={c.label} start={0} onRemove={() => remove(c.id)} />}
      </For>
    </main>
  );
}

A child's slot is its key prop, else the <For> key inside a loop, else its position among the child blocks of the component. A child takes no JSX children (child-children).

#Diagnostics

Each diagnostic has a rule id and a severity. All are errors except let-unassigned, hole-impure and handler-derived-stale, which are warnings. An error stops the build of the module. A warning does not.

Area Rule ids
Body body-statement, body-var, body-destructure, body-return, props-count, props-pattern, props-whole
State let-annotation, let-bare-as, let-init, let-function (a function assigned to a let, as an initial value or in a handler), let-unassigned (warning), init-scope
Derived derived-jsx, derived-cycle
Holes hole-closure, hole-method, hole-global, hole-handler, hole-statement, hole-effect, hole-unknown, hole-operator, hole-literal, hole-object, hole-array, hole-spread, hole-call, hole-member, hole-jsx, hole-message, hole-children, hole-function-value, hole-grammar, hole-impure (warning), helper-closure, hole-dynamic (a path set that code generation cannot bind)
View view-map, condition-operator, for-each, for-key, for-render, for-param, ref-form, ref-key, html-trusted, srcdoc-trusted (a bound srcdoc that is not trusted(...)), html-children, html-element (html on an element that cannot hold raw HTML), title-outside-head, head-title-twice, attr-jsx, attr-event, jsx-spread, jsx-namespace-tag, component-unknown, component-import, component-recursive, child-children
Module module-reserved-name (a declaration or import named like a helper the compiler generates, such as setAttr$)
HTML html-nesting (the parser would close or move the element, or text sits in a table part), html-raw-text (a hole in <script> or <style>, or a hole next to other content in <textarea> or <title>), attr-reserved (an attribute starting with i-), html-implied (the parser would insert <tbody>, <tr> or <colgroup>)
Events event-closure, event-target, event-param, event-mode-twice, event-message-args, event-statement, event-no-call, event-prevent-twice, event-message-body, event-message-event, event-target-nested, event-placeholder (the whole event passed as an argument, such as save(e)), event-passive-prevent, dom-call-position
Blocks try-catch, try-catch-node (a block the catch view of <Try> cannot hold), subscribe-source, subscribe-input-key, subscribe-children, global-attr, global-children
Handlers handler-free, handler-assign, handler-mutate, handler-impure, handler-generator, handler-derived-props (a derived value whose props paths the payload cannot carry), handler-derived-stale (warning: a derived value read after a state write keeps its render-time value), handler-props-unlisted, handler-return-finally, helper-module-state (a helper reassigns a module-level let), await-placement, await-callee, await-nested, await-multiple, await-finally, await-handler
Codegen codegen-component (the file has no component), codegen-event, codegen-raw-text (a bound hole in <textarea> or <title> that cannot patch), codegen-unsupported (a form the code generator does not write yet)
Parser parse