Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
When building mixins, many will start building a lot of logic in their mixin. Use functions instead.
You'll be working in Sassmeister for this course.
Like what you see and want to read more? Check out 'Sass in the Real World' for more on Advanced Sass!
-
0:00
Functions are awesome,
-
0:02
another place that they play a huge role is at when you're building mixins.
-
0:06
So all too often when we're building mixins we will start building a lot of
-
0:09
logic inside of that mixin.
-
0:11
Let's build an example of that.
-
0:14
So let's say we are building yet another grid system.
-
0:18
Yes.
-
0:19
[SOUND] IM says I will build grids.
-
0:22
So when I'm building a grid I'm gonna pass in.
-
0:25
I want my count argument, my context is 12.
-
0:31
I'm gonna set that as a default.
-
0:34
[SOUND] My width is 60.
-
0:40
Comma there.
-
0:42
And my gutter is going to be 20.
-
0:49
Alright. [NOISE].
-
0:54
Now what I want to do is I'm gonna create a variable called grid width,
-
1:02
and grid width is going to be be my count minus one,
-
1:07
which is going to be multiplied by my gutter and then that's gonna added to.
-
1:15
My count multiplied by width.
-
1:24
[SOUND] Hooh, 'Kay, now next I
-
1:28
need to determine a context width all right.
-
1:34
Context width is going to be.
-
1:37
[SOUND] Context multiplied
-
1:45
with plus gutter.
-
1:51
Now, I need to set my width.
-
1:56
All right.
-
1:57
And what I'm going to is I'm going to convert this into a percentage.
-
2:01
So there's already a SASS function I can use called percentage and
-
2:05
what I'm going to pass in here is grid-width
-
2:11
divided by context width.
-
2:17
So now you can see why I needed to create those other variables.
-
2:19
So I have a mixin of the grid and all I really need to pass in there to
-
2:24
get this thing to work is how many columns of a grid that I want.
-
2:27
Cuz it's already set in a context of 12.
-
2:29
The width, by default, is gonna be 16 and the gutter's gonna be 20.
-
2:33
So I need to set an overall grid width, and that's gonna be my count minus one,
-
2:38
times gutter, plus count, times width, right?
-
2:41
So that gives me a gross value.
-
2:43
And then context width is gonna be context times width plus gutter.
-
2:48
So that gives me my entire value.
-
2:50
And then in order for me to figure out what the actual width is of this,
-
2:54
of this particular use case, I need to find out what
-
2:58
the percentage of one thing is divided by another and come out with an, a value.
-
3:03
So if I were to come down here and I then I say, dot block.
-
3:08
And then I do at, include the grid, and then I say ten.
-
3:18
So then I get the width of 81.25 %, beautiful, right?
-
3:23
So this is, this is big.
-
3:26
This is kind of a lot of stuff happening within the space of a a single mixin.
-
3:33
So, it definitely would be preferable if we were to actually take these things out
-
3:37
of the mixin, and then just allow the mixin to be able to produce the rules, but
-
3:43
take out the logic and put those inside of functions.
-
3:46
So let's start dissecting this thing here.
-
3:48
So I'm gonna create @function.
-
3:50
And I'm gonna call this function grid-width, alright?
-
3:56
And then inside this is gonna be count, gutter and width.
-
4:01
Count, gutter and width, okay.
-
4:07
And what this is gonna do is that this is actually going to create this.
-
4:15
So my grid width count minus one gutter and count width.
-
4:18
So I'm actually gonna take this entire guy out of here.
-
4:21
Take that, [SOUND] do that, and then that.
-
4:25
Return grid width, perfect.
-
4:31
All right.
-
4:35
So now I'm gonna do the exact same thing for context width right?
-
4:38
So @function context-width.
-
4:44
And this is gonna take arguments of context width and gutter.
-
4:47
Okay.
-
4:49
Context and width and gutter.
-
4:57
[SOUND] Perfect.
-
5:01
And now I want to take out that context-width variable.
-
5:05
That is context times width, plus gutter, right?
-
5:10
So let's get this guy out.
-
5:11
[SOUND] And then, get my at
-
5:16
return statement in here [SOUND].
-
5:22
Perfect.
-
5:23
[SOUND] Okay.
-
5:27
Basically, we're gonna use what's inside of the mixin, and create the grid
-
5:32
width and grid context variables but we've removed all that unnecessary logic.
-
5:36
So what I want to create now is the [SOUND].
-
5:41
Grid, width.
-
5:44
And then, context width.
-
5:49
Kay.
-
5:50
And then, to get these to work, I'm going to, call on those functions.
-
5:56
So let's, grid width is going to be.
-
5:59
Read with.
-
6:01
And then I can basically you know,
-
6:05
pass those arguments from the mixin itself directly into the function.
-
6:10
So I want count, gutter, and width.
-
6:18
Alright.
-
6:19
Same thing with context width.
-
6:21
So I'm gonna call in the function context width.
-
6:25
Whoops, yes, that's what I wanted.
-
6:28
And I'm gonna, let's get a space in here.
-
6:31
There we go.
-
6:32
So context width is going to consume context.
-
6:37
[SOUND] Width and gutter.
-
6:43
And there we go, so
-
6:44
now we can see over here that our block still came back with 81.25%.
-
6:50
I can come in here, change that to oops, about five end of paren
-
6:55
39.583333% because of the use of gutter, so it's not going to be 50%.
-
7:01
Even if I, whoops!
-
7:09
There we go, 47.91667.
-
7:10
So that that accounts for the gutter within a 50% column.
-
7:15
So as we can see here you know,
-
7:18
this, I was able to make a mixin a lot easier to follow.
-
7:22
Because I didn't have you know,
-
7:24
you can, you can distill these things even down further and further and further.
-
7:27
But you can see that you know,
-
7:29
all I needed to do inside the mixin was create the variable.
-
7:32
So that I could use within line 15.
-
7:34
And putting all of that logic out into functions makes this easier to follow.
-
7:38
Easier to edit, and a lot more scalable in the long run.
-
7:42
>> Functions.
-
7:44
Would you have ever guessed that we would be thinking like this with CSS?
-
7:48
I sure didn't.
-
7:50
But now that I can, I can never walk away from this.
-
7:54
Regardless of aesthetics and emotion, designs are pattern-rich fields.
-
7:59
The more we can do with CSS and
-
8:01
not rely on pixel-dependent images, the more code we have to write.
-
8:06
The more code we have to write, the better we have to get at writing it.
-
8:11
Functions completely take the repetitive nature out of vanilla CSS and
-
8:16
allow us to really rethink on how we build our world.
-
8:20
You just have to get started.
You need to sign up for Treehouse in order to download course files.
Sign up