Compiled Atomic JavaScript?

This ad is not shown to multipass and full ticket holders
JSNation US
JSNation US 2025
November 17 - 20, 2025
New York, US & Online
See JS stars in the US biggest planetarium
Learn More
In partnership with Focus Reactive
Upcoming event
JSNation US 2025
JSNation US 2025
November 17 - 20, 2025. New York, US & Online
Learn more
Bookmark
GithubProject website
Rate this content

Can we apply the lessons of CSS to JavaScript?

While working on StyleX we use compilation and the technique Atomic CSS to create a more efficient system. Having small atomic CSS rules maximizes re-usability and dramatically reduces the size of the CSS at scale. A powerful compiler, makes the process automatic and frees from having to think custom DSLs. 

Can we do the same for JavaScript? What would the trade-offs be? Can it result in unique benefits not possible with most other approaches?

This talk has been presented at React Summit 2025, check out the latest edition of this React Conference.

FAQ

StyleX is a CSS and JS styling solution that operates at compile time, producing a highly optimized atomic stylesheet without runtime style injection. It is similar to Tailwind but considered by its creator, Naman, as one of the best styling solutions available.

Solenoid is a server-first atomic JavaScript framework created by Naman. It uses JSX on the server, with components and signals defined on the server but implemented in HTML, running in the browser without the need for hydration.

In Solenoid, you write code similar to React or SolidJS. The framework generates HTML that includes custom elements and functions, allowing for interactivity even as HTML streams in. This approach avoids the need for hydration and reduces double-data problems.

Solenoid is still in development and not feature complete. It generates larger HTML files, breaks some CSS selectors, and uses custom elements, which may not be the best implementation detail. Despite these trade-offs, it offers benefits like no hydration errors and interactive HTML streaming.

In Solenoid, components must be defined using function declarations, and all internal functions within a component must be arrow functions. This allows the compiler to properly handle and optimize the code.

Solenoid shares some similarities with Quick.js, particularly in its server-side rendering approach and use of HTML and JavaScript streams, but its implementation details differ significantly.

The main advantage of Solenoid is that it eliminates the need for hydration by making HTML the source of truth, thus avoiding hydration errors and making interactivity possible as soon as content streams in.

Solenoid was inspired by the idea of combining server-side rendering with atomic JavaScript principles, aiming to explore whether a new framework could offer unique benefits in terms of interactivity and performance.

Atomic CSS is beneficial because it scales better by reducing the size of CSS as the number of components increases. It allows for a small bundle of CSS to be loaded upfront, eliminating the need for lazy loading, which can otherwise become a performance bottleneck.

Naman Goel
Naman Goel
22 min
13 Jun, 2025

Comments

Sign in or register to post your comment.
Video Summary and Transcription
In 2008, the movie Vantage Point inspired the exploration of diverse perspectives in software development. The evolution from traditional CSS to atomic styles in StyleX and the scalability advantage of atomic JavaScript are significant areas of interest. Rethinking server-side rendering with React, Web Components, and the Hano framework introduces new possibilities for interactive components. Custom elements, Shadow DOM, and the Solenoid framework address challenges in CSS scoping and SSR for lighter-weight HTML. Signal functions in Solenoid offer a unique approach to data management and component development, enhancing app efficiency. Real-time interactive server setup, innovative server-side development, and the use of HTML as a source of truth contribute to project speed and efficiency. Debugging, component definition, HTML streaming, and component usage highlight the declarative nature and streaming capabilities of server-generated HTML.
Available in Español: ¿JavaScript Atómico Compilado?

1. Exploring StyleX and Atomic JavaScript

Short description:

In 2008, the movie Vantage Point showcased diverse perspectives. StyleX, a compile-time CSS and JS solution, offers optimized atomic styles. Atomic CSS's scalability advantage over traditional CSS is significant. Can atomic JavaScript replicate this success in the JavaScript realm?

Back in 2008, there was a movie called Vantage Point. It was a story told from many different perspectives, and you see how different perspectives can be very different from each other and tell you different stories. But then when you put them all together, something new might emerge. Anyway, I'm Naman, I worked at Meta for eight years. And for the last four to five years, I was working on StyleX. So what is StyleX? It's a CSS and JS styling solution, but it's compile time, so there's no style injection at runtime. It produces a highly optimized atomic stylesheet, similar to Tailwind. I would argue it's one of the best styling solutions out there, a lot better than Tailwind. There are some other ones that people don't know about, but that's neither here nor there. Regardless, the important point is, atomic CSS as a concept is winning. But it wasn't always like this. There was a long time in web development where lazy-loaded bundles were ubiquitous. We would have route-specific bundles of CSS, we would lazy load them as we navigated around pages, we would use syntax extensions like Sassless, and we would use architectures like BEM and OOCSS, which was actually just something we had to learn, and there was no tooling for it. But then atomic CSS came along and completely flipped the equation. And the reason that it was so successful is that it just scales better. Instead of your CSS growing with the number of components, where you just get bigger and bigger and bigger CSS, with atomic CSS, after a while, your CSS just stops growing. And as a result, you can have one small bundle of CSS loaded all up front. You don't need to lazy load any more CSS. Your HTML gets a little bit bigger, sure. You have more class names. But overall, a larger HTML is still way cheaper than a larger CSS file. And at scale, you realize how a large CSS file can become a performance bottleneck, which you otherwise don't notice. So I started thinking, can we do atomic JavaScript too and flip the equation in the JavaScript world?

2. Rethinking Server-Side React and Web Components

Short description:

I've used React for 12 years, exploring server-side rendering to recent server-first components. Can server components be interactive? HTMX simplicity appeals, but lacks declarative structure and component consistency. Delving into JSX, Hano framework, and Web Components' Shadow DOM for style isolation.

I've been using React for the last 12 years, and I started using it for server-side rendering. I went through React Create class, ES6 class components, function components with higher order components, Redux, and then Hooks. But then, this big change happened recently, called RSCs. And RSCs kind of flipped the model of React, where now everything is server-first. The data is server-first. We started thinking, why ship components to the client if the output is always going to be static? We can have a smaller client bundle if a lot of our UI can be represented with data or markup in this situation. We can still have client components for interactivity.

So I started thinking, can server components themselves be interactive? Hi, I'm Naman, and I'm not a fan of HTMX. But there are some things that are still kind of interesting to me about it. So this is a very simple HTMX demo, and what I like about it is that the logic of your application is right there in the HTML. There's no extra JavaScript to load, no extra bundles. It's all boiled down to something very simple, very efficient, in terms of the data that has to be loaded in the browser. But also, HTMX has imperative logic everywhere. You're updating things by IDs and inner HTML and outer HTML. There is no declarative system to keep track of everything and no components, no consistent data flow.

But then also, on the flip side, JSX can be used without React. And there's a server framework called Hano. It's a competitor to Express, which has first-class support for JSX, but just for rendering regular HTML. And so I started thinking, could I make a declarative component model that generates something like HTMX? Hi, I'm Naman, and I'm not a big fan of Web Components. But I've been digging into the APIs, looking for anything of value in that whole thing that we've built and put into the browsers. So I started looking at what are Web Components, what makes up Web Components. So the first thing that everybody talks about, because it's the most controversial part, is Shadow DOM. Shadow DOM gives you style isolation. That is its big value. Any CSS outside of the Shadow DOM doesn't apply to the markup inside the Shadow DOM, and any CSS inside the Shadow DOM doesn't affect anything outside of it. But we have Atomic CSS now. We don't have conflicts. We don't need this scoping of CSS in most cases. It really makes SSR hard, or sometimes impossible, if you have to support older browsers.

QnA