Skip to content

Commit a07eac4

Browse files
authored
update API reference docs (#2206)
1 parent a824a6d commit a07eac4

File tree

102 files changed

+2364
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2364
-459
lines changed

site/content/docs/00-introduction.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Before we begin
3+
---
4+
5+
> Temporary note: This document is a work-in-progress. Please forgive any missing or misleading parts, and don't be shy about asking for help in the [Discord chatroom](https://discord.gg/yy75DKs). The [tutorial](tutorial) is more complete; start there.
6+
7+
This page contains detailed API reference documentation. It's intended to be a resource for people who already have some familiarity with Svelte.
8+
9+
If that's not you (yet), you may prefer to visit the [interactive tutorial](tutorial) or the [examples](examples) before consulting this reference.
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
---
2+
title: Component format
3+
---
4+
5+
---
6+
7+
Components are the building blocks of Svelte applications. They are written into `.svelte` files, using a superset of HTML.
8+
9+
All three sections — script, styles and markup — are optional.
10+
11+
```html
12+
<script>
13+
// logic goes here
14+
</script>
15+
16+
<style>
17+
/* styles go here */
18+
</style>
19+
20+
<!-- markup (zero or more items) goes here -->
21+
```
22+
23+
### &lt;script&gt;
24+
25+
A `<script>` block contains JavaScript that runs when a component instance is created. Variables declared (or imported) at the top level are 'visible' from the component's markup. There are four additional rules:
26+
27+
#### 1. `export` creates a component prop
28+
29+
---
30+
31+
Svelte uses the `export` keyword to mark a variable declaration as a *property* or *prop*, which means it becomes accessible to consumers of the component:
32+
33+
```html
34+
<script>
35+
// these properties can be set externally
36+
export let foo;
37+
export let bar = 'optional default value';
38+
39+
// Values that are passed in as props
40+
// are immediately available
41+
console.log(foo, bar);
42+
43+
// function declarations cannot be set externally,
44+
// but can be accessed from outside
45+
export function instanceMethod() {
46+
alert(foo);
47+
}
48+
</script>
49+
```
50+
51+
#### 2. Assignments are 'reactive'
52+
53+
---
54+
55+
To change component state and trigger a re-render, just assign to a locally declared variable.
56+
57+
Update expressions (`count += 1`) and property assignments (`obj.x = y`) have the same effect.
58+
59+
```html
60+
<script>
61+
let count = 0;
62+
63+
function handleClick () {
64+
// calling this function will trigger a re-render
65+
// if the markup references `count`
66+
count = count + 1;
67+
}
68+
</script>
69+
```
70+
71+
#### 3. `$:` marks a statement as reactive
72+
73+
---
74+
75+
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.
76+
77+
```html
78+
<script>
79+
export let title;
80+
81+
// this will update `document.title` whenever
82+
// the `title` prop changes
83+
$: document.title = title;
84+
85+
$: {
86+
console.log(`multiple statements can be combined`);
87+
console.log(`the current title is ${title}`);
88+
}
89+
</script>
90+
```
91+
92+
---
93+
94+
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf.
95+
96+
```html
97+
<script>
98+
export let num;
99+
100+
// we don't need to declare `squared` and `cubed`
101+
// — Svelte does it for us
102+
$: squared = num * num;
103+
$: cubed = squared * num;
104+
</script>
105+
```
106+
107+
#### 4. Prefix stores with `$` to access their values
108+
109+
---
110+
111+
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.
112+
113+
Note that the store must be declared at the top level of the component — not inside an `if` block or a function, for example.
114+
115+
Local variables (that do not represent store values) must *not* have a `$` prefix.
116+
117+
```html
118+
<script>
119+
import { writable } from 'svelte/store';
120+
121+
const count = writable(0);
122+
console.log($count); // logs 0
123+
124+
count.set(1);
125+
console.log($count); // logs 1
126+
</script>
127+
```
128+
129+
130+
### &lt;script context="module"&gt;
131+
132+
---
133+
134+
A `<script>` tag with a `context="module"` attribute runs once when the module first evaluates, rather than for each component instance. Values declared in this block are accessible from a regular `<script>` (and the component markup) but not vice versa.
135+
136+
You can `export` bindings from this block, and they will become exports of the compiled module.
137+
138+
You cannot `export default`, since the default export is the component itself.
139+
140+
```html
141+
<script context="module">
142+
let totalComponents = 0;
143+
144+
// this allows an importer to do e.g.
145+
// `import Example, { alertTotal } from './Example.svelte'`
146+
export function alertTotal() {
147+
alert(totalComponents);
148+
}
149+
</script>
150+
151+
<script>
152+
totalComponents += 1;
153+
console.log(`total number of times this component has been created: ${totalComponents}`);
154+
</script>
155+
```
156+
157+
158+
### &lt;style&gt;
159+
160+
---
161+
162+
CSS inside a `<style>` block will be scoped to that component.
163+
164+
This works by adding a class to affected elements, which is based on a hash of the component styles (e.g. `svelte-123xyz`).
165+
166+
```html
167+
<style>
168+
p {
169+
/* this will only affect <p> elements in this component */
170+
color: burlywood;
171+
}
172+
</style>
173+
```
174+
175+
---
176+
177+
To apply styles to a selector globally, use the `:global(...)` modifier.
178+
179+
```html
180+
<style>
181+
:global(body) {
182+
/* this will apply to <body> */
183+
margin: 0;
184+
}
185+
186+
div :global(strong) {
187+
/* this will apply to all <strong> elements, in any
188+
component, that are inside <div> elements belonging
189+
to this component */
190+
color: goldenrod;
191+
}
192+
</style>
193+
```

0 commit comments

Comments
 (0)