Complex Sass mixins with defaults and multiple arguments

Sass Mixins

SASS mixins are a very handy way to create snippets of CSS that can easily be reused and updated. In a recent project I needed to add some CSS transitions to several elements. A mixin seemed like the ideal way to do this, but as you’ll see it got complex fast…

Basic Sass Mixin

A simple start

Initially, my goal was to apply the same transition to several elements in case I wanted to tweak the timing function or other details in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Mixin */
@mixin transition {
    transition-property: all;
    transition-duration: .5s;
    transition-timing-function: ease;
}

/* Sass declaration */
#myhtmlelement{ @include transition; }

/* Resulting CSS */
#myhtmlelement {
  transition-property: all;
  transition-duration: .5s;
  transition-timing-function: ease; }

Passing Arguments to Sass Mixins

As I built my interface things progressed and I needed to have a little finer control over the properties that were transitioning. In itself this is not a complex requirement. It seems as simple as this…

1
2
3
4
5
6
/* Mixin (simplified, removed
        unchanged properties) */


@mixin transition($property) {
    transition-property:$property;
}

Default values for arguments to Sass Mixins

That’s fine enough, but now I need to go back to all my @include declarations and add “all” or some other property. If I only need to fine tune one or two locations in a complex web application it would really be nice to have a default value for $property. Unfortunately the docs didn’t seem to be completely clear on how to do that, but it only took a little experimentation to find the solution.

1
2
3
4
/* Mixin */
@mixin transition($property:all) {
    transition-property:$property;
}

Now we have something that works like this…

1
2
3
4
5
6
7
8
9
/* Sass declarations */
#myhtmlelement1{ @include transition; }
#myhtmlelement2{ @include transition(width); }

/* Resulting CSS */
#myhtmlelement1 {
  transition-property: all; }
#myhtmlelement2 {
  transition-property: width; }

Passing multiple values to single argument with Sass Mixins

All done right? Well, there’s still room for the wheels to come off. As I continued coding along the time came when I needed to transition multiple properties of an element, but not all. Fortunately, Sass allows you to pass a list to a single property by adding an ellipsis after the property.

1
2
3
4
5
6
7
8
9
10
11
12
/* Mixin */
@mixin transition($property...) {
    transition-property:$property;
}

/* Sass declarations */
#myhtmlelement{
    @include transition(width, opacity); }

/* Resulting CSS */
#myhtmlelement {
  transition-property: width, opacity; }

Perfect! Well almost. Now all our declarations that rely on the default values are broken again!

Passing Complex or Blank Values With Defaults to Mixins

It seems like it should be possible to combine the ellipsis and the default value, but in my testing Sass was not happy with any combination. But that’s ok, lets come at this from another angle. In the process we can make it possible to handle blank defaults and more complex arguments. If we use Sass to evaluate the property we can actually pass a string to the mixin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Mixin */
@mixin transition($property:all) {
    transition-property:#{$property};
}

/* Sass declarations */
#myhtmlelement1{ @include transition; }
#myhtmlelement2{
   @include transition("width, opacity"); }

/* Resulting CSS */
#myhtmlelement1 {
  transition-property: all; }
#myhtmlelement2 {
  transition-property: width, opacity; }

This code also works in the case of allowing a blank default value for cases where that makes sense, just use…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Mixin */
@mixin mymixin($property:"") {
    display:#{$property};
}

/* Sass declarations */
#myhtmlelement1{ @include mymixin; }
#myhtmlelement2{
   @include mymixin(block); }

/* Resulting CSS */
#myhtmlelement1 { }
#myhtmlelement2 {
  display: block; }

More Info

More information on passing variable numbers of arguments to Sass mixins.

Posted in css, Sass Tagged , , , , ,

Recursively Delete all files with names matching a Regular Expression

Logical Progression of building a command line trick

So lets take this step by step…

First list all the files

Lets get a recursive list of all files in the current directory…

1
2
$ find .
...LOTS of files...

Find filenames by Regex

Ok, that’s a big list, but I only need certain files.

1
2
3
4
$ find . | grep \.bak[0-9]*\.txt
test.bak.txt
test.bak1.txt
...etc

Use sed to alter the list a bit

So for each row, were adding quoting it, and adding rm to the beginning. This will just output the list of commands, it won’t actually remove the files. MAKE SURE YOU DO THIS FIRST and you like the list it creates. Or don’t blame me if you delete everything on your whole computer.

1
2
3
4
find . | grep \.bak[0-9]*\.txt | sed -E 's/(.*)/rm "\1"/'
rm "test.bak.txt"
rm "test.bak1.txt"
...etc

Now we get dangerous

You could just copy and paste that list into the terminal. Or you could be really cool and just pipe it to bash…
BUT LIKE I SAID ABOVE, RUN THE LIST FIRST AND MAKE SURE YOU LIKE THE RESULTS BEFORE YOU PULL THE TRIGGER.

1
find . | grep \.bak[0-9]*\.txt | sed -E 's/(.*)/rm "\1"/' | bash

Posted in find, grep, sed Tagged ,

Force Secondary Name Server to Reload Domain

