Skip to content

Commit 6f10dee

Browse files
Sync svelte docs (#1342)
sync svelte docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com>
1 parent 239094c commit 6f10dee

File tree

10 files changed

+224
-1
lines changed

10 files changed

+224
-1
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
3+
title: {@attach ...}
4+
---
5+
6+
Attachments are functions that run when an element is mounted to the DOM. Optionally, they can return a function that is called when the element is later removed from the DOM.
7+
8+
> [!NOTE]
9+
> Attachments are available in Svelte 5.29 and newer.
10+
11+
```svelte
12+
<!--- file: App.svelte --->
13+
<script>
14+
/** @type {import('svelte/attachments').Attachment} */
15+
function myAttachment(element) {
16+
console.log(element.nodeName); // 'DIV'
17+
18+
return () => {
19+
console.log('cleaning up');
20+
};
21+
}
22+
</script>
23+
24+
<div {@attach myAttachment}>...</div>
25+
```
26+
27+
An element can have any number of attachments.
28+
29+
## Attachment factories
30+
31+
A useful pattern is for a function, such as `tooltip` in this example, to _return_ an attachment ([demo](/playground/untitled#H4sIAAAAAAAAE3VT0XLaMBD8lavbDiaNCUlbHhTItG_5h5AH2T5ArdBppDOEMv73SkbGJGnH47F9t3un3TsfMyO3mInsh2SW1Sa7zlZKo8_E0zHjg42pGAjxBPxp7cTvUHOMldLjv-IVGUbDoUw295VTlh-WZslqa8kxsLL2ACtHWxh175NffnQfAAGikSGxYQGfPEvGfPSIWtOH0TiBVo2pWJEBJtKhQp4YYzjG9JIdcuMM5IZqHMPioY8vOSA997zQoevf4a7heO7cdp34olRiTGr07OhwH1IdoO2A7dLMbwahZq6MbRhKZWqxk7rBxTGVbuHmhCgb5qDgmIx_J6XtHHukHTrYYqx_YpzYng8aO4RYayql7hU-1ZJl0akqHBE_D9KLolwL-Dibzc7iSln9XjtqTF1UpMkJ2EmXR-BgQErsN4pxIJKr0RVO1qrxAqaTO4fbc9bKulZm3cfDY3aZDgvFGErWjmzhN7KmfX5rXyDeX8Pt1mU-hXjdBOrtuB97vK4GPUtmJ41XcRMEGDLD8do0nJ73zhUhSlyRw0t3vPqD8cjfLs-axiFgNBrkUd9Ulp50c-GLxlXAVlJX-ffpZyiSn7H0eLCUySZQcQdXlxj4El0Yv_FZvIKElqqGTruVLhzu7VRKCh22_5toOyxsWqLwwzK-cCbYNdg-hy-p9D7sbiZWUnts_wLUOF3CJgQAAA==)):
32+
33+
```svelte
34+
<!--- file: App.svelte --->
35+
<script>
36+
import tippy from 'tippy.js';
37+
38+
let content = $state('Hello!');
39+
40+
/**
41+
* @param {string} content
42+
* @returns {import('svelte/attachments').Attachment}
43+
*/
44+
function tooltip(content) {
45+
return (element) => {
46+
const tooltip = tippy(element, { content });
47+
return tooltip.destroy;
48+
};
49+
}
50+
</script>
51+
52+
<input bind:value={content} />
53+
54+
<button {@attach tooltip(content)}>
55+
Hover me
56+
</button>
57+
```
58+
59+
Since the `tooltip(content)` expression runs inside an [effect]($effect), the attachment will be destroyed and recreated whenever `content` changes.
60+
61+
## Inline attachments
62+
63+
Attachments can also be created inline ([demo](/playground/untitled#H4sIAAAAAAAAE71WbW_aSBD-Kyt0VaBJyGKbqoUkOhdI68qGUkh6pPSDMY6xYwyH12Ab8d9vZtYE6DX38aQQe3fennlm1jvbUmTP3VKj9KcthO3MShelJz9041Ljx7YksiWKcAP2C0V9uazGazcUuDexY_d3-84iEm4kwE3pOnZW_lLcjqOx8OfLxUqwLVvafiTYjj2tFnN2Vr3yVvbUB4NqEJ81x9H11cEounbsaG3HaL_xp2J2s1WVHa5mru_NxMtyW6TAytKgwm5u2RYlYwF4YsEIVSrYDZMaVc8VLblXPlOmZ5UmxkP9P9ynJ9cR5fKxk7EIXQGQIV9wsXL_TtxY6JE_t4W_iO5wv_yURA6uWLhYLMuicrAdi_-2RAMCUGgTReUC8gUTB9mueC2WK1ckq4j9AhVytiPHDX_Fh_-PX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HzMcdZTj1J_d3B_JVRjvnopCv5wD7RVdLDPqG4kscTTpQNfvPgbI3g_f-pS4--a3TGUynH_hvJb9QpDzXJg3fo3eyld1Xq3YHjmbn23lTh7sm1m3Gpwur8Df2umMq16vtlyqLF5cpdurb4zb12Gfu522Dv-HruR_IWpQGmuDdhGMILvNQQq8TdXbwyVB3NP6dT1angaKxyUxqlXuaNf40L8qKWg8-W0XV9weQdDYPXzX4YqsprvXlQpru5Dbf0kRIMSsZ-u8wvGPydeNxPTk-LFSvjlLQEY96Ex_XBXxWv_mroRp6Yoej8hmmV0wnNB7MlEK81j3dT2PXZGxnyRJKBpOyDAYkq7Pb2FsLupzips3KnoPVOY-esXFPes7csrewtYA8Eb5lli1k19qOyAAkMMLxyEsZbuW70i5MMnRR8HntxFvErXiZhguMfmL8gPOXmB3DC-E8aEafNVzVqqEGQXtdRUAcDvq6ioopSr-97tugAqvcyOar3iy3VnZLanbb1T1jZfrjxo2mp8WSHsbv7Bx1mHBBZDAAA)):
64+
65+
```svelte
66+
<!--- file: App.svelte --->
67+
<script>
68+
import { paint } from './gradient.js';
69+
</script>
70+
71+
<canvas
72+
width={32}
73+
height={32}
74+
{@attach (canvas) => {
75+
const context = canvas.getContext('2d');
76+
77+
$effect(() => {
78+
let frame = requestAnimationFrame(function loop(t) {
79+
frame = requestAnimationFrame(loop);
80+
paint(context, t);
81+
});
82+
83+
return () => {
84+
cancelAnimationFrame(frame);
85+
};
86+
});
87+
}}
88+
></canvas>
89+
```
90+
91+
## Passing attachments to components
92+
93+
When used on a component, `{@attach ...}` will create a prop whose key is a [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol). If the component then [spreads](/tutorial/svelte/spread-props) props onto an element, the element will receive those attachments.
94+
95+
This allows you to create _wrapper components_ that augment elements ([demo](/playground/untitled#H4sIAAAAAAAAE3VUS3ObMBD-KxvajnFqsJM2PhA7TXrKob31FjITAbKtRkiMtDhJPfz3LiAMdpxhGJvdb1_fPnaeYjn3Iu-WIbJ04028lZDcetHDzsO3olbVApI74F1RhHbLJdayhFl-Sp5qhVwhufEWNjWiwJtYxSjyQhsEFEXxBiujcxg1_8O_dnQ9APwsEbVyiHDafjrvDZCgkiO4MLCEzxYZcn90z6XUZ6OxA61KlaIgV6i1pFC-sxjDrlbHaDiWRoGvdMbHsLzp5DES0mJnRxGaRBvcBHb7yFUTCQeunEWYcYtGv12TqgFUDbCK1WLaM6IWQhUlQiJUFm2ZLPly51xXMG0Rjoyd69C7UqqG2nu95QZyXvtvLVpri2-SN4hoLXXCZFfhQ8aQBU1VgdEaH_vSgyBZR_BpPp_vi0tY-rw2ulRZkGqpTQRbZvwa2BPgFC8bgbw31CbjJjAsE6WNYBZeGp7vtQXLMqHWnZx-5kM1TR5ycpkZXQR2wzL94l8Ur1C_3-g168SfQf1MyfRi3LW9fs77emJEw5QV9SREoLTq06tcczq7d6xEUcJX2vAhO1b843XK34e5unZEMBr15ekuKEusluWAF8lXhE2ZTP2r2RcIHJ-163FPKerCgYJLOB9i4GvNwviI5-gAQiFFBk3tBTOU3HFXEk0R8o86WvUD64aINhv5K3oRmpJXkw8uxMG6Hh6JY9X7OwGSqfUy9tDG3sHNoEi0d_d_fv9qndxRU0VClFqo3KVo3U655Hnt1PXB3Qra2Y2QGdEwgTAMCxopsoxOe6SD0gD8movDhT0LAnhqlE8gVCpLWnRoV7OJCkFAwEXitrYL1W7p7pbiE_P7XH6E_rihODm5s52XtiH9Ekaw0VgI9exadWL1uoEYjPtg2672k5szsxbKyWB2fdT0w5Y_0hcT8oXOlRetmLS8-g-6TLXXQgYAAA==)):
96+
97+
```svelte
98+
<!--- file: Button.svelte --->
99+
<script>
100+
/** @type {import('svelte/elements').HTMLButtonAttributes} */
101+
let { children, ...props } = $props();
102+
</script>
103+
104+
<!-- `props` includes attachments -->
105+
<button {...props}>
106+
{@render children?.()}
107+
</button>
108+
```
109+
110+
```svelte
111+
<!--- file: App.svelte --->
112+
<script>
113+
import tippy from 'tippy.js';
114+
import Button from './Button.svelte';
115+
116+
let content = $state('Hello!');
117+
118+
/**
119+
* @param {string} content
120+
* @returns {import('svelte/attachments').Attachment}
121+
*/
122+
function tooltip(content) {
123+
return (element) => {
124+
const tooltip = tippy(element, { content });
125+
return tooltip.destroy;
126+
};
127+
}
128+
</script>
129+
130+
<input bind:value={content} />
131+
132+
<Button {@attach tooltip(content)}>
133+
Hover me
134+
</Button>
135+
```
136+
137+
## Creating attachments programmatically
138+
139+
To add attachments to an object that will be spread onto a component or element, use [`createAttachmentKey`](svelte-attachments#createAttachmentKey).

apps/svelte.dev/content/docs/svelte/03-template-syntax/12-use.md renamed to apps/svelte.dev/content/docs/svelte/03-template-syntax/13-use.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-doc
33
title: use:
44
---
55

6+
> [!NOTE]
7+
> In Svelte 5.29 and newer, consider using [attachments](@attach) instead, as they are more flexible and composable.
8+
69
Actions are functions that are called when an element is mounted. They are added with the `use:` directive, and will typically use an `$effect` so that they can reset any state when the element is unmounted:
710

811
```svelte
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
3+
title: svelte/attachments
4+
---
5+
6+
7+
8+
```js
9+
// @noErrors
10+
import { createAttachmentKey } from 'svelte/attachments';
11+
```
12+
13+
## createAttachmentKey
14+
15+
<blockquote class="since note">
16+
17+
Available since 5.29
18+
19+
</blockquote>
20+
21+
Creates an object key that will be recognised as an attachment when the object is spread onto an element,
22+
as a programmatic alternative to using `{@attach ...}`. This can be useful for library authors, though
23+
is generally not needed when building an app.
24+
25+
```svelte
26+
<script>
27+
import { createAttachmentKey } from 'svelte/attachments';
28+
29+
const props = {
30+
class: 'cool',
31+
onclick: () => alert('clicked'),
32+
[createAttachmentKey()]: (node) => {
33+
node.textContent = 'attached!';
34+
}
35+
};
36+
</script>
37+
38+
<button {...props}>click me</button>
39+
```
40+
41+
<div class="ts-block">
42+
43+
```dts
44+
function createAttachmentKey(): symbol;
45+
```
46+
47+
</div>
48+
49+
50+
51+
## Attachment
52+
53+
An [attachment](/docs/svelte/@attach) is a function that runs when an element is mounted
54+
to the DOM, and optionally returns a function that is called when the element is later removed.
55+
56+
It can be attached to an element with an `{@attach ...}` tag, or by spreading an object containing
57+
a property created with [`createAttachmentKey`](/docs/svelte/svelte-attachments#createAttachmentKey).
58+
59+
<div class="ts-block">
60+
61+
```dts
62+
interface Attachment<T extends EventTarget = Element> {/*…*/}
63+
```
64+
65+
<div class="ts-block-property">
66+
67+
```dts
68+
(element: T): void | (() => void);
69+
```
70+
71+
<div class="ts-block-property-details"></div>
72+
</div></div>
73+
74+

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-compiler.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ namespace AST {
306306
});
307307
}
308308
309+
/** A `{@attach foo(...)} tag */
310+
export interface AttachTag extends BaseNode {
311+
type: 'AttachTag';
312+
expression: Expression;
313+
}
314+
309315
/** An `animate:` directive */
310316
export interface AnimateDirective extends BaseNode {
311317
type: 'AnimateDirective';
@@ -399,7 +405,7 @@ namespace AST {
399405
interface BaseElement extends BaseNode {
400406
name: string;
401407
attributes: Array<
402-
Attribute | SpreadAttribute | Directive
408+
Attribute | SpreadAttribute | Directive | AttachTag
403409
>;
404410
fragment: Fragment;
405411
}
@@ -601,6 +607,7 @@ namespace AST {
601607
| AST.Attribute
602608
| AST.SpreadAttribute
603609
| Directive
610+
| AST.AttachTag
604611
| AST.Comment
605612
| Block;
606613

0 commit comments

Comments
 (0)