Giter VIP home page Giter VIP logo

react-fontawesome's Introduction

This package allows you to color individual pieces of Font Awesome icons, using React.

It is basically the same as @fortawesome/react-fontawesome, except <FontAwesomeIcon> accepts a new prop fill={['color1', 'color2', 'color3']} to specify the colors of the different parts of the icon.

npm badge

Installation

Install React FontAwesome as usual, except @fortawesome/react-fontawesome you replace by @ntag/react-fontawesome:

$ npm i --save @fortawesome/fontawesome-svg-core \
  npm i --save @fortawesome/free-solid-svg-icons \
  npm i --save @ntag/react-fontawesome

Usage

In your main file (index.js, App.js):

import { library } from '@fortawesome/fontawesome-svg-core';
import { faCloudSun } from '@fortawesome/free-solid-svg-icons';

library.add(faCloudSun);

In your component:

import FontAwesomeIcon from '@ntag/react-fontawesome';

export const Weather = () => (
  <div>
    <h1>Weather</h1>
    <FontAwesomeIcon icon="cloud-sun" fill={['#3C6997', '#EDFF71', '#F1DB4B']} />
  </div>
);

fill accepts an array of CSS colors. Use currentColor to use the text color. The text color will also be used if the fill array is smaller than the number of pieces in the icon.

Finally, if you don't pass the fill property, <FontAwesomeIcon> will work exactly the same as the original one from @fortawesome/react-fontawesome.

Also, instead of using an array, you can also use multiple props: fill0, fill1, fill2, fill3 to colors the parts 1, 2, 3 and 4 for example.

How does it work?

Font Awesome icons are composed by a single <path> SVG element.

First, the path is split on m or M letters, indicating a movement of the pencil, to get multiple pieces.

We have multiple pieces but two problems: the paths are not correctly positionned, and the subtraction of shapes doesn't work anymore. We start by recomputing the correct position of each piece.

It looks better. However, the second piece should be subtracted to the first one, to create a hole. Even if the two paths are drawn in opposite directions, the subtraction doesn't work because they are now in two different <path>. We need to merge elements which work together.

To do that, the algorithm takes each path, computes if it intersects the previous one, and if that's the case, merge the two <path> together, so the subtraction works. If that's not the case, it doesn't merge them and process the next path.

react-fontawesome's People

Contributors

ntag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

l0g4n1337

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.