diff --git a/site/content/docs/04-run-time.md b/site/content/docs/03-run-time.md similarity index 89% rename from site/content/docs/04-run-time.md rename to site/content/docs/03-run-time.md index ef60ae11f2..5cae811f75 100644 --- a/site/content/docs/04-run-time.md +++ b/site/content/docs/03-run-time.md @@ -3,138 +3,18 @@ title: Run time --- -### Client-side component API - -* `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. - -```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 following initialisation options can be provided: - -| option | default | description | -| --- | --- | --- | -| `target` | **none** | An `HTMLElement` to render to. This option is required -| `anchor` | `null` | A child of `target` to render the component immediately before -| `props` | `{}` | An object of proeprties to supply to the component -| `hydrate` | `false` | See below -| `intro` | `false` | If `true`, will play transitions on initial render, rather than waiting for subsequent state changes - -Existing children of `target` are left where they are. - - ---- - -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 ` ``` -* `beforeUpdate(() => void)` +* `beforeUpdate(callback: () => void)` --- @@ -182,7 +62,7 @@ Schedules a callback to run immediately before the component is updated after an ``` -* `afterUpdate(() => void)` +* `afterUpdate(callback: () => void)` --- @@ -198,7 +78,7 @@ Schedules a callback to run immediately after the component has been updated. ``` -* `onDestroy(() => void)` +* `onDestroy(callback: () => void)` --- @@ -326,8 +206,25 @@ unsubscribe(); // logs 'no more subscribers' ``` * `store = readable((set: (value: any) => void) => () => void)` +* `store = readable((set: (value: any) => void) => () => void, value: any)` -TODO +--- + +Creates a store whose value cannot be set from 'outside'. Instead, the function passed to `readable`, which is called when the subscriber count goes from zero to one, must call the provided `set` value. It must return a function that is called when the subscriber count goes from one to zero. + +If a second argument is provided, it becomes the store's initial value. + +```js +import { readable } from 'svelte/store'; + +const time = readable(set => { + const interval = setInterval(() => { + set(new Date()); + }, 1000); + + return () => clearInterval(interval); +}, new Date()); +``` * `store = derive(a, (a: any) => any)` * `store = derive(a, (a: any, set: (value: any) => void) => void)` @@ -368,4 +265,124 @@ TODO ### svelte/register -TODO \ No newline at end of file +TODO + + +### Client-side component API + +* `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. + +```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 following initialisation options can be provided: + +| option | default | description | +| --- | --- | --- | +| `target` | **none** | An `HTMLElement` to render to. This option is required +| `anchor` | `null` | A child of `target` to render the component immediately before +| `props` | `{}` | An object of proeprties to supply to the component +| `hydrate` | `false` | See below +| `intro` | `false` | If `true`, will play transitions on initial render, rather than waiting for subsequent state changes + +Existing children of `target` are left where they are. + + +--- + +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 `