Skip to content

Instantly share code, notes, and snippets.

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 cmpscabral/b4e448d671a876b95fd16d6bfd5d06e1 to your computer and use it in GitHub Desktop.
Save cmpscabral/b4e448d671a876b95fd16d6bfd5d06e1 to your computer and use it in GitHub Desktop.
Advanced nth-child mixins
@mixin valid-quantity($quantity) {
@if type-of($quantity) != 'number' {
@error 'The "quantity" parameter must be a number!';
}
@if not(unitless($quantity)) {
@error 'The "quantity" parameter must not have a unit!';
}
@if $quantity < 0 {
@error 'The "quantity" parameter must be at least 0!';
}
}
@mixin has-nth($expression, $element: '*') {
&:nth-last-child(#{$expression}):first-child,
&:nth-last-child(#{$expression}):first-child ~ #{$element} {
@content;
}
}
@mixin at-least($quantity, $element: '*') {
@include valid-quantity($quantity);
@include has-nth('n + #{$quantity}', $element) {
@content;
}
}
@mixin at-most($quantity, $element: '*') {
@include valid-quantity($quantity);
@include has-nth('-n + #{$quantity}', $element) {
@content;
}
}
@mixin divisible-by($quantity, $offset: 0, $element: '*') {
@include valid-quantity($quantity);
@include has-nth('#{$quantity}n + #{$offset}', $element) {
@content;
}
}
@mixin has-exactly($quantity, $element: '*') {
@include valid-quantity($quantity);
@include has-nth('#{$quantity}', $element) {
@content;
}
}
@mixin has-odd($element: '*') {
@include has-nth('odd', $element) {
@content;
}
}
@mixin has-even($element: '*') {
@include has-nth('even', $element) {
@content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment