React Stitches is a CSS-in-JS library for styling React components with a focus on type safety, developer experience, and zero runtime overhead. You write styles as JavaScript objects using its styled API, and Stitches generates atomic CSS classes at build time, then injects only the styles you actually use. This article explains how Stitches works, how it compares to other styling solutions, and when it makes sense for your project.
Formula
The calculator above computes your average pace and speed from a finish time and distance. The formulas are:
- Pace (min/km) = Finish time (minutes) ÷ Distance (km)
- Speed (km/h) = Distance (km) ÷ (Finish time (minutes) ÷ 60)
Where:
- t = finish time in minutes
- d = distance in kilometers
- Pace is expressed as minutes per kilometer (min/km)
- Speed is expressed as kilometers per hour (km/h)
Worked Example
Using the default values: finish time = 50 minutes, distance = 10 km.
- Pace = 50 ÷ 10 = 5.00 min/km
- Speed = 10 ÷ (50 ÷ 60) = 10 ÷ 0.8333 = 12.00 km/h
So with a 50-minute 10K, your average pace is 5 minutes per kilometer, and your average speed is 12 km/h.
What Is Stitches?
Stitches is a CSS-in-JS library created by the Modulz team (now part of Radix UI). It was designed to solve common pain points with other CSS-in-JS libraries: runtime cost, specificity issues, and poor TypeScript integration. Stitches compiles your styles into atomic classes at build time using a compiler that runs during the bundling step. This means the browser receives only the CSS rules that are actually used, with no runtime style injection.
Key features include:
- Type-safe variants – You define variants as plain objects, and TypeScript infers the prop types automatically.
- Zero runtime cost – Styles are generated at build time, so there is no JavaScript overhead when the component renders.
- Theming – Stitches supports design tokens and theme switching without extra libraries.
- Atomic CSS – Each unique style declaration becomes a single class, reducing CSS bundle size.
How Stitches Works
Stitches uses a css function to define styles and a styled function to create React components. Here is a basic example:
import { styled } from '@stitches/react';
const Button = styled('button', {
backgroundColor: 'blue',
padding: '10px 20px',
borderRadius: '5px',
'&:hover': {
backgroundColor: 'darkblue',
},
});
When you use Button in your app, Stitches generates a CSS class like c-abc123 that contains only the declarations you wrote. The class is inserted into a <style> tag in the head. Because classes are atomic, if two components share the same declaration (e.g., padding: 10px 20px), they reuse the same class.
Variants are a core feature:
const Button = styled('button', {
variants: {
color: {
primary: { backgroundColor: 'blue' },
secondary: { backgroundColor: 'gray' },
},
size: {
small: { padding: '5px 10px' },
large: { padding: '15px 30px' },
},
},
});
Now you can use <Button color="primary" size="large"> and TypeScript will enforce valid prop values.
Stitches vs. Other Styling Solutions
Stitches is often compared to styled-components, Emotion, and Tailwind CSS. Here is a quick breakdown:
- styled-components – Uses runtime CSS injection. Easier to set up but adds JavaScript overhead and can cause performance issues in large apps.
- Emotion – Similar to styled-components but offers both runtime and zero-runtime modes. More flexible but also more complex.
- Tailwind CSS – Utility-first, not CSS-in-JS. Great for rapid development but requires learning a new class system and often needs a build step.
- Stitches – Combines the developer experience of CSS-in-JS with the performance of atomic CSS. TypeScript support is excellent, and there is no runtime cost.
One major difference is that Stitches generates styles at build time, so you need a bundler plugin (like Vite or Next.js) to compile the styles. This adds a small setup step but pays off in performance.
Common Problems and Misconceptions
One common misconception is that Stitches is dead or unmaintained. While the core library has not seen frequent updates since 2022, it is still widely used and works with current React versions. The Radix team shifted focus to other tools, but Stitches remains stable.
Another issue is that Stitches does not support React 18’s concurrent features out of the box in older versions. However, the latest version (1.4.x) works fine with React 18 in most cases. If you run into issues, check the GitHub issues for workarounds.
Potential pitfalls:
- SSR setup – You need to configure Stitches to work with server-side rendering to avoid FOUC (flash of unstyled content).
- Dynamic styles – If you need to change styles based on runtime values (e.g., a progress bar), you must use CSS variables or inline styles, because Stitches only generates static classes.
- Bundle size – While atomic CSS reduces duplication, the compiler itself is a build-time dependency. The client bundle stays small.
How to Choose Between Stitches and Alternatives
Choosing a styling solution depends on your project’s needs:
- If you want maximum performance and type safety, and you are already using a bundler that supports Stitches, it is a strong choice.
- If you prefer a runtime CSS-in-JS approach for simplicity, styled-components is easier to start with.
- If you want a utility-first approach with a huge ecosystem, Tailwind CSS is popular, but it requires a different mindset.
- If you are using a component library like Radix UI, Stitches integrates nicely because it was built by the same team.
Consider your team’s experience and the long-term maintenance of the library. Stitches is stable but not actively developed, so if you need new features, you might prefer a more actively maintained option like vanilla-extract.
Costs and Performance
Stitches itself is free and open source (MIT license). The main “cost� is the build-time compilation, which adds a few seconds to your build process. However, the runtime performance benefit is significant: no JavaScript is executed to inject styles, and the CSS bundle is often smaller because of atomic class reuse.
For a typical app, the difference in runtime performance between Stitches and styled-components is small, but it becomes noticeable in large applications with thousands of components. The atomic CSS approach also reduces the risk of specificity conflicts, which can save debugging time.
Safety and Maintenance
Since Stitches is a build-time tool, there are no runtime security concerns. However, as with any dependency, you should keep it updated to patch any potential vulnerabilities. The library is widely used, so community support is available, but official support is limited.
If you are starting a new project, consider whether you want to rely on a library that is not actively maintained. Alternatives like vanilla-extract offer similar build-time CSS-in-JS with active development.
Frequently Asked Questions
Is Stitches still maintained?
Stitches is not actively developed, but it is stable and works with current React versions. The last major release was in 2022, and the repository is archived. Many projects still use it successfully, but for new projects, you might prefer a more actively maintained alternative.
Does Stitches work with Next.js?
Yes, Stitches works with Next.js. You need to add the @stitches/react package and configure the styled function. For server-side rendering, you must use the getCssText method to extract styles in _document.tsx.
Can I use Stitches with TypeScript?
Absolutely. Stitches was designed with TypeScript in mind. Variants and themes are fully typed, and the styled function infers prop types automatically. This is one of its biggest advantages over other CSS-in-JS libraries.
What is the difference between Stitches and vanilla-extract?
Both are build-time CSS-in-JS libraries. Vanilla-extract uses CSS modules and requires a separate file for styles, while Stitches allows inline styles in the component file. Vanilla-extract is more actively maintained and has a larger community.
Is Stitches faster than styled-components?
Yes, in most cases. Stitches has zero runtime cost because styles are generated at build time, while styled-components injects styles at runtime. This makes Stitches faster for initial render and updates, especially in large applications.
Can I use Stitches without React?
Stitches is React-specific. However, the core @stitches/core package can be used with any framework, but the @stitches/react package provides the styled API for React components.
Related
Formula used
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 | 100000 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Motorola Phone Recovery: How to Recover Data
- Poco X3 GT Dimensity 1100 67W Fast Charging Launched?5.00 min/km
- Flash Converter: What It Is & How to Use It
- Choosing a Pipe Flow Meter for Swirling Flow
- Convert 2D Videos to Oculus Rift VR: How-To
- DRAM Light on Motherboard: Meaning & Fixes5.00 min/km