Skip to content

Commit 8f07f53

Browse files
committed
Media mixin: support arbitrary units
1 parent a8db49b commit 8f07f53

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/styles/mixins/media.sass

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
+media(123M)
2020
// @media (max-width: 456px)
2121
22+
+media(123em 456Mem)
23+
// @media (min-width: 123em) and (max-width: 456em)
24+
2225
+media($break480 '(screen)', 300 500M, $landscape)
2326
// @media (min-width: 480px) and (screen), (min-width: 300px) and (max-width: 500px), (orientation: landscape)
2427
2528
2629
=media($breakpoints...)
30+
$MAX: 'M'
2731
$media: ''
2832
$disjunctionCount: length($breakpoints)
2933

@@ -35,7 +39,18 @@
3539
$literal: nth($conjunction, $j)
3640

3741
@if type-of($literal) == number
38-
$literal: '(#{if(unit($literal) == 'M', 'max', 'min')}-width: #{$literal / ($literal * 0 + 1)}px)'
42+
$literalUnit: unit($literal)
43+
$maxMode: str-slice($literalUnit, 0, str-length($MAX)) == $MAX
44+
$extremum: 'min'
45+
46+
@if $maxMode
47+
$extremum: 'max'
48+
$literalUnit: str-slice($literalUnit, str-length($MAX) + 1)
49+
50+
@if $literalUnit == ''
51+
$literalUnit: 'px'
52+
53+
$literal: '(#{$extremum}-width: #{$literal / ($literal * 0 + 1)}#{$literalUnit})'
3954

4055
$media: $media + $literal + if($j == $conjunctionCount, '', ' and ')
4156

0 commit comments

Comments
 (0)