aboutsummaryrefslogtreecommitdiffstats
path: root/src/_responsive.scss
blob: 4d98638bd44f0186c69fe175507f86f15b1d6253 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
////
/// responsive properties.
///
/// The mixins and functions in this file allow you to scale any px- or rem-based value depending on
/// the available viewport width. One popular use case is the dynamic scaling of fonts.
///
/// The code in this file is based on an article by Niklas Postulart:
/// http://niklaspostulart.de/2015/10/sass-responsive-type-mixin
/// 
/// The following adjustments were made:
/// - Support any property passed by the user, not just font-size
/// - Allow multiple target viewports / values
/// - Provide a variant of the mixin which integrates include-media for media queries
///
/// @group Responsive
///
/// @access public
////

@use './functions';
@use './contexts';

///
/// Context ID used for responsive environment-related mixins.
///
/// @type string
///
$context-id: 'responsive' !default;

///
/// Context ID used for responsive environment-related mixins.
///
/// @type list
///
$named-viewports: () !default;

/// 
/// Scale a property uniformly between a specific set of target viewports / values.
///
/// @param {string | list} $props            - Property or list of properties to set
/// @param {number}        $responsive-map   - A map with keys = viewports and values = target value
/// @param {bool}          $fluid    [true]  - If enabled, property values will smoothly transition from one viewport to the next
/// @param {bool}          $vertical [false] - If enabled, property viewport height will be used instead of viewport width
///
/// @example scss - Responsive font-size between 2 viewports
///   .something {
///     @include property(font-size, ( 320px: 20px, 720px: 30px ));
///   }
///   
///   // Generates:
///   
///   @media (min-width: 320px) and (max-width: 720px) {
///     .something {
///       font-size: calc(20px + 10 * ((100vw - 20px) / 400));
///     }
///   }
///   
///   @media (max-width: 320px) {
///     .something {
///       font-size: 20px;
///     }
///   }
///   
///   @media (min-width: 720px) {
///     .something {
///       font-size: 30px;
///     }
///   }
///
/// @example scss - Responsive font-size between 3 viewports
///   .something {
///     @include property(font-size, ( 320px: 20px, 720px: 30px, 1280px: 40px ));
///   }
///   
///   // Generates:
///   
///   @media (min-width: 320px) and (max-width: 720px) {
///     .something {
///       font-size: calc(20px + 10 * ((100vw - 20px) / 400));
///     }
///   }
///   
///   @media (min-width: 720px) and (max-width: 1280px) {
///     .something {
///       font-size: calc(30px + 10 * ((100vw - 30px) / 400));
///     }
///   }
///   
///   @media (max-width: 320px) {
///     .something {
///       font-size: 20px;
///     }
///   }
///   
///   @media (min-width: 720px) {
///     .something {
///       font-size: 30px;
///     }
///   }
///
@mixin property($props, $responsive-map, $fluid: true, $vertical: false) {
    @include env(map-keys($responsive-map), $fluid, $vertical) {
        @if type-of($props) == list {
            @each $prop in $props {
                #{$prop}: set(map-values($responsive-map));
            }
        } @else {
            #{$props}: set(map-values($responsive-map));
        }
    }
}

/// 
/// Create a new responsive environment by specifying a set of viewports.
/// Inside a responsive environment, use the set function to make a property scale automatically.
///
/// @param {list} $viewports        - Viewports sorted in ascending order
/// @param {bool} $fluid [true]     - If enabled, property values will smoothly transition from one viewport to the next
/// @param {bool} $vertical [false] - If enabled, property viewport height will be used instead of viewport width
///
/// @content
///
/// @see {function} set
///
/// @example scss - Responsive font-size between 2 viewports
///   .something {
///     @include env((320px, 720px)) {
///       font-size: set(20px, 30px);
///     }
///   }
///   
///   // Generates:
///   
///   @media (min-width: 320px) and (max-width: 720px) {
///     .something {
///       font-size: calc(20px + 10 * ((100vw - 20px) / 400));
///     }
///   }
///   
///   @media (max-width: 320px) {
///     .something {
///       font-size: 20px;
///     }
///   }
///   
///   @media (min-width: 720px) {
///     .something {
///       font-size: 30px;
///     }
///   }
///
@mixin env($viewports, $fluid: true, $vertical: false) {
    @if length($viewports) <= 1 {
        @error '$viewports must contain at least two viewports.';
    }

    $new-viewports: ();

    @each $viewport in $viewports {
        @if map-has-key($named-viewports, $viewport) {
            $viewport: map-get($named-viewports, $viewport);
        }

        @if (type-of($viewport) != number) or unitless($viewport) {
            @error '$viewports contains invalid viewports.';
        }

        $new-viewports: append($new-viewports, $viewport);
    }

    $viewports: functions.quicksort($new-viewports);

    @if $new-viewports != $viewports {
        @error '$viewports was not sorted in ascending order.';
    }

    @if $fluid {
        $first-vp: nth($viewports, 1);
        $last-vp:  nth($viewports, length($viewports));

        @include contexts.push($context-id, 'env', (
            'viewports': $viewports,
            'mode':      set,
            'index':     1,
            'fluid':     $fluid,
            'vertical':  $vertical,
        ));

        @content;

        @include contexts.pop($context-id);

        @for $i from 1 to length($viewports) {
            $prev-vp: nth($viewports, $i);
            $next-vp: nth($viewports, $i + 1);

            @if not $vertical {
                @media (min-width: $prev-vp) and (max-width: $next-vp) {
                    @include contexts.push($context-id, 'env', (
                        'viewports': $viewports,
                        'mode':      transition,
                        'index':     $i,
                        'fluid':     $fluid,
                        'vertical':  $vertical,
                    ));

                    @content;

                    @include contexts.pop($context-id);
                }
            } @else {
                @media (min-height: $prev-vp) and (max-height: $next-vp) {
                    @include contexts.push($context-id, 'env', (
                        'viewports': $viewports,
                        'mode':      transition,
                        'index':     $i,
                        'fluid':     $fluid,
                        'vertical':  $vertical,
                    ));

                    @content;

                    @include contexts.pop($context-id);
                }
            }
        }

        @if not $vertical {
            @media (min-width: $last-vp) {
                @include contexts.push($context-id, 'env', (
                    'viewports': $viewports,
                    'mode':      set,
                    'index':     length($viewports),
                    'fluid':     $fluid,
                    'vertical':  $vertical,
                ));

                @content;

                @include contexts.pop($context-id);
            }
        } @else {
            @media (min-height: $last-vp) {
                @include contexts.push($context-id, 'env', (
                    'viewports': $viewports,
                    'mode':      set,
                    'index':     length($viewports),
                    'fluid':     $fluid,
                    'vertical':  $vertical,
                ));

                @content;

                @include contexts.pop($context-id);
            }
        }
    } @else {
        @include contexts.push($context-id, 'env', (
            'viewports': $viewports,
            'mode':      set,
            'index':     1,
            'fluid':     $fluid,
            'vertical':  $vertical,
        ));

        @content;

        @include contexts.pop($context-id);

        @for $i from 2 through length($viewports) {
            $vp: nth($viewports, $i);

            @if not $vertical {
                @media (min-width: $vp) {
                    @include contexts.push($context-id, 'env', (
                        'viewports': $viewports,
                        'mode':      set,
                        'index':     $i
                    ));

                    @content;

                    @include contexts.pop($context-id);
                }
            } @else {
                @media (min-height: $vp) {
                    @include contexts.push($context-id, 'env', (
                        'viewports': $viewports,
                        'mode':      set,
                        'index':     $i
                    ));

                    @content;

                    @include contexts.pop($context-id);
                }
            }
        }
    }
}

