Skip to content

Instantly share code, notes, and snippets.

@trub
Created April 7, 2014 23:08
Show Gist options
  • Save trub/10071884 to your computer and use it in GitHub Desktop.
Save trub/10071884 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.

by Dale Sande

Icon-fonts. They are pretty awesome, but much like managing Sprite files, there are issues that make them a real pain to manage. Sure there are full libraries out there that you can grab wholesale and rely on their documentation, but for optimization and performance reasons you do not want to load up a series of libraries just to use a few icons. Leveraging the power of HAML and Sass we can make this less painful and at the same time maintain a living style guide. Basically, winning all over the place.

IcoMoon is a fantastic resource that allows users to select specific icons from various libraries, as well as upload custom SVG art and download a customized font library. In the download package there is an HTML document that illustrates the library you just created, but for most professional applications this isn't going to work. Mainly because you will probably not use IcoMoon's code verbatim. Customizing the HTML and CSS per your use is very common.

This leaves us with the task of maintaining our own ico-font documentation. Being a strong believer in style guide driven design, my process of developing UIs that self document is essential. Especially when dealing with things like sprites or icon-fonts, good visual documentation is vital. If the developers on the team cannot quickly see what icons are available, then the feature is pretty useless.

The challenge: when applying the style guide driven method, you need to make your code reusable, scaleable and easy to edit. The bourdon of writing code simply to display in the style guide is typically a deal breaker with most developers. Using some HAML and Sass magic, we can make this happen.

Ico-font library HTML display in the style guide

Starting with the HTML to display the library, we would manually write something like the following example taking inspiration from IcoMoon's supplied documentation.

<article class="styleguide_example">
  <span aria-hidden="true" class="icon-strategy"></span>
  <span aria-hidden="true" class="icon-strategy_01"></span>
  <span aria-hidden="true" class="icon-strategy_02"></span>
  <span aria-hidden="true" class="icon-dev_01"></span>
  <span aria-hidden="true" class="icon-dev_02"></span>
</article>

This seems like a pretty simple list and something that is easy to manage, but there is a lot of repeated code in there and we as developers HATE repeated code. We have the technology to make this better.

Looking at the previous example, just about all the code is duplicated, except for the class names. icon-strategy, icon-strategy_01 and icon-strategy_02 for example. Let's take this and using a simple feature in HAML we can write code that will loop through a list of these names and then write out the HTML we need for the browser. In the following example, we will take those ico-font class names and place them in an array.

- icons = 'icon-strategy', 'icon-strategy_01', 'icon-strategy_02', 'icon-dev_01', 'icon-dev_02'

Using the .each looping function we can write a small chunk of code that will leverage the repeated portions of the HTML and take all this manual updating away from us.

- icons.each do |icon|
    %span{"aria-hidden" => "true", :class => icon}

What we end up with is three lines of HAML versus x# of HTML. Much easer to manage and infinitely scaleable. New icons? No problem, add the class name to the array and you are done.

Ico-font library CSS display in the style guide

Now that we made the markup for the style guide easy to manage, what about the CSS? In the case of ico-fonts, much like sprites, there are multiple lines of CSS that need to be written. Doing this manually, you would write something like the following example.

.icon-strategy_01:after {
  content: "\e002";
}
.icon-dev_03:after {
  content: "\e003";
}
.icon-dev_02:after {
  content: "\e004";
}
.icon-dev_01:after {
  content: "\e005";
}
.icon-design_03:after {
  content: "\e006";
}
.icon-design_02:after {
  content: "\e007";
}
.icon-design_01:after {
  content: "\e008";
}

Once again, to a developer this is extremely painful to look at and the concept of copying and pasting is simply evil. Here is where Sass comes to the rescue. With Sass we can apply a process much like we did with HAML where we can iterate on a list of variables that are feed through a loop of code that in turn processes this manual madness.

Using the previous example, let's pull out the variable and take advantage of the redundant code. In the following example we can replace the icon names and the PUA values with variables.

.$ICO-VAR:after {
  content: "$PUA-VAR";
}

Now that we have the variable pattern, we will apply the values to a list and apply them to the variable $icons.

$icons: icon-strategy icon-strategy_02 icon-strategy_01 icon-dev_03 icon-dev_02 icon-dev_01;

To put this list to use, we will need the @each rule in Sass to loop through the list. The @each rule takes two arguments, one for the variable and the other for the SassScript expression. The @each rule sets the variable to each item in the specified list as it loops through, then outputs the styles it contains using the value of variable.

In the following example we will create $icon as the variable and $icons as the list expression. The iterated value from $icons will redefine the variable of $icon. The value of $icon is then passed into the CSS rule w/pseudo class .#{$icon}:after. The use of interpolation is required when using SassScript variables in selectors.

