Common styles for elements and modifiers - sass

if I have:
.control-button {
border: 1px solid red;
&--primary {
color: blue;
}
&--secondary {
color: white;
}
}
how do I make sure that border gets set in both button classes? As it is right now it generates following css:
.control-button {
border: 1px solid red;
}
.control-button--primary {
color: blue;
}
.control-button--secondary {
color: white;
}

You apply both classes to the element:
<button class="control-button control-button--primary">Primary button</button>
<button class="control-button control-button--secondary">Secondary button</button>
The block class defines any shared styling and modifier classes can be used to override or extend it.

It seems this is possible using string interpolation:
.control-button {
$c: &; // store classname as a var
border: 1px solid red;
&--primary {
#extend #{$c};
color: blue;
}
&--secondary {
#extend #{$c};
color: white;
}
}
That generates following css:
.control-button, .control-button--primary, .control-button--secondary {
border: 1px solid red;
}
.control-button--primary {
color: blue;
}
.control-button--secondary {
color: white;
}
It works, yet this probably, arguably a stupid way. Makes things a bit cryptic. But it is possible.

Related

BEM convention for naming multiple complex modifiers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
i've been confused for finding the best way of naming multiple modifiers,
i know the basic rules of this methodology and i read the official BEM quick-start documentation ( bem.info/methodology/quick-start )
i have problem with naming modifiers when i have some modifiers in a block which one of these has some effects on another modifier ( in other word, a modifier in addition to modifying the main block, also is modifying another modifier of that block )
instead of explaining the problem i use a simple example for this situation, so please just tell me which one of this following examples is the correct and the best option:
Solution 1)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
background: transparent;
}
&--outline#{&}--primary {
border: 1px solid blue;
}
&--outline#{&}--secondary {
border: 1px solid black;
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--secondary {
background: black;
}
.btn--outline {
background: transparent;
}
.btn--outline.btn--primary {
border: 1px solid blue;
}
.btn--outline.btn--secondary {
border: 1px solid black;
}
Sample of usage in HTML:
<a href="#" class="btn btn--primary btn--outline>...</a>
Solution 2)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
background: transparent;
}
&--outline#{&} {
&--primary {
border: 1px solid blue;
}
&--secondary {
border: 1px solid black;
}
}
}
Compiled CSS is same as solution 1
Sample of usage in HTML is also same as solution 1
Actually i just did a nesting in modifiers for prevent repeating the "outline",
Solution 3)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
background: transparent;
&--primary {
border: 1px solid blue;
}
&--secondary {
border: 1px solid black;
}
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--secondary {
background: black;
}
.btn--outline {
background: transparent;
}
.btn--outline--primary {
border: 1px solid blue;
}
.btn--outline--secondary {
border: 1px solid black;
}
Sample of usage in HTML:
...
Solution 4)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
&--primary {
background: transparent;
border: 1px solid blue;
}
&--secondary {
background: transparent;
border: 1px solid black;
}
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--secondary {
background: black;
}
.btn--outline--primary {
background: transparent;
border: 1px solid blue;
}
.btn--outline--secondary {
background: transparent;
border: 1px solid black;
}
Sample of usage in HTML:
...
As you see this solution is easier for usage in HTML, but we are duplicating same CSS property in each primary and secondary modifier ( in this case we are duplicating background: transparent; in other modifiers ), you may think it doesn't matter, but in real cases we may have lots of property which is duplicating in many places so definitely this solution will have important problems for the development of this component in the future
Solution 5)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
&--outline {
background: transparent;
border: 1px solid blue;
}
}
&--secondary {
background: black;
&--outline {
background: transparent;
border: 1px solid black;
}
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--primary--outline {
background: transparent;
border: 1px solid blue;
}
.btn--secondary {
background: black;
}
.btn--secondary--outline {
background: transparent;
border: 1px solid black;
}
Sample of usage in HTML:
...
We also have duplicated properties in this solution ( like the solution 4 )
So it doesn't have a good development structure and it will bother programmer in a large component
i just want to know which solution is the best choice, or if you have another solution for this problem please write base on this button example.
also i mention this point again:
I wrote that example in a very little scale of a real component, so any duplicating of properties or classes name may doesn't seem to care about in this example, but in the more complex cases in the real project it will be a serious problem for development of a component.
Chaining attribute selectors may seem a good fit for your special case.
let's say we have all the buttons containing a .btn at least.
This way you could both keep the logic for each modification case in a the dedicated css rule and also have the duplications to their minimum.
let's say you have a
<button class="btn--primary--outline"></button>
then you could probably write them css rules as follows:
[class^="btn"] {
font-size: 14px;
}
[class^="btn"][class*="--primary"] {
background: blue;
border-color: blue;
}
[class^="btn"][class*="--outline"] {
border: 1px solid;
}

Sass BEM modifiers and children

I have the following BEM setup:
.mytable {
font-size: 16px;
margin: 30px 0;
&--standard {
border: 1px solid red;
&__row {
border: 1px solid blue;
}
}
What I am trying to do is apply the row styles only to the modified table class.
This outputs the following
.mytable--standard__row {
border: 1px solid blue;
}
Which is obviously not what I am trying to achieve.
Is there a neat/standard way to solve this problem?
You can add another ampersand after the modifier to get the desired output:
.mytable {
font-size: 16px;
margin: 30px 0;
&--standard {
border: 1px solid red;
}
&--standard & { //<--
&__row{
border: 1px solid blue;
}
&__some-other-element{}
}
}
We use:
.mytable {
font-size: 16px;
margin: 30px 0;
&--standard {
border: 1px solid red;
}
&--standard &__row {
border: 1px solid blue;
}
}
Its not ideal (a && grandparent selector would be nice), but its the best we could come up with so far

Is it possible with SCSS (or SASS) add same class to several elements, but with different styles for each? [duplicate]

This question already has an answer here:
Append the parent selector to the end with Sass
(1 answer)
Closed 6 years ago.
This is what I want:
From something like this (this code doesn't work, it throws an error):
.ia-loading {
select.& {
border: 2px solid blue;
}
input.& {
border: 2px solid orange;
}
}
Get this:
select.ia-loading { border: 2px solid blue; }
input.ia-loading { border: 2px solid orange; }
I have tried several combinations, none of which have worked:
This one:
.ia-loading {
select & {
border: 2px solid blue;
}
input & {
border: 2px solid orange;
}
}
Produces this css, which is not what I want:
select .ia-loading { border: 2px solid blue; }
input .ia-loading { border: 2px solid orange; }
This one:
.ia-loading {
select& {
border: 2px solid blue;
}
input& {
border: 2px solid orange;
}
}
throws this error:
"&" may only be used at the beginning of a compound selector.
Many thanks in advance.
You could make small mixin for this like so:
#mixin test($elem) {
#{$elem}.test {
#content;
}
}
And if you really want to use it inside your class you can use #at-root directive since Sass 3.3 (else you just put it outside your main class)
.test {
margin: 20px;
#at-root {
#include test(select) {
border: 1px solid red;
}
#include test(input) {
border: 1px solid blue;
}
}
}
DEMO
What is the reason you are using '&' behind input and select?
Its only used when you want to have a specific "action" like hover, active etc.
Example
.ia-loading
&:hover
color: #FFF
Also in Sass you can't use brackets, that is used in scss
I also don't think you can do it the way you want it. It should be done like this:
.ia-loading
border: 2px solid
select
.ia-loading
border-color: blue
input
.ia-loading
border-color: orange

