How to write a loop function or mixin with variables in less.js for DRY - loops

Is there any efficient way to write this code with less.js:
I've got already 3 variables colors : #vert, #violet and #orange
li {
&:nth-child(1) {
background: #vert;
&:hover,
&.open {
>.dropdown-menu li {
background: #vert;
}
}
}
&:nth-child(2) {
background: #violet;
&:hover,
&.open {
>.dropdown-menu li {
background: #violet;
}
}
}
&:nth-child(3) {
background: #orange;
&:hover,
&.open {
>.dropdown-menu li {
background: #orange;
}
}
}
}
I thought of a mixin, but I'm not writing well: Any help please ?
.colored-menu(#number, #color) {
&:nth-child(#number) {
background: #color;
&:hover,
&.open {
>.dropdown-menu li {
background: #color;
}
}
}
}
and calling it like this:
.colored-menu(1,#vert);
.colored-menu(2,#violet);
.colored-menu(3,#orange);

You can use your approach with some edits:
// mixin
.colored-menu(#number; #color) { // the docs recommends semi-colons instead of commas
&:nth-child(#{number}) { // variable requires curly brackets for interpolation
background: #color;
&:hover,
&.open {
> .dropdown-menu li {
background: #color;
}
}
}
}
// ruleset
li {
.colored-menu(1; #vert);
.colored-menu(2; #violet);
.colored-menu(3; #orange);
}
Also, consider using the each list function:
// list
#colors: #vert, #violet, #orange;
// ruleset
li {
each(#colors, {
&:nth-child(#{index}) {
background: #value;
&:hover,
&.open {
> .dropdown-menu li {
background: #value;
}
}
}
});
}
The output from both approaches are the same (in this instance).

Related

ionic 2 calendar cssClass not working

I'm using https://github.com/HsuanXyz/ion2-calendar to generate a calendar in ionic. I can't make color changes to dates using dateconfig's cssClass.
Below is the code i'm using
` daysConfig() {
let _daysConfig = [
{
date:new Date(2018,0,1),
subTitle:'New Year\'s',
marked:false,
cssClass: 'my-cal'
},
{
date:new Date(2017,1,14),
subTitle:'Valentine\'s',
disable:true
},
{
date:new Date(2017,3,1),
subTitle:'April Fools',
marked:true
},
{
date:new Date(2017,3,7),
subTitle:'World Health',
marked:true
},
{
date:new Date(2017,4,31),
subTitle:'No-Smoking',
marked:true
},
{
date:new Date(2017,5,1),
subTitle:'Children\'s',
marked:true
}
];
_daysConfig.push(...this.days);
this.calendarCtrl.openCalendar({
from: new Date(2017,0,1),
to : new Date(2018,11.1),
daysConfig:_daysConfig
})
.then( (res:any) => { console.log(res) })
.catch( () => {} )
}`
css Class
.my-cal {
color: yellow
}
After a long RnD of almost 5 hour could solve this issue.
Please find the below solution in ionic
Define the below class in global.scss
.my-cal {
background-color: red !important;
}
and remember not to define this class anywhere else.
now use this in you days config as below:
let _daysConfig: DayConfig[] = [];
for (let i = 0; i < 31; i++) {
_daysConfig.push({
date: new Date(2020, 4, i + 1),
marked: false,
subTitle: `$${i + 1}`,
cssClass: 'my-cal'
})
}
Hope it will help someone :)
I just encountered the same problem, and I solved it by adding the follow style in the .scss file:
button.days-btn.my-cal {
p, small {
color: yellow;
text-decoration: underline;
font-weight: bold;
}
}
The cssClass is only added to the button that the <p> element containing the text is in.
So to properly change the color, you'll have to reference the <p> element first and assign the color to that.
Example
.ts
cssClass: 'my-cal'
.scss
.my-cal {
p {
color: green;
}
}
One of the solutions would be to add this code to your global scss/css style
ion-calendar { .my-cal{ background-color: #6E6B6B !important; p{ color: white !important; } }}
Later add "my-cal" class to wanted day.

SASS For Loop including 0

I have a for loop in SASS which loops through page classes to insert a colour break for each module. For example:
#for $i from 1 through 4 { // the loop
.m0#{$i} .module-title{
background-color: nth($m_col_lvl_01_list, $i);
}
//- end loop
}
Which compiles to:
.m01 .module-title{
background-color: green;
}
.m02 .module-title{
background-color: blue;
}
.m03 .module-title{
background-color: yellow;
}
.m04 .module-title{
background-color: orange;
}
In the task I have at the moment it includes .m00 Is there a way of including 00 in the loop?
I think you can still achieve what you want using 0 in the for loop.
$list: (green, blue, orange, red, yellow);
//loop from 0 to the length of the list which isn't hardcoded
#for $i from 0 to length($list) {
.m0#{$i} .module-title {
//simply add one to the loop index to get the correct list item
background-color: nth($list, $i + 1);
}
}
This compiles to the following CSS
.m00 .module-title {
background-color: green;
}
.m01 .module-title {
background-color: blue;
}
.m02 .module-title {
background-color: orange;
}
.m03 .module-title {
background-color: red;
}
.m04 .module-title {
background-color: yellow;
}

SASS : Using lists as associative array

I have this kind of list
$colors : (
(redG, #ff0000, #ffffff),
(blueG, #00ff00, #ff4544),
(greenG, #0000ff, #123456)
);
and I'd like to do something like
a {
background-color: $colors[redG, 1]
color: $colors[redG, 2]
}
Thanx
You could use nested maps along with a function like such:
$colors : (
redG : (
1 : #ff0000,
2 : #fff
),
blueG : (
1 : #00ff00,
2 : #ff4544
),
greenG : (
1 : #0000ff,
2 : #123456
)
);
#function color($color, $position: 1) {
#return map-get(map-get($colors, $color), $position)
}
a {
background-color: color(redG, 1);
color: color(redG, 2);
}
a {
background-color: color(blueG, 1);
color: color(blueG, 2);
}
a {
background-color: color(greenG, 1);
color: color(greenG, 2);
}
Which will return:
a {
background-color: #ff0000;
color: #fff;
}
a {
background-color: #00ff00;
color: #ff4544;
}
a {
background-color: #0000ff;
color: #123456;
}

Use array index to change background color to element

I have created an ul element and I have put some li items inside. I want to change the background color of each li item using an array in a for loop. This is what I have, but the result is just a string :(
"use strict";
var colorArray,
i,
ulVar,
listItem;
colorArray = [
"color1",
"color2",
"color3",
"color4",
"color5",
"color6"
]
ulVar = document.createElement("ul");
ulVar.setAttribute("id", "#text-color");
ulVar.innerText = "Some unordered list"
document.body.appendChild(ulVar);
for(i = 0; i < colorArray.length; i++){
listItem = document.createElement("li");
listItem.setAttribute("class", colorArray[i]);
console.log(listItem.getAttribute("class")); //just to check what's inside
listItem.innerText = colorArray[i];
ulVar.appendChild(listItem);
}
The css code is just this:
#text-color {
color: #fff;
}
.color-1 {
background-color: #0000ff;
}
.color-2 {
background-color: #ff6600;
}
.color-3 {
background-color: #cc00cc;
}
.color-4 {
background-color: #009933;
}
.color-5 {
background-color: #669999;
}
.color-6 {
background-color: #663300;
}
I don't know how to make the colorArray[i] to return the class properties and not just string. I'm learning now so please don't judge me too hard :)

Less mixin, extract colors from array and on hover, add darken to that array

Is it possible to darken an array with colors? Like this:
#array: #color1 #color2 #color3 #color4
.color-mix(#i) when (#i > 0) {
.myClass {
li:nth-child(#{i}) {
.background-color(extract(#array, #i));
&:hover {
// THIS IS NOT WORKING, IS THERE A RIGHT APPROACH FOR THIS?
.background-color(extract(darken(#array, 5.5%, #i)));
}
}
.color-mix(#i - 1);
}
}
// Iterate
.color-mix(4);
If I understand your question correctly, yes, you can achieve that. Below is how you do it. Your code was almost correct except that instead of trying to darken the extracted value, it was trying to extract a darkened value (which is not possible).
#array: #fff #00f #ff0 #f00;
.color-mix(#i) when (#i > 0) {
.myClass {
li:nth-child(#{i}) {
.background-color(extract(#array, #i));
&:hover {
.background-color(darken(extract(#array, #i), 5.5%));
}
}
}
.color-mix(#i - 1); /* I have moved it because I think it was wrongly placed */
}
// Iterate
.color-mix(4);
One improvement that I would suggest to your code is to move the :hover selector also within the .background-color mixin like below. This makes it a bit more easier to read as there is no wrapping of a function call within another function and so on.
#array: #fff #00f #ff0 #f00;
.color-mix(#i) when (#i > 0) {
.myClass {
li:nth-child(#{i}) {
.background-color(extract(#array, #i));
}
}
.color-mix(#i - 1);
}
// Iterate
.color-mix(4);
.background-color(#color){
&{
background-color: #color;
&:hover{
background-color: darken(#color, 5.5%);
}
}
}
Even better would be this - just avoid the mixin if you can :)
#array: #fff #00f #ff0 #f00;
.color-mix(#i) when (#i > 0) {
.myClass {
li:nth-child(#{i}) {
#color: extract(#array, #i);
background-color: #color;
&:hover{
background-color: darken(#color, 5.5%);
}
}
}
.color-mix(#i - 1);
}
// Iterate
.color-mix(4);

Resources