///
/// Make a property scale automatically inside a responsive environment.
///
/// @param {list} $values - Value for each viewport in the responsive environment. The first value will be used for the first viewport, the second value for the second viewport, and so on.
/// 
/// @return {number|string}
///
@function set($values, $without-calc: false) {
    $noop: contexts.assert-stack-must-contain($context-id, 'env');

    $data:      nth(contexts.get($context-id, 'env'), 2);
    $viewports: map-get($data, 'viewports');
    $mode:      map-get($data, 'mode');
    $fluid:     map-get($data, 'fluid');
    $vertical:  map-get($data, 'vertical');

    @if length($values) != length($viewports) {
        @error '$values must contain the same number of items as the responsive environment\'s $viewports.';
    }

    @if $mode == set {
        @return nth($values, map-get($data, 'index'));
    } @else {
        $index:      map-get($data, 'index');
        $prev-vp:    nth($viewports, $index);
        $next-vp:    nth($viewports, $index + 1);
        $prev-value: nth($values, $index);
        $next-value: nth($values, $index + 1);

        @return fluid-calc($prev-value, $next-value, $prev-vp, $next-vp, $vertical, $without-calc);
    }
}

/// 
/// Generate the calc() function that uniformly scales a value from $min-value to $max-value depending
/// on the viewport width.
///
/// @param {number} $min-value        - Minimum value
/// @param {number} $max-value        - Maximum value
/// @param {number} $min-viewport     - Minimum viewport size
/// @param {number} $max-viewport     - Maximum viewport size
/// @param {bool}   $vertical [false] - If enabled, property viewport height will be used instead of viewport width
///
/// @access private
///
@function fluid-calc($min-value, $max-value, $min-viewport, $max-viewport, $vertical: false, $without-calc: false) {
    $value-unit:        unit($min-value);
    $max-value-unit:    unit($max-value);
    $viewport-unit:     unit($min-viewport);
    $max-viewport-unit: unit($max-viewport);

    @if $min-value == 0 {
        $value-unit: $max-value-unit;
    }
    @if $max-value == 0 {
        $max-value-unit: $value-unit;
    }
    @if $min-viewport == 0 {
        $viewport-unit: $max-viewport-unit;
    }
    @if $max-viewport == 0 {
        $max-viewport-unit: $viewport-unit;
    }

    @if ($value-unit != $max-value-unit) or ($viewport-unit != $max-viewport-unit) {
        @error 'Units of $min-value and $max-value, $min-viewport and $max-viewport must match.';
    }

    @if ($value-unit == rem) and ($viewport-unit == px) {
        $min-viewport:  functions.px-to-rem($min-viewport);
        $max-viewport:  functions.px-to-rem($max-viewport);
        $viewport-unit: rem;
    } @else if ($value-unit == px) and ($viewport-unit == rem) {
        $min-value:  functions.px-to-rem($min-value);
        $max-value:  functions.px-to-rem($max-value);
        $value-unit: rem;
    }

    @if $value-unit != $viewport-unit {
        @error 'This combination of units is not supported.';
    }

    $value-diff:    functions.strip-unit($max-value - $min-value);
    $viewport-diff: functions.strip-unit($max-viewport - $min-viewport);

    $calc: '';

    @if $min-value != 0 {
        $calc: '#{$min-value} + ';
    }

    @if not $vertical {
        $calc: unquote('#{$calc}#{$value-diff} * (100vw - #{$min-viewport}) / #{$viewport-diff}');
    } @else {
        $calc: unquote('#{$calc}#{$value-diff} * (100vh - #{$min-viewport}) / #{$viewport-diff}');
    }

    @if $without-calc {
        @return $calc;
    } @else {
        @return calc(#{$calc});
    }
}

@include contexts.create($context-id);