@reintent/lib/axis
Choices along an ordered axis of a context (a screen's shape).
choose(prev, axis, x, kind, base) a pick spreads along the axis until it meets another
crossover(prev, axis, x, kind, base) one crossover: the base from x up, the pick below itWith choose, a person picks a possibility on one screen, and it spreads along the axis until it meets another pick. Away from the base end the picked one takes this point and everything below it in its segment; the base one takes this point and everything above it in its segment. choose and crossover build a piecewise decision (0008 §7); the core only reads it.
Functions
choose()
function choose<T>(
prev,
axis,
x,
kind,
base
): Piecewise<T>;Defined in: packages/lib/src/axis.ts
A pick at the point x of axis, spread along it until it meets another pick: kind at x
and, away from base, below it; base at x and above it. prev is the decision so far.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
prev | Decided<T> | undefined |
axis | string |
x | number |
kind | T |
base | T |
Returns
Piecewise<T>
Example
import {
choices,
createContext,
createIntent,
host,
useChoose,
type Provide,
} from '@reintent/core';
import { choose } from '@reintent/lib/axis';
const Screen = createContext<{ id: string; width: number }>('Screen', {
axes: { width: (s) => s.width },
});
function Deck() {
const layout = useChoose(choices('cols1', 'cols2', 'cols3'), {
of: 'deck',
name: 'layout',
defaultValue: 'cols3',
});
return host('deck', { layout });
}
const intent = createIntent({ program: <Deck /> });
const on = (width: number): Provide => [[Screen, { id: `w${width}`, width }]];
// On a 390px phone a person picks one column: it holds at 390 and below.
let layout = choose(undefined, 'Screen.width', 390, 'cols1', 'cols3');
// Then two columns at 768: it spreads down until it meets the phone's pick.
layout = choose(layout, 'Screen.width', 768, 'cols2', 'cols3');
intent.decide({ of: 'deck', name: 'layout' }, layout);
const widths = [320, 390, 600, 768, 1440];
console.log(widths.map((w) => intent.realize(on(w)).reads[0].value));
// → [ 'cols1', 'cols1', 'cols2', 'cols2', 'cols3' ]crossover()
function crossover<T>(
prev,
axis,
x,
kind,
base
): object;Defined in: packages/lib/src/axis.ts
One crossover on the axis: base from the point x up, kind below it, chosen at x (the point
is rounded so that x lands on the side chosen). Choosing base keeps what was below; with
nothing below, not says what the low end may not be (a constraint for the caller to write).
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
prev | Decided<T> | undefined |
axis | string |
x | number |
kind | T |
base | T |
Returns
object
| Name | Type |
|---|---|
not? | T[] |
value | Piecewise<T> |
Example
import { crossover } from '@reintent/lib/axis';
// A column chosen on a 4:5 post (ratio 0.8): narrower screens take it, wider keep the row.
const { value } = crossover(undefined, 'Screen.ratio', 0.8, 'cols1', 'cols3');
console.log(value.segments.map((s) => [s.lo, s.hi, s.value]));
// → [ [ null, 0.801, 'cols1' ], [ 0.801, null, 'cols3' ] ]
// The row chosen again on an iPad (0.6949): the crossover moves there; the column stays below.
const again = crossover(value, 'Screen.ratio', 820 / 1180, 'cols3', 'cols3');
console.log(
again.value.segments.map((s) => [s.lo, s.hi, s.value]),
again.not,
);
// → [ [ null, 0.694, 'cols1' ], [ 0.694, null, 'cols3' ] ] undefinedLast updated on
Edit on GitHub