Architecting a composable style API with Figma and Stitches
By: Jory Tindall
Tags: Figma, React, Web Development, Design Systems, Css In Js

Maintaining a complex design system and style guide across brand, product, and engineering teams is complicated and requires close collaboration between all parties to deliver a cohesive and scalable product. Tooling that reduces the back-and-forth between these parties can alleviate stress around updating and expanding areas of the system and brand.

Figma already accelerates this process with robust styling and variable definitions, but translating those styles into code can sometimes be verbose and lack the DRY (don’t repeat yourself) concept of development.

Though still in beta, Stitches aims to reduce the complexity of translating a design system and brand primitives into a scalable application and component library.

Let’s walk through how simple it is to map a set of brand primitives defined in Figma, to a styled system within an application. I’ll be using the design system I’ve been building for Downbeat Academy as an example, but the strategy and implementation are simple such that these patterns can be applied to almost any design system and application. Access the forked Figma project here.

This isn’t meant to be an exhaustive tutorial on creating a design system in Figma or setting up a Stitches config in a React app, more of a high-level overview of how these tools work together to better the developer experience and encourage more streamlined communication between teams.

Note: Downbeat Academy is going through some massive updates currently which are not reflected in the production environment. Stay tuned for a rollout of some great new educational features!

Why Stitches?

Styling within a single page application is a bit of a hot topic with as many solutions and tooling options as blog posts are stating why you shouldn’t use anything except X. Aside from an excellent developer experience, Stitches touts first-in-class SSR (server-side rendering) support, excellent performance, small bundle size, and a framework-agnostic core library (though for this example we’ll be using React).

These are all excellent value propositions through the lens of a developer, but equally important are the similarities in semantics and architecture between Stitches and Figma. The process and strategy for defining brand primitives as styles within Figma and designing a component library on top are mirrored almost exactly in a Stitches configuration and React component library.

Check out the key features of Stitches.

Compared to Styled Components

Something to note, I’m coming from a background in Styled Components and value styling that is scoped at the component level. I generally use a global style sheet with the createGlobalStyle helper with CSS variables to define colors, fonts, and other global variables.

Mapping styles and brand primitives

The most foundational elements within a Figma design system are styles, documenting the color palette, type stack, box shadows, and other low-level primitives. Consistent organization of these styles is essential to building a cohesive application that scales as new features and products are introduced.

Using the Downbeat Academy color palette as an example (view here), each color is organized using a semantic naming convention into 10 different tints and shades generated using ColorBox from the base color. In the case of Downbeat Academy, each color corresponds with a different fruit. Thus, using the naming convention and numerical breakpoints for tint and shade, the Figma color style for the primary color maps to Passion Fruit / 500 .

Downbeat Academy color system

This has multiple benefits, most notably assigning human-readable values to colors and extending the base color in both directions, allowing for an abundance of color pairings to solve accessibility issues while maintaining brand flexibility.

Defining a Stitches theme

When setting up a project you intend to use Stitches with, all of your styles and brand primitives are encapsulated in a comprehensive config file containing a theme, media breakpoints, and utilities to handle custom logic in your styling. We’re mainly going to focus on the theme created with Stitches in this example.

This config file acts as a single source of truth for all of your brand primitives and generates CSS variables from each token, creating a flexible API for your project.

Not only does this make translating Figma styles into a composable API incredibly easy, but it cuts down significantly on the back and forth between design and engineering when all of the styles are easily accessible within Figma. Mirroring the Figma style hierarchy and the Stitches configuration in this fashion leads to more consistency and simplified communication between brand leaders and engineering.

Theme tokens in context

Defining styles in Figma and mapping them to Stitches theme tokens is one thing, but let’s look at how these tokens are applied in the context of a button.

Once a Stitches config file has been defined, the style function is available throughout the entire application giving you access to tokens and anything else defined in the config.

Token mapping

By default Stitches tokens are mapped to a predefined set of CSS properties; tokens within the color category of the theme are mapped to CSS properties that accept color values, background-color , color , border-color , etc. This makes composing components easier with VS Code Intellisense and TypeScript, however, your tokens are still accessible outside of mapped properties by prefixing the token value with the theme category.

Stitches has excellent documentation, read more about how properties are mapped and theme tokens are created here.

Composing components & variants

Now that we’ve defined styles and tokens for our brand primitives, we can move up in the atomic hierarchy and start building components with multiple variants. Doing so in Figma has been pretty painless since variants were introduced, we’re going to continue our example using a button component from the Downbeat Academy design system here.

Downbeat Academy button variant

Variants can be made up of essentially whatever properties you want, for this example we’re going to focus on button type (different types of buttons used based on the context) and button state (different interactions the user has with the button).

We’ll define button types as:

Primary

Secondary

Tertiary

Ghost

We’ll define button states as:

Rest

Hover

Active

Focus

Obviously, this example could be expanded to include different error and exception handling, sizes, icons, etc, but for the sake of this post, we’ll narrow the focus to these variants and how to combine them.

Defining a variant in Styled Components

Having used Styled Components extensively in the last few years, I’ve come across a pattern that works well for me when defining variants, but let’s analyze the shortcomings of this method.

Not only is this syntax at odds with how variants are composed in Figma, but it’s also incredibly verbose and relies on a ternary operator to check if a parameter is true or false. This isn’t very performant and lacks the semantics that we would expect with a modern library.

Let’s rewrite that example using Stitches.

Here we define a base styling that applies to all buttons, then outline variant-specific styling, using native browser state management.

Using the StyledButton in the application is super simple.

Not only is this syntax straightforward for a non-developer to understand, but it also makes explicit use of the theme tokens we already defined, enabling scalability and making documentation much easier.

Downsides

If you’re coming from Styled Components, the Stitches syntax requires a bit of a brain shift. Stitches uses JSON syntax for almost everything which means writing CSS properties using camelCase rather than traditional CSS property names. However, this is a pretty friendly syntax to work in and doesn’t take a lot of adjustment.

Stitches is also still technically in beta, so things might change rapidly if you’re working on a project. I say technically because the Stitches team is already using it in multiple production environments. There also isn’t currently much documentation surrounding TypeScript, but I have heard rumblings in the Stitches community about plans for supporting TypeScript and adding to their documentation.

Wrapping up

While styling within a SPA is subjective in a lot of circumstances, any tooling that eases the lines of communication between designers and engineers helps. While this setup might not be for everyone, I would imagine that this type of style API design will become commonplace soon.

Try Stitches out for yourself and let me know your thoughts!