Skip to content

Commit 0320d77

Browse files
Rich-HarrisRich Harris
and
Rich Harris
authored
Tippy (#327)
* add tippy * remove some stuff * remove some junk * complete exercise --------- Co-authored-by: Rich Harris <[email protected]>
1 parent c8f87fa commit 0320d77

Some content is hidden

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

85 files changed

+5750
-132
lines changed

content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/README.md

+27-31
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,46 @@ title: Adding parameters
44

55
Like transitions and animations, an action can take an argument, which the action function will be called with alongside the element it belongs to.
66

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.
88

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:
1010

1111
```js
12-
/// file: longpress.js
13-
export function longpress(node, +++duration+++) {
14-
// ...
15-
16-
const handleMousedown = () => {
17-
timer = setTimeout(() => {
18-
node.dispatchEvent(new CustomEvent('longpress'));
19-
}, +++duration+++);
20-
};
12+
/// file: App.svelte
13+
function tooltip(node, +++options+++) {
14+
const tooltip = tippy(node, +++options+++);
2115

22-
// ...
16+
return {
17+
destroy() {
18+
tooltip.destroy();
19+
}
20+
};
2321
}
2422
```
2523

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:
2725

2826
```svelte
2927
/// file: App.svelte
30-
<button
31-
use:longpress+++={duration}+++
32-
on:longpress={() => (pressed = true)}
33-
on:mouseenter={() => (pressed = false)}
34-
>
35-
press and hold
28+
<button use:tooltip+++={{ content, theme: 'material' }}+++>
29+
Hover me
3630
</button>
3731
```
3832

39-
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.
4234

4335
```js
44-
/// file: longpress.js
45-
return {
46-
update(newDuration) {
47-
duration = newDuration;
48-
},
49-
// ...
50-
};
36+
/// file: App.svelte
37+
function tooltip(node, options) {
38+
const tooltip = tippy(node, options);
39+
40+
return {
41+
+++ update(options) {
42+
tooltip.setProps(options);
43+
},+++
44+
destroy() {
45+
tooltip.destroy();
46+
}
47+
};
48+
}
5149
```
52-
53-
> If you need to pass multiple arguments to an action, combine them into a single object, as in `use:longpress={{duration, spiciness}}`

content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/app-a/node_modules/.package-lock.json

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/app-a/node_modules/@popperjs/core/LICENSE.md

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)