React-fontawesome: Functionality Question

Created on 14 Feb 2018  路  6Comments  路  Source: FortAwesome/react-fontawesome

Hey guys,

Love the library, but have a noob question. I have a react app that is using pseudo-selectors in order to pull off some effects incorporating the font awesome icons. Seems if I have to use pseudo-selectors the documentation states I need either a script tag or to pull the whole file into my project and import it from there. If I'm forced to do that though, is there any reason for me to use this library? I like the idea of pulling in only what I need, but it seems like I'll have to pull in the entire fontawesome file which makes this seem pointless. Am I missing anything here? Would love some guidance!

Most helpful comment

@johnnunns and future travellers, you can achieve it like this using styled-components:

import * as React from 'react';
import { renderToString } from 'react-dom/server';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faGithub } from '@fortawesome/free-brands-svg-icons'

const ListItem = styled.li`
  &:before {
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faGithub} />))});
  }
`;

All 6 comments

Hey @johnnunns. So what exactly are the pseudo-selectors doing? If you are relying on pseudo-selectors to replace your <i> tags with <svg> in your React project you are definitely doing it the wrong way. We'll be happy to get you straightened out though ;)

Thanks @robmadole for the quick response. I am using React Inline Edit Kit (RIEK) library in order to create an input that's editable on Click. Unfortunately, the RIEK library hasn't built a way to create a ref that I can run an open() function on to initiate the edit mode. So to give the edit icon the functionality of the onclick the riek input has, I've been using the scss solution:

.editableText {
&::after {
font-family: FontAwesome 5 Pro;
font-size: 12pt;
content: "\f040";
margin-left: 8px;
color: #F8B97F;
vertical-align: top;
cursor: text;
}
}

Is this the most efficient way of pulling this off? Does the use of this react library make sense if this effect is going to force me to use a tag in the header?

Hmm, if you are using that library you might be better off switching to Web Fonts with CSS. We haven't tried pseudo elements in tandem with react-fontawesome and we honestly don't know what will happen.

Here are the docs for Web Fonts: http://fontawesome.com/get-started/web-fonts-with-css

We also have these available as NPM packages: https://www.npmjs.com/package/@fortawesome/fontawesome-free-webfonts

If you have any more questions I'll be happy to re-open this.

@johnnunns and future travellers, you can achieve it like this using styled-components:

import * as React from 'react';
import { renderToString } from 'react-dom/server';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faGithub } from '@fortawesome/free-brands-svg-icons'

const ListItem = styled.li`
  &:before {
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faGithub} />))});
  }
`;

How would one go about changing the color of the content item? I can only get it as black. @eqyiel

This should fix you up @wispyco !

const ListItem = styled.li`
  &:before {
    color: blue;
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faGithub} />))});
  }
`;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gtkatakura-bysoft picture gtkatakura-bysoft  路  5Comments

Ethan-Arrowood picture Ethan-Arrowood  路  4Comments

jducro picture jducro  路  5Comments

jonathanazulay picture jonathanazulay  路  5Comments

v8tix picture v8tix  路  4Comments