Skip to content

Commit f0c63e8

Browse files
committed
Add blog post about using assets
1 parent 1b09096 commit f0c63e8

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Using assets in a project
3+
description: Step-by-step guide on how to add assets like images or icons to your theme.
4+
slug: using-assets
5+
authors: iobrado
6+
date: 2022-05-02
7+
tags: [eightshift, boilerplate, assets, images, icons]
8+
hide_table_of_contents: false
9+
---
10+
11+
Previously we went through the process of adding fonts to your project. While the process of adding additional assets like images or icons has some similarities to adding fonts, it also has its unique steps. In this post, we'll cover multiple ways of adding assets and using them on your site.
12+
<!--truncate-->
13+
14+
## Adding images
15+
16+
Similar to fonts, there is also a dedicated folder for adding images that will be used in a theme. The location of this folder is **_assets/images_**. The procedure to add images is even simpler than fonts, which you can read about in [Adding fonts](/blog/adding-fonts) blog post. Just follow these steps to add a new image:
17+
- add the image in inside **_assets/images_** folder
18+
- include it in **_assets/images/index.js_**
19+
- run `npm start` to rebuild assets
20+
21+
The new image will now be available in the **_public_** folder. The use case for this is adding a favicon or a logo to your project. To use this image in one of your templates or blocks, you have to add the following in your **_php_** file:
22+
23+
```php
24+
use YourNamespace\Manifest\Manifest;
25+
// ...
26+
27+
apply_filters(Manifest::MANIFEST_ITEM, 'logo.svg');
28+
```
29+
30+
The filter we are using is called `manifest-item` and we use it to get the URL of the asset from the **_public_** folder. You can read more about this in [our documentation](/docs/basics/manifest).
31+
32+
You can see how this is being used for rendering both favicon and header logo in your theme's **_header.php_** file.
33+
34+
> **Tip**: Don't hardcode the filter name in the `apply_filters` function. Always call it via class constants.
35+
36+
For better organization, you can add additional folders (e.g. **_icons_**, **_placeholders_**) inside **_assets/images_** folder. Here's an example how to include them:
37+
38+
```js
39+
// Icons
40+
import './icons/upload.svg';
41+
42+
// Placeholders
43+
import './placeholders/post.png';
44+
import './placeholders/page.png';
45+
```
46+
47+
## Using SVG files from manifest
48+
49+
Another approach you can take is to include an SVG file inside your component or block manifest. As a matter of fact, we already mentioned that component in a previous blog post about [Modifying Blocks](/blog/modifying-blocks-color-theme). We are talking about Quote component.
50+
51+
> If you don't have it in your project, be sure to read our blog post about adding blocks and components by using [WP CLI](/blog/adding-blocks-wpcli).
52+
53+
Open **_src/Blocks/components/quote/manifest.json_** and you'll see that all icons used by the component are defined inside `icons` as key-value pairs. Key represents the name which we will use to fetch the icon, while the value is SVG code.
54+
55+
```json
56+
"resources": {
57+
"icon": "<svg fill='none' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'>...</svg>"
58+
}
59+
```
60+
61+
In order to make minification of SVG files as easy as possible, our teammate Goran made an awesome tool called [SVG2WP](https://svg-2-wp.goranalkovic.com/). Some of the options include making attributes JSX compatible, or replacing the color value with `currentColor`, which can then be used to change the SVG color through CSS.
62+
63+
If you've been reading through these blog posts regularly, then you've already seen the use of `currentColor`. In our blog post about [Modifying Blocks](/blog/modifying-blocks-color-theme), we modified the color of the SVG.
64+
65+
The output of the icon on frontend is very simple. In the Quote component, it was done the following way:
66+
```php
67+
<?php $manifest = Components::getManifest(__DIR__); ?>
68+
69+
<i class="<?php echo \esc_attr("{$componentClass}__icon"); ?>">
70+
<?php echo $manifest['resources']['icon']; // phpcs:ignore Eightshift.Security.ComponentsEscape.OutputNotEscaped ?>
71+
</i>
72+
```
73+
74+
An excellent example where you can see in even more detail how SVGs are being used is our `icon` component. It isn't included in Eightshift theme by default, so you have to add it to your project with WP CLI. To include it in your project, use the following command:
75+
76+
```bash
77+
wp boilerplate use_component --name=icon
78+
```
79+
80+
If you include the Icon component inside a block, you will have the option to choose between multiple icons defined in the manifest. Another way to render SVGs from the Icon component is by using the `Components::render` helper method:
81+
82+
```php
83+
<?php
84+
echo Components::render(
85+
'icon',
86+
[
87+
'blockClass' => $componentClass,
88+
'iconName' => 'download',
89+
]
90+
);
91+
?>
92+
```
93+
94+
Here are some examples of icons available out-of-the-box in our Icon component:
95+
96+
![Icon component](/img/blog/icon-component.png)
97+
98+
## Using icons for editor and block options
99+
100+
When developing your blocks and adding new options, you may need to add some icons to improve the user experience. We have many icons already available for use. You can see the full list in our [Storybook](/storybook) under `Editor -> Icons` section. We already added the icon when adding a new Color Theme option for the Quote block. Here is the simplified version:
101+
```jsx
102+
import { ColorPaletteCustom, IconLabel, icons } from '@eightshift/frontend-libs/scripts';
103+
104+
return (
105+
<ColorPaletteCustom
106+
label={<IconLabel icon={icons.color} label={__('Color Theme', 'es-theme')} />}
107+
// ...
108+
/>
109+
);
110+
```
111+
112+
This was the end result when we were adding a new option in our Quote block:
113+
114+
![Color Theme Options](/img/blog/color-theme-options.png)
115+
116+
## Conclusion
117+
118+
As you could see in this blog post, there are multiple ways of adding assets to a project. It all depends on how these will be used and what the scope of their use will be.
5.76 KB
Loading

0 commit comments

Comments
 (0)