SASS Tutorial (build your own CSS library) #8 - Maps

Net Ninja · Beginner ·🌐 Frontend Engineering ·4y ago

Key Takeaways

This SASS tutorial covers the use of maps to group variables and values, and how to manipulate and get data from maps using built-in SASS functions like map-get, map-has-key, map-remove, and map-merge.

Full Transcript

all right then gang so the next thing i want to talk about in sas is the use of maps and maps are a bit like associative arrays in programming languages or maybe a little bit like an object in javascript it's basically a way we can collect a lot of different variables or values together in some kind of collection and that collection is going to have key value pairs and they're really useful for generating utility classes whereby we loop through a map and generate a class for each value in that map and we'll see that later on for now i want to show you how we can use a map to kind of collect different values together that belong together and how we can manipulate or get data from that map so what i'm gonna do is create some kind of color palettes beyond our theme colors right here so it contains other colors as well so let me do a little comment first of all to say color palettes like so and now we're gonna create a map of colors and we're going to store this inside a variable i'm going to call it colors and by the way for any british people watching me i am sorry i always spell colors the american way it's just because in programming with css it uses the american spelling and so i've just picked that habit up all right then so inside this map which is denoted by parentheses by the way we can have different key value pairs so for example i could pass in the primary key and that is going to have a value of this thing right here now i don't have to hard code this down here i can just pass in the actual primary variable and that's going to grab this and then in the future if i want to change the value i'm not changing it in two places i'm only changing it in one and then it's going to update in the map in the future as well so let's do the same thing for the other theme colors because i want all those to be in the color palette and it's going to be secondary we also need one for the error so let's do that and pass in the error and then thirdly it's info and the variable is just info okay so now we can add other colors as well so i could want some kind of blue color and what i'm going to do is just paste in these different hash codes because i don't want to write them all out from scratch in fact i'm going to grab all of these and i'm just going to paste them in because it would be really boring for you to watch me write them all out so we have blue red yellow green orange purple gray black and white all of these extra colors now in our color palette and we've given a hash value for each one of these except these down here they're just using the keywords black and white which we can use in css so we have this map of colors now and this is going to be really really useful for us later when we're making things like button components or utility classes for background colors or text colors things like that for now what i want to show you is how we can use this map in terms of getting values from it adding values to it etc so down here we're just going to use the debug rule which we talked about in the last lesson to manipulate this a little bit or get data from it so i'm going to say at debug and then i'm going to use a function built into sas called map hyphen gets and what this does is get us a value from a map so i can say in here the first parameter is going to be what map we want to look at because we might have multiple maps in our files well i want to look through the colors map and then the value i want to get is the purple key so i have to pass in the key as the second argument and this is going to get us the value right here so if i save this and open up the terminal we can see right here this value 9900 ff which is the purple color awesome so let's do a few more examples see what else we can do i'm going to say at debug again and we'll say map has key and again we pass in as a first argument the map we want to look through and that's going to be colors again and we want to see has it got a secondary key so it does have a secondary key right here so this should return true so let's have a look and we can see true right there however if we look for a key that doesn't exist for example tertiary let me write that in and save it then we're going to get false so we can use this function to kind of evaluate maps to see if we have a specific variable and we'll see how we can evaluate things later on using if statements all right so let's do a couple more examples i'm going to show you how to remove a key now so we'll say at debug and we'll say map hyphen remove and we're going to remove the primary color so i'll say colors and then primary so it's looking at the colors map and it's trying to remove the value with a key of primary this thing right here and what it's going to do is log out to the console down here the map after it's been removed so if i save this now we're going to see the map and you can see now it starts at secondary the primary one is not in this map it was at the top now it's not there so that's how we remove something all right so one more how do we add something well we use the map merge function so debug again then map hyphen merge and what i'm going to do is pass in the map we want to add something to and then what we have to do is pass in another map as a second argument now remember a map is just braces or parentheses rather with key value pairs inside it so i could pass in parentheses another map with a key value pair so i could say i want to add in the pink color and that is going to have a hash of ffc 0 cb which is a pinkish color so now if i save this we're going to see this map again but now we have the pink one added to it cool so there's a few different ways we can interact with this map and we can use these different functions in selectors so let me comment those out for a second and i'm going to create some kind of test button selector like so and inside here i'm going to set the background color and it's going to be map hyphen gets and then we want to get something from the colors palette and we want to grab the purple color so that's going to get this value right here and set it to be the background color of the test button so if i was to save that now we can see it's built and if i go into the index file and go down to the bottom hopefully no we don't see there let me just search for it test hyphen btn okay it's near the top test button and it has this background color so this is how we can use it inside css selectors as well and we're going to use them to a great effect in that as well in the next lesson we're going to loop through the map using an each loop and generate classes for each one of these different colors

Original Description

Hey gang, in this SASS tutorial we'll be talking a look at maps and how we can use them to group together several values. 🐱‍👤 Get the full SASS course now on Net Ninja Pro: https://netninja.dev/p/complete-sass-tutorial 🐱‍👤 Get access to all other premium courses on Net Ninja Pro: https://netninja.dev/ 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/complete-sass-tutorial 🐱‍💻 HTML & CSS Crash Course: On Net Ninja Pro - https://netninja.dev/p/html-css-crash-course On YouTube - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G 🐱‍💻 Modern JavaScript Course: On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 CSS Flexbox Course (on YouTube): https://www.youtube.com/watch?v=Y8zMYaD1bz0&list=PL4cUxeGkcC9i3FXJSUfmsNOx8E7u6UuhG 🐱‍💻 SASS docs - https://sass-lang.com/documentation 🐱‍💻 VS Code - https://code.visualstudio.com/ -------------- Net Ninja Links: 🐱‍💻 My Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/ 🐱‍👤 Get access premium courses on Net Ninja Pro: https://netninja.dev/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial teaches how to use maps in SASS to group variables and values, and how to manipulate and get data from maps using built-in SASS functions. It covers the basics of maps, how to create and use them, and how to use them to generate utility classes.

Key Takeaways
  1. Create a map to store color palettes
  2. Use map-get to retrieve a value from a map
  3. Use map-has-key to check if a key exists in a map
  4. Use map-remove to remove a key-value pair from a map
  5. Use map-merge to add a new key-value pair to a map
  6. Use maps in CSS selectors to generate utility classes
💡 Maps in SASS are a powerful tool for grouping variables and values, and can be used to generate utility classes and simplify CSS code.

Related Reads

📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Learn how to prepare for a Frontend SDE-2 interview at Wayfair, including online assessments, machine coding, and system design.
Medium · Programming
📰
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Learn how HTMX rebuilt a React SPA in a week, replacing 2 years of maintenance work, and discover the benefits of this alternative approach
Medium · Programming
📰
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Learn the 5 levels of front end engineering to improve your skills and avoid getting stuck in a career rut
Medium · Programming
📰
Browser-Based PDF Editing with Vue 3 and pdf-lib
Learn to build a browser-based PDF editor using Vue 3 and pdf-lib, enabling users to edit PDFs directly in the browser
Dev.to · sunshey
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →