Atomic Power: the Story of StyleX

Rate this content
Bookmark

A history of how code transformation has affected the industry. And how it led to changes in how we write CSS

This talk has been presented at React Day Berlin 2024, check out the latest edition of this React Conference.

FAQ

StylX is a tool for generating atomic CSS, maintained by Naman at Meta.

Early CSS lacked namespaces and variables, making it difficult to maintain and requiring repeated code.

CSS preprocessors like Sass and Less introduced features such as variables, nesting, and loops, improving CSS maintainability and functionality.

The community developed CSS modules and CSS in JS to address namespacing and scaling issues, with each approach offering different solutions and benefits.

Atomic CSS breaks styles into small, reusable pieces, maximizing reusability and scalability. It helps keep CSS size manageable as applications grow.

Tailwind CSS uses atomic styles to create a single small CSS file for the entire application, minimizing the need for lazy loading and improving performance.

While both generate atomic CSS, Tailwind works with almost any language and encourages repetition, while StylX is a JavaScript compiler that promotes reusability and is strongly typed.

React Strict DOM is a project that builds on StyleX to provide a common baseline for writing UI across web and React Native, offering web-like APIs that are not global.

Compilers have facilitated advancements in web development by enabling features like atomic CSS, improving performance, and enhancing developer experience.

Initially, web styling relied solely on HTML with limited capabilities like blink and marquee tags. CSS was later introduced to solve styling limitations, offering more power and reusability.

Naman Goel
Naman Goel
25 min
16 Dec, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Hello, I'm Naman and today I'm going to talk about atomic power and the story of StylX. CSS preprocessors like Sass and Less emerged to address challenges with CSS. In the evolution of CSS, there was a fork in the road with some moving towards CSS modules and others towards CSS in JS. Atomic CSS is about breaking styles into small, reusable pieces. Tailwind is a successful implementation of atomic styles. Stylix is a JavaScript compiler that generates atomic CSS and allows for defining styles once and reusing them. The speaker demonstrates the conversion of a design to Tailwind classes using Stylix. The TW function and parser in StyleX are discussed for defining constants and converting them into StyleX objects. Atomic CSS is a widely agreed concept and Tailwind can be used with StyleX. The React Strict DOM project is mentioned as a tool for writing UIs for web and React Native.

1. Introduction to StylX and Atomic Power

Short description:

Hello, I'm Naman. Today, I'm going to talk about atomic power and the story of StylX. In the beginning, there was only HTML without CSS. We used blink tags and marquee tags for styling, which was limited. Then CSS was invented, solving some problems but creating new ones. CSS preprocessors like Sass and Less emerged to address these challenges.

Hello, I'm Naman. I maintain StylX at Mera, and today I'm going to talk about atomic power. And not the nuclear kind, but I'm going to talk about atomic, and I'm going to talk about power. And then using that lens, I'm going to tell you about the story of StylX.

So let's get started from the very beginning. In the beginning, there was HTML and it was the birth of the World Wide Web. And there was no CSS. HTML is all we had. And with that HTML, we used blink tags and marquee tags for some fun, font tags for some styling, and we dealt with table layouts too. That was not fun. This wasn't great, but it was very limited. All kinds of layouts that we do today were simply impossible. And it was very tedious. There was no way to reuse our styles across components. Components didn't even exist yet.

And so in a few years, smart people got together and invented CSS. CSS solved some of our biggest problems with styling in HTML. It was way more powerful, there were actual layouts you could do, and not just with tables. And it was way more reusable. You could define a CSS selector once and use it all over your HTML. But as we solved some problems, we discovered new ones. And we realized that since CSS had no namespaces, it was really difficult to maintain. And since this is early CSS, it didn't even have variables. So we don't have to repeat ourselves in our HTML, but now we have to repeat ourselves in our CSS.

So some more smart people got together, and we came up with a new set of tools. This time, they were CSS preprocessors. Things that would process something you wrote and generate your CSS. You may have heard of Sass and Less. I used to use Stylus, and CSS was CSS in JS before CSS in JS. CSS preprocessors solved some of our biggest problems with CSS, such as variables and nesting.

2. Evolution of CSS Modules and CSS in JS

Short description:

In the next era, there was a fork in the road. Some moved towards CSS modules, while others moved towards CSS in JS. CSS modules solved namespacing, but had scaling issues. CSS in JS provided namespacing and better developer experience. Compiled time CSS in JS evolved, solving some problems but not all scaling issues. The idea of atomic CSS emerged as a convergence between CSS modules and compile-time CSS in JS.

They also added a bunch of other powerful features like loops and includes. But they still didn't have namespacing, and it was still hard to maintain at scale. And so, engineers kept working. And in the next era, there was a fork in the road. And some of the community moved towards CSS modules, and another part of the community moved towards CSS in JS. The reason that this happened was because this is the era when single-page applications were becoming popular, and frameworks like React had started to come out. And just to be clear, there were many people who didn't move to either of these new tools and continued to use good old CSS or CSS preprocessors. But I'm going to be focusing on every step forward that we take and not continue to keep mentioning that, yes, the old tools never went away.

So, over on the CSS module side, it finally solved namespacing. This was huge. This is why CSS modules is so successful. But it was still hard to scale in some ways. In general, code maintainability had been solved for the most part, but you could still run into some amount of CSS conflicts, and your CSS bloat would continue to bloat as your application gets bigger. On the other hand, CSS in JS gave us namespacing as well, and it gave us a whole bunch of other powers. It gave us much better developer experience than anything we had tried before. You could now write your style in the same file as your markup, in the same file as your component. But most of these early CSS and JS libraries had really bad performance because they relied on runtime style injection. And so JavaScript developers, being JavaScript developers, we kept at it.

A bunch of really impressive libraries came and became successful. And eventually we reached the next milestone, which was compiled time CSS in JS. This was an evolution of the CSS in JS idea, where you don't have the performance overhead because you compile all of your CSS at build time, and you don't have any runtime-style injection anymore, but you still have the same great developer experience, which is why CSS in JS was so successful in the first place. And while it solved so many problems, it did not completely solve all of the scaling problems that we've had with basically every CSS solution we've ever tried. As your application gets bigger, your CSS gets bigger. Then you're forced to lazy-load your CSS, which means your page updates get slower because you're waiting on additional CSS to load and process and compute. And so we continued on. A bunch of more great compile-time CSS in JS libraries came out, like Linarea and Vanilla Extract, and we hit some kind of convergence, where the two streams kind of agreed on the idea of atomic CSS. Of course, not everyone agreed. Many people stuck to CSS modules, Vanilla CSS modules, and many others continued to use the compile-time CSS in JS, or even runtime CSS in JS. But it is important to note that a lot of people from both streams did agree that atomic CSS was a good idea. But what is atomic CSS? As soon as the term is mentioned, this thought pops in everyone's head, this elephant in the room.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Build a Design System with React and Tailwind CSS
React Summit 2022React Summit 2022
27 min
Build a Design System with React and Tailwind CSS
Top Content
This Talk discusses design systems and how to build one using React and Tailwind CSS. Tailwind CSS provides utility classes for building complex layouts without writing CSS rules. Custom colors can be added to the Tailwind CSS config file, and font styles and text sizes can be customized. The entire Tailwind CSS configuration can be customized to meet specific requirements. Base styles can be added to the config file itself using a plugin. Reusable components can be created with Tailwind CSS, allowing for easy customization of size and color.
Rethinking CSS - Introducing Stylex
React Finland 2021React Finland 2021
25 min
Rethinking CSS - Introducing Stylex
Top Content
CSS + superpowers - bloat. How Stylex creates a zero-cost abstraction that gives CSS superpowers.
If You Were a React Compiler
React Summit US 2024React Summit US 2024
26 min
If You Were a React Compiler
Top Content
In this talk, the speaker aims to build an accurate understanding of how the new React compiler works, focusing on minimizing re-renders and improving performance. They discuss the concept of memoization and how it can be used to optimize React applications by storing the results of function calls. The React compiler automates this process by analyzing code, checking dependencies, and transpiling JSX. The speaker emphasizes the importance of being aware of memory concerns when using memoization and explains how the React compiler detects changes in function closure values. They also mention the Fibre Tree, which drives the reconciliation process and helps optimize performance in React. Additionally, the speaker touches on JSX transpilation, compiler caching, and the generation of code. They encourage developers to understand the code generated by the compiler to optimize specific sections as needed.
How to achieve layout composition in React
React Summit 2022React Summit 2022
8 min
How to achieve layout composition in React
This talk discusses achieving layout composition in React using Bedrock Layout Primitives. By componentizing CSS layout, complex layouts can be achieved and reused across different components. The talk also covers the challenges of achieving complex layouts, such as card lineups, and provides solutions for maintaining alignment and responsiveness. The BedrockLayout primitive library simplifies web layouts and offers flexibility in composing layouts.
Type-safe Styling for React Component Packages: Vanilla Extract CSS
React Advanced 2023React Advanced 2023
19 min
Type-safe Styling for React Component Packages: Vanilla Extract CSS
Watch video: Type-safe Styling for React Component Packages: Vanilla Extract CSS
Today's Talk introduces Vanilla Extract CSS, a type-safe styling method for React applications. It combines the benefits of scoped styling, zero runtime overhead, and a great developer experience. Vanilla Extract generates a static CSS file at build time, resulting in better performance. It is framework agnostic and offers a powerful toolkit, including Sprinkles for utility classes and CSS utils for calculations. With type safety and the ability to define themes and variants, Vanilla Extract makes it easy to create efficient, scalable, and maintainable design system component packages.
Moving on From Runtime Css-In-Js at Scale
React Summit 2023React Summit 2023
29 min
Moving on From Runtime Css-In-Js at Scale
Watch video: Moving on From Runtime Css-In-Js at Scale
This Talk explores the evolution of styling architecture, dynamic theming with style components, and optimizing style updates. It discusses the challenges of CSS migration and the choice between JavaScript and CSS native tooling. The Talk also touches on CSS tools and libraries, including Tailwind CSS and CSS in JS major libraries like MUI. The importance of picking a stack based on team members' strengths and the use of namespacing CSS for conflict-free dependency trees are highlighted.