Apply less function to element selectors following the function output - css

I have less code which looks as follows:
.my-func(#color) {
&:not(:hover) {
color: #color;
}
}
.class1, .class2 {
.subClass1 {
.my-func("#ffffff");
}
}
This results in the following css:
.class1 .subClass1:not(:hover),
.class2 .subClass1:not(:hover) {
color: "#ffffff";
}
What I want to do is to generate css that, in addition to outputting what is being output right now, will also allow me to add more selectors after the part of the css that is output by the function.
So I want css that looks like this (I added the line breaks):
.class1 .subClass1:not(:hover),
.class2 .subClass1:not(:hover),
.class1 .subClass1:not(:hover) p.some-other-class,
.class2 .subClass1:not(:hover) p.some-other-class,
.class1 .subClass1:not(:hover) p.yet-another-class,
.class2 .subClass1:not(:hover) p.yet-another-class {
color: "#ffffff";
}
So can this be done using less features, so that I can somehow pass in a list of two element selectors to the function (p.some-other-class and p.yet-another-class), and the function will run for (to give the first two rows of output) and will then run for the elements in the list passed in to append these after the output of the function? Something like running an .each function on the list passed in, within the function, to accomplish this?

You can try like below:
#class: some-other-class, yet-other-class;
.my-func(#color) {
&:not(:hover) {
color: #color;
}
each(#class, {
&:not(:hover) p.#{value} {
color: #color;
}
})
}
.class1, .class2 {
.subClass1 {
.my-func(#ffffff);
}
}
If you want to pass the list to the function adjust like below:
.my-func(#color,#list) {
&:not(:hover) {
color: #color;
}
each(#list, {
&:not(:hover) p.#{value} {
color: #color;
}
})
}
#class: some-other-class, yet-other-class;
.class1, .class2 {
.subClass1 {
.my-func(#ffffff,#class);
}
}
More generic with any kind of selector like below:
.my-func(#color,#list) {
&:not(:hover) {
color: #color;
}
each(#list, {
&:not(:hover) #{value} {
color: #color;
}
})
}
#class: ~"p.some-other-class", ~"div.yet-other-class";
.class1, .class2 {
.subClass1 {
.my-func(#ffffff,#class);
}
}

Related

Nested selectors using Sass

How can I have this output using Sass?
.class.active .class-name1 {}
.class.active .class-name2 {}
Here is what I tried :
.class {
&.avtive {
&-name1 {
}
&-name2 {
}
}
}
You need to keep a reference to the outer class name, and interpolate it for your -name classes. Using color: red as an example of the style to apply:
.class {
$outer-class: &;
&.active {
#{$outer-class}-name1 {
color: red;
}
#{$outer-class}-name2 {
color: red;
}
}
}

The Sass ampersand and attribute selectors

I want to create a sass file that the selectors will be attribute selectors.
When I work with class selectors, in most of the cases I will do
.parent {
&-child {
}
}
which gives me the following css: .parent-child {}.
I want to achieve the same thing with attribute selectors:
[data-parent] {
&-child {
}
}
which I want to become: [data-parent-child] {}
someone knows how to achieve this? thanks.
You can use this mixin as a workaround to get the desired result.
#mixin child-attribute($child) {
$string: inspect(&);
$original: str-slice($string, 3, -4);
#at-root #{ selector-replace(&, &, "[#{$original}#{$child}]" ) } {
#content;
}
}
The code simply does the following
$string variable is responsible for turning the parent selector to a string using the inspect function
$original variable is responsible for getting the text content of the $string variable i.e the value 'data-parent' from '([data-parent])'
selector-replace function then replaces the parent selector with the concatenation of the $original variable and child variable
When used in the following ways
[data-parent] {
#include child-attribute('-child') {
color: green;
}
}
The css output
[data-parent-child] {
color: green;
}
Depending on what you want to achieve, it can also be used like this
[grandparent] {
#include child-attribute('-parent') {
color: white;
#include child-attribute('-child') {
color: blue;
}
}
}
Which generates the following css
[grandparent-parent] {
color: white;
}
[grandparent-parent-child] {
color: blue;
}
Hope this helps you
You can create mixin that will set styles for elements with data attribytes.
Scss:
#mixin data($name) {
[data-#{$name}] {
#content;
}
}
* {
#include data('lol') {
color: red;
};
}
Css output:
* [data-lol] {
color: red;
}
DEMO
I would go down a slightly different route of having a class on your elements that contain the data attributes.
<div class="data-obj" data-parent="true"></div>
<div class="data-obj" data-parent-child="true"></div>
then in your SASS do
.data-obj {
...
&[data-parent] { ... }
&[data-parent-child] { ... }
}

Use mixin argument to create class name in LESS

I'm trying to create a simple mixin in LESS for different colors I'll use for a website.
What i want is use mixin argument as a part of class name as well.
#green: #5FBEAA; // my color variable
.text-color(#color) {
.text-{#color} {
color: #color;
}
}
.text-color(#green);
The output i'm getting is:
.text-#5FBEAA {
color:#5FBEAA
}
What I want is:
.text-green {
color:#5FBEAA
}
I think I have the solution using Variable Names.
Less
#green: #5FBEAA;
.text-color(#colorname) {
#color: ~"#{colorname}";
.text-#{color}{
color: ##color;
}
}
.text-color(green);
Output
.text-green {
color: #5FBEAA;
}
I don't think its possible. The closest solution for this will be using an additional variable.
#green: #5FBEAA;
.text-color(#name,#color) {
.text-#{name} {
color: #color;
}
}
.text-color(green,#green);
This will compile to
.text-green {
color: #5FBEAA;
}

SCSS combine :hover and #if

Is it possible to combine the following code and using the :hover selector and #if statement together?
$active: true;
button {
&:hover {
color: red;
}
#if $active {
color: red;
}
}
This is the thing I'd like to do:
button {
&:hover,
& #if $active {
color: red;
}
}
Thanks in advance.
You can actually kind of achieve this using $var: if();. It could look something like this:
$target: if(true, "&", "");
a {
&:hover,
#{$target} {
color: black;
}
}
More info on if() here: http://thesassway.com/news/sass-3-3-released#if

How to change a class format based on body's class with SCSS?

I am making a web app that is used in three (or more) different contexts, and I want each context to have a different color scheme. However, I don't want to have to maintain three different stylesheets when all that changes is colors, typically.
For instance, suppose the themes are red, blue, and orange. One of my stylesheets describes the link colors:
a {
color: $some_color;
}
I want to split this based on the class applied to the body:
body.style1 {
a {
color: $red;
}
}
body.style2 {
a {
color: $blue;
}
}
body.style3 {
a {
color: $orange;
}
}
You can see how this gets unwieldy pretty quickly if you're changing the style for lots of elements. Is there a way to do this more like this?
a {
&closest:body.style1 {
color: $red
}
&closest:body.style2 {
color: $blue;
}
&closest:body.style3 {
color: $orange;
}
}
This way I can code my scss in a clearer, more maintainable way.
It appers you don't have to have the & first, so this works (at least in 3.2.10):
a {
body.style1 & {
color: $red
}
body.style2 & {
color: $blue;
}
body.style3 &{
color: $orange;
}
}
This is what I prefer. Define a mixin like body-style :
#mixin body-style($style, $map) {
body.#{$style} & {
#each $property, $value in $map {
#{$property}: $value;
}
}
}
Then use this for every tag by passing $style as style class of body and $map as map of css keys and values.
a {
#include body-style(style1, (
color: red,
background: white
)
);
}
It will return :
body.style1 a {
color: red;
background: white;
}

Resources