UNPKG

21.4 kBMarkdownView Raw
1<a href="https://fontawesome.com">
2 <img align="right" width="100" height="100" alt="Official Javascript Component" src="https://img.fortawesome.com/349cfdf6/official-javascript-component.svg">
3</a>
4
5# react-fontawesome
6
7[![npm](https://img.shields.io/npm/v/@fortawesome/react-fontawesome.svg?style=flat-square)](https://www.npmjs.com/package/@fortawesome/react-fontawesome)
8
9> Font Awesome 5 React component using SVG with JS
10
11<!-- toc -->
12
13- [Introduction](#introduction)
14 + [Upgrading Font Awesome?](#upgrading-font-awesome)
15 + [Get started](#get-started)
16 + [Learn about our new SVG implementation](#learn-about-our-new-svg-implementation)
17 + [Going from 0.0.x to 0.1.0](#going-from-00x-to-010)
18- [Installation](#installation)
19- [Add more styles or Pro icons](#add-more-styles-or-pro-icons)
20- [or with Yarn](#or-with-yarn)
21- [Usage](#usage)
22 * [Explicit Import](#explicit-import)
23 * [Build a Library to Reference Icons Throughout Your App More Conveniently](#build-a-library-to-reference-icons-throughout-your-app-more-conveniently)
24 * [Unit Testing](#unit-testing)
25 * [Processing i elements into svg using Font Awesome](#processing-i-elements-into-svg-using-font-awesome)
26- [Features](#features)
27 * [Basic](#basic)
28 * [Advanced](#advanced)
29 * [TypeScript](#typescript)
30- [Frequent questions](#frequent-questions)
31 * [How do I import the same icon from two different styles?](#how-do-i-import-the-same-icon-from-two-different-styles)
32 * [I don't think tree-shaking is working; got any advice?](#i-dont-think-tree-shaking-is-working-got-any-advice)
33- [How to Help](#how-to-help)
34- [Contributors](#contributors)
35- [Releasing this project (only project owners can do this)](#releasing-this-project-only-project-owners-can-do-this)
36
37<!-- tocstop -->
38
39## Introduction
40
41Hey there! We're glad you're here...
42
43#### Upgrading Font Awesome?
44
45If you've used Font Awesome in the past (version 4 or older) there are some
46things that you should learn before you dive in.
47
48> https://fontawesome.com/how-to-use/on-the-web/setup/upgrading-from-version-4
49
50#### Get started
51
52This package is for integrating with React. If you aren't using React then it's
53not going to help you. Head over to our "Get Started" page for some guidance.
54
55> https://fontawesome.com/how-to-use/on-the-web/setup/getting-started
56
57#### Learn about our new SVG implementation
58
59This package, under the hood, uses SVG with JS and the `@fortawesome/fontawesome-svg-core` library. This implementation differs drastically from
60the web fonts implementation that was used in version 4 and older of Font Awesome. You might head over there to learn about how it works.
61
62> https://fontawesome.com/how-to-use/on-the-web/advanced/svg-javascript-core
63
64#### Going from 0.0.x to 0.1.0
65
66See [UPGRADING.md](./UPGRADING.md).
67
68You might also be interested in the larger umbrella project [UPGRADING.md](https://github.com/FortAwesome/Font-Awesome/blob/master/UPGRADING.md)
69
70## Installation
71
72```
73$ npm i --save @fortawesome/fontawesome-svg-core
74$ npm i --save @fortawesome/free-solid-svg-icons
75$ npm i --save @fortawesome/react-fontawesome
76```
77
78## Add more styles or Pro icons
79
80Brands are separated into their own style and for customers upgrading from
81version 4 to 5 we have a limited number of Regular icons available.
82
83**Visit [fontawesome.com/icons](https://fontawesome.com/icons) to search for free and Pro icons**
84
85```
86$ npm i --save @fortawesome/free-brands-svg-icons
87$ npm i --save @fortawesome/free-regular-svg-icons
88```
89
90If you are a [Font Awesome Pro](https://fontawesome.com/pro) subscriber you can install Pro packages; this requires [additional configuration](https://fontawesome.com/how-to-use/on-the-web/setup/using-package-managers).
91
92```
93$ npm i --save @fortawesome/pro-solid-svg-icons
94$ npm i --save @fortawesome/pro-regular-svg-icons
95$ npm i --save @fortawesome/pro-light-svg-icons
96```
97
98## or with Yarn
99
100```
101$ yarn add @fortawesome/fontawesome-svg-core
102$ yarn add @fortawesome/free-solid-svg-icons
103$ yarn add @fortawesome/react-fontawesome
104```
105
106## Usage
107
108You can use Font Awesome icons in your React components as simply as this:
109
110```javascript
111<FontAwesomeIcon icon="coffee" />
112```
113
114That simple usage is made possible when you add the `"coffee"` icon, to the
115_library_.
116
117This is one of the two ways you can use Font Awesome 5 with React. We'll
118summarize both ways briefly and then get into the details of each below.
119
1201. **Explicit Import**
121
122 Allows icons to be subsetted, optimizing your final bundle. Only the icons
123 you import are included in the bundle. However, explicitly importing icons
124 into each of many components in your app might become tedious, so you may
125 want to build a library.
126
1272. **Build a Library**
128
129 Explicitly import icons just once in some init module. Then add them to the
130 library. Then reference any of them by icon name as a string from any
131 component. No need to import the icons into each component once they're in
132 the library.
133
134### Explicit Import
135
136For this example, we'll also reference the `@fortawesome/free-solid-svg-icons`
137module, so make sure you've added it to the project as well:
138
139```
140$ npm i --save @fortawesome/free-solid-svg-icons
141```
142
143or
144
145```
146$ yarn add @fortawesome/free-solid-svg-icons
147```
148
149Now, a simple React component might look like this:
150
151```javascript
152import ReactDOM from 'react-dom'
153import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
154import { faCoffee } from '@fortawesome/free-solid-svg-icons'
155
156const element = <FontAwesomeIcon icon={faCoffee} />
157
158ReactDOM.render(element, document.body)
159```
160
161Notice that the `faCoffee` icon is imported from
162`@fortawesome/free-solid-svg-icons` as an object and then provided to the
163`icon` prop as an object.
164
165Explicitly importing icons like this allows us to subset Font Awesome's
166thousands of icons to include only those you use in your final bundled file.
167
168### Build a Library to Reference Icons Throughout Your App More Conveniently
169
170You probably want to use our icons in more than one component in your app,
171right?
172
173But with explicit importing, it could become tedious to import into each of
174your app's components every icon you want to reference in that component.
175
176So, add them to the _library_. Do this setup once in some initializing module
177of your app, adding all of the icons you'll use in your app's React components.
178
179Suppose `App.js` initializes my app, including the library. For this example,
180we'll add two individual icons, `faCheckSquare` and `faCoffee`. We also add all
181of the brands in `@fortawesome/free-brands-svg-icons`. This example would
182illustrate the benefits of building a library even more clearly if it involved
183fifty or a hundred icons, but we'll keep the example brief and leave it to your
184imagination as to how this might scale up with lots of icons.
185
186Don't forget to add `@fortawesome/free-brands-svg-icons`:
187
188```
189$ npm i --save @fortawesome/free-brands-svg-icons
190```
191
192or
193
194```
195$ yarn add @fortawesome/free-brands-svg-icons
196```
197
198In `App.js`, where our app is initialized:
199
200```javascript
201import ReactDOM from 'react-dom'
202import { library } from '@fortawesome/fontawesome-svg-core'
203import { fab } from '@fortawesome/free-brands-svg-icons'
204import { faCheckSquare, faCoffee } from '@fortawesome/free-solid-svg-icons'
205
206library.add(fab, faCheckSquare, faCoffee)
207```
208
209OK, so what's happening here?
210
211In our call to <span style="white-space:nowrap;">`library.add()`</span> we're passing
212
213- `fab`: which represents _all_ of the brand icons in
214 <span style="white-space:nowrap;">`@fortawesome/free-brands-svg-icons`</span>.
215 So any of the brand icons in that package may be referenced by icon name
216 as a string anywhere else in our app.
217 For example: `"apple"`, `"microsoft"`, or `"google"`.
218- `faCheckSquare` and `faCoffee`: Adding each of these icons individually
219 allows us to refer to them throughout our app by their icon string names,
220 `"check-square"` and `"coffee"`, respectively.
221
222Now, suppose you also have React components `Beverage` and `Gadget` in your app.
223You don't have to re-import your icons into them. Just import the `FontAwesomeIcon`
224component, and when you use it, supply the icon prop an icon name as a string.
225
226We'll make `Beverage.js` a functional component:
227
228```javascript
229import React from 'react'
230import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
231
232export const Beverage = () => (
233 <div>
234 <FontAwesomeIcon icon="check-square" />
235 Favorite beverage: <FontAwesomeIcon icon="coffee" />
236 </div>
237)
238```
239
240There's one another piece of magic that's happening in the background when
241providing icon names as strings like this: the `fas` prefix (for Font Awesome
242Solid) is being inferred as the default. Later, we'll look at what that means
243and how we can do something different than the default.
244
245Now suppose `Gadget.js` looks like this:
246
247```javascript
248import React from 'react'
249import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
250
251export const Gadget = () => (
252 <div>
253 <FontAwesomeIcon icon="check-square" />
254 Popular gadgets come from vendors like:
255 <FontAwesomeIcon icon={['fab', 'apple']} />
256 <FontAwesomeIcon icon={['fab', 'microsoft']} />
257 <FontAwesomeIcon icon={['fab', 'google']} />
258 </div>
259)
260```
261
262Notice:
263
264- We used the `"check-square"` icon name again in this component, though we
265 didn't have to explicitly import it into this component. With one explicit import of
266 that icon in `App.js`, and adding it to the library, we've managed to use
267 it by name in multiple components.
268- We used the `"apple"`, `"microsoft"`, and `"google"` brand icons, which were
269 never explicitly _individually_ imported, but they're available to us by
270 name as strings because `fab` was added to our library in `App.js`, and
271 `fab` includes all of those icons.
272- We added the `fab` prefix to reference those brand icons.
273
274Adding a prefix—and the syntax we used to do it—are new. So what's
275going on here?
276
277First, recall when we introduced `<FontAwesomeIcon icon="coffee"/>` and learned
278that a prefix of `fas` was being added to `"coffee"` by default.
279
280The `"check-square"` icon is getting a default prefix of `fas` here too, which
281is what we want, because that icon also lives in the
282`@fortawesome/free-solid-svg-icons` package.
283
284However, the `"apple"`, `"microsoft"`, and `"google"` brand icons live in the
285package `@fortawesome/free-brands-svg-icons`. So we need to specify a
286different prefix for them—not the default `fas`, but `fab`, for Font Awesome
287_Brand_.
288
289When specifying a prefix with an icon name, both are given as strings.
290
291Now, what about that syntax?
292
293The `icon` prop expects a single object:
294
295- It could be an icon object, like `{faCoffee}`.
296- It could a string object, like `"coffee"`.
297 (The curly braces around a string object supplied to a prop are optional,
298 so we've omitted them.)
299- Or it could be an `Array` of strings, where the first element is a prefix,
300 and the second element is the icon name: `{["fab", "apple"]}`
301
302### Unit Testing
303
304When testing components, you'll want to make sure that any icons referenced in those components are available for testing purposes. You have a couple choices here:
305
3061. If you want to test a child component on its own, you can [import its icons explicitly](#explicit-import).
307
3082. If you've built a library instead, and your test doesn't include your root component that defines your library of icons, you may see errors like this:
309
310 `Could not find icon { prefix: 'fas', iconName: 'chevron-right' }`
311
312 If this happens, and the icon isn't important to the particular test, you can mock FontAwesomeIcon like this:
313
314 ```js
315 import React from 'react'
316
317 export function FontAwesomeIcon(props) {
318 return <i className="fa" />
319 }
320 ```
321
322 With [create-react-app](https://github.com/facebook/create-react-app), you can put this code in `src/__mocks__/@fortawesome/react-fontawesome.js` to automatically include it in any tests, and alleviate errors.
323
324### Processing i elements into svg using Font Awesome
325
326Our hope and intention is that React users will use this package (`react-fontawesome`)
327when using Font Awesome. This component leverages React's architecture and philosophy.
328
329However, **if you cannot use these components everywhere in your app and still
330have `<i>` tags on your page that need to be converted to `<svg>` tags we can
331still help you**.
332
333A basic installation of [Font Awesome](https://fontawesome.com/how-to-use/on-the-web/setup/getting-started?using=svg-with-js) has
334the ability to automatically transform `<i class="fas fa-coffee"></i>` into
335`<svg class="...">...</svg>` icons. This technology works with the browser's
336DOM, [`requestAnimationFrame`][raf], and [`MutationObserver`][mo].
337
338When using the `@fortawesome/fontawesome-svg-core` package this **behavior is
339disabled by default**. (We would _highly_ recommend you use `FontAwesomeIcon`
340if you can) This project uses that core package so you will have to explicitly
341enable it like this:
342
343To configure the core library to convert non-React'ified parts of your App:
344
345```javascript
346import { dom } from '@fortawesome/fontawesome-svg-core'
347
348dom.watch() // This will kick of the initial replacement of i to svg tags and configure a MutationObserver
349```
350
351[raf]: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
352[mo]: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
353
354## Features
355
356The following features are available as part of Font Awesome. Note that the syntax is different from our general web-use documentation.
357
358In the following code snippets, we'll use the shortcut notation for the
359icons—referencing the icons by their names as strings.
360
361But remember, that option is only valid after you've either
362explicitly imported and added those icons to the library, or externally
363loaded an icon bundle. See the sections above for the details.
364
365### Basic
366
367[Size](https://fontawesome.com/how-to-use/on-the-web/styling/sizing-icons):
368
369```javascript
370<FontAwesomeIcon icon="spinner" size="xs" />
371<FontAwesomeIcon icon="spinner" size="lg" />
372<FontAwesomeIcon icon="spinner" size="6x" />
373```
374
375[Fixed width](https://fontawesome.com/how-to-use/on-the-web/styling/fixed-width-icons):
376
377```javascript
378<FontAwesomeIcon icon="spinner" fixedWidth />
379```
380
381Inverse:
382
383```javascript
384<FontAwesomeIcon icon="spinner" inverse />
385```
386
387[List Items](https://fontawesome.com/how-to-use/on-the-web/styling/icons-in-a-list):
388
389```javascript
390<FontAwesomeIcon icon="spinner" listItem />
391```
392
393[Rotate](https://fontawesome.com/how-to-use/on-the-web/styling/rotating-icons):
394
395```javascript
396<FontAwesomeIcon icon="spinner" rotation={90} />
397<FontAwesomeIcon icon="spinner" rotation={180} />
398<FontAwesomeIcon icon="spinner" rotation={270} />
399```
400
401Flip horizontally, vertically, or both:
402
403```javascript
404<FontAwesomeIcon icon="spinner" flip="horizontal" />
405<FontAwesomeIcon icon="spinner" flip="vertical" />
406<FontAwesomeIcon icon="spinner" flip="both" />
407```
408
409Spin and pulse [animation](https://fontawesome.com/how-to-use/on-the-web/styling/animating-icons):
410
411```javascript
412<FontAwesomeIcon icon="spinner" spin />
413<FontAwesomeIcon icon="spinner" pulse />
414```
415
416[Border](https://fontawesome.com/how-to-use/on-the-web/styling/bordered-pulled-icons):
417
418```javascript
419<FontAwesomeIcon icon="spinner" border />
420```
421
422[Pull left or right](https://fontawesome.com/how-to-use/on-the-web/styling/bordered-pulled-icons):
423
424```javascript
425<FontAwesomeIcon icon="spinner" pull="left" />
426<FontAwesomeIcon icon="spinner" pull="right" />
427```
428
429Your own class names:
430
431```javascript
432<FontAwesomeIcon icon="spinner" className="highlight" />
433```
434
435### Advanced
436
437[Power Transforms](https://fontawesome.com/how-to-use/on-the-web/styling/power-transforms):
438
439```javascript
440<FontAwesomeIcon icon="spinner" transform="shrink-6 left-4" />
441<FontAwesomeIcon icon="spinner" transform={{ rotate: 42 }} />
442```
443
444[Masking](https://fontawesome.com/how-to-use/on-the-web/styling/masking):
445
446```javascript
447<FontAwesomeIcon icon="coffee" mask={['far', 'circle']} />
448```
449
450[Symbols](https://fontawesome.com/how-to-use/on-the-web/advanced/svg-symbols):
451
452```javascript
453<FontAwesomeIcon icon="edit" symbol />
454<FontAwesomeIcon icon="edit" symbol="edit-icon" />
455```
456
457[Layers](https://fontawesome.com/how-to-use/on-the-web/styling/layering):
458
459```javascript
460<span className="fa-layers fa-fw">
461 <FontAwesomeIcon icon="square" color="green" />
462 <FontAwesomeIcon icon="check" inverse transform="shrink-6" />
463</span>
464```
465
466### TypeScript
467
468Typings are included in this package. However, most types are defined in the
469underlying API library, `@fortawesome/fontawesome-svg-core`.
470
471Suppose that in one module, you add all `fas` icons to the library:
472
473```typescript
474import { library } from '@fortawesome/fontawesome-svg-core'
475import { fas } from '@fortawesome/free-solid-svg-icons'
476
477library.add(fas)
478
479// ...
480```
481
482Then suppose that in another module, you have some code that looks up
483one of the icons in the library. The `import` statement below imports two types
484and one function:
485
486```typescript
487import {
488 IconLookup,
489 IconDefinition,
490 findIconDefinition
491} from '@fortawesome/fontawesome-svg-core'
492
493const coffeeLookup: IconLookup = { prefix: 'fas', iconName: 'coffee' }
494const coffeeIconDefinition: IconDefinition = findIconDefinition(coffeeLookup)
495
496// ...
497
498export class App extends React.Component {
499 render() {
500 return (
501 <div className="App">
502 <h1>
503 <FontAwesomeIcon icon={coffeeIconDefinition} />
504 </h1>
505 </div>
506 )
507 }
508}
509```
510
511NOTE: You wouldn't normally declare intermediate objects like `coffeeLookup`
512just to look up an icon. So this is cumbersome and needlessly verbose for such
513a simple example. The purpose here is just to show how you might import type
514definitions and use them in declarations when it _does_ make sense to do so.
515
516Several types, including `IconLookup` and `IconDefinition`, appearing above,
517actually originate from the `@fortawesome/fontawesome-common-types` package.
518They are re-exported from both `@fortawesome/fontawesome-svg-core` and
519`@fortawesome/free-solid-svg-icons` (and other icon packs). This is just to
520make importing more convenient in some cases. Refer to the `index.d.ts` in any
521module to see which types it exports.
522
523## Frequent questions
524
525### How do I import the same icon from two different styles?
526
527With ES modules and `import` statements we can rename:
528
529```javascript
530import { library } from '@fortawesome/fontawesome-svg-core'
531import { faStroopwafel as fasFaStroopwafel } from '@fortawesome/pro-solid-svg-icons'
532import { faStroopwafel as farFaStroopwafel } from '@fortawesome/pro-regular-svg-icons'
533
534library.add(fasFaStroopwafel, farFaStroopwafel)
535```
536
537### I don't think tree-shaking is working; got any advice?
538
539Check out our [docs here](https://fontawesome.com/how-to-use/with-the-api/other/tree-shaking).
540
541## How to Help
542
543Review the following docs before diving in:
544
545- [CONTRIBUTING.md](CONTRIBUTING.md)
546- [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md)
547
548And then:
549
5501. Check the existing issue and see if you can help!
551
552## Contributors
553
554The following contributors have either hepled to start this project, have contributed
555code, are actively maintaining it (including documentation), or in other ways
556being awesome contributors to this project. **We'd like to take a moment to recognize them.**
557
558| | Name | GitHub |
559| :--------------------------------------------------------: | -------------- | -------------------------------------------------- |
560| <img src="https://github.com/NateRadebaugh.png?size=72" /> | Nate Radebaugh | [@NateRadebaugh](https://github.com/NateRadebaugh) |
561| <img src="https://github.com/kirkbross.png?size=72" /> | Kirk Ross | [@kirkbross](https://github.com/kirkbross) |
562| | Prateek Goel | [@prateekgoel](https://github.com/prateekgoel) |
563| <img src="https://github.com/naortor.png?size=72" /> | Naor Torgeman | [@naortor](https://github.com/naortor) |
564| <img src="https://github.com/mmhand123.png?size=72" /> | Matthew Hand | [@mmhand123](https://github.com/mmhand123) |
565| <img src="https://github.com/calvinf.png?size=72" /> | calvinf | [@calvinf](https://github.com/calvinf) |
566
567If we've missed someone (which is quite likely) submit a Pull Request to us and we'll get it resolved.
568
569The Font Awesome team:
570
571| | Name | GitHub |
572| :--------------------------------------------------------: | -------------- | -------------------------------------------------- |
573| <img src="https://github.com/supercodepoet.png?size=72" /> | Travis Chase | [@supercodepoet](https://github.com/supercodepoet) |
574| <img src="https://github.com/robmadole.png?size=72" /> | Rob Madole | [@robmadole](https://github.com/robmadole) |
575| <img src="https://github.com/mlwilkerson.png?size=72" /> | Mike Wilkerson | [@mlwilkerson](https://github.com/mlwilkerson) |
576| <img src="https://github.com/talbs.png?size=72" /> | Brian Talbot | [@talbs](https://github.com/talbs) |
577
578## Releasing this project (only project owners can do this)
579
580See [DEVELOPMENT.md](DEVELOPMENT.md#release)