Skip to content

Commit 06f4ac7

Browse files
author
James Goldie
committed
Further update docs and example template
1 parent 71e5f45 commit 06f4ac7

File tree

3 files changed

+30
-100
lines changed

3 files changed

+30
-100
lines changed

NEWS.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
## (Unreleased) Sverto 1.0.0
2+
3+
- Significant refactor of Sverto makes it easier to use and more compatible with Quarto's other features
4+
- Use Sverto in a Quarto document by adding `sverto` to `filters` in the document frontmatter
5+
- Add Svelte files to a document using the frontmatter key `sverto.use`
6+
- No need for magic blocks anymore!
7+
- When working in a website project, optionally use the `sverto` project type to cut down on duplicate Svelte compilation Quarto documents
8+
- Works properly with Quarto includes
9+
- Requires Quarto pre-release 1.5.25 or higher on Windows, but should work fine on Quarto 1.4 on macOS and Linux.
10+
11+
## Sverto 0.0.3
12+
13+
- Migrated from [`360-info/sverto`](https://github.comn/360-info/sverto) to [`jimjam-slam/sverto`](htps://github.com/jimjam-slam/sverto). Old GitHub links are maintained.
14+
115
## Sverto 0.0.2
216

317
- Bump minimum Quarto version to 1.3.0.
@@ -7,6 +21,6 @@
721
1. avoid copying the `docs` folder in with the project template; and
822
2. include the `.gitignore` with the template
923

10-
## 0.0.1
24+
## Sverto 0.0.1
1125

1226
- Initial release

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,18 @@ title: "My document"
4949
filters: ["sverto"]
5050
sverto:
5151
use:
52-
example.svelte
52+
- example.svelte
5353
---
5454
```
5555

5656
### Step 2: bring your Svelte component to life
5757

58-
Use an [Observable JS](https://quarto.org/docs/interactive/ojs/) chunk to _instantiate_ your Svelte component.
58+
Use an [Observable JS](https://quarto.org/docs/interactive/ojs/) chunk to _construct_ your Svelte component.
5959

6060
````js
6161
```{ojs}
6262
myChart = new example.default({
6363
target: document.querySelector("#chart")
64-
props: {
65-
chartData: {}
66-
}
6764
})
6865
```
6966

@@ -73,16 +70,17 @@ myChart = new example.default({
7370

7471
- 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
7572
- `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!
7773

78-
### Step 3: make your visual reactive
74+
### Step 3: make your component reactive
7975

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.
76+
If your component has `props` that allow it to change or transition in response to other OJS code, you can update them by assigning the prop directly.
8177

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:
78+
For example, if we have a dataset called `myData` in OJS, and a year slider called `selectedYear`, we might change a prop called `chartData` whenever the user selects a new year like:
8379

8480
````js
81+
```{ojs}
8582
myChart.chartData = myData.filter(d => d.year == selectedYear)
83+
```
8684
````
8785

8886
> **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.

example.qmd

Lines changed: 8 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,31 @@
11
---
2-
title: Svelte example
3-
author: James Goldie, 360info
2+
title: Sverto example
43
date: last-modified
54
filters:
65
- sverto
76
sverto:
87
use:
98
- Circles.svelte
10-
- Test.svelte
119
---
1210

13-
Here's an example of how to use the included example Svelte component, `Circles.svelte`. There are six steps:
11+
Here's an example of how to use the included example Svelte component, `Circles.svelte`.
1412

15-
### 1. Install Sverto
16-
17-
```bash
18-
quarto add jimjam-slam/sverto
19-
```
20-
21-
### 2. Add Sverto to your document
22-
23-
In the document frontmatter:
24-
25-
```yaml
26-
---
27-
# filters:
28-
# - sverto
29-
---
30-
```
31-
32-
#### (For projects only: use the sverto project type)
33-
34-
In `_quarto.yml`:
35-
36-
37-
```yaml
38-
project:
39-
type: sverto
40-
```
41-
42-
### 3. Create a place for your visual to live
43-
44-
We need to create an instance of our imported component, but---unlike a typical OJS block---Svelte components put the visual itself somewhere else in the document.
45-
46-
Create an empty div in your document and give it an #id with the hashtag. This will be the "target" in the next step, when we bring the component to life, or _instantiate_ it:
47-
48-
```markdown
49-
:::{#mycircles}
50-
:::
51-
```
52-
53-
:::{#mycircles}
54-
:::
55-
56-
:::{.callout-tip}
57-
This block can go anywhere in your document! Feel free to put it well after the following steps, where you actually want your visualisation to appear.
58-
:::
59-
60-
:::{.callout-tip}
61-
If you know how how to use CSS, you can target `#mycircles` to position or size your visualisation as you'd like.
62-
:::
63-
64-
### 4. Instantiate your component
65-
66-
Bring your component to life by creating an instance of it with `new [Component Name].default()`. This function takes an object as an argument with two properties:
67-
68-
- `target` is the place your visual will go. Use the `document.querySelector` to point it to the empty block we just made above, `#mycricles`.
69-
- `props`: is an object where we pass props, or properties, to the component. This will vary depending on how the component is written: the one included as an example with this package, `Circles.svelte`, requires a prop called `data`.
70-
71-
Here's how we instantiate `Circles.svelte`:
13+
Create one using `fileName.default()`, where `fileName` is the file name without `.svelte`:
7214

7315
```{ojs}
7416
myCircles = new Circles.default({
75-
target: document.querySelector("#mycircles"),
76-
props: {
77-
data: [5, 15, 25, 17, 8]
78-
}
17+
target: document.querySelector("#mycircles")
7918
});
8019
```
8120

82-
::: {.callout-important}
83-
If you're creating a visual that will react in response to changing OJS (for example, some data that you've calculated in your document, or the value of an Observable Input), **do not put that data here.**
21+
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:
8422

85-
If you put changing data here, the Svelte component will rebuild from scratch every time the data changes, and it will not animate.
86-
87-
Instead, we're going to take advantage of Svelte's own reactivity in the next step.
23+
:::{#mycircles}
8824
:::
8925

90-
### 5. Update your component
91-
92-
Now the fun part - updating our visual when our document changes. It's as simple as referring to the prop using `componentInstance.propName =`.
26+
Now the fun part - updating our visual when our document changes. It's as simple as changing the prop using `componentName.propName =`.
9327

94-
For example, here are three datasets and an Observable Inputs dropdown menu that lets you select one of three datasets:
28+
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:
9529

9630
```{ojs}
9731
// here are some datasets...
@@ -113,22 +47,6 @@ Now we can update the prop `data` of the visual `myCircles` using:
11347
myCircles.data = selectedDataset
11448
```
11549

116-
You can't see anything here, but if we scroll back up to where we added the empty `#mycircles` block, you can see it transition whenever we select a new dataset!
117-
118-
:::{.callout-tip}
119-
You can put `#mycircles` wherever you want in the document - it doesn't have to be in order!
120-
:::
121-
122-
### 6. Render the project
123-
124-
Render your Quarto project as you normally would, with commands like `quarto run` and `quarto preview`.
125-
126-
:::{.callout-note}
127-
You'll probably get the warning `OJS block count mismatch. Line number reporting is likely to be wrong` when rendering.
128-
129-
That's because of what we're doing under the hood to bring the Svelte files in. It won't stop your project from working, but be aware of it if you are referring to line numbers in code blocks!
130-
:::
131-
13250
And there you go! 🚀
13351

13452
For more help writing Svelte components, check out the [Svelte tutorial](https://svelte.dev/tutorial/basics).

0 commit comments

Comments
 (0)