Skip to content

Instantly share code, notes, and snippets.

@rogerioyokoi
Last active January 28, 2019 14:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerioyokoi/bb901a84ccd37acb3a8be00c0dafaca2 to your computer and use it in GitHub Desktop.
Save rogerioyokoi/bb901a84ccd37acb3a8be00c0dafaca2 to your computer and use it in GitHub Desktop.
Contrast Mixin to helper implement accessibility colors.
// ----
// 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