We got engaged!!
Lists
@for repeats an element once for every item in a list.
import { state } from '@markless/core';
export default function Weekend() @{
let plans = state(['gym', 'laundry', 'taxes']);
<section>
<ul>
@for (const plan of plans; key plan) {
<li>{plan}</li>
}
</ul>
<button onClick={() => plans.pop()}>
Feel a bit tired
</button>
</section>
}@for repeats the <li> once for each plan in plans, and key tells the items apart. The button removes the last plan from plans, and its <li> leaves the page.
The key says which item is which
Give each loop item a key, and whatever the reader did to it (a like, a tick, half-typed text) stays with it when the list changes order.
Drag any man to another spot, and see who each slip points at.
It was number 3.
key iIt was Mr Moss.
key suspect.idkey suspect.id tells the items apart by who the man is, so the slip follows him. key i tells them apart by where they stand, so the slip stays on spot 3 and accuses whoever is there now.
Try it on a timeline
Key items by their position and a like lands on the wrong post as soon as a new one arrives. Like the engagement, then show the new post.
Monday. Coffee number three.
- To do: Like “We got engaged!!”, then press Show 1 post. Look at which post your like is on now.
- To do: Change
index i; key itokey post.idand run it again. Like the engagement, then press Show 1 post: your like stays on the engagement.
Sort and filter without changing the list
Show a list sorted or filtered without changing it, and the copy keeps itself up to date: put the sorted list in a computed value and loop over that.
Tap a snail to cheer it on.
- 1stPebble0
- 2ndDoris0
- 3rdSlick0
- 4thNibbles0
- To do: Cheer Pebble, the pink snail, all the way to the lettuce.
- To do: Put the last snail on top: in race.tsrx, change
b.distance - a.distancetoa.distance - b.distance, then race again. - To do: Show only the podium: add
.slice(0, 3)after the sort, then race. Watch a snail drop off the board and come back.
Split one list into several
Change a list once, and every place that shows part of it updates: make each place a computed filter() of the one list. @empty is what a place shows when it has no items.
Drag a shape into a hole.
On the tray: 6
- To do: Drag the square into the square hole. Then try the star in the square hole too.
- To do: Push all six through the square hole. The other three holes still show their cobweb: that is
@empty. - To do: Fix the toy: in rules.ts, make
fitsreturnhole === shape.kind;, then try the star on the square hole again.
No mouse? Focus a shape, press 1 to 4 to choose a hole, then Enter.
How do I show each item's number?
Add index to the loop. It counts from 0 and follows the order on the page:
@for (const runner of board; index place; key runner.id) {
<li>{place + 1}. {runner.name}</li>
}What makes a good key?
Something that belongs to the item and never changes, like an id. Write it as the item or one of its fields (key item, key item.id); anything calculated, like key String(item.id), stops the build. The position works only for a list that never changes order, and the build warns you when you use it.
Coming from React?
The key does the same job as key in React. Here it is part of @for itself, and leaving it out stops the build.
Button sounds are from Josh Comeau's Button Sounds library.