Skip to content

Instantly share code, notes, and snippets.

@kaplas
Created September 3, 2013 14:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaplas/6424861 to your computer and use it in GitHub Desktop.
Save kaplas/6424861 to your computer and use it in GitHub Desktop.
SASS + two different color themes + easy and clean to use + clean CSS output = pure awesomeness!
// Defining colors ===========================================
$color-names : '';
$light-colors : '';
$dark-colors : '';
@mixin define-theme-color( $color-name, $light-color, $dark-color ) {
@if ($color-names != "") and (index($color-names, $color-name) != false) {
@warn "Theme color $color-name is already defined";
} @else {
$color-names: append($color-names, $color-name, comma);
$light-colors: append($light-colors, $light-color, comma);
$dark-colors: append($dark-colors, $dark-color, comma);
}
}
// Outer mixin, in which the content is duplicated with correct
// theme colors ===============================================
$current-color-schema: '';
@mixin theme-colors() {
$current-color-schema: 'light';
.light-theme & {
@content;
}
$current-color-schema: 'dark';
.dark-theme & {
@content;
}
}
// Function for retrieving correct color. Remember to use this
// inside the mixin defined above.
// FYI: This is actually just a lookup function ==============
@function theme-color( $color-name ){
@if ($color-names == "") or (index($color-names, $color-name)) == false {
@warn "Theme color \"" + $color-name + "\" is not defined";
@return "";
} @else {
$color-index: index($color-names, $color-name);
@if ($current-color-schema == "light") {
@return nth( $light-colors, $color-index);
} @else {
@return nth( $dark-colors, $color-index);
}
}
}
@import "color-mixins";
// name light-theme color dark-theme color
@include define-theme-color("component-background", white, black );
@include define-theme-color("component-text", black, green );
<html>
<head>
<link rel="stylesheet" href="main.css">
</head>
<body class="light-theme">
<!-- or class="dark-theme"; use your JavaScript skills here, young padawan -->
<div class="some-component">
I'd like to change my colors easily.
</div>
</body>
</html>
@import "colors";
body {
background-color: silver;
font-size: 2em;
text-align: center;
}
.some-component {
width: 80%;
height: 80%;
margin-left: 10%;
border: 1px dashed silver;
@include theme-colors {
background-color: theme-color("component-background");
color: theme-color("component-text");
}
}
@umersharif1989
Copy link

I am compiling your scss files but this is showing in css file
.theme-light .some-component {
background-color: "";
color: ""; }
.dark-theme .some-component {
background-color: "";
color: ""; }

@jaexplorer
Copy link

Same as above, this code is logically not correct

@syedsimanta03
Copy link

null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment