Skip to content

Examples #2199

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

Merged
merged 17 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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": "Adding data"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let src = 'tutorial/image.gif';
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"
}
9 changes: 9 additions & 0 deletions site/content/examples/00-introduction/02-styling/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<style>
p {
color: purple;
font-family: 'Comic Sans MS';
font-size: 2em;
}
</style>

<p>Styled!</p>
3 changes: 3 additions & 0 deletions site/content/examples/00-introduction/02-styling/meta.json
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>

<style>
p {
color: purple;
font-family: 'Comic Sans MS';
font-size: 2em;
}
</style>

<p>These styles...</p>
<Nested/>
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"
}
5 changes: 5 additions & 0 deletions site/content/examples/00-introduction/04-html-tags/App.svelte
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>
3 changes: 3 additions & 0 deletions site/content/examples/00-introduction/04-html-tags/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "HTML tags"
}
3 changes: 3 additions & 0 deletions site/content/examples/00-introduction/meta.json
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,11 @@
<script>
let count = 0;

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

<button on:click={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,18 @@
<script>
let count = 1;

// the `$:` means 're-run whenever these values change'
$: doubled = count * 2;
$: quadrupled = doubled * 2;

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

<button on:click={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,16 @@
<script>
let count = 0;

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

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

<button on:click={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 site/content/examples/01-reactivity/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Reactivity"
}
5 changes: 5 additions & 0 deletions site/content/examples/02-props/00-declaring-props/App.svelte
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>
export let answer;
</script>

<p>The answer is {answer}</p>
3 changes: 3 additions & 0 deletions site/content/examples/02-props/00-declaring-props/meta.json
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
Expand Up @@ -2,5 +2,5 @@
import Nested from './Nested.svelte';
</script>

<p>This is a top-level element.</p>
<Nested answer={42}/>
<Nested/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let answer = 'a mystery';
</script>

<p>The answer is {answer}</p>
3 changes: 3 additions & 0 deletions site/content/examples/02-props/01-default-values/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Default values"
}
12 changes: 12 additions & 0 deletions site/content/examples/02-props/02-spread-props/App.svelte
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.technology'
};
</script>

<Info {...pkg}/>
12 changes: 12 additions & 0 deletions site/content/examples/02-props/02-spread-props/Info.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
export let name;
export let version;
export let speed;
export let website;
</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>
3 changes: 3 additions & 0 deletions site/content/examples/02-props/02-spread-props/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Spread props"
}
3 changes: 3 additions & 0 deletions site/content/examples/02-props/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Props"
}
19 changes: 19 additions & 0 deletions site/content/examples/03-logic/00-if-blocks/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
let user = { loggedIn: false };

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

{#if user.loggedIn}
<button on:click={toggle}>
Log out
</button>
{/if}

{#if !user.loggedIn}
<button on:click={toggle}>
Log in
</button>
{/if}
3 changes: 3 additions & 0 deletions site/content/examples/03-logic/00-if-blocks/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "If blocks"
}
17 changes: 17 additions & 0 deletions site/content/examples/03-logic/01-else-blocks/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
let user = { loggedIn: false };

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

{#if user.loggedIn}
<button on:click={toggle}>
Log out
</button>
{:else}
<button on:click={toggle}>
Log in
</button>
{/if}
3 changes: 3 additions & 0 deletions site/content/examples/03-logic/01-else-blocks/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Else blocks"
}
11 changes: 11 additions & 0 deletions site/content/examples/03-logic/02-else-if-blocks/App.svelte
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}
3 changes: 3 additions & 0 deletions site/content/examples/03-logic/02-else-if-blocks/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Else-if blocks"
}
17 changes: 17 additions & 0 deletions site/content/examples/03-logic/03-each-blocks/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<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" href="https://www.youtube.com/watch?v={id}">
{i + 1}: {name}
</a></li>
{/each}
</ul>
3 changes: 3 additions & 0 deletions site/content/examples/03-logic/03-each-blocks/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Each blocks"
}
23 changes: 23 additions & 0 deletions site/content/examples/03-logic/04-keyed-each-blocks/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import Thing from './Thing.svelte';

let things = [
{ id: 1, value: 'a' },
{ id: 2, value: 'b' },
{ id: 3, value: 'c' },
{ id: 4, value: 'd' },
{ id: 5, value: 'e' }
];

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

<button on:click={handleClick}>
Remove first thing
</button>

{#each things as thing (thing.id)}
<Thing value={thing.value}/>
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
// `value` is updated whenever the prop value changes...
export let value;

// ...but `valueAtStart` is fixed upon initialisation
const valueAtStart = value;
</script>

<p>{valueAtStart} / {value}</p>
3 changes: 3 additions & 0 deletions site/content/examples/03-logic/04-keyed-each-blocks/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Keyed each blocks"
}
30 changes: 30 additions & 0 deletions site/content/examples/03-logic/05-await-blocks/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script>
let promise = 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 on:click={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}
Loading