// // DOCUMENTATION AND SOURCE AT // https://github.com/tspringborg/responsively/ // // GENERAL SETTINGS $responsively-min-width: 375px !default; $responsively-max-width: 1280px !default; $responsively-rem-context: 16px !default; // UTILITY FUNCTIONS // @function rem($target, $context: $responsively-rem-context) { @return strip-unit($target / $context) * 1rem; } @function em($target, $context: 16, $unitless: false) { $unit: if($unitless, 1, 1em); @return strip-unit($target / $context) * $unit; } @function to-number($value) { @if type-of($value) == "number" { @return $value; } @else if type-of($value) != "string" { $_: log("Value for `to-number` should be a number or a string."); } $result: 0; $digits: 0; $minus: str-slice($value, 1, 1) == "-"; $numbers: ( "0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9 ); @for $i from if($minus, 2, 1) through str-length($value) { $character: str-slice($value, $i, $i); @if not(index(map-keys($numbers), $character) or $character == ".") { @return to-length(if($minus, -$result, $result), str-slice($value, $i)); } @if $character == "." { $digits: 1; } @else if $digits == 0 { $result: $result * 10 + map-get($numbers, $character); } @else { $digits: $digits * 10; $result: $result + map-get($numbers, $character) / $digits; } } @return if($minus, -$result, $result); } // Add `$unit` to `$value` @function to-length($value, $unit) { $units: ( "px": 1px, "cm": 1cm, "mm": 1mm, "%": 1%, "ch": 1ch, "pc": 1pc, "in": 1in, "em": 1em, "rem": 1rem, "pt": 1pt, "ex": 1ex, "vw": 1vw, "vh": 1vh, "vmin": 1vmin, "vmax": 1vmax ); @if not index(map-keys($units), $unit) { $_: log("Invalid unit `#{$unit}`."); @return $_; } @return $value * map-get($units, $unit); } // Removes unit from a value @function strip-unit($value) { @return $value / ($value * 0 + 1); } // Splits a string on a separator @function split-str($string, $separator) { $index: str-index($string, $separator); $str-1: str-slice($string, 1, $index - 1); $str-2: str-slice($string, $index + 1); @return $str-1 $str-2; } @mixin _r($min-vw, $max-vw, $min-value, $max-value, $properties) { @each $property in $properties { #{$property}: $min-value; } $max-minus-min-value: strip-unit($max-value - $min-value); @if (unit($min-value) == "rem" and unit($max-value) == "rem") { $max-minus-min-value: strip-unit($max-minus-min-value * $responsively-rem-context); } @media (min-width: $min-vw) { @each $property in $properties { #{$property}: calc(#{$min-value} + #{$max-minus-min-value} * (100vw - #{$min-vw}) / #{strip-unit($max-vw - $min-vw)}); } } @media (min-width: $max-vw) { @each $property in $properties { #{$property}: $max-value; } } } @mixin responsively($properties, $values) { // default syntax $i: 1; @while $i <= length($values) { $min-vw: $responsively-min-width; $max-vw: $responsively-max-width; $initial: $i; $end: $i + 1; // might need to use previous end value as initial value @if $i + 1 > length($values) { $initial: $i - 1; $end: $i; } $initial-value: nth($values, $initial); $end-value: nth($values, $end); @if type-of($initial-value) == 'list' { // using the list syntax. like ($size: $value, $size: $value2, etc... ) $min-vw: nth($initial-value, 1); $max-vw: nth($end-value, 1); $initial-value: nth($initial-value, 2); $end-value: nth($end-value, 2); } @else { // using the default shorthand syntax @if (type-of($initial-value) == "string") { // So it is a string does it have a "@" symbol to break apart @if (str-index($initial-value, "@")) { // split string helper $initial-value-list: split-str($initial-value, "@"); // unquote the split and cast the string to a number // http://hugogiraudel.com/2014/01/15/sass-string-to-number/ $initial-value: to-number(unquote(nth($initial-value-list, 1))); $min-vw: to-number(unquote(nth($initial-value-list, 2))); } } @if (type-of($end-value) == "string") { // So it is a string does it have a "@" symbol to break apart @if (str-index($end-value, "@")) { // split string helper $end-value-list: split-str($end-value, "@"); // unquote the split and cast the string to a number // http://hugogiraudel.com/2014/01/15/sass-string-to-number/ $end-value: to-number(unquote(nth($end-value-list, 1))); $max-vw: to-number(unquote(nth($end-value-list, 2))); } } } @include _r($min-vw, $max-vw, $initial-value, $end-value, $properties); $i: $i + 2 } }