Skip to content

Commit 62fb4df

Browse files
author
James Goldie
committed
Merge branch 'dev' of https://github.com/jimjam-slam/sverto into dev
2 parents 7ba1ac7 + a29d946 commit 62fb4df

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

.github/workflows/check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jobs:
2626

2727
- name: Install Quarto
2828
uses: quarto-dev/quarto-actions/setup@v2
29+
with:
30+
version: 1.5.25
2931

3032
- name: Install Node
3133
uses: actions/setup-node@v4

README.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,51 @@ This will add the extension itself (which includes some project scripts) to the
3939
4040
## 🎉 Use
4141

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!
4377

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
4579

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

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

58-
**To see this all in practice, check out [`example.qmd`](./example.qmd).**
84+
````js
85+
myChart.chartData = myData.filter(d => d.year == selectedYear)
86+
````
5987

6088
> **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.
6189
>

0 commit comments

Comments
 (0)