AI summary by TwinMind
Facebook Tried Tailwind, Then Built This Instead
Theo - t3․gg
28m 18s
Initial Impressions and Overview
- Speaker initially skeptical of StyleX as Tailwind user
- After reading principles and diving deeper, became excited about StyleX approach
- Documentation still on vercel.app (very early stage)
- Not open source yet despite GitHub link
- Focus has been solving Facebook's internal problems
- Documentation public as "nice to have" to show it's real
StyleX Core Concept and API
- Combines power of inline styles with speed of atomic CSS
- Two-step process: define styles, then apply them
- Similar to React Native StyleSheet approach
- Uses stylex.create() to define style objects
- Access styles via styles.container syntax
- Integrates directly in JS like "CSS modules plus plus"
- Framework agnostic (works with React, vanilla HTML, etc.)
- All styles compiled into atomic class names and bundled into single CSS file
Key Features at a Glance
- Expressive, easy-to-use API simple as inline styles
- Apply styles conditionally, merge and compose arbitrary styles
- Cross component and file boundaries composition
- Cross code base boundaries (different repos can export CSS/JS objects)
- Use local constants and expressions for DRY code
- Dynamic styles with minimal overhead
- Expressive theming API with CSS variables
- Predictable last style always wins (major improvement over CSS)
- Style encapsulation - all styles caused by class names on element directly
- Right-click go to definition brings you to exact style definition
Comparison with Other Tools
StyleX vs Tailwind
- StyleX: static, atomic, styled format, co-location, composition, dynamic styles, types
- Tailwind: kind of co-location (bold claim), not super composable, equally dynamic, types available via tools
- Speaker disagrees with some comparisons - Tailwind has excellent tooling for types/validation
StyleX vs Vanilla Extract
- Vanilla Extract: define CSS in TypeScript with strict typings
- Compiles to CSS, consume by importing created elements
- Similar syntax but StyleX uses nested objects vs separate style tags
- Vanilla Extract lacks co-location (requires separate files)
CSS Variables and Theming System
Variable Definition
- Variables defined in .stylex.js files
- Must be authored in files with .stylex.js extension
- Must be exported as named exports only
- No default exports or nested exports allowed
- No other exports allowed in file
- Exclusively for defining CSS variables
Variable Usage and Overrides
- Import variables using named imports from .stylex.js files
- Can create override providers for sub-themes
- Provider system allows overriding variables in different DOM parts
- Theme parameter in styleX() call allows conditional variable overrides
- Variables compile to traditional CSS, overrides use different implementation
Advanced Variable Patterns
- Define different variable sets for different token types
- Create variables from other variables
- Create multiple themes using providers
- Apply themes to different app parts
- Create conditional/dynamic override patterns
Core Principles Deep Dive
Zero Cost Abstractions
- StyleX should always be fastest way to achieve patterns
- Never pay runtime performance cost unless opting into features
- Basic styles in same file: no runtime cost (stylex.create call deleted)
- Cross-file styles: small cost for additional power (simple object mapping)
- Most other solutions don't allow cross-file style composition
Deterministic and Predictable Styles
- CSS can feel fragile due to selector specificity conflicts
- Existing solutions (BEM, OOCSS) rely on rules/conventions
- Atomic utilities (Tailwind, Tachyons) limit CSS expressive power
- StyleX provides predictable system working across files
- Handles shorthand vs longhand properties (margin vs margin-top)
- Last style applied always wins - major improvement over CSS order issues
- Eliminates need for !important hacks
Readability and Maintainability Over Terseness
- Tailwind extremely terse and quick to write
- But harder to read/maintain long-term, steeper learning curve
- StyleX uses same CSS property names (camel case for JS)
- Enforces styles authored in objects separate from HTML
- Choose appropriate names indicating purpose (styles.active vs cryptic classes)
- Trade-off: more typing but more readable/maintainable
- More cognitive overhead - must name everything before applying styles
Small API Surface Area
- Core: just two functions (stylex.create, stylex.spread)
- Additional utilities feel simple and obvious:
- firstThatWorks: fallback values for properties
- keyframes: CSS keyframes for animations
- createVariables: define CSS variables
- variableProvider: override variable default values
Co-location of Styles and Markup
- Benefits to DRY code but not true for styles
- Styles should be near markup for easier maintenance
- Best/most readable way: write styles in same file as markup
- Repeating yourself not bad thing for styles
- Automatically deduplicates generated CSS for smallest bundle
Type Safety
- Styles largely remained untyped except vanilla extract
- StyleX authored in Flow with strong static types
- Auto-generated types for both Flow and TypeScript
- Custom TypeScript types for compatibility
- All styles are typed
- Can type components to constrain acceptable style props
- Can specify which properties component allows overriding
Framework Agnostic
- CSS-in-JS solution, not CSS-in-React
- Works with any JavaScript framework allowing markup in JS
- Can work with separate templates (not ideal use case)
- React probably best fit, but improvements planned for other frameworks
Style Encapsulation and Debuggability
- All styles on element caused by class names on that element itself
- Throwing away "cascade" part of cascading stylesheets
- Elements shouldn't have styles from parent/child elements
- Only inheritance allowed: color and similar properties
- Direct element styles always take precedence over inherited
- Disallows complex selectors, styles at distance
- Makes debugging easier, eliminates side effects
- Currently impossible: child conditional style on parent hover
- Will find ways to make these patterns possible without breaking principles
Modular and Composable Styles
- npm made code sharing easy, but CSS sharing remained challenging
- Third-party components: either baked-in hard-to-customize styles or completely headless
- Lack of good system for predictable merge/compose across packages
- StyleX aims to easily/reliably share styles with components within npm packages
Single File Better Than Lazy Loaded CSS
- Single CSS file very maintainable, browsers handle well
- Most styling solutions lead to CSS growing linearly
- Large applications make single file infeasible
- Splitting CSS into multiple lazy-loaded files:
- Improves initial load time
- Costs slower updates (browser recalculates on each new file)
- StyleX aims for small enough total CSS to load everything upfront
- Focused on generating smallest, fastest single CSS bundle
- Aligned with atomic CSS movement and Tailwind approach
