<script>
let name = 'world';
</script>
<main>
<h1>Hello {name}!</h1>
</main>
<style>
h1 {
color: blue;
}
</style>
<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>
<script>
let count = 0;
$: {
console.log(`The count is ${count}`);
}
$: if (count >= 10) {
alert('Count is dangerously high!');
count = 9;
}
</script>
<script>
export let name;
export let age = 30; // default value
</script>
<p>{name} is {age} years old.</p>
<script>
import Info from './Info.svelte';
const pkg = {
name: 'svelte',
version: 3,
speed: 'blazing',
website: 'https://svelte.dev'
};
</script>
<Info {...pkg} />
{#if user.loggedIn}
<button on:click={logout}>Log out</button>
{:else}
<button on:click={login}>Log in</button>
{/if}
<ul>
{#each items as item, index (item.id)}
<li>{index + 1}: {item.name}</li>
{/each}
</ul>
{#await promise}
<p>...waiting</p>
{:then number}
<p>The number is {number}</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
<script>
function handleClick() {
alert('Button clicked!');
}
</script>
<button on:click={handleClick}>Click me</button>
<button on:click|once|preventDefault={handleClick}>Click me</button>
<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function sayHello() {
dispatch('message', {
text: 'Hello!'
});
}
</script>
<button on:click={sayHello}>Say hello</button>
<script>
let name = 'world';
</script>
<input bind:value={name}>
<h1>Hello {name}!</h1>
<script>
let checked = false;
</script>
<label>
<input type="checkbox" bind:checked>
Is checked: {checked}
</label>
<script>
import { onMount } from 'svelte';
let data = [];
onMount(async () => {
const res = await fetch('https://api.example.com/data');
data = await res.json();
});
</script>
<script>
import { onDestroy } from 'svelte';
let interval = setInterval(() => {
console.log('Tick');
}, 1000);
onDestroy(() => {
clearInterval(interval);
});
</script>
import { writable } from 'svelte/store';
export const count = writable(0);
<script>
import { count } from './stores.js';
function increment() {
count.update(n => n + 1);
}
</script>
<h1>The count is {$count}</h1>
<button on:click={increment}>+</button>
<script>
import { tweened } from 'svelte/motion';
import { cubicOut } from 'svelte/easing';
const progress = tweened(0, {
duration: 400,
easing: cubicOut
});
</script>
<progress value={$progress}></progress>
<button on:click={() => progress.set(0)}>
0%
</button>
<button on:click={() => progress.set(1)}>
100%
</button>
<script>
import { fade } from 'svelte/transition';
let visible = true;
</script>
<label>
<input type="checkbox" bind:checked={visible}>
visible
</label>
{#if visible}
<p transition:fade>Fades in and out</p>
{/if}
<script>
function pannable(node) {
let x;
let y;
function handleMousedown(event) {
x = event.clientX;
y = event.clientY;
node.dispatchEvent(new CustomEvent('panstart', {
detail: { x, y }
}));
window.addEventListener('mousemove', handleMousemove);
window.addEventListener('mouseup', handleMouseup);
}
function handleMousemove(event) {
const dx = event.clientX - x;
const dy = event.clientY - y;
x = event.clientX;
y = event.clientY;
node.dispatchEvent(new CustomEvent('panmove', {
detail: { x, y, dx, dy }
}));
}
function handleMouseup(event) {
x = event.clientX;
y = event.clientY;
node.dispatchEvent(new CustomEvent('panend', {
detail: { x, y }
}));
window.removeEventListener('mousemove', handleMousemove);
window.removeEventListener('mouseup', handleMouseup);
}
node.addEventListener('mousedown', handleMousedown);
return {
destroy() {
node.removeEventListener('mousedown', handleMousedown);
}
};
}
</script>
<div use:pannable
on:panstart={handlePanStart}
on:panmove={handlePanMove}
on:panend={handlePanEnd}>
Drag me!
</div>
<script>
let active = true;
let danger = false;
</script>
<div class:active class:danger>
Class example
</div>
<!-- Card.svelte -->
<div class="card">
<slot></slot>
</div>
<!-- App.svelte -->
<Card>
<h2>Card Title</h2>
<p>Card content</p>
</Card>
<!-- Layout.svelte -->
<div class="layout">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
<!-- App.svelte -->
<Layout>
<h1 slot="header">Page Title</h1>
<p>Main content</p>
<p slot="footer">Copyright 2023</p>
</Layout>
<script>
import { setContext } from 'svelte';
setContext('theme', {
primaryColor: 'blue',
textColor: 'white'
});
</script>
<script>
import { getContext } from 'svelte';
const { primaryColor, textColor } = getContext('theme');
</script>
<svelte:head>
<title>My Svelte App</title>
<meta name="description" content="This is my Svelte app">
</svelte:head>
<script>
let y;
function handleScroll(event) {
y = window.scrollY;
}
</script>
<svelte:window on:scroll={handleScroll}/>
<p>Scrolled to {y}</p>
<script context="module">
let count = 0;
export function incrementCount() {
count += 1;
}
</script>
<script>
import { incrementCount } from './Counter.svelte';
</script>
<button on:click={incrementCount}>
Clicks: {count}
</button>
<script>
let user = { name: 'John', age: 30 };
</script>
{@debug user}
<h1>{user.name}</h1>
// src/routes/+page.svelte
<h1>Home</h1>
// src/routes/about/+page.svelte
<h1>About</h1>
// src/routes/[slug]/+page.svelte
<script>
export let data;
</script>
<h1>{data.title}</h1>
<div>{@html data.content}</div>
// src/routes/blog/[slug]/+page.server.js
export async function load({ params }) {
const post = await getPostFromDatabase(params.slug);
return { post };
}
<script lang="ts">
let count: number = 0;
function increment(): void {
count += 1;
}
</script>
<button on:click={increment}>
Clicks: {count}
</button>
2024 © All rights reserved - buraxta.com