UNPKG

4.48 kBSCSSView Raw
1// Breakpoint viewport sizes and media queries.
2//
3// Breakpoints are defined as a map of (name: minimum width), order from small to large:
4//
5// (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)
6//
7// The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.
8
9// Name of the next breakpoint, or null for the last breakpoint.
10//
11// >> breakpoint-next(sm)
12// md
13// >> breakpoint-next(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
14// md
15// >> breakpoint-next(sm, $breakpoint-names: (xs sm md lg xl))
16// md
17@function breakpoint-next($name, $breakpoints: $grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
18 $n: index($breakpoint-names, $name);
19 @if not $n {
20 @error "breakpoint `#{$name}` not found in `#{$breakpoints}`";
21 }
22 @return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
23}
24
25// Minimum breakpoint width. Null for the smallest (first) breakpoint.
26//
27// >> breakpoint-min(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
28// 576px
29@function breakpoint-min($name, $breakpoints: $grid-breakpoints) {
30 $min: map-get($breakpoints, $name);
31 @return if($min != 0, $min, null);
32}
33
34// Maximum breakpoint width.
35// The maximum value is reduced by 0.02px to work around the limitations of
36// `min-` and `max-` prefixes and viewports with fractional widths.
37// See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
38// Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
39// See https://bugs.webkit.org/show_bug.cgi?id=178261
40//
41// >> breakpoint-max(md, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
42// 767.98px
43@function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
44 $max: map-get($breakpoints, $name);
45 @return if($max and $max > 0, $max - .02, null);
46}
47
48// Returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front.
49// Useful for making responsive utilities.
50//
51// >> breakpoint-infix(xs, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
52// "" (Returns a blank string)
53// >> breakpoint-infix(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
54// "-sm"
55@function breakpoint-infix($name, $breakpoints: $grid-breakpoints) {
56 @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
57}
58
59// Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
60// Makes the @content apply to the given breakpoint and wider.
61@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
62 $min: breakpoint-min($name, $breakpoints);
63 @if $min {
64 @media (min-width: $min) {
65 @content;
66 }
67 } @else {
68 @content;
69 }
70}
71
72// Media of at most the maximum breakpoint width. No query for the largest breakpoint.
73// Makes the @content apply to the given breakpoint and narrower.
74@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
75 $max: breakpoint-max($name, $breakpoints);
76 @if $max {
77 @media (max-width: $max) {
78 @content;
79 }
80 } @else {
81 @content;
82 }
83}
84
85// Media that spans multiple breakpoint widths.
86// Makes the @content apply between the min and max breakpoints
87@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
88 $min: breakpoint-min($lower, $breakpoints);
89 $max: breakpoint-max($upper, $breakpoints);
90
91 @if $min != null and $max != null {
92 @media (min-width: $min) and (max-width: $max) {
93 @content;
94 }
95 } @else if $max == null {
96 @include media-breakpoint-up($lower, $breakpoints) {
97 @content;
98 }
99 } @else if $min == null {
100 @include media-breakpoint-down($upper, $breakpoints) {
101 @content;
102 }
103 }
104}
105
106// Media between the breakpoint's minimum and maximum widths.
107// No minimum for the smallest breakpoint, and no maximum for the largest one.
108// Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
109@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
110 $min: breakpoint-min($name, $breakpoints);
111 $next: breakpoint-next($name, $breakpoints);
112 $max: breakpoint-max($next);
113
114 @if $min != null and $max != null {
115 @media (min-width: $min) and (max-width: $max) {
116 @content;
117 }
118 } @else if $max == null {
119 @include media-breakpoint-up($name, $breakpoints) {
120 @content;
121 }
122 } @else if $min == null {
123 @include media-breakpoint-down($next, $breakpoints) {
124 @content;
125 }
126 }
127}