@each $icon in $icons {
  .#{$icon}:after {  <--
    foo:bar;
  }
}

First part completed. We have code that will iterate through a list and produce lines of CSS with each name as the class. But the resulting code will be a ton of css classes all with the same declaration of foo:bar. Not very useful.

To make the second part of our code, for the declaration of content: "$VAR";, we need to add the values of $VAR to our list. Since there is a relationship between the name of the class and the PUA content, it is preferred to keep these values together.

Lists have multiple ways of delineating and concatenating values. You can use spaces, commas , and you can even delineate each value between parenthesis (). What's important to note is that by using a combination of these delineators we can then group values in our list.

In our example we have icon-strategy which has a PUA of \e000. Then we have icon-strategy_02 with a PUA of \e001. The following example illustrates how we can use a combination of parenthesis () and spaces to create our combined list of values. By placing icon-strategy and \e000 within parenthesis () we are creating a concatenated value. And by simply placing spaces between each concatenated value, we can add more values to the variable list of $icons. Also note that we are putting the PUA value in single quotes '' because we want our returned value to be in quotes content: "\e000";.

$icons: (icon-strategy '\e000') (icon-strategy_02 '\e002') (icon-strategy_02 '\e001');

Now that we have our list, let's put it to some good use. Here we introduce Sass' concept of the nth function. Simply put, you can use this feature to tell Sass which item within a list to use nth($LIST, val).

In the following example you will see that we updated .#{$icon}:after to use the nth function, .#{nth($icon, 1)}:after. We still require that the returned value is interpolated using the #{} function, but the returned value will now step through the list and take the first value of each concatenated set of values.

@each $icon in $icons {
  .#{nth($icon, 1)}:after {  <--
    foo:bar;
  }
}

For the next part, content: "$VAR";, we need to step through our list again, except this time getting the second value. We will make use of the nth function again, but interpolation is not needed as we want the quoted value to be returned.

@each $icon in $icons {
  .#{nth($icon, 1)}:after {
    content: nth($icon, 2);  <-- 
  }
}

And there you have it. When the time comes and we need to add/delete icons from our ico-font library the hardest part is going to IcoMoon.

One more thing

For our ico-fonts to display correctly we need to add a few more declarations to our CSS. For one, we need the font-family. We don't want to add this to our @each $icon in $icons rule as this will duplicate these declarations each time it loops through the list.

Our solution is pretty simple. The following example illustrates how we can create the ico-font-base CSS rule as a placeholder selector.

 %ico-font-base {
    font-family: 'ico-fonts';
    speak: none;
    font-style: normal;
    font-weight: normal;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
  }

Then we can extend that into the previous @each $icon in $icons as illustrated in the following example.

@each $icon in $icons {
  .#{nth($icon, 1)}:after {
    content: nth($icon, 2);
    @extend %ico-font-base;  <--
  }
}

When the Sass is processed into CSS, this rule will loop through the list as expected and also crate a concatenated list of selectors all using this rule.

SassMeister living gist

Seeing code is one thing, playing with code is another. Check out the SassMeister Gist and happy coding.

List of rants

If you like this post, check out more of my rants

