Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created September 6, 2016 13:44
Show Gist options
  • Save goldsborough/09544deb5cb9ce22e6561a650e84b1f9 to your computer and use it in GitHub Desktop.
Save goldsborough/09544deb5cb9ce22e6561a650e84b1f9 to your computer and use it in GitHub Desktop.
Fun with SCSS
@mixin media-query($type) {
@if $type == phone {
@media only screen and (min-width: 320px) {
@content;
}
} @else if $type == tablet {
@media only screen and (min-width: 768px) {
@content;
}
} @else {
@media only screen and (min-width: 1024px) {
@content;
}
}
}
@mixin expand-map($map) {
@each $property, $value in $map {
#{$property}: #{$value};
}
}
@mixin media-switch($media-map) {
@each $device in phone, tablet {
@include media-query($device) {
@include expand-map(map-get($media-map, $device));
}
}
}
@function present($map, $key) {
@return map-get($map, $key) != null;
}
@function list-to-map($list, $keys) {
$map: ();
@each $key, $value in zip($keys, $list) {
$map: map-merge($map, ($key: $value));
}
@return $map;
}
@function map-add($map, $key, $value) {
@return map-merge($map, ($key: $value));
}
@function map-duplicate($map, $source-key, $destination-key) {
$source-value: map-get($map, $source-key);
@return map-add($map, $destination-key, $source-value);
}
@function default-key($map, $key, $alternative) {
@if present($value-map, tablet) {
@return $map;
}
@return map-duplicate($value-map, $alternative, $key);
}
@function optional-alias($map, $first-key, $second-key) {
@if not present($map, $first-key) {
@return map-duplicate($map, $second-key, $first-key);
}
@if not present($map, $second-key) {
@return map-duplicate($map, $first-key, $second-key);
}
@return $map;
}
@function swap($list, $first, $second) {
$temp: nth($list, $first);
$list: set-nth($list, $first, nth($list, $second));
$list: set-nth($list, $second, $temp);
@return $list;
}
@function quicksort-impl($list, $first, $last) {
@if (($last - $first) < 2) {
@return $list;
}
$i: $first + 1;
$j: $last - 1;
$pivot: nth($list, $first);
@while $i != $j {
@while ($i != $j) and (nth($list, $j) > $pivot) {
$j: $j - 1;
}
@while ($i != $j) and (nth($list, $i) < $pivot) {
$i: $i + 1;
}
@if ($i != $j) {
$list: swap($list, $i, $j);
$i: $i + 1;
}
}
$list: swap($list, $first, $i);
$list: quicksort-impl($list, $first, $i);
$list: quicksort-impl($list, $i + 1, $last);
@return $list;
}
@function quicksort($list) {
$list: quicksort-impl($list, 1, length($list) + 1);
@return $list;
}
@mixin responsive($property, $value-map) {
@if type-of($value-map) == list {
$value-map: list-to-map($value-map, (phone, tablet, desktop));
}
$value-map: optional-alias($value-map, tablet, desktop);
@each $device in phone, tablet, desktop {
@include media-query($device) {
#{$property}: map-get($value-map, $device);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment