Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring over examples #294

Merged
merged 16 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let name = 'world';
</script>

<h1>Hello {name}!</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Hello world'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let src = '/tutorial/image.gif';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't work — cross-origin isolation gremlins strike again

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and this time it's impossible to get it working because we can't add the relaxed attribute on the iframe for security reasons.

I really think we need to make navigations to the SvelteKit tutorial full page reloads, and navigations away from it, too, and only set the headers on the SvelteKit tutorial. That would solve all this iframe/img/audio/video bullshit we had to deal with the last days. I don't expect people to quickly jump back and forth between that tutorial and something else, so it will be fine in practise.

let name = 'Rick Astley';
</script>

<!-- {src} is short for src={src} -->
<img {src} alt="{name} dancing" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Dynamic attributes'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>Styled!</p>

<style>
p {
color: purple;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Styling'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import Nested from './Nested.svelte';
</script>

<p>These styles...</p>
<Nested />

<style>
p {
color: purple;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>...don't affect this element</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Nested components'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let string = `here's some <strong>HTML!!!</strong>`;
</script>

<p>{@html string}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'HTML tags'
---
3 changes: 3 additions & 0 deletions apps/svelte.dev/content/examples/00-introduction/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Introduction'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let count = $state(0);

function handleClick() {
count += 1;
}
</script>

<button onclick={handleClick}>
Clicked {count}
{count === 1 ? 'time' : 'times'}
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Reactive assignments'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
let count = $state(1);

let doubled = $derived(count * 2);
let quadrupled = $derived(doubled * 2);

function handleClick() {
count += 1;
}
</script>

<button onclick={handleClick}>
Count: {count}
</button>

<p>{count} * 2 = {doubled}</p>
<p>{doubled} * 2 = {quadrupled}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Reactive declarations'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
let count = $state(0);

$effect(() => {
if (count >= 10) {
alert(`count is dangerously high!`);
count = 9;
}
});

function handleClick() {
count += 1;
}
</script>

<button onclick={handleClick}>
Clicked {count}
{count === 1 ? 'time' : 'times'}
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Reactive statements'
---
3 changes: 3 additions & 0 deletions apps/svelte.dev/content/examples/01-reactivity/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Reactivity'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Nested from './Nested.svelte';
</script>

<Nested answer={42} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { answer } = $props();
</script>

<p>The answer is {answer}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Declaring props'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import Nested from './Nested.svelte';
</script>

<Nested answer={42} />
<Nested />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { answer = 'a mystery' } = $props();
</script>

<p>The answer is {answer}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Default values'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import Info from './Info.svelte';

const pkg = {
name: 'svelte',
version: 3,
speed: 'blazing',
website: 'https://svelte.dev'
};
</script>

<Info {...pkg} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
let {
name,
version,
speed,
website
} = $props();
</script>

<p>
The <code>{name}</code> package is {speed} fast. Download version {version} from
<a href="https://www.npmjs.com/package/{name}">npm</a>
and <a href={website}>learn more here</a>
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Spread props'
---
3 changes: 3 additions & 0 deletions apps/svelte.dev/content/examples/02-props/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Props'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
let user = $state({ loggedIn: false });

function toggle() {
user.loggedIn = !user.loggedIn;
}
</script>

{#if user.loggedIn}
<button onclick={toggle}> Log out </button>
{/if}

{#if !user.loggedIn}
<button onclick={toggle}> Log in </button>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'If blocks'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let user = $state({ loggedIn: false });

function toggle() {
user.loggedIn = !user.loggedIn;
}
</script>

{#if user.loggedIn}
<button onclick={toggle}> Log out </button>
{:else}
<button onclick={toggle}> Log in </button>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Else blocks'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
let x = 7;
</script>

{#if x > 10}
<p>{x} is greater than 10</p>
{:else if 5 > x}
<p>{x} is less than 5</p>
{:else}
<p>{x} is between 5 and 10</p>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Else-if blocks'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
let cats = [
{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
{ id: 'z_AbfPXTKms', name: 'Maru' },
{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }
];
</script>

<h1>The Famous Cats of YouTube</h1>

<ul>
{#each cats as { id, name }, i}
<li>
<a target="_blank" rel="noreferrer" href="https://www.youtube.com/watch?v={id}">
{i + 1}: {name}
</a>
</li>
{/each}
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Each blocks'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
import Thing from './Thing.svelte';

let things = $state([
{ id: 1, color: 'darkblue' },
{ id: 2, color: 'indigo' },
{ id: 3, color: 'deeppink' },
{ id: 4, color: 'salmon' },
{ id: 5, color: 'gold' }
]);

function handleClick() {
things = things.slice(1);
}
</script>

<button onclick={handleClick}> Remove first thing </button>

<div style="display: grid; grid-template-columns: 1fr 1fr; grid-gap: 1em">
<div>
<h2>Keyed</h2>
{#each things as thing (thing.id)}
<Thing current={thing.color} />
{/each}
</div>

<div>
<h2>Unkeyed</h2>
{#each things as thing}
<Thing current={thing.color} />
{/each}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script>
// `current` is updated whenever the prop value changes...
let { current } = $props();

// ...but `initial` is fixed upon initialisation
const initial = current;
</script>

<p>
<span style="background-color: {initial}">initial</span>
<span style="background-color: {current}">current</span>
</p>

<style>
span {
display: inline-block;
padding: 0.2em 0.5em;
margin: 0 0.2em 0.2em 0;
width: 4em;
text-align: center;
border-radius: 0.2em;
color: white;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Keyed each blocks'
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
let promise = $state(getRandomNumber());

async function getRandomNumber() {
const res = await fetch(`/tutorial/random-number`);
const text = await res.text();

if (res.ok) {
return text;
} else {
throw new Error(text);
}
}

function handleClick() {
promise = getRandomNumber();
}
</script>

<button onclick={handleClick}> generate random number </button>

{#await promise}
<p>...waiting</p>
{:then number}
<p>The number is {number}</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Await blocks'
---
3 changes: 3 additions & 0 deletions apps/svelte.dev/content/examples/03-logic/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'Logic'
---
Loading
Loading