02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating SASS Variables

In this tutorial, we’ll integrate SASS into our React project and explore the power of SASS variables. SASS extends CSS by adding programming-like features such as variables, making styles more modular and reusable. This lesson covers configuring Webpack for SASS, creating variables, and understanding their application in dynamic styling.


1. Installing Required Packages

To work with SASS, you need two additional packages:

  1. SASS Loader: Allows Webpack to process SASS files.
  2. Node SASS: Provides access to the SASS library.

Run the following command in your terminal:

npm install --save-dev sass-loader node-sass

2. Configuring Webpack for SASS

  1. Update the Webpack Configuration:
    In your webpack.config.js file, add support for SASS files. Modify the module rules to include:

    module: {
        rules: [
            {
                test: /\.(scss|sass)$/, // Matches both .scss and .sass files
                use: [
                    'style-loader', // Injects styles into DOM
                    'css-loader',   // Turns CSS into CommonJS
                    'sass-loader'   // Compiles SASS to CSS
                ]
            }
        ]
    }
    
  2. Save Changes:
    Ensure you save the Webpack configuration file to apply these changes.


3. Renaming and Updating CSS Files

  1. Rename the Existing CSS File:
    Change your app-source.css file extension to .scss:

    app-source.scss
    
  2. Modify Imports in Your React App:
    Update the import path in your App.js or main component:

    import './app-source.scss';
    
  3. Start the Server:
    Restart the server to apply changes:

    npm start
    

4. Creating and Using SASS Variables

  1. Define Variables:
    In your .scss file, define variables using the $ syntax:

    $background-color: #f0f0f0;
    $text-color: #333;
    
    .custom {
        background-color: $background-color;
        color: $text-color;
    }
    
  2. Use Variables in Styles:
    Replace hardcoded values in your styles with variables. For example:

    .button {
        background-color: $background-color;
        color: $text-color;
    }
    

5. Testing SASS Variables

  1. Save the .scss file and refresh your application.
  2. Inspect the changes in your browser to ensure the styles are applied correctly.

6. Exploring Dynamic Variables

SASS variables can simplify updates to styles across your application. For instance:

  • Global Text Colors:

    $primary-color: #3498db;
    $secondary-color: #2ecc71;
    
    h1 {
        color: $primary-color;
    }
    
    p {
        color: $secondary-color;
    }
    
  • Dynamic Backgrounds:

    $bg-dark: #34495e;
    $bg-light: #ecf0f1;
    
    body {
        background-color: $bg-light;
    }
    
    .footer {
        background-color: $bg-dark;
    }
    

Conclusion

You’ve successfully integrated SASS into your React project and learned to use SASS variables for cleaner and more maintainable styles. In the next lesson, we’ll delve deeper into SASS features, such as importing external files for modular styles.

Video Transcript

