<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <id>https://stylex-docusaurus-nmn.vercel.app/blog</id>
    <title>StyleX Blog</title>
    <updated>2023-11-01T00:00:00.000Z</updated>
    <generator>https://github.com/jpmonette/feed</generator>
    <link rel="alternate" href="https://stylex-docusaurus-nmn.vercel.app/blog"/>
    <subtitle>StyleX Blog</subtitle>
    <icon>https://stylex-docusaurus-nmn.vercel.app/img/favicon.svg</icon>
    <entry>
        <title type="html"><![CDATA[Introducing StyleX]]></title>
        <id>https://stylex-docusaurus-nmn.vercel.app/blog/introducing-stylex</id>
        <link href="https://stylex-docusaurus-nmn.vercel.app/blog/introducing-stylex"/>
        <updated>2023-11-01T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[We are thrilled to introduce StyleX. StyleX is an expressive, deterministic,]]></summary>
        <content type="html"><![CDATA[<p>We are thrilled to introduce StyleX. StyleX is an expressive, deterministic,
reliable, and scalable styling system for ambitious applications. We've taken
the best ideas from the styling libraries that have come before to create
something that is simultaneously familiar and uniquely new.</p><h2 class="anchor anchorWithStickyNavbar_fF9Z" id="what-is-stylex">What is StyleX?<a href="#what-is-stylex" class="hash-link" aria-label="Direct link to What is StyleX?" title="Direct link to What is StyleX?">​</a></h2><p>StyleX takes the developer experience of CSS-in-JS libraries and uses
compile-time tooling to bridge it with the performance and scalability of static
CSS. However, StyleX is not just another compiler-based CSS-in-JS library.
StyleX has been carefully designed to meet the requirements of large
application, reusable component libraries, and statically typed codebases.</p><ol><li>StyleX supports an expressive subset of CSS. It avoids complex selectors and
guarantees no specificity conflicts in the generated CSS.</li><li>StyleX transforms, organizes, and optimizes styles into "atomic" CSS class
names. There's no need to learn or manage a separate library of utility class
names.</li><li>StyleX allows styles to be merged across file and component boundaries,
making it ideal for component libraries that allow user customization.</li><li>StyleX is fully typed and provides type utilities to allow fine-grained
control over what properties and values can be accepted by a component.</li></ol><h2 class="anchor anchorWithStickyNavbar_fF9Z" id="what-are-the-advantages-of-stylex">What are the advantages of StyleX?<a href="#what-are-the-advantages-of-stylex" class="hash-link" aria-label="Direct link to What are the advantages of StyleX?" title="Direct link to What are the advantages of StyleX?">​</a></h2><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="fast">Fast<a href="#fast" class="hash-link" aria-label="Direct link to Fast" title="Direct link to Fast">​</a></h3><p>StyleX is designed to be fast at both compile-time and runtime. The babel
transforms do not significantly slow down builds.</p><p>At runtime, StyleX entirely avoids the costs associated with using JavaScript to
insert styles at runtime, and does little more than efficiently combine class
name strings when necessary. And the generated CSS is optimized for size,
ensuring that the styles for even the largest websites can be quickly parsed by
browsers.</p><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="scalable">Scalable<a href="#scalable" class="hash-link" aria-label="Direct link to Scalable" title="Direct link to Scalable">​</a></h3><p>StyleX is designed to scale to extremely large codebases, like the ones we have
at Meta. The babel plugin can handle processing styles in many thousands of
components at compile-time by leveraging atomic builds and file-level caching.
And because StyleX is designed to encapsulate styles, it allows new components
to be developed in isolation with the expectation that they will render
predictably once used within other components.</p><p>By generating atomic CSS class names, StyleX helps minimize the size of the CSS
bundle. As the number of components in an application grows, the size of the CSS
bundle starts to plateau. This frees developers from having to manually optimize
or lazy-load CSS files.</p><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="predictable">Predictable<a href="#predictable" class="hash-link" aria-label="Direct link to Predictable" title="Direct link to Predictable">​</a></h3><p>StyleX automatically manages the specificity of CSS selectors to guarantee that
there are no collisions between the generated rules. StyleX gives developers a
system that reliably applies styles, and ensures that "the last style applied
always wins"</p><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="composable">Composable<a href="#composable" class="hash-link" aria-label="Direct link to Composable" title="Direct link to Composable">​</a></h3><p>StyleX styles are easy to compose. Not only can multiple local styles be applied
conditionally, styles can also be passed across files and components. Styles
always merge with predictable results.</p><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="type-safe">Type-Safe<a href="#type-safe" class="hash-link" aria-label="Direct link to Type-Safe" title="Direct link to Type-Safe">​</a></h3><p>You can constrain the styles a component accepts by using Typescript or Flow
types. Every style property and variable is fully typed.</p><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="colocation">Colocation<a href="#colocation" class="hash-link" aria-label="Direct link to Colocation" title="Direct link to Colocation">​</a></h3><p>StyleX allows and encourages authoring styles in the same file as the component
that uses them. This co-location helps make styles more readable and
maintainable in the long run. StyleX is able to use static analysis and
build-time tools to de-duplicate styles across components and to remove unused
styles.</p><h3 class="anchor anchorWithStickyNavbar_fF9Z" id="testable">Testable<a href="#testable" class="hash-link" aria-label="Direct link to Testable" title="Direct link to Testable">​</a></h3><p>StyleX can be configured to output debug class names <em>instead</em> of functional
atomic class names. This can be used to generate snapshots that don't change as
often in response to minor design changes.</p><h2 class="anchor anchorWithStickyNavbar_fF9Z" id="how-does-stylex-work">How does StyleX work?<a href="#how-does-stylex-work" class="hash-link" aria-label="Direct link to How does StyleX work?" title="Direct link to How does StyleX work?">​</a></h2><p>StyleX is a collection of tools that work together.</p><ul><li>A Babel plugin</li><li>A small runtime library</li><li>An ESlint plugin</li><li>A growing collection of integrations with bundlers and frameworks.</li></ul><p>The most important part of StyleX is the Babel plugin. It finds and extracts all
the styles defined within your source code and converts them to atomic class
names at compile time. A helper function deduplicates, sorts, and writes the
collected styles to a CSS file. These tools are used to implement bundler
plugins.</p><p>To make using StyleX feel as natural as possible, StyleX supports various static
patterns to define your styles by using local constants and expressions.
Additionally, in order to give you the best performance possible, the Babel
plugin also pre-computes the final class names when possible to remove any
runtime cost — even merging class names — from a given file. If a component is
defining and using styles within the same file statically, the runtime cost will
be <strong>ZERO</strong>.</p><p>When using more powerful patterns such as style composition, a tiny runtime
merges objects of class names dynamically. This runtime has been optimized to be
extremely fast and the results are then memoized.</p><h2 class="anchor anchorWithStickyNavbar_fF9Z" id="the-origins-of-stylex">The origins of StyleX<a href="#the-origins-of-stylex" class="hash-link" aria-label="Direct link to The origins of StyleX" title="Direct link to The origins of StyleX">​</a></h2><p>The previous Facebook website used something akin to CSS modules and sufferred
from various problems that inspired
<a href="https://blog.vjeux.com/2014/javascript/react-css-in-js-nationjs.html" target="_blank" rel="noopener noreferrer">the initial idea for CSS-in-JS</a>.
The average visitor to <a href="https://www.facebook.com/" target="_blank" rel="noopener noreferrer">facebook.com</a> would download
tens of megabytes of CSS. Much of it unused. In order to optimize the initial
load, we would lazy load our CSS which would, in turn, lead to slow update (or
"Interaction to Next Paint") times. Usage of complex selectors would lead to
conflicts or "specificity wars". Engineers would often resort to using
<code>!important</code> or more complex selectors to solve their problems, making the
entire system progressively worse.</p><p>A few years ago, when we were rebuilding
<a href="https://www.facebook.com/" target="_blank" rel="noopener noreferrer">facebook.com</a> from the ground up using React, we
knew we needed something better and built StyleX.</p><p>StyleX was designed to scale, and the design has proven itself in our years of
experience using it. We've added new features to StyleX without regressing on
performance or scalability while making StyleX more of a joy to use.</p><p>Using StyleX has been a massive improvement in both scalability and expressivity
for us at Meta. On <code>facebook.com</code> we were able to bring down our CSS bundle from
tens of megabytes of lazy-loaded CSS to a single bundle of a couple hundred
kilobytes.</p><p>We created StyleX not only to meet the styling needs of React developers on the
web, but to unify styling for React across web and native.</p><h2 class="anchor anchorWithStickyNavbar_fF9Z" id="how-does-meta-use-stylex">How does Meta use StyleX?<a href="#how-does-meta-use-stylex" class="hash-link" aria-label="Direct link to How does Meta use StyleX?" title="Direct link to How does Meta use StyleX?">​</a></h2><p>StyleX has became the preferred way to style components for every web surface
within Meta. StyleX is used to style React components for every major external
and internal product at Meta including Facebook, WhatsApp, Instagram, Workplace,
and Threads. It has changed the way we author components, and resolved the
issues our teams previously had with not being able to encapsulate and scale
their styled components.</p><p>We expanded the original capabilities of StyleX so that engineers at Meta can
use StyleX to author both static and dynamic styles. Our teams are using StyleX
theming APIs to develop "universal" components that are themed to take on the
appearance of different design systems used within different Meta products. And
we're gradually expanding support for cross-platform styling, thanks to StyleX
being aligned with the principles of encapsulation introduced by React Native's
styling system.</p><h2 class="anchor anchorWithStickyNavbar_fF9Z" id="open-source">Open Source<a href="#open-source" class="hash-link" aria-label="Direct link to Open Source" title="Direct link to Open Source">​</a></h2><p>What we're open sourcing is what we use internally. We develop on Github first
and sync it back to Meta. Although StyleX was originally created at Meta for
Meta, it is not specific to Meta.</p><p>That said, this is still just the beginning. We look forward to working with the
community to introduce further optimizations and more integrations.</p><p>We hope you love using StyleX as much as we do. ❤️</p>]]></content>
        <author>
            <name>Naman Goel</name>
            <uri>https://github.com/nmn</uri>
        </author>
        <author>
            <name>Nicolas Gallagher</name>
            <uri>https://github.com/necolas</uri>
        </author>
        <category label="announcement" term="announcement"/>
    </entry>
</feed>