Last active
March 16, 2018 01:01
-
-
Save kfranqueiro/6a47cafa274f2d774b2d133ed7750f02 to your computer and use it in GitHub Desktop.
MDC Web #2415 experimentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@function replace-or-append($list, $target, $value) { | |
$newlist: (); | |
$replaced: false; | |
@for $i from 1 through length($list) { | |
@if (nth($list, $i) == $target) { | |
$newlist: append($newlist, $value); | |
$replaced: true; | |
} @else { | |
$newlist: append($newlist, nth($list, $i)); | |
} | |
} | |
@if not $replaced { | |
$newlist: append($newlist, $value); | |
} | |
@return $newlist; | |
} | |
@function replace-parent-or-append($selectors, $target, $value) { | |
@for $i from 1 through length($selectors) { | |
$selectors: set-nth($selectors, $i, replace-or-append(nth($selectors, $i), $target, $value)); | |
} | |
@return $selectors; | |
} | |
@mixin mdc-bar-color($color) { | |
$selectors: replace-parent-or-append(&, '.mdc-bar', '.mdc-bar__sub'); | |
@at-root #{$selectors} { | |
color: $color; | |
} | |
} | |
.mdc-bar { | |
@include mdc-bar-color(#000); | |
} | |
.mdc-foo { | |
@include mdc-bar-color(#333); | |
} | |
.mdc-foo .mdc-bar { | |
@include mdc-bar-color(#333); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.mdc-bar__sub { /* Problematic, since we presumably _want_ .mdc-bar here! */ | |
color: #000; | |
} | |
.mdc-foo .mdc-bar__sub { /* Working as intended, adds the class */ | |
color: #333; | |
} | |
.mdc-foo .mdc-bar__sub { /* Working as intended, replaces the class */ | |
color: #333; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment