LESS-variable with value == null, undefinded? - css

Just to get this clear, im talking about "LESS" as described on http://lesscss.org/
i read the documentation and searched a little around but i didn't find a possibilty to call an mixin with some "null" or undefined parameters.
For example this mixin from retina.js's less file:
.at2x(#path, #w: auto, #h: auto) has 2 optional parameters.
Now i only want to call it with a defined path and height. How do i do this?
things that don't work:
.at2x(EXAMPLEURL, '', 500px)
.at2x(EXAMPLEURL, null, 500px)
.at2x(EXAMPLEURL, , 500px)
.at2x(EXAMPLEURL,, 500px)
.at2x(EXAMPLEURL, undefined, 500px)
call that works of course:
.at2x(EXAMPLEURL, auto, 500px)

A Null Value
Is an escaped empty string, like so:
.at2x(EXAMPLEURL, ~'', 500px);
This would output "nothing" for the #w variable except a white space character. This will not necessarily eliminate the property it is used for, but it will make that property of no effect in the css. So you might get this if it is used for setting width: #w:
width: ;
Which would be ignored in css. Or if it is part of background-size: #w #h, you would get something like this:
background-size: 500px;
Of course, this is not truly NULL in the normal programming sense of the term, but I think it is what you are seeking for.

You can use "Named Parameters":
.at2x(EXAMPLEURL, #h: 500px);

I've the same problem with Less and i find a fix for this.
FIX :
#MyEmptyVar: e("");
Problem example :
Before :
/* Input (Style.less)*/
#IconPrefix: "";
/*
* With my less version this
* #IconPrefix: ; cause a compilation error
*/
.#{IconPrefix}glass:before {
content: "\f000";
}
/* Output (Style.css)*/
.""glass:before {
content: "\f000";
}
After:
/* Input (Style.less)*/
#IconPrefix: e("");
.#{IconPrefix}glass:before {
content: "\f000";
}
/* Output (Style.css)*/
.glass:before {
content: "\f000";
}

Related

Can I use mixins to generate new mixins in LESS?

I'm trying to use LESS to dynamically generate a set of mixins that would help me write cleaner media query code. So far in my limited knowledge of the language I've put together code that looks like this:
#sizes: xxs, xs, sm, md, lg;
.mediaQueries(#iterator:1) when(#iterator <= length(#sizes)) {
//Extract name
#sizeName: extract(#sizes, #iterator);
//Attempt to build min-width query
.MQ-min-#{sizeName} (#content) {
#media (min-width: #screen-#{sizeName}) {
#content();
}
}
//Attempt to build max-width query
.MQ-max-#{sizeName} (#content) {
#media (max-width: #screen-#{sizeName}) {
#content();
}
}
.mediaQueries((#iterator + 1));
}
.mediaQueries();
The goal is to have a set of mixins that would allow me to easily and cleanly define some CSS properties for a specific breakpoint, like so:
.generic-class {
background: black;
//Sizes #screen-sm and up
.MQ-min-sm({
background: transparent;
})
}
It doesn't work. Something to note, I'm trying to interpolate the size name into a variable name that would then output me a the px value of that variable into the #media query. Is something like this even possible?
Otherwise my compiler currently breaks on the start of the nested mixin (.MQ-min-#{sizeName} (#content) {) with the error:
Potentially unhandled rejection [2] Missing closing ')' in file ../mixins.less line no. 43
Is something like what I'm trying to achieve possible?
I think the simplest way for you to achieve this is by using a single parametric mixin like given below. This avoids the need for all those iterations, dynamic mixin creations etc.
#sizes: xxs, xs, sm, md, lg;
#screen-xxs: 100px;
#screen-sm: 200px;
.MQ(#content, #sizeName, #max-min) { /* get ruleset, size name and min/max as input */
#selector: ~"(#{max-min}-width: #{screen-#{sizeName}})"; /* form the media selector */
#media #selector { /* use it */
#content();
}
}
.generic-class {
background: black;
.MQ({
background: transparent;
}, /* ruleset */
sm, /* screen size */
max /* min/max */
);
}
If the mixins are for your own usage then this is all that you need. If it is for distribution as library then you may want to put some guards on #sizeName and #max-min variables to restrict invalid values.
Note: Less compiler always had a problem with the interpolation here - #media (min-width: #screen-#{sizeName}) also (I am not sure if it has been addressed) and that's why I created a temp variable.

Subtract from a value obtained through variable interpolation

I'm having trouble calculating a value when using variable interpolation.
Here's an example on the Less preview site: http://goo.gl/GVHXUs
Below is my code:
#breakpoint-sm: 600px;
#breakpoint-md: 800px;
.Mq(#breakpoint; #rules; #maxMin: min) {
& when (#maxMin = min) {
#query: ~"(min-width: #{breakpoint-#{breakpoint}})";
#media screen and #query {#rules();};
}
& when not (#maxMin = min) {
#break: ~"#{breakpoint-#{breakpoint}}" - 1;
#query: ~"(max-width: #{break})";
#media screen and #query {#rules();};
}
}
.test {
.Mq(sm; {
width: 100%;
height: 200px
}; max);
.Mq(md; {
width: 100%;
height: 200px
});
}
Result:
#media screen and (max-width: 600px - 1) {
.test {
width: 100%;
height: 200px;
}
}
#media screen and (min-width: 800px) {
.test {
width: 100%;
height: 200px;
}
}
So what I'm trying to achieve is that when something other than min is passed to the #maxMin it should subtract 1 from the breakpoint. I guess I'll be the laughing stock of Stackoverflow now, but hell, I can't figure it out.
The output of ~"#{breakpoint-#{breakpoint}}" is always a string and so the compiler just appends the number to the string instead of performing the math operation.
One way would be to use a temporary variable like shown below (have added only the part that needs modification) and then perform the arithmetic operation.
.Mq(#breakpoint; #rules; #maxMin: min) {
/* the rest of the mixin */
& when not (#maxMin = min) {
#temp: ~"breakpoint-#{breakpoint}";
#break: (##temp - 1); /* the braces are mandatory, without which it again appends */
#query: ~"(max-width: #{break})";
#media screen and #query {#rules();};
}
}
/* the selector blocks and mixin calls */
Below are few things that I found while working on the solution which have left me stumped. I'm trying to find the reason and will update the answer when I do find it out.
The braces play an important role in the #break variable. Without it, the output in media query is still a concatenation. However, if the same variable is used outside the media query (in a normal property-value pair like prop: #break, it prints the subtracted value).
The below code returns concatenated value (800px - 1)
#break: ~"#{breakpoint-#{breakpoint}}" - 1;
prop: #break;
whereas the below gives a "Operation on invalid type" compiler error.
#break: ~"#{breakpoint-#{breakpoint}}";
prop: #break - 1;
while I can see the reason behind them (first one results in string concatenation whereas second says subtraction can't happen on a string value), I am a bit stumped as to why the behavior is not consistent between the two.
(You are definitely not a laughing stock. Though I knew the reason for the problem, it took time for me to find a solution.)

What is the purpose of including "all" in #media rules?

So you see a lot of code examples do something like
#media all and (max-width:640px) {
div {
background-color:red;
}
}
Now afaik, the keywords "all" and "screen" and some others are for selecting the device type this applies to and the line is just supposed to provide a boolean output.
Since "all" applies to every device, one would imagine that its always 1 and (1 && x) always equals x so "all and" should make no difference whatsoever.
I tried out
#media (max-width:640px) {
div {
background-color:red;
}
}
and at least my browsers agree. Is there anything else I should know about?
See the spec: https://www.w3.org/TR/css3-mediaqueries/
The ‘print’ and ‘screen’ media types are defined in HTML4. The complete list of media types in HTML4 is: ‘aural’, ‘braille’, ‘handheld’, ‘print’, ‘projection’, ‘screen’, ‘tty’, ‘tv’. CSS2 defines the same list, deprecates ‘aural’ and adds ‘embossed’ and ‘speech’. Also, ‘all’ is used to indicate that the style sheet applies to all media types.
...
A shorthand syntax is offered for media queries that apply to all media types; the keyword ‘all’ can be left out (along with the trailing ‘and’). I.e. if the media type is not explicitly given it is ‘all’.
/* I.e. these are identical: */
#media all and (min-width:500px) { … }
#media (min-width:500px) { … }
/* As are these: */
#media (orientation: portrait) { … }
#media all and (orientation: portrait) { … }
In addition, the following media types: 'tty', 'tv', 'projection', 'handheld', 'braille', 'embossed', 'aural' have been deprecated in Media Queries Level 4.
all refers to: all media type devices, print: used for printing, screen: used for desktop screens, mobiles, tablets etc and speech: used for screen-readers that "reads" the page out loud.
In your case where you have specified media type as all, you can try printing the page by right clicking. The printed page will have all the styles applied in short it will exactly look the same.
Now take another example where you specify the media type as screen. If you try to print the page you will not see all the styles getting applied to the page as the styles were defined for screen alone.
If one does not specify all in media query it is by default taken as all.
#media screen {
div {
color: blue;
}
.print{
display: none;
}
}
#media print and (min-width: 200px){
div{
color: tomato;
}
div.not('.example'){
display:none !important;
}
.print{
display: block;
}
}
<div class="example">
<div>Try printing me. See if this blue color appears while printing</div>
<div class="print">I am only visible while printing.</div>
</div>

