-
-
Save disco0/04bcdca15cf1b3dc3d6b36313432e08c to your computer and use it in GitHub Desktop.
Stylus @extend inside a media query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extend in a media query | |
// ============================================================================= | |
// Mixins | |
// ----------------------------------------------------------------------------- | |
clearfix() | |
&:before, | |
&:after | |
content: ' ' | |
display: tablet | |
&:after | |
clear: both | |
hide() | |
position: absolute | |
left: -9999em | |
text-align: left | |
direction: ltr | |
// Generate placeholders | |
// ----------------------------------------------------------------------------- | |
placeholders = ('clearfix' 'hide') | |
for placeholderName in placeholders | |
${placeholderName} | |
fn = lookup(placeholderName) | |
fn() | |
// Generate breakpoints variables | |
// ----------------------------------------------------------------------------- | |
breakpoints = { | |
smalltablet: 501px, | |
tablet: 769px, | |
desktop: 1025px | |
} | |
for breakpointName, breakpointValue in breakpoints | |
breakpointName = '$' + breakpointName | |
// define breakpoint variable | |
define(breakpointName, s('(min-width: %s)', breakpointValue)) | |
// create breakpoint placeholders | |
@media breakpointName | |
for placeholderName in placeholders | |
{breakpointName}_{placeholderName} | |
fn = lookup(placeholderName) | |
fn() | |
// Dynamic extend | |
// ----------------------------------------------------------------------------- | |
_extend(name) | |
// check if in media | |
_currentMedia = current-media() | |
if _currentMedia | |
_breakpoint = replace('@media \((.*)\)', '$1', _currentMedia) | |
if lookup(_breakpoint) | |
@extend {_breakpoint + '_' + name} | |
else | |
warn(_breakpoint + ' does not exist') | |
else | |
@extend {'$' + name} | |
// Demo | |
// ----------------------------------------------------------------------------- | |
.element-a | |
background: #000 | |
_extend('clearfix') | |
.element-b | |
background: #FFF | |
@media $smalltablet | |
_extend('clearfix') | |
opacity: .8 | |
.element-c | |
background: #666 | |
@media $smalltablet | |
_extend('clearfix') | |
opacity: .8 | |
@media $tablet | |
_extend('hide') | |
opacity: .8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment