I've written some mixins for myself that use this standard syntax, and it's really helpful. But I'd like to hide some of the code if a certain value shows up.
Here's what works (breakpoints are fakes, and the ~""
code is a default telling LESS to compile to nothing):
// LESS MIXIN (CURRENT)
.margin(@lg: ~""; @md: ~""; @sm: ~""; @xs: ~""; @xxs: ~"") {
margin: @lg;
@media screen and (max-width: 1200px) {
margin: @md;
}
@media screen and (max-width: 800px) {
margin: @sm;
}
@media screen and (max-width: 600px) {
margin: @xs;
}
@media screen and (max-width: 400px) {
margin: @xxs;
}
}
which can be implemented like this
// LESS IMPLEMENTATION (CURRENT & EXPECTED)
div {
.margin(20px; ~""; 15px; ~""; 10px);
}
and will output like this:
/* CSS OUTPUT (CURRENT) */
div {
margin: 20px;
}
@media screen and (max-width: 1200px) {
div {
margin: ;
}
}
@media screen and (max-width: 800px) {
div {
margin: 15px;
}
}
@media screen and (max-width: 600px) {
div {
margin: ;
}
}
@media screen and (max-width: 400px) {
div {
margin: 10px;
}
}
It's simple and keeps me from doing a ton of @media breakpoints for basic styles. But I'd LOVE to not even print an empty @media line; I'd like the code to compile like this:
/* CSS OUTPUT (DESIRED) */
div {
margin: 20px;
}
@media screen and (max-width: 800px) {
div {
margin: 15px;
}
}
@media screen and (max-width: 400px) {
div {
margin: 10px;
}
}
SO... I was trying a few things and found this & when
code here, but it doesn't seem to work when I implement it here. This is what I've tried:
// LESS MIXIN (FAILED ATTEMPT TO SMARTEN)
.margin(@lg: ~""; @md: ~""; @sm: ~""; @xs: ~""; @xxs: ~"") {
& when not (@lg = ~"") {
margin: @lg;
}
& when not (@md = ~"") {
@media screen and (max-width: 1200px) {
margin: @md;
}
}
& when not (@sm = ~"") {
@media screen and (max-width: 800px) {
margin: @sm;
}
}
& when not (@xs = ~"") {
@media screen and (max-width: 600px) {
margin: @xs;
}
}
& when not (@xxs = ~"") {
@media screen and (max-width: 400px) {
margin: @xxs;
}
}
}
Anyone have any ideas to hide the @media code that would work in LESS?