@reuters-graphics/graphics-components 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Block/Block.stories.svelte +7 -1
- package/dist/components/Block/Block.stories.svelte.d.ts +8 -5
- package/dist/components/FeaturePhoto/stories/docs/archieML.md +37 -0
- package/dist/components/GraphicBlock/stories/docs/archieML.md +40 -0
- package/dist/components/PhotoPack/stories/docs/archieML.md +63 -0
- package/dist/components/ReferralBlock/ReferralBlock.svelte +22 -8
- package/dist/components/ReferralBlock/filterCurrentPage.d.ts +2 -0
- package/dist/components/ReferralBlock/filterCurrentPage.js +32 -0
- package/dist/components/ReferralBlock/types.d.ts +99 -0
- package/dist/components/ReferralBlock/types.js +1 -0
- package/dist/components/SEO/stories/docs/archieML.md +36 -0
- package/dist/components/Scroller/stories/docs/ai2svelte.md +0 -82
- package/dist/components/Scroller/stories/docs/archieML.md +87 -0
- package/dist/components/SiteHeader/NavBar/NavDropdown/StoryCard/index.svelte +12 -5
- package/dist/components/SiteHeadline/stories/docs/archieML.md +26 -0
- package/dist/docs/docs-components/CopyColourTable/styles.module.scss +4 -2
- package/dist/docs/docs-components/Herbie/Herbie.d.ts +7 -0
- package/dist/docs/docs-components/Herbie/Herbie.tsx +47 -0
- package/dist/docs/docs-components/Highlight/Highlight.d.ts +6 -0
- package/dist/docs/docs-components/Highlight/Highlight.tsx +19 -0
- package/dist/docs/docs-components/SubtleHighlight/SubtleHighlight.d.ts +6 -0
- package/dist/docs/docs-components/SubtleHighlight/SubtleHighlight.tsx +16 -0
- package/dist/docs/docs-components/ThemeBuilder/NewTheme/styles.module.scss +3 -1
- package/dist/docs/docs-components/syntax/nord.d.ts +7 -0
- package/dist/docs/docs-components/syntax/nord.js +155 -0
- package/dist/docs/guides/archieml.mdx +441 -0
- package/dist/docs/guides/svelte-components.mdx +138 -0
- package/package.json +2 -1
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { Meta } from '@storybook/blocks';
|
|
2
|
+
import { parameters } from '../utils/docsPage.js';
|
|
3
|
+
import Herbie from '../docs-components/Herbie/Herbie';
|
|
4
|
+
|
|
5
|
+
<Meta title="Guides/Using with ArchieML docs" parameters={{ ...parameters }} />
|
|
6
|
+
|
|
7
|
+
# Using components with ArchieML docs
|
|
8
|
+
|
|
9
|
+
Most of the snippets in these docs show how to use components by passing data into their props directly. In most cases, though, you don't want to hard-code content in your project and instead will get those values from an [ArchieML](https://archieml.org/)-formatted document, hosted in either [RNGS.io](https://rngs.io) or a Google Doc.
|
|
10
|
+
|
|
11
|
+
In most cases, it's straight forward to fill in component props from ArchieML values, but in some cases you may need to write a small bit of code to translate strings from an ArchieML doc into a different data type or to rearrange data from a doc into a specific format to match a component's props.
|
|
12
|
+
|
|
13
|
+
## Simple example
|
|
14
|
+
|
|
15
|
+
Let's look at a basic component, a `ProfileCard`, with a demo that looks like this in the docs:
|
|
16
|
+
|
|
17
|
+
```svelte
|
|
18
|
+
<script>
|
|
19
|
+
import { ProfileCard } from '@reuters-graphics/graphics-components';
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<ProfileCard
|
|
23
|
+
name="Kitty"
|
|
24
|
+
age="{2}"
|
|
25
|
+
img="https://cats.com/cat1.jpg"
|
|
26
|
+
birthday="{new Date('2020-09-25')}"
|
|
27
|
+
bio="Some notes.\n\nWith multiple paragraphs."
|
|
28
|
+
isGood="{true}"
|
|
29
|
+
/>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Notice the component's props includes strings, a date, a number and a boolean.
|
|
33
|
+
|
|
34
|
+
<Herbie number="1">
|
|
35
|
+
{`In our ArchieML doc, we might fill out a block for this component like ...`}
|
|
36
|
+
</Herbie>
|
|
37
|
+
|
|
38
|
+
```yaml
|
|
39
|
+
[blocks]
|
|
40
|
+
# ...
|
|
41
|
+
|
|
42
|
+
type: profile-card
|
|
43
|
+
name: Tom
|
|
44
|
+
age: 10
|
|
45
|
+
picture: images/tom-the-cat.jpg
|
|
46
|
+
birthday: 2020-09-25
|
|
47
|
+
bio: A very frisky feline.
|
|
48
|
+
|
|
49
|
+
... and an avid mouser!
|
|
50
|
+
:end
|
|
51
|
+
isGood: true
|
|
52
|
+
|
|
53
|
+
# ...
|
|
54
|
+
[]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
It's often a good idea to give the properties in your ArchieML doc the same names as the props on the component you're using. That's what we've done above, but you can also name them something else. For example, we went with `picture` instead of `img`.
|
|
58
|
+
|
|
59
|
+
> If the ArchieML syntax above looks foreign to you, check out the Help docs on any story in [RNGS.io](https://rngs.io) or read the official [ArchieML docs](https://archieml.org/#demo) to get familiar with the basics.
|
|
60
|
+
|
|
61
|
+
<Herbie number="2">
|
|
62
|
+
{`When we download the doc, our ArchieML will turn into JSON data like ...`}
|
|
63
|
+
</Herbie>
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"blocks": [
|
|
68
|
+
{
|
|
69
|
+
"type": "profile-card",
|
|
70
|
+
"name": "Tom",
|
|
71
|
+
"age": "10",
|
|
72
|
+
"picture": "images/tom-the-cat.jpg",
|
|
73
|
+
"birthday": "2020-09-25",
|
|
74
|
+
"bio": "A very frisy feline.\n\n... and an avid mouser!",
|
|
75
|
+
"isGood": "true"
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Notice all the values in the data are **strings**. More on that soon.
|
|
82
|
+
|
|
83
|
+
<Herbie number="3">
|
|
84
|
+
{`Now we can use that data to feed our component the information it needs for its props:`}
|
|
85
|
+
</Herbie>
|
|
86
|
+
|
|
87
|
+
```svelte
|
|
88
|
+
<script>
|
|
89
|
+
// These are usually already imported for you
|
|
90
|
+
import { assets } from '$app/paths';
|
|
91
|
+
// Your ArchieML doc
|
|
92
|
+
import content from '$locales/en/content.json';
|
|
93
|
+
|
|
94
|
+
// Your component from graphics-components
|
|
95
|
+
import { ProfileCard } from '@reuters-graphics/graphics-components';
|
|
96
|
+
</script>
|
|
97
|
+
|
|
98
|
+
{#each content.blocks as block}
|
|
99
|
+
{#if block.type === 'text'}
|
|
100
|
+
<!-- ... -->
|
|
101
|
+
{:else if block.type === 'profile-card'}
|
|
102
|
+
<ProfileCard
|
|
103
|
+
name="{block.name}"
|
|
104
|
+
age="{parseInt(block.age)}"
|
|
105
|
+
img="{`${assets}/${block.picture}`}"
|
|
106
|
+
birthday="{new Date(block.birthday)}"
|
|
107
|
+
bio="{block.bio}"
|
|
108
|
+
isGood="{block.isGood === 'true'}"
|
|
109
|
+
/>
|
|
110
|
+
<!-- ... -->
|
|
111
|
+
{/if}
|
|
112
|
+
{/each}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### But let's break that last bit down ...
|
|
116
|
+
|
|
117
|
+
This is a loop that goes over each block in the `blocks: []` array in the JSON data we downloaded from our ArchieML doc.
|
|
118
|
+
|
|
119
|
+
```svelte
|
|
120
|
+
{#each content.blocks as block}
|
|
121
|
+
<!-- ... -->
|
|
122
|
+
{/each}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
While we're looping over each individual block, we check its `type` so we know which component to match it with. For example, `block.type === 'text'` will signal we want to use our [BodyText](./?path=/docs/components-text-elements-bodytext--docs) component in this case.
|
|
126
|
+
|
|
127
|
+
```svelte
|
|
128
|
+
{#if block.type === 'text'}
|
|
129
|
+
<!-- ... -->
|
|
130
|
+
{:else if block.type === 'profile-card'}
|
|
131
|
+
<!-- ... -->
|
|
132
|
+
{/if}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Once we've identified we have the right block for our component, we need to convert several of the data values from strings to the appropriate data type for each prop.
|
|
136
|
+
|
|
137
|
+
```svelte
|
|
138
|
+
<ProfileCard
|
|
139
|
+
name="{block.name}"
|
|
140
|
+
age="{parseInt(block.age)}"
|
|
141
|
+
img="{`${assets}/${block.picture}`}"
|
|
142
|
+
birthday="{new Date(block.birthday)}"
|
|
143
|
+
bio="{block.bio}"
|
|
144
|
+
isGood="{block.isGood === 'true'}"
|
|
145
|
+
/>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
For example, we're converting Tom's age into a number with JavaScript's [parseInt](https://www.w3schools.com/jsref/jsref_parseint.asp) function ...
|
|
149
|
+
|
|
150
|
+
```svelte
|
|
151
|
+
{parseInt(block.age)}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
... and we're parsing his birthday into a JavaScript Date ...
|
|
155
|
+
|
|
156
|
+
```svelte
|
|
157
|
+
{new Date(block.birthday)}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
... and we're checking if he's a good cat by converting the string into a `true`/`false` boolean:
|
|
161
|
+
|
|
162
|
+
```svelte
|
|
163
|
+
{block.isGood === 'true'}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**Especially note** how we're using the `assets` module we talked about in ["Using with the Graphics Kit"](./?path=/docs/guides-using-with-the-graphics-kit--docs) to turn the _relative_ path to Tom's profile pic in our ArchieML doc into an _absolute_ path that will have the full `https://reuters.com...` bit attached.
|
|
167
|
+
|
|
168
|
+
```svelte
|
|
169
|
+
{`${assets}/${block.picture}`}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Sum it all up 🏁
|
|
173
|
+
|
|
174
|
+
Tying your ArchieML doc to a component is as easy as:
|
|
175
|
+
|
|
176
|
+
<Herbie number={1}>Read what props your component needs in these docs.</Herbie>
|
|
177
|
+
|
|
178
|
+
<Herbie number={2}>Write your ArchieML block to match those props.</Herbie>
|
|
179
|
+
|
|
180
|
+
<Herbie number={3}>
|
|
181
|
+
{`Convert the string values in your ArchieML JSON into the data types the
|
|
182
|
+
component's props expect and pass them into your component!`}
|
|
183
|
+
</Herbie>
|
|
184
|
+
|
|
185
|
+
## A slightly more complex example
|
|
186
|
+
|
|
187
|
+
The simple example above is a variation of the majority of the components in these docs. Sometimes, though, a component's props get a little more complex, especially when they require an _array_ of objects with different data.
|
|
188
|
+
|
|
189
|
+
Let's look at another example component:
|
|
190
|
+
|
|
191
|
+
```svelte
|
|
192
|
+
<Timeline
|
|
193
|
+
title="A brief history of BitCoin"
|
|
194
|
+
dates="{[
|
|
195
|
+
{
|
|
196
|
+
date: new Date('1992-01-01'),
|
|
197
|
+
subhed:
|
|
198
|
+
'Cynthia Dwork and Moni Naor come with an idea for "cryptocurrency"',
|
|
199
|
+
img: `${assets}/images/dwork-naor.jpeg`,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
date: new Date('2008-08-18'),
|
|
203
|
+
subhed: 'Domain name for bitcoin.org registered',
|
|
204
|
+
link: 'https://bitcoin.org',
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
date: new Date('2013-07-22'),
|
|
208
|
+
subhed: 'The Winklevoss twins buy in',
|
|
209
|
+
img: `${assets}/images/winkle-boys.jpeg`,
|
|
210
|
+
},
|
|
211
|
+
]}"
|
|
212
|
+
/>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Notice our `Timeline` component's `dates` prop takes an array of objects, each with required `date` and `subhed` properties and what appears to be _optional_ `img` or `link` properties.
|
|
216
|
+
|
|
217
|
+
To match that structure in our ArchieML doc, we might write it like ...
|
|
218
|
+
|
|
219
|
+
```yaml
|
|
220
|
+
[blocks]
|
|
221
|
+
# ...
|
|
222
|
+
|
|
223
|
+
type: timeline
|
|
224
|
+
title: A brief history of Bitcoin
|
|
225
|
+
[.dates]
|
|
226
|
+
date: 1992-01-01
|
|
227
|
+
subhed: Cynthia Dwork and Moni Naor ...
|
|
228
|
+
img: images/dwork-naor.jpeg
|
|
229
|
+
|
|
230
|
+
date: 2008-08-18
|
|
231
|
+
subhed: Domain name for bitcoin.org ...
|
|
232
|
+
link: https://bitcoin.org
|
|
233
|
+
|
|
234
|
+
date: 2013-07-22
|
|
235
|
+
subhed: The Winklevoss twins ...
|
|
236
|
+
img: images/winkle-boys.jpeg
|
|
237
|
+
[]
|
|
238
|
+
|
|
239
|
+
# ...
|
|
240
|
+
[]
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**Especially notice** the `[.dates] ... []` bit. The `.` on `.dates` creates a _nested_ array on this block.
|
|
244
|
+
|
|
245
|
+
That leaves us with JSON data that looks like ...
|
|
246
|
+
|
|
247
|
+
```json
|
|
248
|
+
{
|
|
249
|
+
"blocks": [
|
|
250
|
+
{
|
|
251
|
+
"type": "timeline",
|
|
252
|
+
"title": "A brief history of Bitcoin",
|
|
253
|
+
"dates": [
|
|
254
|
+
{
|
|
255
|
+
"date": "1992-01-01",
|
|
256
|
+
"subhed": "Cynthia Dwork and Moni Naor ...",
|
|
257
|
+
"img": "images/dwork-naor.jpeg"
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
"date": "2008-08-18",
|
|
261
|
+
"subhed": "Domain name for bitcoin.org ...",
|
|
262
|
+
"link": "https://bitcoin.org"
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
"date": "2013-07-22",
|
|
266
|
+
"subhed": "The Winklevoss twins ...",
|
|
267
|
+
"img": "images/winkle-boys.jpeg"
|
|
268
|
+
}
|
|
269
|
+
]
|
|
270
|
+
}
|
|
271
|
+
]
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Now our data looks OK and all the properties on each date object that go into the Timeline component's `dates` prop appear to match. It may look like we can pass our data straight into our component like ...
|
|
276
|
+
|
|
277
|
+
```svelte
|
|
278
|
+
<!-- ... -->
|
|
279
|
+
{:else if block.type === 'timeline'}
|
|
280
|
+
<Timeline
|
|
281
|
+
title="{block.title}"
|
|
282
|
+
dates="{block.dates}""
|
|
283
|
+
/>
|
|
284
|
+
{/else}
|
|
285
|
+
<!-- ... -->
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
... but we have a couple problems and they all have to do with data types.
|
|
289
|
+
|
|
290
|
+
According to the component's docs, our `date` property's value should be a JavaScript Date and our `img` needs to be an absolute path.
|
|
291
|
+
|
|
292
|
+
But this is a problem we can solve by looping over our JSON data and making a new array where all the values are of the right type for our props.
|
|
293
|
+
|
|
294
|
+
```svelte
|
|
295
|
+
<script>
|
|
296
|
+
// ...
|
|
297
|
+
|
|
298
|
+
const timelineBlock = content.blocks.find((block) => block.type === 'timeline');
|
|
299
|
+
|
|
300
|
+
const timelineDates = timelineBlock.dates.map((d) => {
|
|
301
|
+
const date = new Date(d.date);
|
|
302
|
+
const subhed = d.subhed;
|
|
303
|
+
let img;
|
|
304
|
+
let link;
|
|
305
|
+
|
|
306
|
+
if (d.img) {
|
|
307
|
+
img = `${assets}/${d.img}`;
|
|
308
|
+
}
|
|
309
|
+
if (d.link) {
|
|
310
|
+
link = d.link;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
date,
|
|
315
|
+
subhed,
|
|
316
|
+
img,
|
|
317
|
+
link,
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
</script>
|
|
321
|
+
|
|
322
|
+
<!-- ... -->
|
|
323
|
+
|
|
324
|
+
{:else if block.type === 'timeline'}
|
|
325
|
+
<Timeline
|
|
326
|
+
title="{block.title}"
|
|
327
|
+
dates="{timelineDates}""
|
|
328
|
+
/>
|
|
329
|
+
{/else}
|
|
330
|
+
|
|
331
|
+
<!-- ... -->
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### OK, let's break down that new chunk of code ...
|
|
335
|
+
|
|
336
|
+
First, notice I'm writing this code inside my `script` tags so I can easily just write it with JavaScript.
|
|
337
|
+
|
|
338
|
+
```svelte
|
|
339
|
+
<script>
|
|
340
|
+
// Normal JavaScript is OK here ...
|
|
341
|
+
</script>
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Now the first thing I do is get ahold of _just_ my timeline block, leaving behind all the other blocks on my page, using a simple [find](https://www.w3schools.com/jsref/jsref_find.asp) function.
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
// Gets JUST my timeline block from the array of all the blocks that make up my page
|
|
348
|
+
const timelineBlock = content.blocks.find((block) => block.type === 'timeline');
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
With my timeline block in hand, I can loop over all my dates and create a _new_ array with all the values correctly parsed using a basic [map](https://www.w3schools.com/jsref/jsref_map.asp) function.
|
|
352
|
+
|
|
353
|
+
```javascript
|
|
354
|
+
const timelineDates = timelineBlock.dates.map((d) => {
|
|
355
|
+
// "d" is each date object in the ArchieML JSON
|
|
356
|
+
// ...
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Inside my `map` I can convert the required properties into new variables matching my component's prop names. In this case, I need to convert the data, but the subhed is just a string ...
|
|
361
|
+
|
|
362
|
+
```javascript
|
|
363
|
+
const timelineDates = timelineBlock.dates.map((d) => {
|
|
364
|
+
const date = new Date(d.date);
|
|
365
|
+
const subhed = d.subhed;
|
|
366
|
+
// ...
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
For the optional ones, I'll do a tiny `if` to check if the date has them before adding them to a variable:
|
|
371
|
+
|
|
372
|
+
```javascript
|
|
373
|
+
const timelineDates = timelineBlock.dates.map((d) => {
|
|
374
|
+
// ...
|
|
375
|
+
|
|
376
|
+
let img; // Start as an empty variable ...
|
|
377
|
+
let link;
|
|
378
|
+
|
|
379
|
+
// If our date has an image, we'll make it an absolute URL
|
|
380
|
+
if (d.img) {
|
|
381
|
+
img = `${assets}/${d.img}`;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// If it has a link, we'll just pass that on, as is
|
|
385
|
+
if (d.link) {
|
|
386
|
+
link = d.link;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// ...
|
|
390
|
+
});
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
All that done, I can create my new object by returning it from my `map` ...
|
|
394
|
+
|
|
395
|
+
```javascript
|
|
396
|
+
const timelineDates = timelineBlock.dates.map((d) => {
|
|
397
|
+
// ...
|
|
398
|
+
|
|
399
|
+
return {
|
|
400
|
+
date,
|
|
401
|
+
subhed,
|
|
402
|
+
img,
|
|
403
|
+
link,
|
|
404
|
+
};
|
|
405
|
+
});
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
**Finally**, I have a new array of dates all sorted for my timeline and I can pass them direcly in to the component's `dates` prop:
|
|
409
|
+
|
|
410
|
+
```svelte
|
|
411
|
+
<script>
|
|
412
|
+
const timelineDates = timelineBlock.dates.map((d) => {
|
|
413
|
+
// ...
|
|
414
|
+
});
|
|
415
|
+
</script>
|
|
416
|
+
|
|
417
|
+
{:else if block.type === 'timeline'}
|
|
418
|
+
<Timeline
|
|
419
|
+
title="{block.title}"
|
|
420
|
+
dates="{timelineDates}""
|
|
421
|
+
/>
|
|
422
|
+
{/else}
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
> This is just _one way_ to prepare your data for your component in JavaScript. In many cases, generative AI tools like ChatGPT can also help you write the small bit of code you need. Just provide it a sample of the ArchieML JSON data and describe the format you need to convert it to for your component.
|
|
426
|
+
|
|
427
|
+
### Sum it all up 🏁
|
|
428
|
+
|
|
429
|
+
If your component has more complex props like arrays of objects ...
|
|
430
|
+
|
|
431
|
+
<Herbie
|
|
432
|
+
number={1}
|
|
433
|
+
>{`Write your ArchieML doc with nested arrays to match your component's props.`}</Herbie>
|
|
434
|
+
|
|
435
|
+
<Herbie
|
|
436
|
+
number={2}
|
|
437
|
+
>{`Format your data in JavaScript using simple \`find\` and \`map\` functions.`}</Herbie>
|
|
438
|
+
|
|
439
|
+
<Herbie
|
|
440
|
+
number={3}
|
|
441
|
+
>{`Pass the new formatted array into your components' props instead of the original ArchieML data.`}</Herbie>
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Meta } from '@storybook/blocks';
|
|
2
|
+
import { parameters } from '../utils/docsPage.js';
|
|
3
|
+
import Highlight from '../docs-components/Highlight/Highlight';
|
|
4
|
+
|
|
5
|
+
<Meta title="Guides/Bare minimum Svelte" parameters={{ ...parameters }} />
|
|
6
|
+
|
|
7
|
+
# Introduction to Svelte components
|
|
8
|
+
|
|
9
|
+
Svelte is a modern JavaScript framework for building parts of HTML pages. Components are the building blocks of Svelte pages, allowing you to create reusable and interactive parts of your page.
|
|
10
|
+
|
|
11
|
+
Here's **the bare minimum** you need to know to start using graphics components:
|
|
12
|
+
|
|
13
|
+
## What's a component?
|
|
14
|
+
|
|
15
|
+
A Svelte <Highlight>component</Highlight> is just a `.svelte` file.
|
|
16
|
+
|
|
17
|
+
A component is usually composed of several parts: JavaScript for managing data, HTML to represent the elements it creates on the page and CSS to style those elements.
|
|
18
|
+
|
|
19
|
+
```svelte
|
|
20
|
+
<!-- JavaScript -->
|
|
21
|
+
<script>
|
|
22
|
+
export let imgSrc = 'https://reuters.com/image.jpg';
|
|
23
|
+
export let altText = 'A cat';
|
|
24
|
+
export let caption = 'Mousing all day is hard work ...';
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<!-- HTML -->
|
|
28
|
+
<figure>
|
|
29
|
+
<img src="{imgSrc}" alt="{altText}" />
|
|
30
|
+
<figcaption>{caption}</figcaption>
|
|
31
|
+
</figure>
|
|
32
|
+
|
|
33
|
+
<!-- CSS -->
|
|
34
|
+
<style>
|
|
35
|
+
figure {
|
|
36
|
+
max-width: 600px;
|
|
37
|
+
margin: 0 auto;
|
|
38
|
+
img {
|
|
39
|
+
width: 100%;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
</style>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Importing and using components
|
|
46
|
+
|
|
47
|
+
To use a component, you first need to import it. For example, below is a component called `Button.svelte`:
|
|
48
|
+
|
|
49
|
+
```svelte
|
|
50
|
+
<script>
|
|
51
|
+
export let text = 'Click me';
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<!-- Button.svelte -->
|
|
55
|
+
<button>{text}</button>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
You can import and use this component in another file:
|
|
59
|
+
|
|
60
|
+
```svelte
|
|
61
|
+
<!-- App.svelte -->
|
|
62
|
+
<script>
|
|
63
|
+
import Button from './Button.svelte';
|
|
64
|
+
</script>
|
|
65
|
+
|
|
66
|
+
<main>
|
|
67
|
+
<h1>Welcome to Svelte</h1>
|
|
68
|
+
<Button text="Press here" />
|
|
69
|
+
</main>
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Here, the `Button` component is imported and used in the `App` component.
|
|
73
|
+
|
|
74
|
+
The components in this project are pre-made so you can install, import and use them directly in your page without needing to write them yourself or have a copy of each component in your project.
|
|
75
|
+
|
|
76
|
+
For example, a basic page might look like this:
|
|
77
|
+
|
|
78
|
+
```svelte
|
|
79
|
+
<script>
|
|
80
|
+
// Importing our pre-made components from this project
|
|
81
|
+
import {
|
|
82
|
+
BodyText,
|
|
83
|
+
SiteHeader,
|
|
84
|
+
SiteFooter,
|
|
85
|
+
Headline,
|
|
86
|
+
} from '@reuters-graphics/graphics-components';
|
|
87
|
+
</script>
|
|
88
|
+
|
|
89
|
+
<SiteHeader />
|
|
90
|
+
|
|
91
|
+
<Headline hed="My new story" dek="The beginning of a beautiful page" />
|
|
92
|
+
<BodyText text="Lorem ipsum ..." />
|
|
93
|
+
|
|
94
|
+
<SiteFooter />
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Passing data to components with props
|
|
98
|
+
|
|
99
|
+
All graphics components are written to work across different stories. That means they expect to be given the content they'll add (or "render") on the page by you.
|
|
100
|
+
|
|
101
|
+
We pass the data components use to render their part of a page using <Highlight>props</Highlight>.
|
|
102
|
+
|
|
103
|
+
In the `Button.svelte` example above, the `text` prop provides a way to pass the text that will be rendered in the button like this:
|
|
104
|
+
|
|
105
|
+
```svelte
|
|
106
|
+
<Button text="Click me!" />
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
<button>Click me!</button>
|
|
110
|
+
|
|
111
|
+
## Reading component props
|
|
112
|
+
|
|
113
|
+
All graphics components are documented with a code snippet showing how to import them and what props that component has with the kind of data each expects.
|
|
114
|
+
|
|
115
|
+
For example, the default FeaturePhoto example looks like:
|
|
116
|
+
|
|
117
|
+
```svelte
|
|
118
|
+
<script>
|
|
119
|
+
import { FeaturePhoto } from '@reuters-graphics/graphics-components';
|
|
120
|
+
</script>
|
|
121
|
+
|
|
122
|
+
<FeaturePhoto
|
|
123
|
+
src="https://reuters.com/images/myImage.jpg"
|
|
124
|
+
altText="Some alt text"
|
|
125
|
+
caption="A caption"
|
|
126
|
+
width="normal"
|
|
127
|
+
/>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The component's props are `src`, `altTtext`, `caption` and `width` and each shows an example of the type of data that should go into that prop. For example, the `src` prop expects a full URL to the image it will render.
|
|
131
|
+
|
|
132
|
+
## Next steps
|
|
133
|
+
|
|
134
|
+
To learn more about Svelte, you're not short of great tutorials. There are dozens of YouTube videos, online courses and articles to introduce you to more advanced features of the framework. Watch out for our own training courses on the Graphics Team to learn more about Svelte or JavaScript. But in a pinch, each desk has at least one developer who can help you through any problems as you're working with Svelte.
|
|
135
|
+
|
|
136
|
+
All are also welcome to join the "⚙️ Graphics Dev Group" channel in Teams, where we occasionally chat about code tips or tricks.
|
|
137
|
+
|
|
138
|
+
> **NOTE:** As of now, we are using version 4 Svelte syntax. We will soon upgrade to version 5, but for now, we recommend looking for older tutorials online and not the official guides for the latest version.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reuters-graphics/graphics-components",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"homepage": "https://reuters-graphics.github.io/graphics-components",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"@storybook/blocks": "^8.4.4",
|
|
43
43
|
"@storybook/builder-vite": "^8.4.4",
|
|
44
44
|
"@storybook/components": "^8.4.4",
|
|
45
|
+
"@storybook/csf": "^0.1.12",
|
|
45
46
|
"@storybook/manager-api": "^8.4.4",
|
|
46
47
|
"@storybook/mdx2-csf": "^1.1.0",
|
|
47
48
|
"@storybook/svelte": "^8.4.4",
|