dx-styles
A zero-runtime CSS-in-TS library providing deterministic styling, recipes, and token contracts at build time for high-performance design systems.
dx-styles is a compiler-backed CSS-in-TS library created to provide a robust styling foundation for modern design systems. By leveraging the wyw-in-js engine, it eliminates the need for runtime style injection, thereby removing common issues such as Flash of Unstyled Content (FOUC) and reliance on React context providers. The library is designed for framework-agnostic usage, ensuring compatibility with Server Components and static site generation, while keeping authoring logic entirely within TypeScript. By shifting styling computations to build time, dx-styles produces deterministic, static CSS files that can be tree-shaken and optimized by standard bundlers. It provides a focused set of primitives that prioritize semantic clarity and stable class names, making the styling layer fully auditable and performant for large-scale applications.
Some of the key features are:
- Zero Runtime: Emits static CSS at build time, eliminating runtime rendering overhead and provider dependencies.
- Deterministic Output: Guarantees stable class names across builds, ensuring predictable styling and easier code reviews.
- Recipe Primitives: Provides typed
recipeandslotRecipeAPIs for managing variant-driven components with ease. - Token Contracts: Supports creation of robust token contracts to manage design tokens and theme variables with strict type safety.
- Compile-time RTL: Includes built-in support for RTL mirroring, allowing developers to write LTR and handle mirrored layouts automatically.
- Explain Tooling: Offers advanced diagnostic tools to trace emitted CSS classes back to their exact source declaration and composition.
- Composition: Allows for powerful style composition using
cssandcreateRecipeStyleHandleswithout needing to fork source code. - Runtime Escape Hatch: Provides
assignVarsfor applying dynamic values as typed CSS variables, avoiding unnecessary style rule generation.
Operationally, dx-styles functions through a sophisticated build-time pipeline that interprets tagged TypeScript declarations such as css, recipe, and slotRecipe. During the build process, these definitions are evaluated and transformed into static CSS sidecars. Any dynamic values that cannot be evaluated at build time are handled via CSS custom properties, ensuring that the runtime remains lightweight. Developers integrate the library by configuring a supported bundler plugin, such as those provided by the wyw-in-js ecosystem, which orchestrates the extraction process automatically. This setup allows for colocated styles or separate style files, providing flexibility in how design systems are structured and maintained.
Some common use cases include:
- Design System Development: Building and maintaining consistent UI component libraries with reusable variants and tokens.
- Performance-Critical Applications: Eliminating runtime style processing to improve initial load times and overall frame rates.
- Server Component Integration: Styling applications in environments like Next.js that utilize React Server Components without runtime rendering constraints.
- Large-Scale Refactoring: Migrating legacy styled-components codebases to a zero-runtime model while maintaining high performance and type safety.
- Theme Management: Implementing flexible theming systems that shift between light, dark, and custom palettes by toggling CSS variable sets at the root.