diff --git a/site/content/docs/01-component-format.md b/site/content/docs/01-component-format.md index 94c19c2e44..616c662476 100644 --- a/site/content/docs/01-component-format.md +++ b/site/content/docs/01-component-format.md @@ -21,14 +21,480 @@ All three sections — script, styles and markup — are optional. ### Script -A ` +``` + + +#### 2. Assignments are 'reactive' + +To change component state and trigger a re-render, just assign to a locally declared variable: + +```html + +``` + +Update expressions (`count += 1`) and property assignments (`obj.x = y`) have the same effect. + + +#### 3. `$:` marks a statement as reactive + +Any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it with the `$:` label. Reactive statements run immediately before the component updates, whenever the values that they depend on have changed: + +```html + +``` + +If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf: + +```html + +``` + + +#### 4. Prefix stores with `$` to access their values + +Any time you have a reference to a store, you can access its value inside a component by prefixing it with the `$` character. This causes Svelte to declare the prefixed variable, and set up a store subscription that will be unsubscribed when appropriate: + +```html + +``` + +Local variables (that do not represent store values) must *not* have a `$` prefix. + + +#### Module context + +A ` + + +``` + +You cannot `export default`, since the default export is the component itself. -* TODO explain run-on-initialisation -* export -* $: -* $ ### Styles +CSS inside a ` +``` + +This works by adding a class to affected elements, which is based on a hash of the component styles (e.g. `svelte-123xyz`). + +To apply styles to a selector globally, use the `:global(...)` modifier: + +```html + +``` + + +### Markup + +#### Tags + +A lowercase tag, like `
{a} + {b} = {a + b}.
+``` + +#### HTML expressions + +In a text expression, characters like `<` and `>` are escaped. An expression can inject HTML with `{@html expression}`: + +```html +what was the question?
+{/if} +``` + +Additional conditions can be added with `{:else if condition}`, optionally ending in an `{:else}` clause: + +```html +{#if porridge.temperature > 100} +too hot!
+{:else if 80 > porridge.temperature} +too cold!
+{:else} +just right!
+{/if} +``` + + +#### Each blocks + +Iterating over lists of values can be done with an `{#each list as item}` block, where `list` is any valid JavaScript expression and `item` is a valid JavaScript identifier, or a destructuring pattern. + +```html +{todo.text}
+{:else} +No tasks today!
+{/each} +``` + + +#### Await blocks + +Await blocks allow you to branch on the three possible states of a Promise — pending, fulfilled or rejected: + +```html +{#await promise} + +waiting for the promise to resolve...
+{:then value} + +The value is {value}
+{:catch error} + +Something went wrong: {error.message}
+{/await} +``` + +The `catch` block can be omitted if no error is possible, and the initial block can be omitted if you don't care about the pending state: + +```html +{#await promise then value} +The value is {value}
+{/await} +``` + + +#### DOM events + +Use the `on:` directive to listen to DOM events: + +```html + + + +``` + +You can add *modifiers* to DOM events with the `|` character: + +```html + +``` + +The following modifiers are available: + +* `preventDefault` — calls `event.preventDefault()` before running the handler +* `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) +* `capture` — fires the handler during the *capture* phase instead of the *bubbling* phase +* `once` — remove the handler after the first time it runs + +Modifiers can be chained together, e.g. `on:click|once|capture={...}`. + +If the `on:` directive is used without a value, the component will *forward* the event, meaning that a consumer of the component can listen for it. + + +#### Component events + +Components can emit events using [createEventDispatcher](#docs/createEventDispatcher), or by forwarding DOM events. Listening for component events looks the same as listening for DOM events: + +```html +