Reintent

Along an axis

Let a person's pick on one screen hold for the screens like it, until it meets another pick.

The family reads Sam's card on a tablet at the dinner table, and Sam checks it on a phone. A large title suits the tablet and is too much for the phone. Sam picks a small title on the phone. The pick should hold on the other phones and leave the tablet alone. That makes one decision whose value depends on the screen's width.

You will learn

  • How a context's axis lets one decision differ along it
  • How choose spreads a pick along the axis until it meets another
  • What the decision it builds looks like, and where the base value stays
  • How crossover keeps a single boundary

A pick that spreads

The card on four screens, 320 to 1280 pixels wide, each with its own size buttons:

/** @jsxImportSource @reintent/core */
import { choices, createContext, host, useChoose, useContext } from '@reintent/core';

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

export const SIZES = ['small', 'medium', 'large'] as const;
export type Size = (typeof SIZES)[number];
const FONT: Record<Size, number> = { small: 14, medium: 19, large: 26 };

/** The screen the card is on. Its width is an axis: a decision can differ along it. */
export const Screen = createContext<{ id: string; width: number }>('Screen', {
  axes: { width: (s) => s.width },
});

/** What a size button does: the intent gives it to every evaluation (index.intent.tsx). */

/** Sam's phone, a small phone, the tablet at the dinner table, a laptop. */
export const WIDTHS = [320, 390, 768, 1280];

function Card() {
  const screen = useContext(Screen);
  const size = useChoose(choices<Size>(...SIZES), { of: 'title', name: 'size', defaultValue: 'large' });
  return host('div', {
    style: { padding: 10, borderRadius: 10, background: '#fdebd3', color: '#3a2a1c' },
    children: [
      host('small', { children: `${screen.width}px` }),
      host('h3', {
        style: { color: '#b5562b', fontSize: FONT[size], margin: '4px 0' },
        children: `Happy ${GRANDMA.age}th!`,
      }),
      host('p', {
        style: { margin: 0 },
        children: SIZES.map((s) =>
          host('button', {
            disabled: s === size,
            onClick: { event: 'pick', args: [screen.width, s] },
            children: s,
          }),
        ),
      }),
    ],
  });
}

export default function App() {
  return host('div', {
    style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 },
    children: WIDTHS.map((width) => (
      <Screen.Provider value={{ id: `w${width}`, width }}>
        <Card />
      </Screen.Provider>
    )),
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

Screen is a context with an axis, width (createContext with axes; see Passing it down with context). Each screen provides its own value, so the same choice is read four times, once per screen. choose(prev, axis, x, kind, base) takes the decision so far and a pick, kind at the point x of axis, and returns the new decision. intent.decide writes it like any other.

Press small on the 390 screen. The title turns small at 390 and on everything narrower, the 320 phone too. The tablet and the laptop keep the default. Then press medium at 768. It spreads down until it meets the phone's pick, and takes everything above 390 up to 768. Each pick takes its own point and everything below it, down to the next pick.

The decision it builds

After the two picks, the decision choose returned is:

{
  $axis: 'Screen.width',
  segments: [
    { lo: null, hi: 390.000000001, value: 'small' },
    { lo: 390.000000001, hi: 768.000000001, value: 'medium' },
    { lo: 768.000000001, hi: null }, // no value: nobody picked above 768
  ],
}

This is a piecewise decision (Piecewise): one value for each segment of the axis, from lo up to just below hi. A pick's own point sits just inside its segment, so 390 is small. What happened lists the segments, rounded. The intent reads the segment a screen falls in. A segment with no value leaves the choice to the layers below, here the default.

base is the value at the wide end, the one the wide screens keep. Picking it works the other way round: it takes its point and everything above, within the segment it lands in. Press large on the 320 phone. 320 to 390 turns large, and anything narrower than 320 stays small.

One crossover

Some hosts keep a single boundary: the small title below some width, the large one above it. crossover(prev, axis, x, kind, base) builds that decision with two segments, kind below the crossover and base from it up:

import { entity } from '@reintent/core';
import { crossover } from '@reintent/lib/axis';

const title = entity('title', { size: { kinds: ['small', 'medium', 'large'], default: 'large' } });

// Small picked on the 390 phone: 390 and below are small, the rest large.
let { value } = crossover(undefined, 'Screen.width', 390, 'small', 'large');
// → segments [ null, 390.001, 'small' ], [ 390.001, null, 'large' ]

// Large picked again at 768: the crossover moves there, and small stays below it.
({ value } = crossover(value, 'Screen.width', 768, 'large', 'large'));
// → segments [ null, 768, 'small' ], [ 768, null, 'large' ]
intent.decide(title.size(), value);

The point is rounded so that x lands on the side picked. Unlike choose, a new pick moves the one crossover rather than adding a segment. Picking base keeps what was below. With nothing below, the result also has not, the values the narrow end may not be, here ['large'], for the host to write as a constraint.

Recap

  • A context's axis (axes: { width }) lets one decision differ along it.
  • choose(prev, axis, x, kind, base) adds a pick and returns the whole decision; intent.decide writes it.
  • A pick takes its point and everything below it, down to the next pick; the base value takes its point and everything above.
  • The decision is a list of segments; a segment with no value leaves the choice to the layers below.
  • crossover(prev, axis, x, kind, base) keeps one boundary, kind below and base from it up; a new pick moves it.

Last updated on

Edit on GitHub