Write Your Own Compass Mixin

As a developer working with CSS, one of the things that I find a bit troubling is the amount of style definitions that I have to repeat over and over to achieve the design I desire. One of the basic tenets of software development is to utilize the DRY principle, otherwise known as Don’t Repeat Yourself. Fortunately, when you implement Compass and SASS in your project to generate your CSS, you have a way to avoid copy-paste programming.

Compass adds funtionality to SASS that allows us to create mixins that you can call from anywhere in your .scss files, passing in parameters that can be defaulted if not provided. A sample mixin would be similar to the ellipsis mixin below, along with its usage.

@mixin ellipsis() {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
}
.ellipsis {
  	@include ellipsis;
}

While this is nice, and works well, you can add a few things to this ellipsis mixin to make sure that it works with a few additional features added in. Lets say we want all text that we are going to have automatically apply an ellipsis to should be blue and underlined. We have a couple of ways of doing it. The first way doesn’t take advantage of the power of a mixin.

@mixin ellipsis() {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
}
.ellipsis {
  	@include ellipsis;
    text-decoration: underline;
    color: blue;
}

This simply adds the desired styles to the .ellipsis class, making sure that it does not apply anywhere else. However, if we decide to make this functionality come with the mixin, then we can easily add those parameters to the call of the mixin like so.

@mixin ellipsis($color, $textDecoration) {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
    color: $color;
    text-decoration: $textDecoration;
}
.ellipsis {
  	@include ellipsis(blue, underline);
}

This will accomplish what we want, but it does require us to pass in the parameters for the color and whether we want the text underlined or strikethrough, but it makes it more difficult to simply add an ellipsis to text. Instead, we can do the following.

@mixin ellipsis($color: black, $textDecoration: none) {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
    color: $color;
    text-decoration: $textDecoration;
}
.ellipsis {
  	@include ellipsis(blue, underline);
}
.ellipsis.noSpecial {
	@include ellipsis;
}

Now, we can optionally pass in the color of the text we want and how we want the text decorated, and if we don’t want any special decorations at all, we can simply call @include ellipsis without passing in any parameters.

Related Posts

Mar 3, 2015
2 minutes

Avoid 'Persistent storage maximum size reached' in Firefox

One of the nice tools out there for tracking down issues that your website’s visitors are having is TrackJS. We started noticing the other day that we were getting overwhelmed by errors with the text Persistent storage maximum size reached for our Magento site. When we looked further into the issue, it quickly became obvious that all of the errors originated from a single user that was running Firefox.

It quickly became obvious that there was a single user that had exhausted their localStorage resources on their browser, but why was it only one user? Well, as it turns out, there is one browser that allows the user to set the amount of space that localStorage can use, and that one browser is Firefox.

Mar 5, 2015
2 minutes

Don't Be a Dunce, Save Your Orders

There are some gotchas that you think that you will always see coming. One such gotcha is the need to save an object to the datastore to persist any changes you may have made to that object.

While it seems like a reasonable concept at the base level, there are times that the need to save an object completely escapes your mind. It seems that for many non-developers, this occurs when they have been working a long time on a file, typically a Microsoft Word document, shortly before their computer blue screens, losing all of their work.

Mar 16, 2015
2 minutes

Google Code Shutting Down

Google just recently announced that they are going to begin the process of shutting down their Google Code project hosting service. In the blog post announcing that they were shuttering the service, they let it slip that even Google had quit using Google Code for their project hosting, instead transitioning thousands of their projects to GitHub. Google seemingly blames the fact that GitHub and BitBucket handle project hosting better than Google does as the main reason that they are discontinuing the service.