From 3ba245ce8b35aa2c1893eeb90ef33f0e84fee689 Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Thu, 27 May 2021 16:26:48 +0200 Subject: [PATCH] Add Svelte notes --- JavaScript/Svelte/Svelte.md | 205 ++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 JavaScript/Svelte/Svelte.md diff --git a/JavaScript/Svelte/Svelte.md b/JavaScript/Svelte/Svelte.md new file mode 100644 index 0000000..e261dc4 --- /dev/null +++ b/JavaScript/Svelte/Svelte.md @@ -0,0 +1,205 @@ +# [Svelte](https://svelte.dev/docs) + +```sh +npx degit sveltejs/template + +# set project to use typescript +npm install --save-dev @tsconfig/svelte typescript svelte-preprocess svelte-check +node scripts/setupTypeScript.js +``` + +## App Entrypoint + +```js +import App from "./App.svelte"; // import the component + +const app = new App({ + target: document.body, + props: { + // props passed to the App component + }, +}); + +export default app; +``` + +## Components (`.svelte`) + +### Basic Structure + +```html + + + + + + + + + +
{variable}
+ + + +``` + +### If-Else + +```js +{#if } + // markup here +{:else if } + // markup here +{:else} + // markup here +{/if} +``` + +### Loops + +```js +{#each array as item, index} // index is optional + // markup here +{/each} + +{#each array as item (key)} // usse key to determine changes + // markup here +{/each} +``` + +### Await Blocks + +```js +{#await promise} +

...waiting

+{:then number} +

The number is {number}

+{:catch error} +

{error.message}

+{/await} +``` + +### Event Handling + +The full list of modifiers: + +- `preventDefault` — calls `event.preventDefault()` before running the handler. Useful for client-side form handling, for example. +- `stopPropagation` — calls `event.stopPropagation()`, preventing the event reaching the next element +- `passive` — improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so) +- `nonpassive` — explicitly set `passive: false` +- `capture` — fires the handler during the capture phase instead of the bubbling phase +- `once` — remove the handler after the first time it runs +- `self` — only trigger handler if `event.target` is the element itself + +```js + + +