You can create scaffolding utilities for yourself.
This is very powerful because they make the rest of your utility building experience a lot simpler, but most people don’t see it.
I’ve tried building a lot of different scaffolds and ended up not using many of them.
These are the few that I keep using consistently.
Border Scaffold
It is quite common to reuse the same border with border-style, border-color, and border-radius throughout the entire page, so it makes sense to have a global way to determine these values.
I felt that the best way to determine it is in either @theme or in :root.
:root { --border-width: 1px; --border-style: solid; --border-color: var(--color-neutral-700); --radius: calc(var(--spacing) * 2);}I want to use these values across all of my different utilities, so it makes sense to create a scaffold around them.
@utility border-scaffold { border: var(--border-width) var(--border-style) var(--border-color); border-radius: var(--radius);}When I create utilities or components, if I need a border scaffold, I can just apply it.
.card { @apply border-scaffold;}Transition Scaffold
There is a similar idea when it comes to transitions. I want the transitions across the site to be kept consistent as much as I can, so I begin by creating transition variables upfront:
:root { --transition-duration: 200ms; --transition-delay: 0ms; --transition-easing: ease-out;}Then I create a transition-scaffold to apply these values throughout my codebase. This transition-scaffold includes a basic list of transition properties that I use that don’t cause jank.
@utility transition-scaffold { --transition-values: var(--transition-duration) var(--transition-delay) var(--transition-easing); --transition-props: background-color, border-color, color, fill, stroke, opacity, outline, transform, translate, scale, rotate, skew, perspective; transition: var(--transition-values); transition-property: var(--transition-props);}Yes — this is an alarming number of properties to transition. But writing all
of them down is better than a lazy all property that transitions everything.
Then whenever I want to include transitions, I do not have to manually write down the values. I can simply use the transition-scaffold.
If I want to change transition properties, I can simply write a new transition-property value (or append to it, depending on circumstances).
.component { @apply transition-scaffold; transition-property: /* Added as necessary */;}Extended transition scaffold
I also have a scaffold for more things that I typically do not transition, that includes backdrop-filter, filter, gap, display and overlay.
I only use this when transitioning these properties are necessary.
@utility transition-scaffold-extended { @apply transition-scaffold; --transition-props: backdrop-filter, background, border-color, color, display, fill, filter, gap, opacity, outline, overlay, stroke, transform, translate, scale, rotate, skew, perspective; transition-behavior: allow-discrete;}Outline Scaffold
Nowadays, I prefer using outline to highlight the focused element. This is helpful because it’s easier to locate the focused element when there is a consistent visual marker to look for.
So I started by creating outline properties in the :root.
:root { /* Outline */ --outline-width: 2px; --outline-style: solid; --outline-focus-color: oklch(61.52% 0.178 257.59deg); --outline-offset: 0px;}The outline scaffold is a little bit more complex because we only want the outline to show when it is being focused on. So we need to:
- Set the
outline-colorto be transparent in the beginning. - And only show it when the element is focused
@utility outline-scaffold { outline: var(--outline-width) var(--outline-style) transparent; outline-offset: var(--outline-offset);
&:focus-visible { outline-color: var(--outline-focus-color); }}Focus Within
Sometimes I want elements to contain an outline when items within are being focused. For that, I create a focus-within utility. This is usually on the same element as the interact-within variant.
Here I need to make sure only the parent gets the focus. All the internal items do not have an outline focus.
@utility focus-within { outline: var(--outline-width) var(--outline-style) transparent; outline-offset: var(--outline-offset);
&:focus-within { outline-color: var(--outline-focus-color); }
* { --outline-focus-color: transparent; }}That’s it!
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson