Skip to content

Commit 9ee57ca

Browse files
feat: bring over examples (#294)
* bring over examples (old syntax) and wire them up correctly * expose examples in the UI through select * migrate to runes, remove deprecated stuff * add redirect * prettier * fix entries generation * use temporary redirects so we don't regret it in the future when we want to reuse those * get it working on build by moving examples retrieval into prerendered endpoint * lint * move examples dropdown to icon on the right * show "edited" in the title once changing contents * add "create new" option * add examples to search (with low priority) * fix * remove duplicate content --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent b1bce68 commit 9ee57ca

File tree

280 files changed

+5142
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

280 files changed

+5142
-263
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let name = 'world';
3+
</script>
4+
5+
<h1>Hello {name}!</h1>
Lines changed: 3 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let src = '/tutorial/image.gif';
3+
let name = 'Rick Astley';
4+
</script>
5+
6+
<!-- {src} is short for src={src} -->
7+
<img {src} alt="{name} dancing" />
Lines changed: 3 additions & 0 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>Styled!</p>
2+
3+
<style>
4+
p {
5+
color: purple;
6+
font-family: 'Comic Sans MS', cursive;
7+
font-size: 2em;
8+
}
9+
</style>
Lines changed: 3 additions & 0 deletions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
import Nested from './Nested.svelte';
3+
</script>
4+
5+
<p>These styles...</p>
6+
<Nested />
7+
8+
<style>
9+
p {
10+
color: purple;
11+
font-family: 'Comic Sans MS', cursive;
12+
font-size: 2em;
13+
}
14+
</style>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>...don't affect this element</p>
Lines changed: 3 additions & 0 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let string = `here's some <strong>HTML!!!</strong>`;
3+
</script>
4+
5+
<p>{@html string}</p>
Lines changed: 3 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
let count = $state(0);
3+
4+
function handleClick() {
5+
count += 1;
6+
}
7+
</script>
8+
9+
<button onclick={handleClick}>
10+
Clicked {count}
11+
{count === 1 ? 'time' : 'times'}
12+
</button>
Lines changed: 3 additions & 0 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
let count = $state(1);
3+
4+
let doubled = $derived(count * 2);
5+
let quadrupled = $derived(doubled * 2);
6+
7+
function handleClick() {
8+
count += 1;
9+
}
10+
</script>
11+
12+
<button onclick={handleClick}>
13+
Count: {count}
14+
</button>
15+
16+
<p>{count} * 2 = {doubled}</p>
17+
<p>{doubled} * 2 = {quadrupled}</p>
Lines changed: 3 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
let count = $state(0);
3+
4+
$effect(() => {
5+
if (count >= 10) {
6+
alert(`count is dangerously high!`);
7+
count = 9;
8+
}
9+
});
10+
11+
function handleClick() {
12+
count += 1;
13+
}
14+
</script>
15+
16+
<button onclick={handleClick}>
17+
Clicked {count}
18+
{count === 1 ? 'time' : 'times'}
19+
</button>
Lines changed: 3 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import Nested from './Nested.svelte';
3+
</script>
4+
5+
<Nested answer={42} />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { answer } = $props();
3+
</script>
4+
5+
<p>The answer is {answer}</p>
Lines changed: 3 additions & 0 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import Nested from './Nested.svelte';
3+
</script>
4+
5+
<Nested answer={42} />
6+
<Nested />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { answer = 'a mystery' } = $props();
3+
</script>
4+
5+
<p>The answer is {answer}</p>
Lines changed: 3 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import Info from './Info.svelte';
3+
4+
const pkg = {
5+
name: 'svelte',
6+
version: 3,
7+
speed: 'blazing',
8+
website: 'https://svelte.dev'
9+
};
10+
</script>
11+
12+
<Info {...pkg} />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
let {
3+
name,
4+
version,
5+
speed,
6+
website
7+
} = $props();
8+
</script>
9+
10+
<p>
11+
The <code>{name}</code> package is {speed} fast. Download version {version} from
12+
<a href="https://www.npmjs.com/package/{name}">npm</a>
13+
and <a href={website}>learn more here</a>
14+
</p>
Lines changed: 3 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let user = $state({ loggedIn: false });
3+
4+
function toggle() {
5+
user.loggedIn = !user.loggedIn;
6+
}
7+
</script>
8+
9+
{#if user.loggedIn}
10+
<button onclick={toggle}> Log out </button>
11+
{/if}
12+
13+
{#if !user.loggedIn}
14+
<button onclick={toggle}> Log in </button>
15+
{/if}
Lines changed: 3 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let user = $state({ loggedIn: false });
3+
4+
function toggle() {
5+
user.loggedIn = !user.loggedIn;
6+
}
7+
</script>
8+
9+
{#if user.loggedIn}
10+
<button onclick={toggle}> Log out </button>
11+
{:else}
12+
<button onclick={toggle}> Log in </button>
13+
{/if}
Lines changed: 3 additions & 0 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let x = 7;
3+
</script>
4+
5+
{#if x > 10}
6+
<p>{x} is greater than 10</p>
7+
{:else if 5 > x}
8+
<p>{x} is less than 5</p>
9+
{:else}
10+
<p>{x} is between 5 and 10</p>
11+
{/if}
Lines changed: 3 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
let cats = [
3+
{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
4+
{ id: 'z_AbfPXTKms', name: 'Maru' },
5+
{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }
6+
];
7+
</script>
8+
9+
<h1>The Famous Cats of YouTube</h1>
10+
11+
<ul>
12+
{#each cats as { id, name }, i}
13+
<li>
14+
<a target="_blank" rel="noreferrer" href="https://www.youtube.com/watch?v={id}">
15+
{i + 1}: {name}
16+
</a>
17+
</li>
18+
{/each}
19+
</ul>
Lines changed: 3 additions & 0 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<script>
2+
import Thing from './Thing.svelte';
3+
4+
let things = $state([
5+
{ id: 1, color: 'darkblue' },
6+
{ id: 2, color: 'indigo' },
7+
{ id: 3, color: 'deeppink' },
8+
{ id: 4, color: 'salmon' },
9+
{ id: 5, color: 'gold' }
10+
]);
11+
12+
function handleClick() {
13+
things = things.slice(1);
14+
}
15+
</script>
16+
17+
<button onclick={handleClick}> Remove first thing </button>
18+
19+
<div style="display: grid; grid-template-columns: 1fr 1fr; grid-gap: 1em">
20+
<div>
21+
<h2>Keyed</h2>
22+
{#each things as thing (thing.id)}
23+
<Thing current={thing.color} />
24+
{/each}
25+
</div>
26+
27+
<div>
28+
<h2>Unkeyed</h2>
29+
{#each things as thing}
30+
<Thing current={thing.color} />
31+
{/each}
32+
</div>
33+
</div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script>
2+
// `current` is updated whenever the prop value changes...
3+
let { current } = $props();
4+
5+
// ...but `initial` is fixed upon initialisation
6+
const initial = current;
7+
</script>
8+
9+
<p>
10+
<span style="background-color: {initial}">initial</span>
11+
<span style="background-color: {current}">current</span>
12+
</p>
13+
14+
<style>
15+
span {
16+
display: inline-block;
17+
padding: 0.2em 0.5em;
18+
margin: 0 0.2em 0.2em 0;
19+
width: 4em;
20+
text-align: center;
21+
border-radius: 0.2em;
22+
color: white;
23+
}
24+
</style>
Lines changed: 3 additions & 0 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script>
2+
let promise = $state(getRandomNumber());
3+
4+
async function getRandomNumber() {
5+
const res = await fetch(`/tutorial/random-number`);
6+
const text = await res.text();
7+
8+
if (res.ok) {
9+
return text;
10+
} else {
11+
throw new Error(text);
12+
}
13+
}
14+
15+
function handleClick() {
16+
promise = getRandomNumber();
17+
}
18+
</script>
19+
20+
<button onclick={handleClick}> generate random number </button>
21+
22+
{#await promise}
23+
<p>...waiting</p>
24+
{:then number}
25+
<p>The number is {number}</p>
26+
{:catch error}
27+
<p style="color: red">{error.message}</p>
28+
{/await}
Lines changed: 3 additions & 0 deletions
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)