Introducing Font Awesome 5 and SVG icons

Ok, adding custom icons in a plugin should now be doable as of
https://github.com/discourse/discourse/commit/47cbfb14989bcf828c66b211827cb9355214e71a

So, basically, add the SVG sprites to your plugin, in plugins/svg-icons. See https://raw.githubusercontent.com/discourse/discourse/master/vendor/assets/svg-icons/discourse-additional.svg for an example. Next, register your icon, and you should be good to go.

(I would prefix the icon ids in the plugin, to avoid any potential clash with FontAwesome icon names.)

18 Likes

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

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?

1 Like

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:

11 Likes

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 )

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

2 Likes

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

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

3 Likes

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

Font Awesome is itself SVG in V5

1 Like

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

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

3 Likes

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!

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

3 Likes

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

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

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!

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.

8 Likes

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 Like

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.

8 Likes