Using LESS variables in media queries

When I enter the following less code: (paste it into http://less2css.org)
#item-width: 120px;
#num-cols: 3;
#margins: 2 * 20px;
#layout-max-width: #num-cols * #item-width + #margins;
#media (min-width: #layout-max-width) {
.list {
width: #layout-max-width;
}
}
... the resulting CSS is:
#media (min-width: 3 * 120px + 40px) {
.list {
width: 400px;
}
}
Notice that the same variable #layout-max-width - in the media query it produces an expression (which isn't what I want) and when used as the value for the width property it produces 400px (which is also what I want for the media query.)
Is there formal syntax in LESS which enables me to do this?
If not - is there a workaround?
Due to Math Settings
LESS by default expects math operations to be in parenthesis (strict-math=on). So your variable needs those around the values to calculate correctly, like so:
#layout-max-width: (#num-cols * #item-width + #margins);
Then your original code will output as you expect.

Possible Bug in Nested Mixins in Media Queries using LESS

EDIT: RESOLVED
I am working on a way to easily write LESS code that takes parameters but still works with media queries. This is turning out to be rather convoluted, but I have gotten it working – on all sizes except one. The medium and large sizes work, but small is for some reason not printing the parameter, leaving me with css like font-size: ;.
Here I define my media sizes:
#m-small = ~"screen and (max-width: 799px)";
#m-medium = ~"screen and (min-width: 800px) and (max-width: 1299px)";
#m-large = ~"screen and (min-width: 1300px)";
Then, the main function I call, where #attr is the CSS property (e.g. font-size) and #parameter is the variable (e.g. fs-medium). To use this, I can write .media('font-size', 'fs-medium'), which is significantly less verbose than defining every media query.
Edit: There was a bug here, hence the problem; I have fixed it.
.media(#attr, #parameter) {
#media #m-small {
.small(#attr, #parameter);
}
#media #m-medium {
.medium(#attr, #parameter);
}
#media #m-large {
.large(#attr, #parameter);
}
}
These functions store the default values for parameters at various sizes, allowing me to consolidate where I define my variables, grouped by media query:
.small(#attr, #parameter) {
#fs-small : 1.4rem;
#fs-medium : 2.0rem;
#fs-large : 3.4rem;
#logo-width : 10rem;
.guards();
}
.medium(#attr, #parameter) {
#fs-small : 1.4rem;
#fs-medium : 2.4rem;
#fs-large : 3.8rem;
#logo-width : 12rem;
.guards();
}
.large(#attr, #parameter) {
#fs-small : 1.4rem;
#fs-medium : 1.8rem;
#fs-large : 5rem;
#logo-width : auto;
.guards();
}
In the above code, I call .guards() to render the content. This checks through my list of guards for one with a matching attribute, because LESS does not allow variables to be used in CSS property names. In these guards, I dynamically call the parameter, so that if I passed fs-medium, it will render #fs-medium's value.
.guards() when (#attr = 'font-size') {
font-size: ##parameter;
}
.guards() when (#attr = 'width') {
width: ##parameter;
}
Now, as I said, this works fine for the medium and large sizes, so I feel like there is either a typo in my code (I've checked) or a bug in LESS. One piece of code that uses this is as follows:
nav {
.media('font-size', 'fs-medium');
}
Which renders the following content:
#media screen and (max-width: 799px){
nav{ font-size:; }
}
#media screen and (min-width: 800px) and (max-width: 1299px){
nav{ font-size:2.4rem; }
}
#media screen and (min-width: 1300px){
nav{ font-size:1.8rem; }
}
Why is the small font-size missing?
I have discovered that I do indeed have a typo in my question, where I typed 'paremeter' under the .small mixin. I have edited it in the original post, but I am leaving it here for others trying to use media queries in LESS in a generalized way.
Verdict: typo.

Resources