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
Let's create a Volto block that will display weather information for Brasilia. For this we can use [Open-Meteo API](https://open-meteo.com/). Open-Meteo is an open-source weather API and offers free access for non-commercial use. No API key required.
12
+
Let's create a Volto block that will display weather information for Helsinki. For this we can use [Open-Meteo API](https://open-meteo.com/). Open-Meteo is an open-source weather API and offers free access for non-commercial use. No API key required.
13
13
14
-
Creating a basic block in Volto involves several steps. Below, I'll outline the steps to create a Volto block that displays the weather forecast in Brasilia.
14
+
Creating a basic block in Volto involves several steps. Below, I'll outline the steps to create a Volto block that displays the weather forecast in Helsinki.
15
15
16
16
1.**Setup Your Volto Project:** If you haven't already, set up a Volto project. You can use the instructions presented in [Installation -> Bootstrap a new Volto project](installation.md) section.
5.**Register the Block:** In your Volto project, locate the "components/index.js" file and add an the entries for your "Weather Block"
272
+
5. **Add css for the Block:** In your Volto project , inside src add "theme" folder and create "weather.less" for adding style to your "Weather Block"
273
+
274
+
```less
275
+
// Variables
276
+
@primary-bg: #f5f5f5;
277
+
@text-color: #666;
278
+
@border-radius:8px;
279
+
280
+
.weather-block {
281
+
padding:1rem;
282
+
background: @primary-bg;
283
+
border-radius: @border-radius;
284
+
margin-bottom:2rem;
285
+
.date {
286
+
font-size:1.1rem;
287
+
color: @text-color;
288
+
margin-bottom:1rem;
289
+
font-style: italic;
290
+
}
291
+
292
+
.hourly-forecast {
293
+
display: flex;
294
+
overflow-x: auto;
295
+
padding:1rem0;
296
+
flex-flow: wrap;
297
+
gap:1rem;
298
+
height:200px;
299
+
align-items: flex-end;
300
+
}
301
+
302
+
.hourly-item {
303
+
display: flex;
304
+
flex-direction: column;
305
+
align-items: center;
306
+
min-width:40px;
307
+
308
+
.hour {
309
+
font-size:0.8rem;
310
+
color: @text-color;
311
+
margin-bottom:0.5rem;
312
+
}
313
+
314
+
.probability {
315
+
height:100px;
316
+
width:20px;
317
+
background: #eee;
318
+
border-radius:10px;
319
+
overflow: hidden;
320
+
position: relative;
321
+
322
+
&-bar {
323
+
position: absolute;
324
+
bottom:0;
325
+
width:100%;
326
+
transition: height 0.3s ease;
327
+
border-radius:10px;
328
+
}
329
+
330
+
&-value {
331
+
font-size:0.8rem;
332
+
margin-top:0.5rem;
333
+
}
334
+
}
335
+
}
336
+
337
+
.temperature-legend {
338
+
display: flex;
339
+
gap:20px;
340
+
margin:10px0;
341
+
flex-wrap: wrap;
342
+
padding:10px;
343
+
background: @primary-bg;
344
+
border-radius:4px;
345
+
346
+
.legend-item {
347
+
display: flex;
348
+
align-items: center;
349
+
gap:8px;
350
+
351
+
.legend-color {
352
+
width:20px;
353
+
height:20px;
354
+
border-radius:4px;
355
+
display: inline-block;
356
+
}
357
+
358
+
.legend-text {
359
+
font-size:14px;
360
+
color: @text-color;
361
+
}
362
+
}
363
+
}
364
+
365
+
.legend {
366
+
text-align: center;
367
+
color: @text-color;
368
+
margin-top:1rem;
369
+
font-size:0.9rem;
370
+
}
371
+
}
372
+
```
373
+
374
+
6. **Register the Block:** In your Volto project, add "components/Blocks/Weather/index.js" file and add an the entries for your "Weather Block" files and export them.
We need to configure the project to make it aware of a new block by adding it to the object configuration that is located in {file}`src/index.js`.
384
+
We need to configure the project to make it aware of a new block by adding it to the object configuration that is located in {file}`src/config/blocks.ts`.
163
385
For that we need the two blocks components we created and a SVG icon that will be displayed in the blocks chooser.
@@ -181,20 +401,29 @@ export default function applyConfig(config) {
181
401
restricted:false,
182
402
mostUsed:false,
183
403
sidebarTab:1,
184
-
blocks: {},
185
-
security: {
186
-
addPermission: [],
187
-
view: [],
188
-
},
189
404
};
190
405
191
-
...
192
-
193
406
return config;
194
407
};
195
408
...
196
409
```
197
410
198
-
6.**Use the Weather Block:** In Volto's Dexterity-based content types, create or edit a content type that includes the "Weather Block" in the allowedBlocks field. Then, create a content item and add the "Weather Block" to display the weather information for the location you specify.
411
+
And then import the block config in {file}`src/index.ts` along with the css for the weather block.
412
+
413
+
```ts
414
+
importtype { ConfigType } from"@plone/registry";
415
+
importinstallSettingsfrom"./config/settings";
416
+
importinstallBlocksfrom"./config/blocks";
417
+
import"./theme/weather.less";
418
+
functionapplyConfig(config:ConfigType) {
419
+
installSettings(config);
420
+
installBlocks(config);
421
+
return config;
422
+
}
423
+
424
+
exportdefaultapplyConfig;
425
+
```
426
+
427
+
7. **Use the Weather Block:** In Volto's Dexterity-based content types, create or edit a content type that includes the "Weather Block" in the allowedBlocks field. Then, create a content item and add the "Weather Block" to display the weather information for the location you specify.
199
428
200
429
Additionally, you may customize the UI and add more weather details based on the API's response data as needed.
0 commit comments