SASS Tutorial (build your own CSS library) #8 - Maps
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Medium · Programming
Browser-Based PDF Editing with Vue 3 and pdf-lib
Dev.to · sunshey
🎓
Tutor Explanation
DeepCamp AI