From b52c1e4fcdb38545b4ff2606d3c55234d73f3a83 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 14 Mar 2019 20:11:39 -0400 Subject: [PATCH] mostly finish template syntax section --- site/content/docs/02-template-syntax.md | 220 +++++++++++++++++++++++- 1 file changed, 216 insertions(+), 4 deletions(-) diff --git a/site/content/docs/02-template-syntax.md b/site/content/docs/02-template-syntax.md index d530ca8922..8778694789 100644 --- a/site/content/docs/02-template-syntax.md +++ b/site/content/docs/02-template-syntax.md @@ -781,8 +781,220 @@ Local transitions only play when the block they belong to is created or destroye ``` -### TODO +### Animations -* animations -* slots -* special elements +TODO i can't remember how any of this works + + +### Slots + +* `` +* `` +* `` + +--- + +Components can have child content, in the same way that elements can. + +The content is exposed in the child component using the `` element, which can contain fallback content that is rendered if no children are provided. + +```html + + +

this is some child content

+
+ + +
+ + this will be rendered if someone does + +
+``` + +--- + +Named slots allow consumers to target specific areas. They can also have fallback content. + +```html + + +

Hello

+

Copyright (c) 2019 Svelte Industries

+
+ + +
+ No header was provided +

Some content between header and footer

+ +
+``` + +--- + +Slots can be rendered zero or more times, and can pass values *back* to the parent using props. The parent exposes the values to the slot template using the `let:` directive. + +The usual shorthand rules apply — `let:item` is equivalent to `let:item={item}`, and `` is equivalent to ``. + +```html + + +
{item.text}
+
+ + +
    + {#each items as item} +
  • + +
  • + {/each} +
+``` + +--- + +Named slots can also expose values. The `let:` directive goes on the element with the `slot` attribute. + +```html + + +
{item.text}
+

Copyright (c) 2019 Svelte Industries

+
+ + +
    + {#each items as item} +
  • + +
  • + {/each} +
+ +
+``` + + +### <svelte:self> + +--- + +The `` element allows a component to include itself, recursively. + +It cannot appear at the top level of your markup; it must be inside an if or each block to prevent an infinite loop. + +```html + + +{#if count > 0} +

counting down... {count}

+ +{:else} +

lift-off!

+{/if} +``` + +### <svelte:component> + +* `` + +--- + +The `` element renders a component dynamically, using the component constructor specified as the `this` property. When the property changes, the component is destroyed and recreated. + +If `this` is falsy, no component is rendered. + +```html + +``` + + +### <svelte:window> + +* `` +* `` + +--- + +The `` element allows you to add event listeners to the `window` object without worrying about removing them when the component is destroyed, or checking for the existence of `window` when server-side rendering. + +```html + + + +``` + +--- + +You can also bind to the following properties: + +* `innerWidth` +* `innerHeight` +* `outerWidth` +* `outerHeight` +* `scrollX` +* `scrollY` +* `online` — an alias for window.navigator.onLine + +All except `scrollX` and `scrollY` are readonly. + +```html + +``` + + +### <svelte:body> + +* `` + +--- + +As with ``, this element allows you to add listeners to events on `document.body`, such as `mouseenter` and `mouseleave` which don't fire on `window`. + +```html + +``` + + +### <svelte:head> + +* `` + +--- + +This element makes it possible to insert elements into `document.head`. During server-side rendering, `head` content exposed separately to the main `html` content. + +```html + + + +``` + + +### <svelte:options> + +* `` + +--- + +The `` element provides a place to specify per-component compiler options, which are detailed in the next section. The possible options are: + +* `immutable={true}` — you never use mutable data, so the compiler can do simple referential equality checks to determine if values have changed +* `immutable={false}` — the default. Svelte will be more conservative about whether or not mutable objects have changed +* `namespace="..."` — the namespace where this component will be used, most commonly "svg" +* `tag="..."` — the name to use when compiling this component as a custom element + +```html + +``` \ No newline at end of file