-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.qmd
54 lines (40 loc) · 1.49 KB
/
example.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
---
title: Sverto example
date: last-modified
filters:
- sverto
sverto:
use:
- Circles.svelte
---
This document shows you how to use a basic example Svelte component called `Circles.svelte`.
Create one using `fileName.default()`, where `fileName` is the file name without `.svelte`:
```{ojs}
myCircles = new Circles.default({
target: document.querySelector("#mycircles")
});
```
It will appear in a [div](https://quarto.org/docs/authoring/markdown-basics.html#divs-and-spans) called `#mycircles`, which I've added directly below:
:::{#mycircles}
:::
Now the fun part - updating our visual when our document changes. It's as simple as changing the prop using `componentName.propName =`.
For example, here are three datasets and an [Observable Inputs](https://observablehq.com/documentation/inputs/overview) dropdown menu that lets you select one of three datasets:
```{ojs}
// here are some datasets...
allDatasets = new Map([
["Dataset A", [5, 15, 25, 17, 8]],
["Dataset B", [25, 5, 5]],
["Dataset C", [12, 5, 8, 21, 5]]
]);
```
```{ojs}
viewof selectedDataset =
Inputs.select(allDatasets, { label: "Selected dataset" });
```
Now we can update the prop `data` of the visual `myCircles` using:
```{ojs}
//| output: false
myCircles.data = selectedDataset
```
And there you go! 🚀
For more help writing Svelte components, check out the [Svelte tutorial](https://svelte.dev/tutorial/basics) or take look at some of the examples in the [Sverto documentation](https://sverto.jamesgoldie.dev).