123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Theming UI Components</h1>

<h2>Using BEM naming</h2>
<button class="btn btn--primary">Primary Button</button>
<hr class="rule rule-t-info"/>
<h2>Introducing First Class Theme Support</h2>
<p>Each component needs to be aware that it may be used within a colour scheme so it needs to own the process of creating the various classes required to support different uses. Sometimes semantic classes are the way forward, but perhaps we should just allow the use of names to describe the particular style (think Solarized & Darcula, not Primary, Secondary, Success, etc.)</p>

<blockquote class="note note-t-info">Names such as Primary, Secondary, etc. could and should be used to set other theme properties, such as size or font</blockquote>

<p>Here we create a themes map, which contains the colour definitions for a variety of named themes. This makes it easier to re-use a colour scheme across components and removes the cases where two component madifier classes (with different names) confusingly implement the same colour scheme. e.g. no <code>.btn--primary</code> and <code>.panel--brand</code> which actually use the same colours</p>

<p>How we use this in practise:</p>

<button class="btn btn-t-sassmeister">I'm a button</button>
<button class="btn btn-t-codepen">I'm a button</button>
<button class="btn btn-t-codepen-light">I'm a button</button>
<button class="btn btn-t-info">I'm a button</button>

<p>Read more about this approach in my blog post - <a href="http://www.ian-thomas.net/a-sassy-approach-to-theming-components/" class="link-t-sassmeister-dark">A Sassy Approach To Theming Components</a></p>
            
          
!
            
              $color-primary: #fff;
$color-primary-background: #E86EA4;

.btn {
  display: block;
  width: 100%;
  padding: 0.8em 1em;
  margin-bottom: 0.8em;
  font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial;
  font-size: 1.2em;
  border-width: 1px;
  border-style: solid;
  
  &:after {
    content: attr(class);
    margin-left: 6px;
    font-style: italic;
    opacity: 0.5;
  }
}

.btn--primary {
  //arguably, all the styles in here are theme related
  color: $color-primary;
  background-color: $color-primary-background;
  border: 1px solid darken($color-primary-background, 10);
  
  &:hover {
    color: $color-primary;
    background-color: lighten($color-primary-background, 10);
  }
}

/**
Themes are all stored in a map data-structure which gives flexibilty
when traversing based on keys and also looking up values based on strings
*/

$themes: (
  sassmeister: (
    foreground: #fff,
    background: #E86EA4,
    border: darken(#E86EA4, 10),
    emphasis: darken(#FFFFFF, 10),
    subtle: lighten(#E86EA4, 10),
    highlight: lighten(#E86EA4, 10),
    radius: 0
   ),
  sassmeister-dark: (
    foreground: #E86EA4,
    background: #fff,
    border: darken(#fff, 10),
    emphasis: darken(#E86EA4, 10),
    subtle: lighten(#E86EA4, 10),
    highlight: darken(#fff, 10),
    radius: 0
   ),
  codepen: (
    foreground: #CCCCCC,
    background: #1D1F20,
    border: #000000,
    emphasis: darken(#999999, 10),
    subtle: lighten(#999999, 10),
    highlight: lighten(#1D1F20, 10),
    radius: 3px
   ),
  codepen-light: (
    foreground: #1D1F20,
    background: #F2F2F2,
    border: darken(#F2F2F2, 15),
    emphasis: lighten(#1D1F20, 10),
    subtle: darken(#1D1F20, 10),
    highlight: darken(#F2F2F2, 10),
    radius: 3px
   ),
  info: (
    foreground: #FFFFFF,
    background: #809BBD,
    border: darken(#809BBD, 15),
    subtle: darken(#FFFFFF, 10),
    highlight: lighten(#809BBD, 10),
    radius: 6px,
    font-style: italic
  )
);

@function get-theme($name) {
  @return map-get($themes, $name);
}

/**
 * This function should warn if a theme requirement isn't met, 
 * helping to catch any issues at compile time in development
 */
@function requires($themeName, $props) {
  $theme: get-theme($themeName);
  @each $key in $props {
    @if (map-has-key($theme, $key) == false) {
      @warn "To use this theme requires #{$key} to be set in the map";
      @return false;
    } 
  }
  @return true;
}

.note {
  padding: 2em;
  
  font-size: 1.2rem;
  font-weight: 300;

  margin: 2em 0 2em 4em;
  width: 50%;
}

.note--small {
  font-size: 0.8rem;
  padding: 0.5em 1em;
  margin: 1em 0;
}

.note--inline {
  display: inline-block;
}

.rule {
  border: 0;
  margin: 2em 0;
  width: 100%;
  height: 1px;
 
  background-color: #ccc;
}

a{
  font-weight: bold;
}

@mixin link-theme($name) {
  $theme: get-theme($name);
  
  .link-t-#{$name} {
    color: map-get($theme, foreground);
    
    &:hover, &:active {
      color: map-get($theme, emphasis);
    }
  }
}

$link-requirements: foreground emphasis;

@mixin button-theme($name) {
  $theme: map-get($themes, $name);
  
  .btn-t-#{$name} {
    color: map-get($theme, "foreground");
    background-color: map-get($theme, "background");
    border-color: map-get($theme, "border");
    border-radius: map-get($theme, "radius");
    
    &:hover {
      background-color: map-get($theme, "highlight");
    }
  }
}

$button-requirements: foreground background border radius highlight;

@mixin note-theme($name) {
  $theme: map-get($themes, $name);
  
  .note-t-#{$name} {
    background: map-get($theme, "background");
    color: map-get($theme, "foreground");
    @if map-has-key($theme, "font-style") {
      font-style: map-get($theme, "font-style");
    }
  }  
}

$note-requirements: background foreground; // font-style is optional

@mixin rule-theme($name) {
  $theme: map-get($themes, $name);
  
  .rule-t-#{$name}{
    background-color: map-get($theme, "background");    
  }
}

$rule-requirements: background;

@each $themeName in map-keys($themes) {
  @if (requires($themeName, $button-requirements)) {
      @include button-theme($themeName);      
  }

  @if (requires($themeName, $note-requirements)) {
      @include note-theme($themeName);    
  }
  
  @if (requires($themeName, $rule-requirements)) {
    @include rule-theme($themeName);  
  }
  
  @if (requires($themeName, $link-requirements)) {
    @include link-theme($themeName);  
  }
}









// Boilerplate code to get the pen setup

body {
  line-height: 1.5;
  padding: 2% 10%;
  color: #5B5B5B;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console