Sass Mixin vs Extends

I had a quick question I wanted to ask regarding this. I have some files in a shared directory that I want to namespace.
>scss
->shared/
-->_forms.scss
>scss
->shared/
-->_forms.scss
I want _forms.scss to have reusable styling that I can use in other classes around the codebase. I don't need any argument features that mixin supports - I really just need it to work like extends. The problem is, I can't namespace extends. So if I try to do:
@use "@shared/forms" as f;

.form {
@extend f.%form; // error!
@use "@shared/forms" as f;

.form {
@extend f.%form; // error!
This doesn't work. I can however do this with mixins:
@use "@shared/forms" as f;

.form {
@include f.form; // success!
@use "@shared/forms" as f;

.form {
@include f.form; // success!
The problem is, this is leading to single line mixins:
@mixin commerce-layout-padding() {
padding: t.$sp-xs;
}
@mixin commerce-layout-padding() {
padding: t.$sp-xs;
}
My question is, is this a totally valid use case, or am I in the complete wrong direction and I should quit as a webdev? 😂
59 Replies
vince
vinceOP2mo ago
I was really worried about duplicate css, but the Sass documentation does allieviate my concerns a bit:
Most web servers compress the CSS they serve using an algorithm that’s very good at handling repeated chunks of identical text. This means that, although mixins may produce more CSS than extends, they probably won’t substantially increase the amount your users need to download. So choose the feature that makes the most sense for your use-case, not the one that generates the least CSS!
ἔρως
ἔρως2mo ago
🤔 i think you're worrying about the wrong thing
vince
vinceOP2mo ago
wym?
ἔρως
ἔρως2mo ago
you're worrying about having single-line mixins when you have single-use mixins
vince
vinceOP2mo ago
They're not single use
ἔρως
ἔρως2mo ago
it does look like it
vince
vinceOP2mo ago
I just didn't show the whole example Nah this is just an easy example I whipped up
ἔρως
ἔρως2mo ago
do you have a more concrete example?
vince
vinceOP2mo ago
No description
vince
vinceOP2mo ago
Brb putting up a tv lol might take me a while
ἔρως
ἔρως2mo ago
lol, take your time why the long name? why single-letter names?
vince
vinceOP2mo ago
I've got it in one shared/_forms.scss style, and I didn't want to abstract another layer so I just prefixed it with commerce- Single letter, idk i've just been doing that. Like all my tokens I do @use "@tokens" as t Might be better to make it tokens and forms respectively but with the longer variable names like commerce-form-input I'm glad I don't have to write forms.commerce-form-input haha
ἔρως
ἔρως2mo ago
you know what's shorter? commerce-form.input
vince
vinceOP2mo ago
I was trying to avoid premature abstraction (and the reason I'm saying that is because once I get to the point where I'm naming files like that I usually abstract to a level deeper and put it under something like forms/commerce) But since I don't have any other form mixins right now, I can just rename it to commerce-form like you're saying
ἔρως
ἔρως2mo ago
🤔 im curious to see the final result
vince
vinceOP2mo ago
Appreciate you talking through this with me epic I think I've been thinking so much about design systems and things I'm really overcomplicating things I literally just refactored pretty much everything here - I had these commerce components mixed out between several files for titles, layouts, buttons, etc - so I ended up just merging everything in one _commerce.scss partial. I removed the mixins, and literally just made them classes. I still might use a mixin or two because of that weird use case i talked about in #off-topic. I think I was trying to combine a sass-only approach with utility classes and it just was a weird almagamation. either one or the other
ἔρως
ἔρως2mo ago
something you need to learn: you can use an _index.scss to automate including all scss files in a folder
vince
vinceOP2mo ago
You're talking about @forwards?
ἔρως
ἔρως2mo ago
no, import it's fine to mix both
vince
vinceOP2mo ago
@import is deprecated I use @forwards everywhere 🫣
ἔρως
ἔρως2mo ago
it is, but it's still so damn useful
vince
vinceOP2mo ago
/*===================*
* Fonts /
*==================*/
@forward "./fonts/families" as ff-*;
@forward "./fonts/sizes" as fs-*;
@forward "./fonts/lines" as lh-*;
@forward "./fonts/weights";


/*===================*
* Borders /
*==================*/
@forward "./borders/borders" as br-*;
@forward "./borders/radius" as bu-*;


/*===================*
* Uncategorized /
*==================*/
@forward "./clip-paths" as cp-*;
@forward "./colors" as clr-*;
@forward "./heights" as h-*;
@forward "./media-queries" as mq-*;
@forward "./shadows" as bs-*;
@forward "./spacings" as sp-*;
/*===================*
* Fonts /
*==================*/
@forward "./fonts/families" as ff-*;
@forward "./fonts/sizes" as fs-*;
@forward "./fonts/lines" as lh-*;
@forward "./fonts/weights";


/*===================*
* Borders /
*==================*/
@forward "./borders/borders" as br-*;
@forward "./borders/radius" as bu-*;


/*===================*
* Uncategorized /
*==================*/
@forward "./clip-paths" as cp-*;
@forward "./colors" as clr-*;
@forward "./heights" as h-*;
@forward "./media-queries" as mq-*;
@forward "./shadows" as bs-*;
@forward "./spacings" as sp-*;
ἔρως
ἔρως2mo ago
you have to forward everything one by one
vince
vinceOP2mo ago
This is in my _tokens.scss and then I just do @use "@tokens" as t; Import you can do it automatically?
ἔρως
ἔρως2mo ago
you can just do import 'borders' and BAM! everything in it is available, if you have an index.scss file which is absolutely amazing for configurations but the dot syntax is really nice too
vince
vinceOP2mo ago
Ah that's just @use "_partial.scss" no? what's the difference I like namespacing a lot personally which is why I use an alias
ἔρως
ἔρως2mo ago
makes sense the difference is that everything is global which may not be desirable
vince
vinceOP2mo ago
Yea I don't like that personally haha
ἔρως
ἔρως2mo ago
i like it because i don't want to have to repeat @use everywhere
vince
vinceOP2mo ago
Yea totally get that, I'm in the headspace of at least, if another dev comes on and they see variables used, they know exactly where it's coming from type of thing
ἔρως
ἔρως2mo ago
imagine i want the function color-from-settings i have to @use that file EVERYWHERE it's so irritatingly annoying
vince
vinceOP2mo ago
Ah I see what you're saying
ἔρως
ἔρως2mo ago
you know that there's extensions that tell you where the variables are declared, right?
vince
vinceOP2mo ago
I don't mind it honestly, I still think it's a bit more clear when you're manually importing it and namespacing it since you'll know where the variables come from Nay 😅 What if someone doesn't have that extension? I'd rather be more verbose for the sake of maintainability But I get what you're saying for sure
ἔρως
ἔρως2mo ago
it's just 2 different ways of doing it i prefer my way because i can just import everything i need once, and it is available to all the children if im importing, it's because i will use it everywhere and if i want something just locally, i can always just @use a specific file
vince
vinceOP2mo ago
Lol perhaps you're convincing me...
ἔρως
ἔρως2mo ago
this is why i think that the @import being deprecated is just dumb sass is supposed to be a "[...] the most mature, stable, and powerful professional grade CSS extension language in the world." removing imports, a css feature, is neither mature, neither stable and not powerful and stops being a css extension and moves to it's own language
vince
vinceOP2mo ago
Why did they even remove it in the first place? Wasn't it because there were unintended side effects or something with it?
ἔρως
ἔρως2mo ago
they want to remove it because of it being harder to reason with and harder for libraries for example, if you want to use bootstrap, you can just use the specific file you need or import it all and all the junk comes together that's another downside of the import however, if im @importing bootstrap, it means i will use everything anyways
vince
vinceOP2mo ago
Yea sounds more like a user issue rather than them having to remove it entirely
ἔρως
ἔρως2mo ago
yes, but if you want a library but don't want to use everything, you need to manually pick and choose what you import and be careful with where you import
vince
vinceOP2mo ago
So instead they're forcing you to manually import everything rather than giving you the option. Don't really like the idea of not having that option
ἔρως
ἔρως2mo ago
yup also, it's really useful to just do @import './folder-name' and everything is imported, without you having to do it all manually
vince
vinceOP2mo ago
I do that already with an index partial
ἔρως
ἔρως2mo ago
yeah, but you have to type each file manually
vince
vinceOP2mo ago
Ahhh that's not too big of a deal imo
ἔρως
ἔρως2mo ago
it isn't also, can you do this?
.form {
@import './form';
}
.form {
@import './form';
}
// _form.scss
&-small {
@import './small';
}
// _form.scss
&-small {
@import './small';
}
and it generates this:
.form-small {
/* properties here */
}
.form-small {
/* properties here */
}
this can be really useful sometimes
vince
vinceOP2mo ago
I don't know honestly looks cool though 👀
ἔρως
ἔρως2mo ago
it does you can also mix both: @use configurations, functions and stuff, but @import fragmented styles and you have to emulate what the @import can do by messing about with weird @mixins
vince
vinceOP2mo ago
I think I'm just going to try to stick to the simpler stuff at this point 😂 use one or the other I have bad experiences trying to mix 2 things at once 🤭
ἔρως
ἔρως2mo ago
im not trying to convince you to change to imports
vince
vinceOP2mo ago
Ya I know haha
ἔρως
ἔρως2mo ago
just saying that there's a different alternative that is nice and that alternative will be removed because of nonsense
vince
vinceOP2mo ago
Hey epic I have a quick question related to this. Since I ended up just making them all classes, if i @use them in multiple files, it duplicates the css output. So I've moved to only @use it in my main.scss file so it's imported once. The problem is, now I can't use @extend since I'm not importing it in each file. Do I really need to make a separate partial file with a placeholder selector or mixin that both the main utility class file and separate files can import? The whole reason in the first place too why I want to use @extend is because I want to avoid classes like "form sign-in", I'd rather just have something like "form--sign-in" and then extend the .form class, but maybe that's a bad motivation
ἔρως
ἔρως2mo ago
don't worry about the output css being duplicated unless it is repeating to an extreme, it shouldn't cause any issues and obviously, check the compressed output size as well just make things easier for you if size becomes an issue, then deal with it at that time
vince
vinceOP2mo ago
What the hell, after going to the gym and coming back and trying to do literally the same thing as before, it no longer duplicates css. I even hard refreshed & rebuilt several times before too... Sass is so confusing 😂
ἔρως
ἔρως2mo ago
sass is telling you to go rest
vince
vinceOP2mo ago
I can't I got OT to get this website preview out by Monday. I really shouldn't have tried to get this fancy and start semi-refactoring stuff this far in But I can't help it 😂 The old way was getting too messy, with several layers deep of folders Ah okay no it still is duplicating nvm oh well I think I'm just going to do something like class="form form--inner" for now, where form is the base utility class
ἔρως
ἔρως2mo ago
dude, whatever gets the project done faster that's the most important
Want results from more Discord servers?
Add your server