-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_fluid.scss
80 lines (67 loc) · 2.32 KB
/
_fluid.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@use "sass:math";
@function strip-unit($num) {
@return math.div($num, $num * 0 + 1);
}
@function map-get-deep($map, $keys...) {
@each $key in $keys {
$map: map - get($map, $key);
}
@return $map;
}
@mixin fluid($type, $minValues, $maxValues, $direction: 'horizontal', $breakpointMin: 640px, $breakpointMax: 1500px) {
$valueLength: 0;
$_minValue: null;
$_maxValue: null;
$_fluidValue: null;
$_direction: null;
$_breakpointType: null;
// check direction
@if $direction == 'vertical' {
$_direction: 'vh';
$_breakpointType: 'height';
}
@else if $direction == 'horizontal' {
$_direction: 'vw';
$_breakpointType: 'width';
}
@else {
@error 'Unknown direction only vertical and horizontal are supported';
}
// check if value length match
@if length($minValues) != length($maxValues) {
@error 'Min value length `#{$minValues}`(#{length($minValues)}) does not match with max value length `#{$maxValues}`(#{length($maxValues)})';
}
@else {
$valueLength: length($maxValues);
}
@for $i from 1 through $valueLength {
$minValue: nth($minValues, $i);
$maxValue: nth($maxValues, $i);
$formula: $minValue;
/*
check if we need to do some calculations, we don't need a calculation if the min and max values are the same.
*/
@if $minValue != $maxValue {
// because we can not divide 0 set the zero's to 1px.
@if $minValue == 0 {
$minValue: 1px;
}
@if $maxValue == 0 {
$maxValue: 1px;
}
$formula: calc(#{$minValue} + #{strip-unit($maxValue - $minValue)} * ((100#{$_direction} - #{$breakpointMin}) / #{strip-unit($breakpointMax - $breakpointMin)}));
}
$_fluidValue: #{$_fluidValue} #{$formula};
$_minValue: #{$_minValue} #{$minValue};
$_maxValue: #{$_maxValue} #{$maxValue};
}
// when we are between the breakpoint render a calculation value
#{$type}: #{$_fluidValue};
//otherwise set fixed values
@media screen and (max-#{$_breakpointType}: $breakpointMin) {
#{$type}: #{$_minValue};
}
@media screen and (min-#{$_breakpointType}: $breakpointMax) {
#{$type}: #{$_maxValue};
}
}