Last active
February 16, 2016 21:45
-
-
Save dalgard/fcb3fa60ed10ef1e9a27 to your computer and use it in GitHub Desktop.
Sass mixin to help put 'the checkbox hack' (which I rather consider a 'technique') to good use. This version requires a media mixin, e.g. from Neat
This file contains 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
@mixin toggled($selector, $media...) { | |
#{unquote($selector)}:checked ~ & { | |
@if length($media) > 0 { | |
@include media($media...) { | |
@content; | |
} | |
} | |
@else { | |
@content; | |
} | |
} | |
} | |
@mixin untoggled($selector, $media...) { | |
#{unquote($selector)}:not(:checked) ~ & { | |
@if length($media) > 0 { | |
@include media($media...) { | |
@content; | |
} | |
} | |
@else { | |
@content; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With a few other nice techniques, the mixin can be used like this:
When the element is nested, like in the HTML above, the
@include toggled
statement has to go inside the full path of the element in relation to the actual checkbox – like above – since the compiled selector has to look like this to work:Of course, the toggle may be something less silly/more useful – such as this:
... which would output something like this: