|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +// |
| 7 | +// BREAKPOINTS |
| 8 | +// |
| 9 | + |
| 10 | +@use "sass:list"; |
| 11 | +@use "sass:map"; |
| 12 | +@use "sass:meta"; |
| 13 | + |
| 14 | +// breakpoints |
| 15 | + |
| 16 | +$hds-breakpoints: ( |
| 17 | + sm: 480px, |
| 18 | + md: 768px, |
| 19 | + lg: 1088px, |
| 20 | + xl: 1440px, |
| 21 | + xxl: 1920px, |
| 22 | +) !default; |
| 23 | +$hds-breakpoints-names: map.keys($hds-breakpoints); |
| 24 | + |
| 25 | +// functions |
| 26 | + |
| 27 | +@function hds-get-breakpoint-next($name) { |
| 28 | + $n: list.index($hds-breakpoints-names, $name); |
| 29 | + |
| 30 | + @if $n != null and $n < list.length($hds-breakpoints-names) { |
| 31 | + @return list.nth($hds-breakpoints-names, $n + 1); |
| 32 | + } |
| 33 | + |
| 34 | + @return null; |
| 35 | +} |
| 36 | + |
| 37 | +@function hds-get-breakpoint-prev($name) { |
| 38 | + $n: list.index($hds-breakpoints-names, $name); |
| 39 | + |
| 40 | + @if $n != null and $n > 1 { |
| 41 | + @return list.nth($hds-breakpoints-names, $n - 1); |
| 42 | + } |
| 43 | + |
| 44 | + @return null; |
| 45 | +} |
| 46 | + |
| 47 | +// mixins |
| 48 | + |
| 49 | +@mixin hds-breakpoint-above($name) { |
| 50 | + @if map.has-key($hds-breakpoints, $name) { |
| 51 | + $width: map.get($hds-breakpoints, $name); |
| 52 | + |
| 53 | + @media screen and (min-width: $width) { |
| 54 | + @content; |
| 55 | + } |
| 56 | + } @else { |
| 57 | + @error 'Unable to find a breakpoint with name `#{$name}`. Expected one of: (#{$hds-breakpoints-names})'; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +@mixin hds-breakpoint-below($name) { |
| 62 | + @if map.has-key($hds-breakpoints, $name) { |
| 63 | + // We borrow this logic from bootstrap for specifying the value of the |
| 64 | + // max-width. The maximum width is calculated by finding the breakpoint and |
| 65 | + // subtracting .02 from its value. This value is used instead of .01 to |
| 66 | + // avoid rounding issues in Safari |
| 67 | + // https://github.com/twbs/bootstrap/blob/c5b1919deaf5393fcca9e9b9d7ce9c338160d99d/scss/mixins/_breakpoints.scss#L34-L46 |
| 68 | + $width: map.get($hds-breakpoints, $name) - 0.02; |
| 69 | + |
| 70 | + @media screen and (max-width: $width) { |
| 71 | + @content; |
| 72 | + } |
| 73 | + } @else { |
| 74 | + @error 'Unable to find a breakpoint with name `#{$name}`. Expected one of: (#{$hds-breakpoints})'; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +@mixin hds-breakpoint-between($lower, $upper) { |
| 79 | + $min: map.get($hds-breakpoints, $lower); |
| 80 | + $max: map.get($hds-breakpoints, $upper); |
| 81 | + |
| 82 | + @if $min and $max { |
| 83 | + @media screen and (min-width: $min) and (max-width: $max) { |
| 84 | + @content; |
| 85 | + } |
| 86 | + } @else if $min != null and $max == null { |
| 87 | + @include hds-breakpoint-above($lower) { |
| 88 | + @content; |
| 89 | + } |
| 90 | + } @else if $min == null and $max != null { |
| 91 | + @include hds-breakpoint-below($upper) { |
| 92 | + @content; |
| 93 | + } |
| 94 | + } @else { |
| 95 | + @error 'Unable to find a breakpoint to satisfy: (#{$lower},#{$upper}). Expected both to be one of (#{$hds-breakpoints-names}).'; |
| 96 | + } |
| 97 | +} |
0 commit comments