Introducing Font Awesome 5 and SVG icons

Just thought I’d post how I got these working with html after reading closely through the thread. (I would say in future to let everyone know in the original thread about the new system settings to get icons going on headers etc).

To add some icons go to /admin/site_settings/category/all_results?filter=icons and add the prefixes accordingly. “Add additional FontAwesome 5 icons that you would like to include in your assets. Use prefix ‘fa-’ for solid icons, ‘far-’ for regular icons and ‘fab-’ for brand icons.”

Eg fa-newspaper.

Then reference them in your html. Eg <svg class="fa d-icon d-icon-newspaper svg-icon svg-node" aria-hidden="true"><use xlink:href="#newspaper"></use></svg>.

לייק 1

That worked so well I’d like to add a second button. I naively tried:

<script type="text/discourse-plugin" version="0.4">
const { iconNode } = require("discourse-common/lib/icon-library");
api.decorateWidget('header-icons:before', helper => {
    return helper.h('li',
      [
        helper.h('a#home-button.icon', {
            href:'https://docs.google.com/spreadsheets',
            title: 'Spreadsheet'
        }, iconNode('table')),
        helper.h('a#home-button.icon', {
            href:'https://google.com',
            title: 'Google'
        }, iconNode('globe')),
      ]
    );
});
</script>

But that gave a weird alignment:

image

How can I make the globe vertically aligned with the rest of the buttons?

3 לייקים

There is a well-maintained theme component to use that is already updated to works with FA5 and that add more than 1 button before the header icons:

12 לייקים

Can I have some help?
I have previous registered a couple of extra icons:

api.decorateWidget('header-icons:before', helper => {
    return [
        helper.h('li', [
            helper.h('a#home-button.icon', {
                href: '/',
                title: 'McRides Home'
            }, helper.h('i.fa.fa-home.home-button-icon.d-icon')),
        ]),
                helper.h('li', [
            helper.h('a#weather-button.icon', {
                href: 'https://www.mcrides.co.nz/t/weather-forecast/77',
                title: 'Weather Forecast'
            }, helper.h('i.fa.fa-cloud.weather-button-icon.d-icon')),
        ])
    ];
});

I can see in this thread how I would redo this (or I’m not that skilled :slight_smile: probably this )

לייק 1

The theme component introduced just before will help you do it really easily

But if you really want to do it manually, the first post of this howto will help

3 לייקים

how do i
add the icon to the svg icon subset site setting

Here’s what I’ve got:

api.decorateWidget('header-icons:before', helper => {
    return [
        helper.h('li', [
            helper.h('a#home-button.icon', {
                href: '/',
                title: 'McRides Home'
            }, iconNode('home')),
        ]),
        helper.h('li', [
            helper.h('a#weather-button.icon', {
                href: 'https://www.mcrides.co.nz/t/weather-forecast/77',
                title: 'Weather Forecast'
            }, iconNode('cloud-sun')),
        ])
    ];
});

the home icon works, the cloud-sun (I also tried cloud) doesn’t

לייק 1

Search for the setting svg icon and type the name of the icon with the right prefix

image

fa-cloud or fa-cloud-sun and it should work after a refresh of the page

4 לייקים

Does this mean that Discourse still ships with Font Awesome even though it uses SVG icons ?

לייק 1

Font Awesome is itself SVG in V5

3 לייקים

So v 2.2 does come with FA pre loaded ? I thought you skipped it for its size.

לייק 1

You should check our release topic to know more about included features in version 2.2. Also the blog post.

5 לייקים

I read it. It says about dropping the font icon.

We’ve also fully converted to SVG icons instead of relying on a font icon. This is more efficient than shipping a giant and mostly unused icon font. Plus, it’s now much easier to switch any visible icon in Discourse with something else, even a completely custom one-off glyph that you created.

Prefer to use your custom logo instead of the default like heart? Make it so!

לייק 1

Yes, we moved to SVG icons instead of fonts. But that doesn’t mean we skipped Font Awesome :wink:.

4 לייקים

I see. So you are using FA as SVG images ? I never knew we can extract them individually.

לייק 1

Thanks !
I didn’t even know it was there :slight_smile:

לייק 1

Anybody can help?
I need to replace this code to svg:
<i class="fa fa-long-arrow-left" aria-hidden="true"></i>
Thank you so much!

לייק 1

That specific icon has been removed from the list of free font awesome icons. The closest equivalent is long-arrow-alt-left. If you are limited to using HTML, you would use:

<svg class="fa d-icon d-icon-long-arrow-alt-left svg-icon svg-node" aria-hidden="true">
  <use xlink:href="#long-arrow-alt-left"></use>
</svg>

You will also need to add long-arrow-alt-left to the svg icon subset site setting.

9 לייקים

Thanks @tshenry
You are so awesome :smile:
I tried to use long-arrow-alt-left, it worked well.
But can you tell me more if I still want to use long-arrow-left.

לייק 1

Unfortunately, as I mentioned above, that icon is not available in the free tier of Font Awesome icons. If long-arrow-alt-left will not work for you, I think your best option might be to create your own version and use a plugin as described in the post above.

9 לייקים

oh, I see :joy:
Thanks @tshenry so much!

2 לייקים