Ok, the situation is I just updated my primary name server (BIND on Linux.) In most cases with everything configured properly (and assuming you updated the “serial” in your zone configuration,) the change should propagate quickly. But if something goes wrong you can force the retransfer with this command:

1
/usr/sbin/rndc retransfer example.com

BTW, one last tip. If you can’t remember if you updated the serial it helps to make it the date. For instance 20130223. Just an easy way to keep track and it lets you know the last time you updated the zone.

Posted in Domain Name Servers, Linux, Server Administration, Web Servers Tagged , , , ,

Resize EBS volume attached to Amazon Web Services EC2 instance

Resizing EBS volume

Unfortunately, you can not currently resize an EBS volume on the fly. However, you can pretty easily resize with a quick reboot. The process is as follows:

  1. Create a snapshot of your current volume
  2. Create a new volume from that snapshot (bigger of course)
  3. Restart your instance using the new volume
  4. Resize the “partition” on that volume

The first three items can be done in the AWS interface. Number four is done in the command line with root access.

Once everything is up and running again df seems to say that you don’t have any more space. But of course since the volume came from a snapshot of a smaller “partition” it was not too shocking to see…

1
2
3
4
 df -h
Filesystem Size Used Avail Use% Mounted on
[...]
/dev/xvdf 8.0G 3.5G 4.5G 44% /some/mount/point

The command line tool you want is resize2fs

1
sudo resize2fs /dev/xvdf

And now everything should be happy…

1
2
3
4
 df -h
Filesystem Size Used Avail Use% Mounted on
[...]
/dev/xvdf 45G 3.5G 41G 8% /some/mount/point

Posted in Amazon Web Services, EBS, EC2 Tagged , , , , ,

Microsoft Word Export to PDF without borders

Creating PDFs With no White Borders

When designing documents I often like to have logos and images go to the edge of the document. Unfortunately when I print or save as PDF from Word I was unable to create a file with no white border at the edge.

Fortunately I was not the only person annoyed by this.  The secret is to create a custom size paper with “User Defined,” “Non-Printable areas.”

The instructions can be found on the Apple support forum here.

(Please report dead links or moved content in the comment section, I’ll update the post)

 

Posted in Applications, Microsoft Word Tagged , , , , ,

IE 7 or 8 Won’t Load My CSS Files

CSS files not working in Internet Exploder Explorer 7 or 8?

Did you know there is a limit on the number of CSS files Internet Explorer will load? After 31 css files Internet Explorer will just ignore additional css links.

Workaround for Development Servers

You can get around this limit by using the CSS @import statement. The way you have to do it is arrange your css files into groups of 31 or less. So you could have 50 css fils if you did something like…

1
2
3
4
<link rel="stylesheet" type="text/css"
              href="/css/group1.css" />
<link rel="stylesheet" type="text/css"
              href="/css/group2.css" />

Then inside of each of those files you would have 25 import statements…

/css/group1.css

1
2
3
4
@import url(/css/mycss01.css);
@import url(/css/mycss02.css);
...
@import url(/css/mycss25.css);

/css/group2.css

1
2
3
4
@import url(/css/mycss26.css);
@import url(/css/mycss27.css);
...
@import url(/css/mycss50.css);

Why you shouldn’t try to work around the limit

For managing CSS in a big project with lots of reusable components the above solution is perfect in development. But in production you are adding an enormous amount of overhead for your server and your users. The next step should be to create a script that combines your css files and optimizes them for production. That way you can use the individual files in development and track them in source control, but you run your optimization script each time you release which can give you a much faster loading site.

Posted in Browser Compatibility Tagged , , , , ,

Apple Magic Mouse Won’t Reconnect After Battery Change

Magic Mouse is Not So Magic When The Batteries Die

Usually I don’t have any problems when I get the notice that my magic mouse needs new juice. I switch it off, change the batteries, and usually when I switch it back on it reconnects automatically. But every once in a while it just does not want to reconnect. Sometimes I have a USB mouse handy, but it is really sad to have to connect another mouse just to get my Magic mouse working again.

Keyboard Access To Bluetooth Menu

If you have your bluetooth status displayed in the menubar reconnecting with the keyboard is a fairly simple matter. You can access the bluetooth menu with Control-fn-F8 (assuming your keyboard is in “special features” mode which uses the F keys for things like sound and brightness.) otherwise you can just hit Control-F8.

Once you have the Bluetooth menu focused you can use the down arrow key to navigate to your mouse, right arrow to select the submenu and return to “Connect.” In a few seconds you should be all set.

Of course if you have bluetooth menubar status off, you’ll have to continue reading.

Keyboard Navigation in OS X

In the Windows world you can fairly easily navigate most things with the keyboard, but other than Command-Tabbing through open apps and other obvious shortcuts I could not think how I would get to the bluetooth mouse settings so I did a little research.

First Time Keyboard Setup

If you’ve regained control at this point and you just want to know how to navigate for the future this will be a little easier. You want to go to System Preferences->Keyboard->Keyboard Shortcuts  and change the radio button at the bottom for Full Keyboard Access to “All Controls”. That will give you the ability to navigate the controls you’ll need.

