mirror of https://github.com/sveltejs/svelte
				
				
				
			
							parent
							
								
									ff32efd8d9
								
							
						
					
					
						commit
						5bd43fba51
					
				@ -0,0 +1,5 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let name = 'world';
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<h1>Hello {name.toUpperCase()}!</h1>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Adding data"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,6 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let src = 'tutorial/image.gif';
 | 
				
			||||
	let name = 'Rick Astley';
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<img {src} alt="{name} dancing">
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Dynamic attributes"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,9 @@
 | 
				
			||||
<style>
 | 
				
			||||
	p {
 | 
				
			||||
		color: purple;
 | 
				
			||||
		font-family: 'Comic Sans MS';
 | 
				
			||||
		font-size: 2em;
 | 
				
			||||
	}
 | 
				
			||||
</style>
 | 
				
			||||
 | 
				
			||||
<p>This is a paragraph.</p>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Styling"
 | 
				
			||||
}
 | 
				
			||||
@ -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>This is a paragraph.</p>
 | 
				
			||||
<Nested/>
 | 
				
			||||
@ -0,0 +1 @@
 | 
				
			||||
<p>This is another paragraph.</p>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Nested components"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,5 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let string = `this string contains some <strong>HTML!!!</strong>`;
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<p>{@html string}</p>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "HTML tags"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Introduction"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,11 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let count = 0;
 | 
				
			||||
 | 
				
			||||
	function handleClick() {
 | 
				
			||||
		count += 1;
 | 
				
			||||
	}
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<button on:click={handleClick}>
 | 
				
			||||
	Clicked {count} {count === 1 ? 'time' : 'times'}
 | 
				
			||||
</button>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Assignments"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,14 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let count = 0;
 | 
				
			||||
	$: doubled = count * 2;
 | 
				
			||||
 | 
				
			||||
	function handleClick() {
 | 
				
			||||
		count += 1;
 | 
				
			||||
	}
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<button on:click={handleClick}>
 | 
				
			||||
	Clicked {count} {count === 1 ? 'time' : 'times'}
 | 
				
			||||
</button>
 | 
				
			||||
 | 
				
			||||
<p>{count} doubled is {doubled}</p>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Declarations"
 | 
				
			||||
}
 | 
				
			||||
@ -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>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Statements"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Reactivity"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,5 @@
 | 
				
			||||
<script>
 | 
				
			||||
	import Nested from './Nested.svelte';
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<Nested answer={42}/>
 | 
				
			||||
@ -0,0 +1,5 @@
 | 
				
			||||
<script>
 | 
				
			||||
	export let answer;
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<p>The answer is {answer}</p>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Declaring props"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,6 @@
 | 
				
			||||
<script>
 | 
				
			||||
	import Nested from './Nested.svelte';
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<Nested answer={42}/>
 | 
				
			||||
<Nested/>
 | 
				
			||||
@ -0,0 +1,5 @@
 | 
				
			||||
<script>
 | 
				
			||||
	export let answer = 'a mystery';
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<p>The answer is {answer}</p>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Default values"
 | 
				
			||||
}
 | 
				
			||||
@ -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}/>
 | 
				
			||||
@ -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>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Spread props"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Props"
 | 
				
			||||
}
 | 
				
			||||
@ -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}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "If blocks"
 | 
				
			||||
}
 | 
				
			||||
@ -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}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Else blocks"
 | 
				
			||||
}
 | 
				
			||||
@ -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}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Else-if blocks"
 | 
				
			||||
}
 | 
				
			||||
@ -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>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Each blocks"
 | 
				
			||||
}
 | 
				
			||||
@ -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}
 | 
				
			||||
@ -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>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Keyed each blocks"
 | 
				
			||||
}
 | 
				
			||||
@ -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}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Await blocks"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Logic"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,16 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let m = { x: 0, y: 0 };
 | 
				
			||||
 | 
				
			||||
	function handleMousemove(event) {
 | 
				
			||||
		m.x = event.clientX;
 | 
				
			||||
		m.y = event.clientY;
 | 
				
			||||
	}
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<style>
 | 
				
			||||
	div { width: 100%; height: 100%; }
 | 
				
			||||
</style>
 | 
				
			||||
 | 
				
			||||
<div on:mousemove={handleMousemove}>
 | 
				
			||||
	The mouse position is {m.x} x {m.y}
 | 
				
			||||
</div>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "DOM events"
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,11 @@
 | 
				
			||||
<script>
 | 
				
			||||
	let m = { x: 0, y: 0 };
 | 
				
			||||
</script>
 | 
				
			||||
 | 
				
			||||
<style>
 | 
				
			||||
	div { width: 100%; height: 100%; }
 | 
				
			||||
</style>
 | 
				
			||||
 | 
				
			||||
<div on:mousemove="{e => m = { x: e.clientX, y: e.clientY }}">
 | 
				
			||||
	The mouse position is {m.x} x {m.y}
 | 
				
			||||
</div>
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
{
 | 
				
			||||
	"title": "Inline handlers"
 | 
				
			||||
}
 | 
				
			||||
Some files were not shown because too many files have changed in this diff Show More
					Loading…
					
					
				
		Reference in new issue