Reading a .tsrx file

5 min·Assumes: nothing

Every .tsrx file is made of two things:
and
.
import { state } from '@markless/core';

export default function GuestList() @{
	let guests = state(['Ada', 'Grace']);

	<h2>{guests.length} going</h2>
}

{ } holds a value: {guests.length} becomes 2. @{ } 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:

Result
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.

PlaygroundRestored your edit

Coming?

Grandalf’s party, 8pm.

He arrives precisely when he means to.

Try this
  1. 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.
  2. 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 @{ } adds a few more setup lines right where they're needed.

PlaygroundRestored your edit

Hosted by Frodough

Frodough is here. Guests: 0

Try this
  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.
  2. To do: Try breaking it: after </section>, add <p>See you!</p>. Two elements at the end stop the build.
  3. 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 and @for go right among the elements.

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 and @switch show one thing or another: Conditionals
  • @for repeats an element once for every item in a list: Lists
  • @try waits for a slow value: Async