Conditionals

4 min·Assumes: state, events

@if shows its
while a condition is true, and takes them off the page when it isn't.
import { state } from '@markless/core';

export default function Lava() @{
	let alive = state(true);

	<section>
		@if (alive) {
			<p>Level 1</p>
		} @else {
			<p>Game over</p>
		}
		<button onClick={() => (alive = false)}>
			Touch the lava
		</button>
	</section>
}

@if shows its elements while the condition is true, and @else shows what goes there instead.

Show one thing or the other

A condition is a question with a yes or no answer. One side is on the page for yes, the other for no.

PlaygroundRestored your edit

x99Fight or run?
Try this
  1. To do: Press Fight until one of them is down, then Run.
  2. To do: Press Fight: they come back, and whoever was down is still down.
  3. To do: Give her one point: in fight.ts, change 99 * bar to 1, then press Fight.

What isn't showing isn't on the page

When the condition is false, the elements inside aren't hidden. They are taken off the page, and built new the next time it is true. Smash the pots, then leave the room and come back.

An ink drawing, seen from above, of a dungeon corridor with a hooded hero in it and a pink door at its end. Beyond the door is bare paper. Press the door: the hero steps through and the room behind it is drawn, with two clay pots you can smash. Smash one, then press the door to leave: the whole room is gone from the page, not darkened. Go back in: the room is built new and both pots are whole again.

To keep something, declare it above the @if:

let note = state('');

@if (open) {
	<textarea
		value={note}
		onInput={(event) => {
			note = event.currentTarget.value;
		}}
	/>
}

The first true condition wins

@else if asks another question when the one before it was false. They are asked from the top, and @else catches whatever is left. Throw a lever.

An ink drawing, seen from above, of a mine-cart track. The main line runs left to right to a pile of rocks. It has two sets of points, each with a pink lever: the first can send the cart down a branch to a pile of gold, the second down a branch to a pile of gems. Only the track the cart runs on is inked; every other stretch is a faint pencil plan. Press a lever, or focus it and press Enter: the cart takes the first branch whose lever is thrown. With lever one thrown, lever two changes nothing.

@if (firstLever) {
	<Gold />
} @else if (secondLever) {
	<Gems />
} @else {
	<Rocks />
}

Pick one of many with @switch

@switch compares one value with each @case and shows the first that matches. @default shows when none does.

PlaygroundRestored your edit
SprigPipWhat will Pip do?
Try this
  1. To do: Win the fight: find the move a leaf creature can't stand.
  2. To do: Press wiggle. No case is named for it, so @default plays.
  3. To do: Teach Pip a fifth move: add 'snowball' to moves in rules.ts, and @case 'snowball': { <i class="snow" /> } above @default. Then press snowball.
  4. To do: Misspell a case: change @case 'fireball': to @case 'firebal':, then press fireball. No case matches, so @default plays and the fireball never lands.
Can a case be a condition instead of a value?

Yes. Write true as the value, and each @case becomes a question of its own:

@switch (true) {
	@case hp < 20: {
		<p>Run!</p>
	}
	@case hp < 50: {
		<p>Drink a potion.</p>
	}
	@default: {
		<p>Keep fighting.</p>
	}
}
What does the build stop?

Three mistakes, each with a message that says how to fix it:

Coming from JSX?

In JSX you write {open && <Panel />} or a ternary inside the markup. Here the same choice is a block, @if (open) { <Panel /> }, so each side reads top to bottom and @else if needs no nesting.