From b661bb0a7c9b9282ff7f0cef473d3cd5e05ab7ca Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Thu, 14 Mar 2019 22:32:58 -0400 Subject: [PATCH] start documenting client-side component API --- site/content/docs/04-run-time.md | 121 +++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/site/content/docs/04-run-time.md b/site/content/docs/04-run-time.md index 09d8382eaf..809e34d41f 100644 --- a/site/content/docs/04-run-time.md +++ b/site/content/docs/04-run-time.md @@ -2,45 +2,152 @@ title: Run time --- -TODO ### Client-side component API -* `const component = new Component(options);` -* `component.$set(...);` -* `component.$on(event, callback);` -* `component.$destroy();` -* `component.x` if `accessors: true` +* `const component = new Component(options)` + +--- + +A client-side component — that is, a component compiled with `generate: 'dom'` (or the `generate` option left unspecified) is a JavaScript class. + +The following initialisation options can be provided: + +* `target` (`HTMLElement`, **required**) — the parent DOM node +* `anchor` (`HTMLElement`, default `null`) — the next sibling DOM node. Must be a child of `target` +* `props` (`Object`, default `{}`) — an object of properties to supply to the component +* `hydrate` (`boolean`, default `false`) — see below +* `intro` (`boolean`, default `false`) — whether to play in transitions on initial render + +Existing children of `target` are left where they are. + + +```js +import App from './App.svelte'; + +const app = new App({ + target: document.body, + props: { + // assuming App.svelte contains something like + // `export let answer`: + answer: 42 + } +}); +``` + +--- + +The `hydrate` option instructs Svelte to upgrade existing DOM (usually from server-side rendering) rather than creating new elements. It will only work if the component was compiled with the `hydratable: true` option. + +Whereas children of `target` are normally left alone, `hydrate: true` will cause any children to be removed. For that reason, the `anchor` option cannot be used alongside `hydrate: true`. + +The existing DOM doesn't need to match the component — Svelte will 'repair' the DOM as it goes. + +```js +import App from './App.svelte'; + +const app = new App({ + target: document.querySelector('#server-rendered-html'), + hydrate: true +}); +``` + +* `component.$set(props)` + +--- + +Programmatically sets props on an instance. `component.$set({ x: 1 })` is equivalent to `x = 1` inside the component's `