Sass / Scss: Changing the order of nested psuedo selectors for :before and :hover in generated CSS?

Given the following Sass:
div.test {
display: inline-block;
background-color: #ffffff;
color: #000000;
&:before {
& {
&:hover {
border: 1px solid salmon;
}
}
width: 25px;
height: 25px;
content: "";
}
}
The resulting CSS compiles to:
div.test {
display: inline-block;
background-color: #ffffff;
color: #000000;
}
div.test:before {
width: 25px;
height: 25px;
content: "";
}
div.test:before:hover {
border: 1px solid salmon;
}
What I am attempting to do is generate div.test:hover:before (the current output is before:hover).
NOTE: I am able to generate the expected CSS by using the following Sass:
div.test {
display: inline-block;
background-color: #ffffff;
color: #000000;
&:hover {
&:before {
border: 1px solid salmon;
}
}
&:before {
width: 25px;
height: 25px;
content: "";
}
}
However I would like to know if it is possible using the first nested approach or some modification of it.
The goal was to avoid having to repeat &:before if there was such a way to do so using Sass syntax. I am also OK with knowing it isn't possible.
While initially the plan was to have '&' available in SassScript as a string that could be manipulated so that you could insert values wherever you wanted, those plans have been abandoned for 3.3 due to complication. Unfortunately you'll have to wait a while to be able to do this. At the moment '&' is immutable and just means "whatever the selector chain up to this point is".
EDIT (2020.02.15):
it is now technically possible to achieve this with recent versions of dart-sass:
#use "sass:selector";
#mixin unify-parent($child) {
#at-root #{selector.unify(&, $child)} {
#content;
}
}
div.test {
display: inline-block;
background-color: #ffffff;
color: #000000;
&:before {
width: 25px;
height: 25px;
content: "";
#include unify-parent(":hover") {
border: 1px solid salmon;
}
}
}
Sources:
https://sass-lang.com/blog/the-module-system-is-launched
https://sass-lang.com/documentation/style-rules/parent-selector#advanced-nesting

Custom directives within scss files? Perhaps for a new pseudo class?

Is it be possible to use some sort of #directive creation syntax, similar to creating #mixins? Secondly, is it possible to create a SASS-only pseudo class?
I'd like to declare my own SASS directive,although I'd prefer not to have to force my teammates to install an extra ruby gem to use it so I'd want to store it in a scss partial. I do understand that they are orders of levels in complexity, so perhaps it just isn't possible.
In addition to perhaps creating a new scss-only pseudo class (such as :parent, :clicked, :unchecked, etc) I'd be interested in a custom-made directive that assists with using checkboxes to direct css animations ("css checkbox hack"). Here is my scss pseudocode to generalize what I'm trying to do:
// this code monitors when a checkbox (#someinput) is checked,
// then applies style to div #target div. Basically an 'onClick' event in scss.
body {
#wrapper {
#targetdiv {
#spotcheck(#someinput) { #
color: red; border: 2px solid blue; # <-- move this ...
} #
color: blue; border: 0;
#someinput {
width: 20px; height: 20px;
}
}
}
}
// ... the above scss should be converted to this pre-compiled state, also scss
body {
#someinput:checked ~ #targetdiv { #
color: red; border: 2px solid blue; # <-- ..to here. it needs to be
} # above the #targetdiv
#wrapper {
#targetdiv {
color: blue; border: 0;
#someinput {
width: 20px; height: 20px;
}
}
}
}
Make your selectors only as specific as they absolutely need to be and no more. A mixin would only be more verbose with no real benefit.
#targetdiv {
color: blue; border: 0;
#someinput:checked ~ & {
color: red; border: 2px solid blue;
}
}
#someinput {
width: 20px; height: 20px;
}
Output:
#targetdiv {
color: blue;
border: 0;
}
#someinput:checked ~ #targetdiv {
color: red;
border: 2px solid blue;
}
#someinput {
width: 20px;
height: 20px;
}
Alternately, this would give the same result with the ability to overqualify as much as you want:
#targetdiv {
color: blue; border: 0;
}
#someinput {
width: 20px; height: 20px;
~ #targetdiv {
color: red; border: 2px solid blue;
}
}

Resources