Skip to content

Instantly share code, notes, and snippets.

@indieisaconcept
Created December 11, 2013 23:57
Show Gist options
  • Save indieisaconcept/7920840 to your computer and use it in GitHub Desktop.
Save indieisaconcept/7920840 to your computer and use it in GitHub Desktop.
Example of the wrong way of using variables with lists to display accent colors
<div class="news">
<div class="module-header">news</div>
</div>
<div class="sport">
<div class="module-header">sport</div>
</div>
<div class="business">
<div class="module-header">business</div>
</div>
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
// Pass this look up function two arrays of equal dimensions and a search value
// If nothing is found in the first pass of the properties the values
// will then be indexed
//
// $property: an array of property values
// $value: an array of values
// $search: the property (or value) to get a matching value
//
// returns: a matching value from the index
//
// eg:
//
// $myProperties: a, b, c;
// $myValues: 1, 2, 3;
// searchLookup($myProperties, $myValues, b) = 2
// searchLookup($myProperties, $myValues, 3) = c
//
@function searchLookup($property, $value, $search) {
$n1: index($property, $search);
$n2: index($value, $search);
@if($n1) {
@return nth($value, $n1);
} @else {
@return nth($property, $n2);
}
}
// VERTICAL BRANDING
$vertical-news-accent-color: blue !default;
$vertical-sport-accent-color: green !default;
$vertical-business-accent-color: grey !default; // Grey
// Create a hacky little lookup
$verticals: news,
sport,
business;
$vertical-accent: $vertical-news-accent-color,
$vertical-sport-accent-color,
$vertical-business-accent-color;
@each $vertical in $verticals {
.#{$vertical} .module-header {
background: searchLookup($verticals, $vertical-accent, $vertical);
}
}
.news .module-header {
background: blue;
}
.sport .module-header {
background: green;
}
.business .module-header {
background: grey;
}
<div class="news">
<div class="module-header">news</div>
</div>
<div class="sport">
<div class="module-header">sport</div>
</div>
<div class="business">
<div class="module-header">business</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment