SCSS for loop: mix variable with index - loops

In the following situation, I would like to combine variables with the loop index (in SCSS):
Predefined variables
$item-width-1: 400px;
$item-height-1: 340px;
$item-spacing-1: 40px;
$item-width-2: 200px;
$item-height-2: 440px;
$item-spacing-2: 100px;
$item-width-3: 300px;
$item-height-3: 240px;
$item-spacing-3: 60px;
For loop:
#for $i from 2 through 3 {
&:nth-child(#{$i}) {
.image { width: $item-width-#{$i}; height: $item-height-#{$i}; bottom: $item-spacing-#{$i}; }
.content { transform: translate(calc(#{$item-width-#{$i}} + 40px), 0); bottom: $item-spacing-#{$i}; }
.line { height: 108px + $item-spacing-#{$i}; left: $item-width-#{$i} / 2; }
&.active { width: $item-width-#{$i} + ($content-width + 80px); }
}
}
But it shows the following error:
Any ideas on how I could fix this?
Thank you in advance!

Related

React Material ui input field to custom

Hi does anyone have any idea to make the input field behave like this example?
https://www.westpac.com.au/personal-banking/home-loans/calculator/mortgage-calculator/
we should not allow user to delete 0 and when user type should replace 0 with the value, and when we delete all the value, the cursor will always stays at the end of the first digit.
The below code should work the way you want it.
function theFunction(e) {
if (e.value <= 0) return e.value = 0
if (e.value[0] == 0) return e.value = e.value.substring(1)
}
body{
width: 100vw;
height: 100vh;
}
.custom-input {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
background-color: rgb(42, 46, 66);
color: rgb(249, 249, 251);
padding: 18px;
font-size: 48px;
height: 84px;
margin-bottom: 18px;
border-color: rgb(171, 175, 177);
}
<body>
<input type="number" class="custom-input" id="custom-input" oninput="theFunction(this)" value="800">
</body>
I think you can use the if condition in your handleChange function, for example :
...
var result = 0;
if(input <= 0 || input == ''){
result = 0;
} else {
result = event.target.value;
}
this.setState({
inputValue: result
})
...
Hope this will help you :)

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;
}

LessCss: nesting groups of selectors

Say in my main.less file which gets compiled into main.css, I have the following:
.section1 {
.col-xs-1, .col-sm-1, .col-md-1, .col-lg-1,
.col-xs-2, .col-sm-2, .col-md-2, .col-lg-2,
.col-xs-3, .col-sm-3, .col-md-3, .col-lg-3,
.col-xs-4, .col-sm-4, .col-md-4, .col-lg-4,
.col-xs-5, .col-sm-5, .col-md-5, .col-lg-5,
.col-xs-6, .col-sm-6, .col-md-6, .col-lg-6,
.col-xs-7, .col-sm-7, .col-md-7, .col-lg-7,
.col-xs-8, .col-sm-8, .col-md-8, .col-lg-8,
.col-xs-9, .col-sm-9, .col-md-9, .col-lg-9,
.col-xs-10, .col-sm-10, .col-md-10, .col-lg-10,
.col-xs-11, .col-sm-11, .col-md-11, .col-lg-11,
.col-xs-12, .col-sm-12, .col-md-12, .col-lg-12
{
border: 1px solid green;
}
}
.section2 {
.col-xs-1, .col-sm-1, .col-md-1, .col-lg-1,
.col-xs-2, .col-sm-2, .col-md-2, .col-lg-2,
.col-xs-3, .col-sm-3, .col-md-3, .col-lg-3,
.col-xs-4, .col-sm-4, .col-md-4, .col-lg-4,
.col-xs-5, .col-sm-5, .col-md-5, .col-lg-5,
.col-xs-6, .col-sm-6, .col-md-6, .col-lg-6,
.col-xs-7, .col-sm-7, .col-md-7, .col-lg-7,
.col-xs-8, .col-sm-8, .col-md-8, .col-lg-8,
.col-xs-9, .col-sm-9, .col-md-9, .col-lg-9,
.col-xs-10, .col-sm-10, .col-md-10, .col-lg-10,
.col-xs-11, .col-sm-11, .col-md-11, .col-lg-11,
.col-xs-12, .col-sm-12, .col-md-12, .col-lg-12
{
border: 1px solid red;
}
}
Clearly this is already becoming unwieldy, excessively cluttering my main.less file and adding duplication of sorts which could cause multiple unnecessary changes.
How can I improve this? Is there a way I could for example do something like this:
.allCellTypes
{
.col-xs-1, .col-sm-1, .col-md-1, .col-lg-1,
.col-xs-2, .col-sm-2, .col-md-2, .col-lg-2,
.col-xs-3, .col-sm-3, .col-md-3, .col-lg-3,
.col-xs-4, .col-sm-4, .col-md-4, .col-lg-4,
.col-xs-5, .col-sm-5, .col-md-5, .col-lg-5,
.col-xs-6, .col-sm-6, .col-md-6, .col-lg-6,
.col-xs-7, .col-sm-7, .col-md-7, .col-lg-7,
.col-xs-8, .col-sm-8, .col-md-8, .col-lg-8,
.col-xs-9, .col-sm-9, .col-md-9, .col-lg-9,
.col-xs-10, .col-sm-10, .col-md-10, .col-lg-10,
.col-xs-11, .col-sm-11, .col-md-11, .col-lg-11,
.col-xs-12, .col-sm-12, .col-md-12, .col-lg-12
}
.section1 .allCellTypes
{
border: 1px solid green;
}
.section2 .allCellTypes
{
border: 1px solid red;
}
Thanks
Edit 1: As per #Alessandro Minoccheri's suggestion, I compiled but the resulting css was as follows:
.section1 .allCellTypes
{
border: 1px solid green;
}
.section2 .allCellTypes
{
border: 1px solid red;
}
Any mention of the classes that would be covered by .allCellTypes are completely omitted. Perhaps there are two reasons for this?
The definitions for the classes which would be grouped into .allCellTypes are defined elsewhere (within compiled bootstrap files).
I compile the .less files using lessphp, perhaps it has a bug?
Edit 2: I placed the following code in the demo window on the lessphp page...
.cell1 {
background-color: red;
}
.cell2 {
background-color: green;
}
.cell3 {
background-color: blue;
}
.allCellTypesClass {
.cell1;
.cell2;
.cell3;
}
.section1 {
.allCellTypes {
.allCellTypesClass;
border: 1px solid red;
}
}
and I got the following result:
.cell1 {
background-color: red;
}
.cell2 {
background-color: green;
}
.cell3 {
background-color: blue;
}
.allCellTypesClass {
background-color: red;
background-color: green;
background-color: blue;
}
.section1 .allCellTypes {
background-color: red;
background-color: green;
background-color: blue;
border: 1px solid red;
}
So I'm afraid your answer, #Alessandro Minoccheri, is not doing what I wanted!
The gist of what I do is as follows:
My Mixin:
.red-border () {
border:#value #value #value;
}
Applying:
.section-2 [class*="col-"] {
.red-border;
}
If you don't want to affect nested columns, then:
.section-2 > [class*="col-"] {
.red-border;
}
If you only want a certain column breakpoint:
.section-2 > [class*="col-sm-"] {
.red-border;
}
If you don't want to affect forms (haven't tested this but should work fine).
.section-2 > [class*="col-"]:not(form) {
.red-border;
}
Just in case, you can get it in LESS like this:
.section1 {
.allCells; .allCellsProperties() {
border: 1px solid green;
}
}
.section2 {
.allCells; .allCellsProperties() {
border: 1px solid red;
}
}
.allCells() {.col- {&xs-, &sm-, &md-, &lg- {
&1, &2, &3, &4, &5, &6, &7, &8, &9, &10, &11, &12 {
.allCellsProperties();
}
}}}
But indeed, the attribute-selector-based solution suggested by #cab is more clean and less bloating.
You can create a class .allCellTypes and add all class that you want that inherited.After you can call this class and add other property
you can write this:
.allCellTypesClass
{
.col-xs-1;
.col-sm-1;
.col-md-1;
.col-lg-1;
.col-xs-2;
.col-sm-2;
.col-md-2;
.col-lg-2;
.col-xs-3;
.col-sm-3;
.col-md-3;
.col-lg-3;
.col-xs-4;
.col-sm-4;
.col-md-4;
.col-lg-4;
.col-xs-5;
.col-sm-5;
.col-md-5;
.col-lg-5;
.col-xs-6;
.col-sm-6;
.col-md-6;
.col-lg-6;
.col-xs-7;
.col-sm-7;
.col-md-7;
.col-lg-7;
.col-xs-8;
.col-sm-8;
.col-md-8;
.col-lg-8;
.col-xs-9;
.col-sm-9;
.col-md-9;
.col-lg-9;
.col-xs-10;
.col-sm-10;
.col-md-10;
.col-lg-10;
.col-xs-11;
.col-sm-11;
.col-md-11;
.col-lg-11;
.col-xs-12;
.col-sm-12;
.col-md-12;
.col-lg-12;
}
.section1
{
.allCellTypes
{
.allCellTypesClass;
border: 1px solid green;
}
}
.section2
{
.allCellTypes
{
.allCellTypesClass ;
border: 1px solid red;
}
}

SCSS for each loop with Multiple Variables with decimal values

I'm having trouble with a SASS #each loop with multiple variables using decimals. I can get it to work on a basic level but want to add decimals, which throws me errors.
For example..
$columns: 100, 25, 33;
#each $proportion in $columns {
.col_#{nth($proportion, 1)} {
width: percentage($proportion/100);
}
}
... will successfully compile to...
.col_100 {
width: 100%;
}
.col_25 {
width: 25%;
}
.col_33 {
width: 33%;
}
However, trying to add a decimal to the variable list throws me an error. For example how can I get this to to work...
$columns: 100, 25, 33.333;
You could round the value for the class name:
#each $proportion in $columns {
.col_#{floor(nth($proportion, 1))} {
width: percentage($proportion/100);
}
}
Output:
.col_100 {
width: 100%;
}
.col_25 {
width: 25%;
}
.col_33 {
width: 33.333%;
}

Resources