Am I refactoring unnecessarily?

We have some base styles created by .js config files through Tailwind (media query values, colors, etc) and then compiled automatically into css variables. We have a .js file like this:
theme: {
// Breakpoints. https://v2.tailwindcss.com/docs/breakpoints
screens: {
// Translates to => @media (min-width: 320px) { ... }
xs: { min: "320px" },
// Translates to => @media (min-width: 640px) { ... }
sm: { min: "640px" },
// Translates to => @media (min-width: 768px) { ... }
md: { min: "768px" },
// Translates to => @media (min-width: 1024px) { ... }
lg: { min: "1024px" },
// Translates to => @media (min-width: 1280px) { ... }
xl: { min: "1280px" },
// Translates to => @media (min-width: 1536px) { ... }
xxl: { min: "1536px" },
},
theme: {
// Breakpoints. https://v2.tailwindcss.com/docs/breakpoints
screens: {
// Translates to => @media (min-width: 320px) { ... }
xs: { min: "320px" },
// Translates to => @media (min-width: 640px) { ... }
sm: { min: "640px" },
// Translates to => @media (min-width: 768px) { ... }
md: { min: "768px" },
// Translates to => @media (min-width: 1024px) { ... }
lg: { min: "1024px" },
// Translates to => @media (min-width: 1280px) { ... }
xl: { min: "1280px" },
// Translates to => @media (min-width: 1536px) { ... }
xxl: { min: "1536px" },
},
And then we use it in our breakpoints like this:
@media (min-width: theme("screens.lg")) {}
@media (min-width: theme("screens.lg")) {}
There wasn't documentation on this from what I know and only learned how this actually works through asking one of the leads. I tried ctrl + shift + f and couldn't find where something like "screens.lg" was being declared (because it's being initialized inside an object so it's hard to search for). I ended up just redefining these separately as Sass vars:
// These are from `tailwindVars.scss`
// The tailwind values are generated from `/tailwind/customer-base.js`
// Using this file mostly for documentation and ease-of-use
// over using something like `theme("screens.lg")` we can
// just use `$mq-lg` instead
$mq-sm: 640px;
$mq-md: 768px;

$mq-phone: $mq-sm;
...
// These are from `tailwindVars.scss`
// The tailwind values are generated from `/tailwind/customer-base.js`
// Using this file mostly for documentation and ease-of-use
// over using something like `theme("screens.lg")` we can
// just use `$mq-lg` instead
$mq-sm: 640px;
$mq-md: 768px;

$mq-phone: $mq-sm;
...
30 Replies
vince
vinceOP4mo ago
My question is if I'm creating unnecessary complication doing this? This makes it easier to understand how it works without needing to know beforehand (you can just ctrl+shift+f and find the declaration), but now there's a split between the old way and the new way that I haven't cleaned up yet. I did add comments in my Sass file about how it actually works, what's happening and why I did it etc but don't know if I'm just making more work It's definitely easier for me and saves me time typing lol, and I'm one of like 2 people working on this project so I don't think it's too big of a deal either way but I'm trying to do it 'the right way' I guess. I don't want to be that junior that changes things around for no reason lol
ἔρως
ἔρως4mo ago
yikes! tailwind v2
vince
vinceOP4mo ago
Ya lol
ἔρως
ἔρως4mo ago
how about you use a mixin for this? the map can still be generated by js, but you then don't even need to know what each size is - because it isn't too important
vince
vinceOP4mo ago
What should that look like?
ἔρως
ἔρως4mo ago
like what bootstrap does, but less complicated
vince
vinceOP4mo ago
I'll look into how they do that 👀 for now I thinks what I have is pretty decent
ἔρως
ἔρως4mo ago
it is pretty decent
vince
vinceOP4mo ago
So you don't think it was an unnecessary / junior refactor? lol I know you're way more senior than me I showed my lead it and she thought it was pretty cool but 🤷‍♂️ don't know if she was being nice 😂 It does seem more maintainable
ἔρως
ἔρως4mo ago
you can make a map with all the values and then just do a mixin that gets the value from the map and you can have a mixin that generates the media query too
vince
vinceOP4mo ago
Okay I see so they're linked together instead of separate. That makes sense I wasn't sure what you meant earlier
ἔρως
ἔρως4mo ago
saves you a ton of work later yup, and so you can refer to it by name you cant do $#{'sm'} or something so you need the map
vince
vinceOP4mo ago
I'll try to circle back to that and lyk how it goes cause I was trying to at least do something simple like $token: --token but it wasn't working since css vars aren't available in sass, did you mean the other way around? --token: $token?
ἔρως
ἔρως4mo ago
you can do that, but no im talking about a map, in sass like how you have in js but in sass
vince
vinceOP4mo ago
Yea I'll have to look it up, I've only heard of them I've never used them 😅 let me get back to you on this 🙏 ty
ἔρως
ἔρως4mo ago
you're welcome
vince
vinceOP4mo ago
Sooo did you mean something like this?
$media-queries: ("sm": var(--screen-small), "md": var(--screen-md), "lg": var(--screen-lg), "desktop": var(--screen-lg));

// Another file...
@media (min-width: map.get($media-queries, "desktop") {}
$media-queries: ("sm": var(--screen-small), "md": var(--screen-md), "lg": var(--screen-lg), "desktop": var(--screen-lg));

// Another file...
@media (min-width: map.get($media-queries, "desktop") {}
If so I still don't think this will work because I tried directly doing $mq-small: var(--screen-sm) and it wasn't working. Let me try that specifically in a codepen and see if it was a config issue on that specific project or because css vars aren't able to be put in sass vars Ohh that's where the interpolation comes from that you mentioned earlier
ἔρως
ἔρως4mo ago
you can't use css variables in media queries
vince
vinceOP4mo ago
Right
ἔρως
ἔρως4mo ago
what i meant was this:
$media-queries: (
"sm": $mq-sm,
"md": $mq-md,
// ...
);
$media-queries: (
"sm": $mq-sm,
"md": $mq-md,
// ...
);
vince
vinceOP4mo ago
Ohh then what's the point of even doing that?
ἔρως
ἔρως4mo ago
if you make a mixin, you can do something like this:
@include mq($min: "sm") {
...
}
@include mq($min: "sm") {
...
}
vince
vinceOP4mo ago
Why not just do @media (min-width: $mq-sm)?
ἔρως
ἔρως4mo ago
you also can you can also do @media (min-width: mq("sm")) if you decide to write it as a function which can be useful for you to control which breakpoint something is at
vince
vinceOP4mo ago
Sorry lots of questions 😅 so when would i actually want to use something like this? If I write it in a mixin it would probably be a little clearer just because it's named a bit better?
ἔρως
ἔρως4mo ago
imagine you need the breakpoint + 10px because something is being an idiot and breaking the layout or you need to limit the maximum width of the "main" element
vince
vinceOP4mo ago
So instead of adding calc everywhere you just add it to the mixin instead?
ἔρως
ἔρως4mo ago
or just get the value
vince
vinceOP4mo ago
Hm okay writing a codepen will see what i come up w 🤔
ἔρως
ἔρως4mo ago
try it

Did you find this page helpful?