00:00In the last lecture, we dynamically created a CSS file
00:04and we're we wanna append to that exact same file SaaS files
00:08as we explore and understand how they work.
00:10So in the process, we're gonna take a look like at how
00:13to set up with a web pack so SaaS will load up
00:15and also take a look at what variables are.
00:19All righty. So we're ready to move into the next level,
00:22which means that it's time for us to integrate SaaS
00:25into our builds instead of using just basic CSS.
00:29It's incredibly powerful,
00:31it adds programming capabilities into CSS.
00:35So let's go ahead and break out of our server
00:37and I'm gonna go ahead and install the
00:39packages that we need.
00:40And again, as almost always,
00:42I'm gonna install these new items into our dev.
00:47And I want the CSS loader,
00:51which I believe we already loaded so we don't need it.
00:53Again, we need the SAS loader
00:57and we need the node SaaS loader.
01:00Okay. Oh, not, not loader.
01:02The first one is basically the loader.
01:04And the second one is based enables us
01:06access to the library.
01:07Similar to how we had to have a loader
01:10and we had to have Babel separately.
01:12So it's the same thing where this is the gateway.
01:15Well this is the actual access to the library.
01:17This is the, the communicator with web pack.
01:21Okay, so while this is installing, I want to go into
01:24and continue the process of configuring our web pack.
01:27And similarly to the way that we've added CSS,
01:30lemme just duplicate that.
01:32I'm OI also want to add access to two file types.
01:36The first tile type,
01:37and let me just, also, I'm gonna, the first file type is
01:42SASS SaaS.
01:45Now I'm gonna open a round bracket and close around bracket
01:48and add here an OR because I want Aura do SaaS file
01:52or a css.
01:56Now the differences between the two,
01:57this one is a more minimalistic typing.
02:00While this is more, U is more human readable.
02:03I prefer working with the more human readable
02:05and that is what we're gonna be working with.
02:08The next step is once I, once I've done that, I need
02:10to also tell it
02:11what are the libraries that we're gonna be using?
02:14What are the loaders we're gonna be using?
02:16We're still going to use the CSS,
02:18but we're gonna add to it also the SaaS loader itself.
02:22And a reminder, I could have wrote here
02:23loader, but I don't have to.
02:25Web pack is smart enough
02:26to realize if I didn't add the dash loader that
02:29that's what I mean.
02:30Okay, so let's go ahead and say this.
02:32That's all the changes we really had
02:33to do within our web pack.
02:35And now that we've went ahead
02:36and installed the packages, all that's really left for us
02:39to do is to go ahead and start it up.
02:41So I'm gonna go ahead and call NPM start.
02:44And when my server is back running,
02:46I'm gonna go back into my application
02:48and I'm gonna go into my app source.
02:50And I'm just gonna go ahead here
02:52and rename our app source from dot CSS to SCSS.
02:57Now that it's a dot
03:01SCSS, whenever I'm gonna try to load that file.
03:06Webpac is gonna realize that I'm trying
03:08to load up a SaaS file
03:09and is gonna go through the conversion process,
03:12treating it like a SaaS file.
03:14Although currently we won't see really any difference yet.
03:16Let's go ahead and create some sort of a difference.
03:19So I'm gonna go into my c well it's so hard to call this,
03:23I'm gonna just call it a SaaS file
03:24just 'cause it's so hard to say.
03:26So I won't do that. Okay, now all the way on the top,
03:29I'm gonna go ahead and show you one of the cool features
03:31that are available in SaaS.
03:33Whenever you put the dollar sign,
03:35you could literally create variables.
03:37For example, I'm gonna put your variable called image
03:40and I'm gonna go ahead
03:41and set that image value to be the value of our image.
03:46Now obviously in this scenario,
03:47we probably wouldn't be incredibly useful,
03:50but in a real world scenario
03:51where I'm gonna repeat something more than once,
03:53it would become incredibly useful
03:55and I could literally just assign that variable.
03:58Let's go ahead and create another one.
04:00For example, we might have a variable for text color.
04:03So color text,
04:07text color.
04:09And for our text color, we wanna set it to be white.
04:11And again, all I have to do is refer to that variable
04:15and I could just assign that variable at any point in time
04:18that I want to or need to use it.
04:21And basically that is what it is.
04:24And I'm just basically ending things with a semicolon.
04:28Just like in programming,
04:29I'm literally just defining a variable
04:31and then using that variable.
04:33Now let's go ahead and change that color just
04:35so we'll see some sort of a difference.
04:37Let's change it to yellow and I'm gonna go ahead and save it
04:40and go back into my application.
04:41And if the, if everything was working according to plan,
04:45then it'll be able to, and by the way, the variable
04:47that I created here seems to be illegal.
04:49So let me just go ahead and grab
04:51and put here the full URL image element there.
04:56Round bracket, round, round, round where,
04:59where, where are you?
05:00Round. Round has disappeared. There you go.
05:04Lemme go ahead and save that and hopefully that will do
05:07and that sure did.
05:09And we got ourselves a a yellow
05:10and it's, it's a playing around.
05:12You see an error? That's fine.
05:13So basically, 'cause I didn't, it doesn't really know what
05:15to do with just like an image.
05:16It needs to be set as a URL. Okay, fine. We set it as a URL.
05:20So there you go. That's the basics of
05:22how you create variables.
05:23With sa, which are then compiled,
05:26because we're working again with, with Webpack
05:29and we're using extract text, pug plugin,
05:31it's all gonna be extracted into app css.
05:34So by the way, if I go into my app CSS
05:37and refresh that, we should see
05:38that now this is automatically built through SaaS itself.
05:43Extraordinarily cool.
05:47In this lecture we really sort the basics, basics of SaaS
05:50as we explored the integration
05:52of it into Webpack and variables.
05:55In the next lecture, we'll take a look at another feature
05:57available in SaaS, which is the capability to load in
06:00or import external files from it, from within it.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!