Auto Layout conversion

For the most part, Auto Layout is pretty simple to use, but you can easily get yourself in a complicated mess if you’re not careful.

Container behaviour

If you’re like me, your buttons will be made up of rectangles and text layers. With Auto Layout, that concept goes out the window. Frames themselves form the rectangle base, taking on all the styles and effects from the container shape it’s just swallowed up.

It’s actually a simple but smart implementation from Figma. Let’s take a look at what would happen if you enabled Auto Layout on your rectangle base and text layer component, without that conversion taking place…

The rectangle and text layer would be treated as siblings, and so have the layout rules applied to them. In this instance, the layout settings are set to horizontal. If I change that to vertical, it looks like this…

Once you’ve converted your button to use Auto Layout, you’re no longer able to drag/nudge the contents of the component manually. You adjust the internal padding with the controls over in the right panel.

Buttons with icons

Once you’ve grasped the above, adding more elements to a component with Auto Layout enabled behaves as you’d expect. Let’s take a look at a button with an icon.

The construct and settings of a simple button with an icon

As we learnt above, the component frame itself is handling the blue container, so all we have inside are the icon and text layers. Padding is handled via the settings on the right, with 18px padding left and right, and 9px padding top and bottom. The space between the icon and text is set to 8px.

Pretty simple stuff, however the eagle-eyed among you might have noticed the left and right padding doesn’t look equal. Visually, you’d be right, and this is the first quirk I want to highlight.

In our design system all our icons are wrapped inside a container. Specifically, 16px icons are inside a 24px container. There are two main reasons for this. First, it allows us to have full control over an icons vertical and horizontal alignment. Ever used a triangular icon and it doesn’t look quite aligned? Second, it forces us to respect the hit area our icon buttons when designing new interfaces.

A snippet of the grid system we use for our 16px icons

This of course is creating problems with Auto Layout. Auto Layout doesn’t care about white space, it just cares about about the size of the elements inside the component. Admittedly this behaviour is what you’d expect, but it means it doesn’t play nice for those of you who adopt a similar system.

To resolve this we’d need to be able to set independent values for each padding setting. For now, we’re living with this slight offset in the hope it gets resolved quickly, because we believe our way of managing our icons is the correct way.

Nested components

This is where things can get complicated quickly.

As an aside, I found out just how quickly I can mess things up by assuming Shift+A acts as a toggle. It doesn’t. Repeatedly hitting Shift+A will just send you down an endless pit of Auto Layout hell.

The option menu and “Remove Auto Layout” is your friend here.

Anyway, nested components are where things start getting rather impressive. Let’s take a look at our dropdown menu component…

The biology of our dropdown menu component

The master component is on the left, and contains [what we call] “modules” on the right. In the example above, the menu is just made up of menu items. The modules are mostly 30px in height, but the divider line is only 20px. In the past, this would have played havoc when swapping menu item instances with dividers, as nothing would resize to compensate.

Alas! No more…

In order for this magic to happen however, there was an important step that had to be done. Because the divider line is 20px as opposed to 30px in height, I had to enable Auto Layout on that component and set its top and bottom padding values. This is so the master component knows the padding values it has to use when the component gets swapped out, otherwise simply swapping a menu item instance with a divider would just force the divider to inherit the 30px height.

And because we have Auto Layout enabled on the master component, the container resizes itself accordingly.

On-the-fly overrides

With nested components and Auto Layout, you’d expect to be able to add elements and have it all organically grow/shrink wouldn’t you? Well, it can, but maybe not as seamlessly as you’re imagining.

Let’s take a look at another one of our components…