Skip to content

Instantly share code, notes, and snippets.

@certainlyakey
Last active August 2, 2018 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save certainlyakey/89f0feb17a6a2399983cc09d01e04b45 to your computer and use it in GitHub Desktop.
Save certainlyakey/89f0feb17a6a2399983cc09d01e04b45 to your computer and use it in GitHub Desktop.
Last parent selector in SASS (like & but only the last one)
// same as &, but applies the last (closest) parent only
// usage: @include u-direct-parent('.parent'<, ...>) {}
@mixin u-direct-parent($parent-selectors...) {
@each $parent-selector in $parent-selectors {
$current-sequences: &;
$new-sequences: ();
@each $sequence in $current-sequences {
$current-selector: nth($sequence, -1);
$prepended-selector: join($parent-selector, $current-selector);
$new-sequence: set-nth($sequence, -1, $prepended-selector);
$new-sequences: append($new-sequences, $new-sequence, comma);
}
@at-root #{$new-sequences} {
@content;
}
}
}
@certainlyakey
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment