Last active
January 28, 2019 14:28
-
-
Save rogerioyokoi/bb901a84ccd37acb3a8be00c0dafaca2 to your computer and use it in GitHub Desktop.
Contrast Mixin to helper implement accessibility colors.
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
// ---- | |
// libsass (v3.5.4) | |
// ---- | |
$contrasts: 'base', 'contrast-white', 'contrast-black'; | |
$colors: ( | |
'primary': ( | |
'base': red, | |
'contrast-white': white, | |
'contrast-black': black | |
), | |
'secondary': ( | |
'base': green, | |
'contrast-white': purple, | |
'contrast-black': gray | |
) | |
); | |
@function get-color($color, $contrast: 'base') { | |
$setColor: map-get(map-get($colors, $color),$contrast); | |
@if $setColor { | |
@return $setColor; | |
} @else { | |
// Libsass still doesn't seem to support @error | |
@warn "=> ERROR: COLOR NOT FOUND! <= | Your $color-name, $contrast combination did not match any of the values in the $colors map."; | |
} | |
}; | |
@mixin set-contrast($color-name, $opacity: 1) { | |
@for $i from 1 through length($contrasts){ | |
$contrast: nth($contrasts, $i); | |
$color: rgba(get-color($color-name, $contrast), $opacity) !global; | |
@if($contrast == 'base'){ | |
@content; | |
} @else { | |
.#{$contrast} & { | |
@content; | |
} | |
} | |
} | |
} | |
.app-header{ | |
display: flex; | |
color: get-color('primary'); | |
@include set-contrast('secondary'){ | |
border: 1px solid $color; | |
} | |
@include set-contrast('primary'){ | |
background: $color; | |
} | |
label { | |
@include set-contrast('primary'){ | |
background: $color; | |
} | |
} | |
color: rgba(#007fad, .5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment