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: README.md
+41-13Lines changed: 41 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,23 +39,51 @@ This will add the extension itself (which includes some project scripts) to the
39
39
40
40
## 🎉 Use
41
41
42
-
Here's the short way to add Svelte component you've written to a Quarto doc:
42
+
### Step 1: add Svelte to your document
43
+
44
+
In the document frontmatter, add `sverto` to `filters`, and add one or more `.svelte` files to `sverto.use`:
45
+
46
+
```yaml
47
+
---
48
+
title: "My document"
49
+
filters: ["sverto"]
50
+
sverto:
51
+
use:
52
+
example.svelte
53
+
---
54
+
```
55
+
56
+
### Step 2: bring your Svelte component to life
57
+
58
+
Use an [Observable JS](https://quarto.org/docs/interactive/ojs/) chunk to _instantiate_ your Svelte component.
59
+
60
+
````js
61
+
```{ojs}
62
+
myChart = new example.default({
63
+
target: document.querySelector("#chart")
64
+
props: {
65
+
chartData: {}
66
+
}
67
+
})
68
+
```
69
+
70
+
:::{#chart}
71
+
:::
72
+
````
73
+
74
+
- the `target` is where it will appear. This needs to be an existing part of the document — you can put a [Pandoc div](https://quarto.org/docs/authoring/markdown-basics.html#divs-and-spans) right after this code, or put one anywhere else on the page
75
+
-`example` is the file name of your Svelte component, without the file extension
76
+
- if your Svelte component has any `props`, add default values here too. Don't put reactive OJS code in here; we'll update the props separately!
43
77
44
-
1. Add a magic placeholder block to your document with a [Quarto include](https://quarto.org/docs/authoring/includes.html) to the path to your Quarto doc, prefixed with `/.sverto/`. For example:
78
+
### Step 3: make your visual reactive
45
79
46
-
```
47
-
:::{}
48
-
{{< include /.sverto/example.qmd >}}
49
-
:::
50
-
```
80
+
If your visual has `props` that allow it to change or transition in response to other OJS code, you can update it by assigning the prop directly.
51
81
52
-
2. Import your Svelte component in OJS with `Component = import_svelte("Component.svelte")`
53
-
3. Add a target block for your visual using `:::` and give it an `#id`
54
-
4. Instantiate the Svelte component with `myVisual = Component.default()` using some default props and your target block
55
-
5. Update the instantiated component with `myVisual.propName`
56
-
6. Render your Quarto website as usual with `quarto render` or `quarto preview`.
82
+
For example, if we have a dataset called `myData` in OJS, and a year slider called `selectedYear`, we can change a prop called `chartData` whenever the user selects a new year like:
57
83
58
-
**To see this all in practice, check out [`example.qmd`](./example.qmd).**
> **Note:**`quarto preview` won't "live reload" when you modify your Svelte component—but if you modify and save the Quarto doc that imports it, that will trigger a re-render. You may need to hard reload the page in your browser to see the updated Svelte component.
0 commit comments