OS X Keyboard System Preferences

"All Controls" gives you keyboard access to... you guessed it! ALL CONTROLS!

Now, if you’re reading this on another system trying to figure out how to regain control of your frozen mouse don’t worry, you can do this with your keyboard. If you looked at the screenshot above you should see one hint, control F7 will change the setting, but you have to get there first.

Navigate System Preferences with the Keyboard

If you have your keyboard setup to use the function (F#) keys as “special features” (brightness, mute, volume etc) which is the default, you can just hit Option-F1 to pop open the display preferences. If you are set up to use the F keys as F keys, you will probably need Option-fn-F1.

Alternatively you can get to the System Preferences by typing Control-fn-F3 in “special feature mode” or Control-F3 in F key mode. Now you can use the arrow keys to navigate to System Preferences, and hit return to open it.

  • Command-L will show all preferences.
  • k – to highlight “keyboard”
  • Spacebar – Open keyboard preferences
  • Control-F7 (Control-fn-F7 for special features mode) – will toggle Full Keyboard access (you want “All controls”Smilie: ;)

Connect Bluetooth Mouse In Keyboard All controls mode

Assuming you are still in the System Preferences panel it is fairly easy to reconnect your mouse (if not, read the above section far enough to get to system preferences).

  • Command-L – Show all system preferences
  • b – Highlight bluetooth
  • Spacebar – Open bluetooth preferences
Hopefully at this point you can see your mouse and it says “Not Connected,” if it is the only bluetooth device it should already be selected.
  • Tab (repeateadly, about six times) – select Gear icon below devices list
  • Down Arrow, twice- Show menu, select “Connect”
  • Return
After a few seconds you should be back in business!

Posted in OSX Tagged , , , , , ,

OS X 10.8 Mountain Lion Preview 1 VMware Tools Kernel Panic

Testing Mountain Lion OS X 10.8 in VMware

When trying to install VMware tools in OS X 10.8 preview 1 there seems to be a bit of an incompatibility. Everything works fine installing up to that point. So you have two choices. You can either use Mountain Lion without VMware tools and deal with not being able to resize your screen and other irritants. Or you can read these excellent instructions on how to get things working by Rob Griffiths. (Oh, and don’t skip step 1! Those snapshots are a lifesaver!!)

Thanks Rob!!

 

Posted in OS X Mountain Lion 10.8, VMware Fusion

VMware Fusion 4 Black Screen

Screensaver and Power Options

Ever since upgrading to VMware Fusion 4 I have been intermittently getting a black screen when VMware has been in the background for a while. Looking at the thumbnail for the machine in the “Virtual Machine Library” I can see that the machine is still responding, but I can’t actually see anything in the VMware Fusion window. It seems that the screensaver or monitor power off has been leaving things in an odd state.

Since my mac has it’s own screensaver and power settings modes, the Windows counterparts are a bit redundant. So let’s turn them off.

But wait, if I could turn them off there wouldn’t be a problem!

Yeah, that’s a problem. In addition to clicking like a mad man, I sent key combos from the menu to try to wake things up. But nothing seemed to work. I changed to full screen mode, and the interesting thing was I could see the screen while it was zooming to or out of full screen. But as soon as the zoom was complete, the black screen was back. Finally I tried changing to “Unity View” so I could use an app, and that worked like a charm and after switching back the black screen was gone.

  1. On the VM window, click on the unity view button on the upper right.
  2. Do something in an open window. Just something small, even changing focus.
  3. Under the view menu in VMware, switch back to single window.
If that does not work, you may have to restart the machine.

Turn off Screen Saver and Monitor Sleep (Windows 7)

  1. Richt click on the desktop
  2. Choose “Personalize”
  3. Click on “Screen Saver” in the lower right
  4. Set Screen saver to (none)
  5. Click on “Change power settings” at the bottom of the screen saver window
  6. Choose the radio button for “High Performance”
  7. Click on “Change plan settings”
  8. Change Turn off display and Put the computer to sleep to “Never”
That has stopped the black screen most of the time for me. Although I did still get it one time, that is an improvement from every time Windows went to sleep.

Posted in Virtualization, VMware Fusion, Windows 7 Tagged , , ,

How can I configure different emails for different Git servers or repositories?

“.gitconfig” in your home directory

If you’re like me, and you wanted to configure a different email for different repo’s the first place you look is ~/.gitconfig. This is the default configuration for git for your account. You can set your default user name there, as well as helper apps and other good stuff. But what if you want to change the email or user name for different repositories?

“git config” from the command line

Once you have a local clone of a git repository there is another config file in <my_repo_location>/.git/config. You can edit that manually, or use git to edit it for you…

1
2
3
4
5
6
7
$ cd <my_repo_location>
$ git config user.name "My Name"
$ git config user.email "myemail@sample.com"
$ tail -n 3 .git/config
[user]
    name = My Name
    email = myemail@sample.com

You can also edit the global defaults in a similar way…

1
2
$ git config --global user.name "My Default Name"
$ git config --global user.email "mydefaultemail@default.com"

Posted in Command Line, Git Tagged , ,