In part one of this series we demoed reactstrap
to create simple forms quickly and efficiently. In this installment, weโre going to use Font Awesome 5 to add some SVG icons and make our forms even more attractive.
Note: Font Awesome 5.1 welcomed huge improvements which included a complete rewrite of the codebase. If you are using version 4, follow their upgrade steps to ensure consistency.
Part 2: Using Font Awesome 5 in React ๐ฆ
FA went all out with version 5 by rewriting their entire codebase and providing users with new and refreshing features.
V5 adds a wide variety of icons (over 1000!) and even category packs that include travel, emoji ๐, and design in a fancy SVG format.
If youโve worked with Font Awesome in the past, something like โfa-profileโ might look familiar. In V5 FA introduced icon styles like โfasโ for Font Awesome solid, โfalโ for Font Awesome Light, โfabโ for Font Awesome Brands and lastly โfarโ for Font Awesome Regular so the new icons might look more like โfar fa-profileโ. This adds a great deal of flexibility for UI/UX design.
Additionally, there are FA packages (like the one weโre about to use!) for React, Angular, Vue and Ember.
To fund this rapid and robust development, FA introduced Font Awesome Pro which gives users additional design flexibility and functionality. If youโre a fan of the free functionality in V4, not to fret! All functionality in V4 remains in the free plan with bug fixes and updates.
Check out their update page for the full scoop.
Letโs pick up from where we left off in part one. We took the liberty of adding some CSS to get an ๐.io feel.
To get started, weโre going to install react-fontawesome
along with the SVG libraries fontawesome-svg-core
and fontawesome-svg-icons
. The packages weโre installing only contain the free version. If youโre looking to utilize new pro icons and styles, check out their Github repo for additional installation and configuration instructions.
Hereโs how to install whatโs needed via npm or Yarn:
$ npm install @fortawesome/react-fontawesome @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons
# or, using Yarn:
$ yarn add @fortawesome/react-fontawesome @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons
Now that weโre golden on the installation, weโre going to jump right in and implement these bad boys.
There are multiple ways to use FA icons but weโre going to focus on building a library to easily access all icons throughout an app.
In our App.js
file, weโre going to import the icons we need.
In this example weโre going to use the envelope and key icons. All icons can be found in Font Awesomeโs icon library. All icon imports are converted to camelCase with fa at the start leaving out all the dashes. For example, the key icon will be imported as faKey. Weโll start by importing the required files and calling fontawesome-svg-core
โs 'library.add
to pull our icons.
import { library } from '@fortawesome/fontawesome-svg-core';
import { faEnvelope, faKey } from '@fortawesome/free-solid-svg-icons';
library.add(faEnvelope, faKey);
// ...
As our project grows, we only need to import and add icons to our library in App.js
. Imagine that we have a component called SignUp.js
where we need to use these icons. Since they have already been added to the library in App.js
we simply add the import below for the ** component.
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
/// ...
Next, as part of the Username label, weโre going to use the FontAwesomeIcon
component.
// ...
<Label>
<FontAwesomeIcon
icon="envelope"
/>
Username
</Label>
Notice that the icon name provided isnโt written the same as when itโs imported. When passing the icon name as a prop to the FontAwesomeIcon component, we only need the iconโs name in lowercase letters. For additional styling I also added a className
. Now just rinse and repeat for the key
icon. The result should look something like this:
Our icons and text are a little squished and, dare I say, boring, so letโs add a space between the icon and the label text and change the iconโs color and size:
// ...
<label>
<FontAwesomeIcon
icon="envelope"
color="#6DB65B"
size="sm"
/>
{' '}Username
</label>
As you can see, the FontAwesomeIcon component can take a few different props to change the icon style. Here we used the color
and size
props. The size
prop expects a string value like xs, lg, 2x, 3xโฆ There are quite a few more props that can be passed-in. Notably, the spin
and pulse
boolean props will have the icon rotate on itself.
Thatโs all there is too it! Look at that lovely UI.
reactstrap
+ react-fontawesome
= one happy dev ๐
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the โreport an issueโ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.