Reintent

Your first intent

Split the card into components, give them children, and read λ as data.

The card in Quick start's first step is one function. For the dinner it grows: a line only a 90th birthday gets, and a signature from every grandchild. Before anything is left open, the card needs parts that fit together.

You will learn

  • How components take props and use each other
  • How a component takes children, with childrenOf and fragments
  • What JSX turns into: h
  • How to read λ as data, with isHostNode

Components and props

The heading and the signature become components of their own:

/** @jsxImportSource @reintent/core */
import { host } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

/** The heading: a component of the two props it needs. */
export function Title(p: { to: string; age: number }) {
  return host('h1', { style: { color: '#b5562b' }, children: `Happy ${p.age}th birthday, ${p.to}!` });
}

export function Signature(p: { from: string }) {
  return host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` });
}

export function Card(p: Person) {
  return host('div', {
    style: { maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
    children: [
      <Title to={p.to} age={p.age} />,
      host('p', { children: 'Wishing you a wonderful day.' }),
      <Signature from={p.from} />,
    ],
  });
}

export default function App() {
  return <Card {...GRANDMA} />;
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

A component (Component) is a function of its props. It returns what it is made of, a Child: host nodes, other components, text, or a list of them. Card uses <Title to age /> and <Signature from /> the same way it uses host('p', …).

host(kind, props) makes a host node. Its kind is a name the host understands, and its props are whatever the host needs to draw it, children among them. Switch the card's pane to λ: Title and Signature are gone, and the host nodes they returned stand in their place.

Children

The card's paper should hold whatever Sam puts on it, and every wish should get a paragraph of its own:

/** @jsxImportSource @reintent/core */
import { childrenOf, host, type Child } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
export const FAMILY = ['Sam', 'Mia', 'Leo'];

/** The card's paper: whatever is put inside it. */
export function Card(p: { children?: Child }) {
  return host('div', {
    style: { maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
    children: p.children,
  });
}

export function Title(p: { to: string; age: number }) {
  return host('h1', { style: { color: '#b5562b' }, children: `Happy ${p.age}th birthday, ${p.to}!` });
}

/** One paragraph per wish: what draws nothing (false, null) is left out. */
export function Wishes(p: { children?: Child }) {
  return childrenOf(p.children).map((wish) => host('p', { children: wish }));
}

/** A line per name, with no wrapper around them. */
export function Signatures(p: { names: string[] }) {
  return (
    <>
      {p.names.map((name) =>
        host('p', { style: { textAlign: 'right', fontStyle: 'italic', margin: 0 }, children: `— ${name}` }),
      )}
    </>
  );
}

export default function App() {
  const p = GRANDMA;
  return (
    <Card>
      <Title to={p.to} age={p.age} />
      <Wishes>
        {'Wishing you a wonderful day.'}
        {p.age >= 90 && 'Ninety looks good on you.'}
        {p.age < 18 && 'Have a great party!'}
      </Wishes>
      <Signatures names={FAMILY} />
    </Card>
  );
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

  • What is written between <Card> and </Card> is Card's children prop. Card hands it on to its div.
  • childrenOf(children) gives the children as a flat list, and Wishes makes a paragraph of each. For anyone under 90, p.age >= 90 && '…' is false. childrenOf leaves out false, null and undefined, so a wish that does not apply draws nothing.
  • <>…</>, a fragment, returns several children with nothing around them. Signatures puts its three lines straight into the card. A component may also return an array, as Wishes does.

Change age: 90 in GRANDMA to 12, and the card wishes Grandma a great party.

JSX is h

A script or a tool that writes a program has no JSX. Every element is also a function call:

/** @jsxImportSource @reintent/core */
import { createIntent, Fragment, h } from '@reintent/core';
import { Card, FAMILY, Signatures, Title, Wishes } from './App.intent';

// JSX is a shorter way to call h(type, props, ...children).
const written = <Title to="Grandma" age={90} />;
const called = h(Title, { to: 'Grandma', age: 90 });
console.log(written.type === called.type, called.props); // → true { to: 'Grandma', age: 90 }

// A fragment, <>…</>, is h(Fragment, null, ...children).
const both = h(Fragment, null, 'Sam', 'Mia');
console.log(both.type === Fragment, both.props.children); // → true [ 'Sam', 'Mia' ]

// The whole card, without JSX.
export const intent = createIntent({
  program: h(
    Card,
    null,
    h(Title, { to: 'Grandma', age: 90 }),
    h(Wishes, null, 'Wishing you a wonderful day.', 'Ninety looks good on you.'),
    h(Signatures, { names: FAMILY }),
  ),
});
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

<Title to="Grandma" age={90} /> is h(Title, { to: 'Grandma', age: 90 }), and <Card>{a}{b}</Card> is h(Card, null, a, b). The arguments after the props become children. Both make the same IntentElement, with a type, props and a key. A fragment is h(Fragment, null, …). In index.intent.tsx, the whole card is built from h calls, and the first lines in What happened compare the two forms.

λ is data

At the dinner, someone reads the card aloud. Sam's app could read it out too, since every line on the card is in λ:

/** @jsxImportSource @reintent/core */
import { createIntent, isHostNode, type Rendered } from '@reintent/core';
import App from './App.intent';

const intent = createIntent({ program: <App /> });

/** Every line of text on the card, in order: what someone reads out at the dinner. */
function lines(node: Rendered | Rendered[] | null): string[] {
  if (node == null) return [];
  if (Array.isArray(node)) return node.flatMap(lines);
  if (isHostNode(node)) return node.children.flatMap(lines);
  return [String(node)];
}

const { tree } = intent.realize();
console.log(isHostNode(tree) && tree.kind); // → 'div'
console.log(lines(tree).length, lines(tree)[0]); // → 6 'Happy 90th birthday, Grandma!'
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

intent.realize().tree is λ. Each item in it is a Rendered: a host node, text or a number, plain data that a host can walk. isHostNode(x) tells whether x is a host node (HostNode), with a kind, props and children. lines walks the tree and collects the text in order: six lines, from the heading to Leo's signature. Drawing λ covers what a host does with it.

The host sees host nodes only

λ holds what the components returned: a div, an h1, paragraphs. The host draws these and knows nothing of Card or Title, so any host that understands these host nodes can draw the same components.

Recap

  • A component is a function of its props. host(kind, props) makes a host node, and λ is made of host nodes.
  • children is what is written between the tags. childrenOf flattens it and leaves out false, null and undefined.
  • A fragment, <>…</>, returns several children with no wrapper.
  • JSX is h(type, props, ...children), which makes an IntentElement.
  • intent.realize().tree is λ, plain data. isHostNode tells host nodes from text.

Last updated on

Edit on GitHub