Skip to content

Commit bd0157a

Browse files
committed
add Attachment interface
1 parent a7aa1a7 commit bd0157a

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

documentation/docs/03-template-syntax/[email protected]

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Attachments are functions that run when an element is mounted to the DOM. Option
99
1010
```svelte
1111
<script>
12-
function myAttachment(node) {
13-
console.log(node.nodeName); // 'DIV'
12+
/** @type {import('svelte/attachments').Attachment} */
13+
function myAttachment(element) {
14+
console.log(element.nodeName); // 'DIV'
1415
1516
return () => {
1617
console.log('cleaning up');
@@ -33,9 +34,13 @@ A useful pattern is for a function, such as `tooltip` in this example, to _retur
3334
3435
let content = $state('Hello!');
3536
37+
/**
38+
* @param {string} content
39+
* @returns {import('svelte/attachments').Attachment}
40+
*/
3641
function tooltip(content) {
37-
return (node) => {
38-
const tooltip = tippy(node, { content });
42+
return (element) => {
43+
const tooltip = tippy(element, { content });
3944
return tooltip.destroy;
4045
};
4146
}
@@ -106,9 +111,13 @@ This allows you to create _wrapper components_ that augment elements ([demo](/pl
106111
107112
let content = $state('Hello!');
108113
114+
/**
115+
* @param {string} content
116+
* @returns {import('svelte/attachments').Attachment}
117+
*/
109118
function tooltip(content) {
110-
return (node) => {
111-
const tooltip = tippy(node, { content });
119+
return (element) => {
120+
const tooltip = tippy(element, { content });
112121
return tooltip.destroy;
113122
};
114123
}

packages/svelte/scripts/generate-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ await createBundle({
3535
[pkg.name]: `${dir}/src/index.d.ts`,
3636
[`${pkg.name}/action`]: `${dir}/src/action/public.d.ts`,
3737
[`${pkg.name}/animate`]: `${dir}/src/animate/public.d.ts`,
38-
[`${pkg.name}/attachments`]: `${dir}/src/attachments/index.js`,
38+
[`${pkg.name}/attachments`]: `${dir}/src/attachments/public.d.ts`,
3939
[`${pkg.name}/compiler`]: `${dir}/src/compiler/public.d.ts`,
4040
[`${pkg.name}/easing`]: `${dir}/src/easing/index.js`,
4141
[`${pkg.name}/legacy`]: `${dir}/src/legacy/legacy-client.js`,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface Attachment {
2+
(element: Element): void | (() => void);
3+
}
4+
5+
export * from './index.js';

packages/svelte/types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ declare module 'svelte/animate' {
625625
}
626626

627627
declare module 'svelte/attachments' {
628+
export interface Attachment {
629+
(element: Element): void | (() => void);
630+
}
628631
/**
629632
* Creates an object key that will be recognised as an attachment when the object is spread onto an element,
630633
* as a programmatic alternative to using `{@attach ...}`. This can be useful for library authors, though

0 commit comments

Comments
 (0)