You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/README.md
+27-31
Original file line number
Diff line number
Diff line change
@@ -4,50 +4,46 @@ title: Adding parameters
4
4
5
5
Like transitions and animations, an action can take an argument, which the action function will be called with alongside the element it belongs to.
6
6
7
-
Here, we're using a `longpress` action that fires an event with the same name whenever the user presses and holds the button for a given duration. Right now, if you switch over to `longpress.js`, you'll see it's hardcoded to 500ms.
7
+
In this exercise, we want to add a tooltip to the `<button>` using the [`Tippy.js`](https://atomiks.github.io/tippyjs/) library. The action is already wired up with `use:tooltip`, but if you hover over the button (or focus it with the keyboard) the tooltip contains no content.
8
8
9
-
We can change the action function to accept a `duration` as a second argument, and pass that `duration`to the `setTimeout` call:
9
+
First, the action needs to accept some options and pass them to Tippy:
10
10
11
11
```js
12
-
/// file: longpress.js
13
-
exportfunctionlongpress(node, +++duration+++) {
14
-
// ...
15
-
16
-
consthandleMousedown= () => {
17
-
timer =setTimeout(() => {
18
-
node.dispatchEvent(newCustomEvent('longpress'));
19
-
}, +++duration+++);
20
-
};
12
+
/// file: App.svelte
13
+
functiontooltip(node, +++options+++) {
14
+
consttooltip=tippy(node, +++options+++);
21
15
22
-
// ...
16
+
return {
17
+
destroy() {
18
+
tooltip.destroy();
19
+
}
20
+
};
23
21
}
24
22
```
25
23
26
-
Back in `App.svelte`, we can pass the `duration` value to the action:
24
+
Then, we need to pass some options into the action:
This _almost_ works — the event now only fires after 2 seconds. But if you slide the duration down, it will still take two seconds.
40
-
41
-
To change that, we can add an `update` method in `longpress.js`. This will be called whenever the argument changes:
33
+
The tooltip now works — almost. If we change the text in the `<input>`, the tooltip will not reflect the new content. We can fix that by adding an `update` method to the returned object.
42
34
43
35
```js
44
-
/// file: longpress.js
45
-
return {
46
-
update(newDuration) {
47
-
duration = newDuration;
48
-
},
49
-
// ...
50
-
};
36
+
/// file: App.svelte
37
+
functiontooltip(node, options) {
38
+
consttooltip=tippy(node, options);
39
+
40
+
return {
41
+
+++update(options) {
42
+
tooltip.setProps(options);
43
+
},+++
44
+
destroy() {
45
+
tooltip.destroy();
46
+
}
47
+
};
48
+
}
51
49
```
52
-
53
-
> If you need to pass multiple arguments to an action, combine them into a single object, as in `use:longpress={{duration, spiciness}}`
Copy file name to clipboardExpand all lines: content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/app-a/node_modules/.package-lock.json
Copy file name to clipboardExpand all lines: content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/app-a/node_modules/@popperjs/core/LICENSE.md
0 commit comments