Permalink
Fetching contributors…
Cannot retrieve contributors at this time
402 lines (362 sloc) 12.9 KB
//////////////////////////////
// Default Variables
//////////////////////////////
$breakpoint-default-feature: 'min-width' !default;
$breakpoint-default-media: 'all' !default;
$breakpoint-force-media-all: false !default;
$breakpoint-default-pair: 'width' !default;
$breakpoint-to-ems: false !default;
$breakpoint-prefixes: 'webkit' 'moz' !default;
$breakpoint-prefixed-queries: 'device-pixel-ratio' 'min-device-pixel-ratio' 'max-device-pixel-ratio' !default;
$breakpoint-no-queries: false !default;
$breakpoint-no-query-wrappers: false !default;
$breakpoint-base-font-size: false;
//////////////////////////////
// Import Breakpoint Helpers
//////////////////////////////
@import 'breakpoint/helpers';
@import 'breakpoint/context';
//////////////////////////////
// Breakpoint Mixin
//////////////////////////////
@mixin breakpoint($breakpoint, $media: $breakpoint-default-media, $no-query: false, $base-font-size: $breakpoint-base-font-size) {
// Query and Media String Defaults
$query: false !default;
$query-holder: false !default;
$media-string: false !default;
$do-prefix: false !default;
$webkit: false !default;
$webkit-first: true !default;
$moz: false !default;
$moz-first: true !default;
$o: false !default;
$o-first: true !default;
$ms: false !default;
$ms-first: true !default;
// Holder for Count
$first: true !default;
// Reset Context
@include TXkgcmVzZXQhIEdvIGF3YXkh;
// Set Media Context
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh('media', $media);
// Initialize Query String
@if $media != 'all' or $breakpoint-force-media-all {
$media-string: "#{$media} ";
}
@else {
$media-string: "";
}
// If we have a single query, let's just work with that.
@if is_breakpoint_list($breakpoint) == false {
@each $prefix-query in $breakpoint-prefixed-queries {
@if $do-prefix == false {
$do-prefix: featureExists($prefix-query, $breakpoint);
}
}
@if $do-prefix {
@each $prfx in $breakpoint-prefixes {
@if $prfx == 'webkit' {
$webkit: breakpoint-switch($breakpoint, $media-string, true, $prfx, $base-font-size: $base-font-size);
}
@if $prfx == 'moz' {
$moz: breakpoint-switch($breakpoint, $media-string, true, $prfx, $base-font-size: $base-font-size);
}
@if $prfx == 'o' {
$o: breakpoint-switch($breakpoint, $media-string, true, $prfx, $base-font-size: $base-font-size);
}
@if $prfx == 'ms' {
$ms: breakpoint-switch($breakpoint, $media-string, true, $prfx, $base-font-size: $base-font-size);
}
}
}
@else {
$query: breakpoint-switch($breakpoint, $media-string, true, $base-font-size: $base-font-size);
}
}
@else {
// See if Prefix Query exists
@each $prefix-query in $breakpoint-prefixed-queries {
@if $do-prefix == false {
$do-prefix: featureExists($prefix-query, $breakpoint);
}
}
@if $do-prefix {
@each $prfx in $breakpoint-prefixes {
@each $bkpt in $breakpoint {
@if $prfx == 'webkit' {
@if $webkit-first {
$webkit: breakpoint-switch($bkpt, $media-string, true, $prfx, $base-font-size: $base-font-size);
$webkit-first: false;
}
@else {
$webkit: join($webkit, breakpoint-switch($bkpt, $media-string, $prefix: $prfx, $base-font-size: $base-font-size));
}
}
@if $prfx == 'moz' {
@if $moz-first {
$moz: breakpoint-switch($bkpt, $media-string, true, $prfx, $base-font-size: $base-font-size);
$moz-first: false;
}
@else {
$moz: join($moz, breakpoint-switch($bkpt, $media-string, $prefix: $prfx, $base-font-size: $base-font-size));
}
}
@if $prfx == 'o' {
@if $o-first {
$o: breakpoint-switch($bkpt, $media-string, true, $prfx, $base-font-size: $base-font-size);
$o-first: false;
}
@else {
$o: join($o, breakpoint-switch($bkpt, $media-string, $prefix: $prfx, $base-font-size: $base-font-size));
}
}
@if $prfx == 'ms' {
@if $ms-first {
$ms: breakpoint-switch($bkpt, $media-string, true, $prfx, $base-font-size: $base-font-size);
$ms-first: false;
}
@else {
$ms: join($ms, breakpoint-switch($bkpt, $media-string, $prefix: $prfx, $base-font-size: $base-font-size));
}
}
}
}
}
@else {
@each $bkpt in $breakpoint {
@if $first == true {
$query: breakpoint-switch($bkpt, $media-string, true, $base-font-size: $base-font-size);
$first: false;
}
@else {
$query: join($query, breakpoint-switch($bkpt, $media-string, $base-font-size: $base-font-size));
}
}
}
}
@if $breakpoint-no-queries {
@if $no-query {
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh('no queries', true);
@if $breakpoint-no-query-wrappers and type-of($no-query) == string {
#{$no-query} & {
@content;
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh('no query wrapper', $no-query);
}
}
@else {
@content;
}
}
}
@else {
@if $breakpoint-no-query-wrappers and type-of($no-query) == string {
#{$no-query} & {
@content;
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh('no query wrapper', $no-query);
}
}
@if $query {
@media #{$query} {
@content;
}
}
@else {
$pf-queries: $webkit, $moz, $o, $ms;
$pf-query: ();
@each $pfq in $pf-queries {
@if $pfq {
$pf-query: append($pf-query, $pfq, comma);
}
}
@media #{$pf-query} {
@content;
}
}
}
@include TXkgcmVzZXQhIEdvIGF3YXkh;
}
@function breakpoint-switch($breakpoint, $media-string, $first: false, $prefix: false, $base-font-size: $breakpoint-base-font-size) {
// Feature/Value/Length/Query Placehoders:
$feature: false !default;
$min-feature: "min-#{$breakpoint-default-pair}" !default;
$max-feature: "max-#{$breakpoint-default-pair}" !default;
$value: false !default;
$min-value: false !default;
$max-value: false !default;
$length: false !default;
$query: false !default;
$length: length($breakpoint);
// Check to see if there is only one item.
@if $length == 1 {
$value: $breakpoint;
@if type-of($breakpoint) == 'number' {
$feature: $breakpoint-default-feature;
}
// If EM Breakpoints are active, do it!
@if $breakpoint-to-ems and type-of($value) == 'number' {
$value: breakpoint-to-base-em($value, $base-font-size);
}
// Build the Query
$query: breakpoint-generate($media-string, $feature, $value, $first);
// Set Context
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($feature, $value);
}
@else if $length == 2 {
// If both are numbers, we've got a double!
@if type-of(nth($breakpoint, 1)) == 'number' and type-of(nth($breakpoint, 2)) == 'number' {
// See which is larger.
@if nth($breakpoint, 1) > nth($breakpoint, 2) {
$min-value: nth($breakpoint, 2);
$max-value: nth($breakpoint, 1);
}
@else {
$min-value: nth($breakpoint, 1);
$max-value: nth($breakpoint, 2);
}
// If EM Breakpoints are active, do it!
@if $breakpoint-to-ems and type-of($min-value) == 'number' {
$min-value: breakpoint-to-base-em($min-value, $base-font-size);
}
@if $breakpoint-to-ems and type-of($max-value) == 'number' {
$max-value: breakpoint-to-base-em($max-value, $base-font-size);
}
// Min/Max for given
$query: breakpoint-generate($media-string, $min-feature, $min-value, $first);
$query: join($query, breakpoint-generate($media-string, $max-feature, $max-value));
// Set Context
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($min-feature, $min-value);
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($max-feature, $max-value);
}
@else if type-of(nth($breakpoint, 1)) == 'string' and type-of(nth($breakpoint, 2)) == 'string' {
@if breakpoint-string-value(nth($breakpoint, 1)) == true {
$feature: nth($breakpoint, 1);
$value: nth($breakpoint, 2);
}
@else {
$feature: nth($breakpoint, 2);
$value: nth($breakpoint, 1);
}
// If EM Breakpoints are active, do it!
@if $breakpoint-to-ems and type-of($value) == 'number' {
$value: breakpoint-to-base-em($value, $base-font-size);
}
// Build the Query
$query: breakpoint-generate($media-string, $feature, $value, $first);
// Set Context
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($feature, $value);
}
@else {
// Because we can have either the first or second option be the feature, we switch on it.
@if type-of(nth($breakpoint, 1)) == string {
$feature: nth($breakpoint, 1);
$value: nth($breakpoint, 2);
}
@else if type-of(nth($breakpoint, 2)) == string {
$feature: nth($breakpoint, 2);
$value: nth($breakpoint, 1);
}
@if $feature == 'device-pixel-ratio' or $feature == 'min-device-pixel-ratio' or $feature == 'max-device-pixel-ratio' {
$feature: breakpoint-experimental($feature, $prefix);
//$value: 96 * $value * 1dpi;
// @if $feature == 'device-pixel-ratio' {
// $feature: 'resolution';
// }
// @else if $feature == 'min-device-pixel-ratio' {
// $feature: 'min-resolution';
// }
// @else if $feature == 'max-device-pixel-ratio' {
// $feature: 'max-resolution';
// }
}
// If EM Breakpoints are active, do it!
@if $breakpoint-to-ems and type-of($value) == 'number' {
$value: breakpoint-to-base-em($value, $base-font-size);
}
// Build the Query
$query: breakpoint-generate($media-string, $feature, $value, $first);
// Set Context
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($feature, $value);
}
}
@else if $length == 3 {
@if type-of(nth($breakpoint, 1)) == 'string' {
$feature: nth($breakpoint, 1);
// See which is larger.
@if nth($breakpoint, 2) > nth($breakpoint, 3) {
$min-value: nth($breakpoint, 3);
$max-value: nth($breakpoint, 2);
}
@else {
$min-value: nth($breakpoint, 2);
$max-value: nth($breakpoint, 3);
}
}
@else {
$feature: nth($breakpoint, 3);
// See which is larger.
@if nth($breakpoint, 1) > nth($breakpoint, 2) {
$min-value: nth($breakpoint, 2);
$max-value: nth($breakpoint, 1);
}
@else {
$min-value: nth($breakpoint, 1);
$max-value: nth($breakpoint, 2);
}
}
// If EM Breakpoints are active, do it!
@if $breakpoint-to-ems and type-of($min-value) == 'number' {
$min-value: breakpoint-to-base-em($min-value, $base-font-size);
}
@if $breakpoint-to-ems and type-of($max-value) == 'number' {
$max-value: breakpoint-to-base-em($max-value, $base-font-size);
}
@if breakpoint-min-max($feature) == true {
@if $feature == 'device-pixel-ratio' {
$min-feature: breakpoint-experimental('min-#{$feature}', $prefix);
$max-feature: breakpoint-experimental('max-#{$feature}', $prefix);
}
@else {
$min-feature: 'min-#{$feature}';
$max-feature: 'max-#{$feature}';
}
// Min/Max for given
$query: breakpoint-generate($media-string, $min-feature, $min-value, $first);
$query: join($query, breakpoint-generate($media-string, $max-feature, $max-value));
// Set Context
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($min-feature, $min-value);
$context: U2V0IHlvdXIgb3duIGRhbW4gY29udGV4dHMh($max-feature, $max-value);
}
@else {
@warn '#{$feature} cannot have a min/max value!';
}
}
@return $query;
}
@function breakpoint-generate($media, $feature, $value, $first: false) {
// Media Query string to be returned
$new-string: "";
// If it's the first item, it gets special treatment
@if $first == true {
// And Statement
$and: 'and ';
// If $media is blank (i.e. all), remove and statement
@if $media == '' {
$and: '';
}
@if $feature != false {
$new-string: #{$media}unquote("#{$and}(#{$feature}: #{$value})");
}
@else {
$new-string: #{$media}unquote("#{$and}(#{$value})");
}
}
@else {
@if $feature != false {
$new-string: unquote("and (#{$feature}: #{$value})");
}
@else {
$new-string: unquote("and (#{$value})");
}
}
@return $new-string;
}