That’s where Sass comes in. Sass allows you to extend the CSS language with variables, divide style sheets into multiple files, and more. Sass also compiles its source files into a single CSS file that you can include in your web page just like any other CSS style sheet.
Looking to get started with Sass and SCSS? Check out our Introduction to SCSS.
In this blog post, you’ll learn how to integrate Zurb’s Foundation framework using Sass mixins to minimize your CSS size and make more semantic markups.
You will be using the Create React App command-line tool to create the application. First of all, make sure that you have installed Node on your system with a version greater than 10. If you are unsure which Node version you have, open a terminal and run the following command.
Provided your Node version is up to date, the Create React App command-line tool can be run without any installation. In the terminal, navigate to a directory of your choice and run the following command.
This will create a new folder,
react-calculator initialize the React application and download all necessary packages. After a minute or two, once the process has finished, change into the new folder with the command below.
Any serious web application will need some sort of user authentication. Implementing your own not only takes time and effort but can also introduce security risks if you don’t know exactly what you are doing. Okta lets you set up authentication with just a few commands.
If you don’t already have an account with Okta, you need to register for a free account. Open your browser and navigate to https://developer.okta.com. Click on Create Free Account and complete the registration process.
Once you are done you can see your Okta developer dashboard. Click on Applications > Add Application to register a new application. On the next screen, choose Single Page App and click Next.
The following screen lets you edit the application’s settings. Under Allowed grant types, check the box Authorization Code. Then make sure that the port number is 3000. Change the base URI to
http://localhost:3000/ and set the Login Redirect URI to
Once you are done, you will see a Client ID which you will need in a minute.
Okta has created a tool to add authentication to React applications in seconds. It uses the Angular Schematics command-line tool to inject code into your skeleton application. Install the Schematics CLI on your system by running the following command in your terminal.
TIP: Depending on your system, you might need to run this command using
You might be asking why I am telling you to use an Angular tool when you are developing a React application? It turns out that this tool is generic and works for Angular and Vue too! Not only that, but it even has support for Ionic and React Native!
Install OktaDev Schematics:
Now, add Okta for authentication to your React application by running the command below.
You will be asked for your issuer’s URL. This can be found at API > Authorization Servers in your Okta dashboard. .
You will also need the application’s client ID which you received when earlier when setting up your application. Once the questions have been answered, the schematic will insert all the necessary code into your application to provide the authentication flow. Pretty neat, don’t you think?
Now it’s time to implement the calculator. This calculator is a nice demonstration of how to use stack operations to process user input and perform mathematical operations. The calculator has four basic operations
/, as well as a
% button. It takes care of operator precedence.
Create a new file called
src/Calculator.js and paste the following code into it.
You can see that the HTML of the calculator consists of groups of buttons. Each button is linked to a callback function in the
Calculator class. To give you an insight into what the stack contains at any time, a separate
div contains a table displaying the stack entries.
src/Home.js and remove the import of
logo.svg. Add the following import to the top of the file.
Further down in the file, replace the
render() function with the following.
Cool! The calculator should be functional. If you run
npm start, you should be able to open your browser at
http://localhost:3000 and see a Login button. After logging in you will be able to use the calculator. But we haven’t styled it yet, so it won’t look nice.
Back in the days when CSS was all the rage, it came with a promise of a bright future where content and style were completely separated. Your HTML code would only contain the actual content and the markup would be completely semantic. One of the promises was that class names would only relate to the meaning of the content and not the way that it was displayed on the screen.
Then along came CSS frameworks. They introduced CSS classes for creating a responsive grid layout, such as
col-md-4. Other classes determined the size of buttons, such as
btn-sm. While incredibly useful, this broke the separation of content and design.
Using Sass together with the Foundation CSS framework, it is possible to regain this strict separation. You might have noticed in the code above, that I have used semantic
className attributes. The classes tell you what is contained in a
div and not how it should be shown on the screen. This actually has an accessibility advantage because screen readers can group the contents in a meaningful way.
To get started with Sass and Foundation, install two more packages. In the terminal run the following command.
There are actually two Sass packages available through
node-sass is generally faster but does require a compilation step during installation. I have opted for the more compatible
Copy the default settings from the Foundation folder to your
src/ folder by running the following command in a terminal.
src/_settings.scss and, and change line 63 (
@import 'util/util';) to the following:
If you look through the settings file, you can see that it defines a huge number of Sass variables. Foundation is highly customizable but the default settings provide a good starting point. Now rename
src/App.scss and replace its content with the following.
In this style sheet, I have used a number of Sass features. I have used a number of
@include statements to include mixins from the Foundation framework.
@include xy-cell(6); will add styles to the surrounding class to turn it into a 6 column wide cell. Note also, how I have used the
$secondary-color variables to define the button style of the reset and the number buttons.
These variables are defined in the
src/_settings.scss file. If you want to learn more about the Sass language, the official documentation is a good starting point.
How I Theme My React App With Sass is a good tutorial that teaches you more about the power of mixins.
To include the stylesheet in your application, open
Home.js and change the import of
App.css to the following.
The React scripts installed by the
create-react-app command use webpack to compile and package everything. By default, they also install the module loaders for
.scss files. By changing the import above, everything should now work out of the box without any further configuration. Simply run the following command and your perfectly styled calculator will be available on
Congratulations, you should now see something like this in your browser.
In this tutorial, you learned how to use Sass in a React application. Using the
create-react-app command makes it extremely easy to include Sass without any further configuration needed. I also showed you how to achieve truly semantic markup in your application while still using a powerful CSS framework. In this tutorial, I used Zurb’s Foundation as an example. You can achieve a similar effect when using the Bootstrap framework.
You can find the source code for this tutorial on GitHub in the oktadeveloper/okta-react-sass-example repository.
If you want to learn more about Sass, React, or the use of Schematics, feel free to check out the links below.