The search index did not load. Type again to retry.
Nothing in the docs matches “”.
↑↓ to moveenter to goesc to close
Reading a .tsrx file
5 min·Assumes: nothing
Every .tsrx file is made of two things:
Anything that produces a result, like guests.length. Written inside { }.In the glossary
and
Lines that run in order, top to bottom, like let guests = state([]). Written inside @{ }.In the glossary
.
import { state } from '@markless/core';export default functionGuestListfunction GuestList(): Element() @{@{ · Statement containerHolds instructions: setup lines, then the element to show. letguestslet guests: string[]=statestate · Watched valueDeclares a variable the page watches. You read and assign it like any other variable, and the page keeps up.(['Ada', 'Grace']); <h2<h2> · HTML elementA second-level heading.>{guests.length(property) Array<string>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.} going</h2<h2> · HTML elementA second-level heading.>}
{ }{ } · Expression containerHolds one value. The page shows its result. holds a value: {guests.length} becomes 2. @{ }@{ · Statement containerHolds instructions: setup lines, then the element to show. holds instructions: some lines of setup, then the element to show.
Point at a line
Hover any line of this file, or tap it. It shows whether the line is a value or an instruction, and what it puts on the page.
A printout of guest-list.tsrx beside the small page it produces: Ada, Grace and a Clear button. Press a part of the file, or move between parts with the arrow keys, to see what it is and what it becomes on the page. Escape clears.
{ } holds a value
A value is an expression: anything that produces a result. Whatever fits after let x = is one. Each pair of braces holds one.
Whatever you write between the braces, the page shows its result. Step through four of them:
An ink drawing of a card printed with the word Result, followed by a window cut in the shape of a pair of curly braces. Under the card is a pile of four sheets. The top sheet says at its top what was written, and the window shows its result: name shows Ada, guests.length shows 2, name.toUpperCase() shows ADA, and name + '!' shows Ada!. The next arrow, or pressing the pile, sends the top sheet to the back; the previous arrow brings the back sheet to the top. The card and its braces never move.
Text, attributes and handlers
Braces go in three places: in text, in an attribute, and as a handler. The playground below is a party invitation with an RSVP button that uses all three.
Its button says Going once you've pressed it, and RSVP before that.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { state } from '@markless/core';export default function Rsvp() @{ let going = state(false); <sectionclass="wizvite"> <imgsrc="/examples/ink/grandalf.svg"alt="" /> <h2>{going ? 'See you there' : 'Coming?'}</h2> <p>Grandalf’s party, 8pm.</p> <p>He arrives precisely when he means to.</p> <buttonclass={going ? 'yes' : 'maybe'}onClick={() => (going = !going)} > {going ? 'Going' : 'RSVP'} </button> </section>}
To do: Click RSVP. All three values change: the heading, the button’s class and its label. Grandalf lights up, because the stylesheet looks for the yes class.
To do: Change 'Coming?' to 'Are you in?'.
@{ } holds instructions, run top to bottom
An instruction is a statement: a line that does something. It makes a variable, defines a function, or decides what to show.
The setup lines come first and the element comes last.
The playground below keeps its guest list in a plain guests.ts file and imports it. Inside its <section>, a second @{ }@{ · Statement containerHolds instructions: setup lines, then the element to show. adds a few more setup lines right where they're needed.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { state } from '@markless/core';import { nextGuest } from './guests.ts';export default function Party() @{ let guests = state(['Frodough']); const host = guests[0]; function ring() { const next = nextGuest(guests.length); if (next) guests = [...guests, next]; } <sectionclass="doorway"> <p>Hosted by {host}</p>@{ const others = guests.length - 1; const last = guests[others]; const who = last.toLowerCase(); const face = '/examples/ink/' + who; <pclass="door"> <imgsrc={face + '.svg'} alt="" /> {last} is here. Guests: {others} </p> } <buttononClick={ring}>Ring the bell</button> </section>}
const LATE = ['Linus', 'Smeagle', 'Grandalf'];// Who rings next, once `count` guests are in.export function nextGuest( count: number,): string | undefined { return LATE[count - 1];}
To do: Press Ring the bell three times. Each ring lets one more guest in; the inner @{ } works out who came last, and Grandalf is the last to arrive.
To do: Try breaking it: after </section>, add <p>See you!</p>. Two elements at the end stop the build.
To do: Fix it: wrap both in <> and </>.
How is this different from JSX?
In JSX a component returns its markup. Anything that isn't a value, like an if or a loop, has to sit above the return, or be rewritten as a ternary and a .map().
Here the markup is written straight in the body, with no return, and @if@if · ConditionalShows its block while the test is true. and @for@for · ListShows its block once per item. It needs a key. go right among the elements.
A nested @{ } doesn't work straight inside a top-level <> yet.
Blocks that start with @
A line that starts with @ decides what the page shows. The file above has two of them. Each kind has its own page:
@if@if · ConditionalShows its block while the test is true. and @switch@switch · Value matchShows the one @case that matches, or @default. show one thing or another: Conditionals
@for@for · ListShows its block once per item. It needs a key. repeats an element once for every item in a list: Lists
@try@try · Async blockShows its block once the slow value is ready. waits for a slow value: Async