Skip to content

Instantly share code, notes, and snippets.

@rikonor
Created December 12, 2015 18:28
Show Gist options
  • Save rikonor/ec17c76ade520f090779 to your computer and use it in GitHub Desktop.
Save rikonor/ec17c76ade520f090779 to your computer and use it in GitHub Desktop.
Sass
@import "utils"
// Glboal breakpoints
$breakpoints: (
small: 412px,
medium: 992px,
large: 1200px
);
// This is an extenstion of http://www.sitepoint.com/using-sass-maps/
@mixin respond-to($breakpoint) {
@if map-has-key($breakpoints, $breakpoint) {
@media (min-width: #{map-get($breakpoints, $breakpoint)}) {
@content;
}
}
@else if $breakpoint == "default" {
@content;
}
@else if is-px($breakpoint) {
@media (min-width: #{$breakpoint}) {
@content;
}
}
}
@mixin bp-options($configKey, $config, $transform: keep-same) {
@each $configBreakpoint, $configValue in $config {
@include respond-to($configBreakpoint) {
#{$configKey}: call($transform, $configValue);
}
}
}
// Given <X>px return true, otherwise return false
@function is-px($str) {
$tStr: quote($str);
@return str-index($tStr, px);
}
// Identity function
@function keep-same($val) {
@return $val;
}
.element {
color: blue;
}
@media (min-width: 767px) {
.element {
color: yellow;
}
}
@media (min-width: 1000px) {
.element {
color: white;
}
}
.element {
@include bp-options(color, (
default: blue,
small: yellow,
1000px: white
));
}
.page {
min-height: calc(100vh - 45px);
}
@media (min-width: 767px) {
.page {
min-height: calc(100vh - 60px);
}
}
// Let's assume we have a nav with a set of breakpoint specific heights
$navHeights: (
default: 45px,
small: 60px
);
// We are interested in having another element's property be dependent on the nav height in the following way
@function page-min-height($navHeight) {
@return calc(100vh - $navHeight);
}
.page {
@include bp-options(min-height, $navHeights, page-min-height);
}
// In this way - if we change $navHeights, the .page min-height will automatically be adjusted as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment