Sam
Sam

Reputation: 2331

Using font awesome icon for background image in React

I have a dropdown, where the caret is styled using the CSS background-image property. I'd like to figure out how to get this working using a font awesome icon.

I tried the following two background image styles, but neither worked, the result is just a dropdown without a caret

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCaretDown } from '@fortawesome/free-solid-svg-icons';

backgroundImage: `url("data:image/svg+xml, ${faCaretDown})`
backgroundImage: `url("data:image/svg+xml, ${<FontAwesomeIcon icon={faCaretDown} />})`

Here is the original code for my dropdown

select{
  box-shadow: 0 0 0.2rem rgba(0, 0, 0, 0.7);
  border-radius: 1rem;
  padding-top: 0.25rem;
  padding-bottom: 0.25rem;
  padding-right: 1rem;
  padding-left: 0.5rem;
  border: 0;
  appearance: none;
  -moz-appearance: none;
  -webkit-appearance: none;
  background-image: url('data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23007CB2%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E');
  background-repeat: no-repeat, repeat;
  background-position: right .7em top 50%;
  background-size: .65em auto;
}
<select type="dropdown">
  <option>Option 1</option>
  <option>Option 2</option>
</select>

Upvotes: 2

Views: 4858

Answers (2)

Aaron J Spetner
Aaron J Spetner

Reputation: 2155

You can get the SVG from the icon via the fontawesome-svg-core library. I made this utility function to allow me to also change the color of the icon (since it won't inherit from CSS when used as a background-image.

import { toHtml, icon } from "@fortawesome/fontawesome-svg-core";
import { faCaretDown } from "@fortawesome/free-solid-svg-icons";
const getSVGURI = (faIcon, color) => {
  const abstract = icon(faIcon).abstract[0];
  if (color) abstract.children[0].attributes.fill = color;
  return `data:image/svg+xml;base64,${btoa(toHtml(abstract))}`;
};

<div style={{ backgroundImage: `url(${getSVGURI(faCaretDown)})` }}/>

Demo with all icons

If you prefer to do it without a library, you can use this function instead:

const getSVGURI = ({ prefix, iconName, icon }, color) =>
  `data:image/svg+xml;base64,${btoa(
    `<svg data-prefix="${prefix}" data-icon="${iconName}"
      xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${icon[0]} ${icon[1]}">
      <path fill="${color || "currentColor"}" d="${icon[4]}"></path>
    </svg>`)}`;

Upvotes: 4

Hasham Minhas
Hasham Minhas

Reputation: 508

Try like this

const reactSvgComponentToMarkupString = (Component) =>
  `data:image/svg+xml,${encodeURIComponent(
    renderToStaticMarkup(createElement(Component))
  )}`;

&:after {
    content: url(${reactSvgComponentToMarkupString(FaArrowRight)});
    position: absolute;
    opacity: 1;
  }

but for this, you need to use Styled Components

https://codesandbox.io/s/friendly-ptolemy-2h04m?file=/src/index.js:504-625 This link will be helpful for you.

Upvotes: 2

Related Questions