// ----
// Sass (v3.3.4)
// Compass (v1.0.0.alpha.18)
// ----
$icons: (strategy '\e000') (strategy_02 '\e001') (strategy_01 '\e002');
%ico-font-base {
font-family: 'substantial-ico-fonts';
speak: none;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
}
@each $icon in $icons {
.icon-#{nth($icon, 1)}:after {
content: nth($icon, 2);
@extend %ico-font-base;
}
}
.icon-strategy:after, .icon-strategy_02:after, .icon-strategy_01:after {
font-family: 'substantial-ico-fonts';
speak: none;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
}
.icon-strategy:after {
content: "\e000";
}
.icon-strategy_02:after {
content: "\e001";
}
.icon-strategy_01:after {
content: "\e002";
}
<p>by <a href="https://twitter.com/anotheruiguy">Dale Sande</a></p>
<p><strong><em>Icon-fonts. They are pretty awesome, but much like managing Sprite files, there are issues that make them a real pain to manage. Sure there are full libraries out there that you can grab wholesale and rely on their documentation, but for optimization and performance reasons you do not want to load up a series of libraries just to use a few icons. Leveraging the power of HAML and Sass we can make this less painful and at the same time maintain a living style guide. Basically, winning all over the place.</em></strong></p>
<p><a href="http://icomoon.io/" title="ico-font management resource">IcoMoon</a> is a fantastic resource that allows users to select specific icons from various libraries, as well as upload custom SVG art and download a customized font library. In the download package there is an HTML document that illustrates the library you just created, but for most professional applications this isn&#39;t going to work. Mainly because you will probably not use IcoMoon&#39;s code verbatim. Customizing the HTML and CSS per your use is very common. </p>
<p>This leaves us with the task of maintaining our own ico-font documentation. Being a strong believer in style guide driven design, my process of developing UIs that self document is essential. Especially when dealing with things like sprites or icon-fonts, good visual documentation is vital. If the developers on the team cannot quickly see what icons are available, then the feature is pretty useless. </p>
<p>The challenge: when applying the style guide driven method, you need to make your code reusable, scaleable and easy to edit. The bourdon of writing code simply to display in the style guide is typically a deal breaker with most developers. Using some HAML and Sass magic, we can make this happen. </p>
<h2>Ico-font library HTML display in the style guide</h2>
<p>Starting with the HTML to display the library, we would manually write something like the following example taking inspiration from IcoMoon&#39;s supplied documentation. </p>
<pre><code>&lt;article class=&quot;styleguide_example&quot;&gt;
&lt;span aria-hidden=&quot;true&quot; class=&quot;icon-strategy&quot;&gt;&lt;/span&gt;
&lt;span aria-hidden=&quot;true&quot; class=&quot;icon-strategy_01&quot;&gt;&lt;/span&gt;
&lt;span aria-hidden=&quot;true&quot; class=&quot;icon-strategy_02&quot;&gt;&lt;/span&gt;
&lt;span aria-hidden=&quot;true&quot; class=&quot;icon-dev_01&quot;&gt;&lt;/span&gt;
&lt;span aria-hidden=&quot;true&quot; class=&quot;icon-dev_02&quot;&gt;&lt;/span&gt;
&lt;/article&gt;
</code></pre>
<p>This seems like a pretty simple list and something that is easy to manage, but there is a lot of repeated code in there and we as developers <strong>HATE</strong> repeated code. We have the technology to make this better.</p>
<p>Looking at the previous example, just about all the code is duplicated, except for the class names. <code>icon-strategy</code>, <code>icon-strategy_01</code> and <code>icon-strategy_02</code> for example. Let&#39;s take this and using a simple feature in HAML we can write code that will loop through a list of these names and then write out the HTML we need for the browser. In the following example, we will take those ico-font class names and place them in an array.</p>
<pre><code>- icons = &#39;icon-strategy&#39;, &#39;icon-strategy_01&#39;, &#39;icon-strategy_02&#39;, &#39;icon-dev_01&#39;, &#39;icon-dev_02&#39;
</code></pre>
<p>Using the <code>.each</code> looping function we can write a small chunk of code that will leverage the repeated portions of the HTML and take all this manual updating away from us. </p>
<pre><code>- icons.each do |icon|
%span{&quot;aria-hidden&quot; =&gt; &quot;true&quot;, :class =&gt; icon}
</code></pre>
<p>What we end up with is three lines of HAML versus x# of HTML. Much easer to manage and infinitely scaleable. New icons? No problem, add the class name to the array and you are done. </p>
<h2>Ico-font library CSS display in the style guide</h2>
<p>Now that we made the markup for the style guide easy to manage, what about the CSS? In the case of ico-fonts, much like sprites, there are multiple lines of CSS that need to be written. Doing this manually, you would write something like the following example.</p>
<pre><code>.icon-strategy_01:after {
content: &quot;\e002&quot;;
}
.icon-dev_03:after {
content: &quot;\e003&quot;;
}
.icon-dev_02:after {
content: &quot;\e004&quot;;
}
.icon-dev_01:after {
content: &quot;\e005&quot;;
}
.icon-design_03:after {
content: &quot;\e006&quot;;
}
.icon-design_02:after {
content: &quot;\e007&quot;;
}
.icon-design_01:after {
content: &quot;\e008&quot;;
}
</code></pre>
<p>Once again, to a developer this is extremely painful to look at and the concept of copying and pasting is simply evil. Here is where Sass comes to the rescue. With Sass we can apply a process much like we did with HAML where we can iterate on a list of variables that are feed through a loop of code that in turn processes this manual madness. </p>
<p>Using the previous example, let&#39;s pull out the variable and take advantage of the redundant code. In the following example we can replace the icon names and the PUA values with variables. </p>
<pre><code>.$ICO-VAR:after {
content: &quot;$PUA-VAR&quot;;
}
</code></pre>
<p>Now that we have the variable pattern, we will apply the values to a list and apply them to the variable <code>$icons</code>. </p>
<pre><code>$icons: icon-strategy icon-strategy_02 icon-strategy_01 icon-dev_03 icon-dev_02 icon-dev_01;
</code></pre>
<p>To put this list to use, we will need the <code>@each</code> rule in Sass to loop through the list. The <code>@each</code> rule takes two arguments, one for the variable and the other for the SassScript expression. The <code>@each</code> rule sets the variable to each item in the specified list as it loops through, then outputs the styles it contains using the value of variable.</p>
<p>In the following example we will create <code>$icon</code> as the variable and <code>$icons</code> as the list expression. The iterated value from <code>$icons</code> will redefine the variable of <code>$icon</code>. The value of <code>$icon</code> is then passed into the CSS rule w/pseudo class <code>.#{$icon}:after</code>. The use of interpolation is required when using SassScript variables in selectors.</p>
<pre><code>@each $icon in $icons {
.#{$icon}:after { &lt;--
foo:bar;
}
}
</code></pre>
<p>First part completed. We have code that will iterate through a list and produce lines of CSS with each name as the class. But the resulting code will be a ton of css classes all with the same declaration of <code>foo:bar</code>. Not very useful. </p>
<p>To make the second part of our code, for the declaration of <code>content: &quot;$VAR&quot;;</code>, we need to add the values of <code>$VAR</code> to our list. Since there is a relationship between the name of the class and the PUA content, it is preferred to keep these values together. </p>
<p>Lists have multiple ways of delineating and concatenating values. You can use spaces, commas <code>,</code> and you can even delineate each value between parenthesis <code>()</code>. What&#39;s important to note is that by using a combination of these delineators we can then group values in our list.</p>
<p>In our example we have <code>icon-strategy</code> which has a PUA of <code>\e000</code>. Then we have <code>icon-strategy_02</code> with a PUA of <code>\e001</code>. The following example illustrates how we can use a combination of parenthesis <code>()</code> and spaces to create our combined list of values. By placing <code>icon-strategy</code> and <code>\e000</code> within parenthesis <code>()</code> we are creating a concatenated value. And by simply placing spaces between each concatenated value, we can add more values to the variable list of <code>$icons</code>. Also note that we are putting the PUA value in single quotes <code>&#39;&#39;</code> because we want our returned value to be in quotes <code>content: &quot;\e000&quot;;</code>. </p>
<pre><code>$icons: (icon-strategy &#39;\e000&#39;) (icon-strategy_02 &#39;\e002&#39;) (icon-strategy_02 &#39;\e001&#39;);
</code></pre>
<p>Now that we have our list, let&#39;s put it to some good use. Here we introduce Sass&#39; concept of the <code>nth</code> function. Simply put, you can use this feature to tell Sass which item within a list to use <code>nth($LIST, val)</code>.</p>
<p>In the following example you will see that we updated <code>.#{$icon}:after</code> to use the <code>nth</code> function, <code>.#{nth($icon, 1)}:after</code>. We still require that the returned value is interpolated using the <code>#{}</code> function, but the returned value will now step through the list and take the first value of each concatenated set of values.</p>
<pre><code>@each $icon in $icons {
.#{nth($icon, 1)}:after { &lt;--
foo:bar;
}
}
</code></pre>
<p>For the next part, <code>content: &quot;$VAR&quot;;</code>, we need to step through our list again, except this time getting the second value. We will make use of the <code>nth</code> function again, but interpolation is not needed as we want the quoted value to be returned. </p>
<pre><code>@each $icon in $icons {
.#{nth($icon, 1)}:after {
content: nth($icon, 2); &lt;--
}
}
</code></pre>
<p>And there you have it. When the time comes and we need to add/delete icons from our ico-font library the hardest part is going to IcoMoon. </p>
<h2>One more thing</h2>
<p>For our ico-fonts to display correctly we need to add a few more declarations to our CSS. For one, we need the <code>font-family</code>. We don&#39;t want to add this to our <code>@each $icon in $icons</code> rule as this will duplicate these declarations each time it loops through the list. </p>
<p>Our solution is pretty simple. The following example illustrates how we can create the <code>ico-font-base</code> CSS rule as a placeholder selector. </p>
<pre><code> %ico-font-base {
font-family: &#39;ico-fonts&#39;;
speak: none;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
}
</code></pre>
<p>Then we can extend that into the previous <code>@each $icon in $icons</code> as illustrated in the following example. </p>
<pre><code>@each $icon in $icons {
.#{nth($icon, 1)}:after {
content: nth($icon, 2);
@extend %ico-font-base; &lt;--
}
}
</code></pre>
<p>When the Sass is processed into CSS, this rule will loop through the list as expected and also crate a concatenated list of selectors all using this rule. </p>
<h2>SassMeister living gist</h2>
<p>Seeing code is one thing, playing with code is another. Check out the <a href="http://sassmeister.com/gist/4731881">SassMeister Gist</a> and happy coding. </p>
<h2>List of rants</h2>
<p>If you like this post, check out <a href="http://roughdraft.io/4435659" title="My rants">more of my rants</a></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment