Reactivity
Declare state
Update state
Templating
Styling
Loop
Event click
Dom ref
Conditional
<script>
const TRAFFIC_LIGHTS = ["red", "orange", "green"];
let lightIndex = 0;
$: light = TRAFFIC_LIGHTS[lightIndex];
function nextLight() {
lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length;
}
</script>
<button on:click={nextLight}>Next light</button>
<p>Light is: {light}</p>
<p>
You must
{#if light === "red"}
<span>STOP</span>
{:else if light === "orange"}
<span>SLOW DOWN</span>
{:else if light === "green"}
<span>GO</span>
{/if}
</p>
Aurelia 2
<button click.trigger="nextLight()">Next light</button>
<p>Light is: ${light}</p>
<p switch.bind="light">
You must
<span case="red">STOP</span>
<span case="orange">SLOW DOWN</span>
<span case="green">GO</span>
</p>
Lifecycle
On mount
On unmount
Component composition
Props
Emit to parent
<script>
import AnswerButton from "./AnswerButton.svelte";
let isHappy = true;
function onAnswerNo() {
isHappy = false;
}
function onAnswerYes() {
isHappy = true;
}
</script>
<p>Are you happy?</p>
<AnswerButton on:yes={onAnswerYes} on:no={onAnswerNo} />
<p style="font-size: 50px;">{isHappy ? "😀" : "😥"}</p>
Aurelia 2
<p>Can I come ?</p>
<answer-button action-handler.bind="handleAnswer"></answer-button>
<p style="font-size: 50px">${isHappy ? "😀" : "😥"}</p>
Slot
Slot fallback
Context
<script>
import { setContext } from "svelte";
import UserProfile from "./UserProfile.svelte";
import createUserStore from "./createUserStore.js";
const userStore = createUserStore({
id: 1,
username: "unicorn42",
email: "[email protected]",
});
setContext("user", userStore);
</script>
<h1>Welcome back, {$userStore.username}</h1>
<UserProfile />
Aurelia 2
<h1>Welcome back, {{ user.username }}</h1>
<user-profile />
Form input
Input text
Checkbox
<script>
let isAvailable = false;
</script>
<input id="is-available" type="checkbox" bind:checked={isAvailable} />
<label for="is-available">Is available</label>
Aurelia 2
<input id="is-available" type="checkbox" checked.bind="isAvailable" />
<label for="is-available">Is available</label>: ${isAvailable}
Radio
<script>
let picked = "red";
</script>
<div>Picked: {picked}</div>
<input id="blue-pill" bind:group={picked} type="radio" value="blue" />
<label for="blue-pill">Blue pill</label>
<input id="red-pill" bind:group={picked} type="radio" value="red" />
<label for="red-pill">Red pill</label>
Aurelia 2
<div>Picked: ${picked}</div>
<input id="blue-pill" checked.bind="picked" type="radio" value="blue" />
<label for="blue-pill">Blue pill</label>
<input id="red-pill" checked.bind="picked" type="radio" value="red" />
<label for="red-pill">Red pill</label>
Select
<script>
let selectedColorId = 2;
const colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
</script>
<select bind:value={selectedColorId}>
{#each colors as color}
<option value={color.id} disabled={color.isDisabled}>
{color.text}
</option>
{/each}
</select>
Aurelia 2
<select value.bind="selectedColorId">
<option value="">Select A Color</option>
<option
repeat.for="color of colors"
value.bind="color.id"
disabled.bind="color.isDisabled"
>
${color.text}
</option>
</select>
Webapp features
Render app
Svelte 4
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./app.js"></script>
</body>
</html>
Aurelia 2
<!doctype html>
<html>
<head>
<script type="module" src="./main.ts"></script>
</head>
<body>
<app></app>
</body>
</html>
Fetch data
<script>
import useFetchUsers from "./useFetchUsers";
const { isLoading, error, data: users } = useFetchUsers();
</script>
{#if $isLoading}
<p>Fetching users...</p>
{:else if $error}
<p>An error occurred while fetching users</p>
{:else if $users}
<ul>
{#each $users as user}
<li>
<img src={user.picture.thumbnail} alt="user" />
<p>
{user.name.first}
{user.name.last}
</p>
</li>
{/each}
</ul>
{/if}
Aurelia 2
<template promise.bind="useFetchUsers.fetchData()">
<p pending>Fetching users...</p>
<p catch>An error ocurred while fetching users</p>
<ul then.from-view="users">
<li repeat.for="user of users">
<img src.bind="user.picture.thumbnail" alt="user" />
<p>${ user.name.first } ${ user.name.last }</p>
</li>
</ul>
</template>