Pigment CSS migration limits for runtime theming and dynamic sx usage
0 reputation · 17 Aug 2021, 06:12 UTC
0 reputation · 17 Aug 2021, 06:12 UTC
Material UI's default styling resolves at runtime through the styled engine, while the zero-runtime Pigment CSS alternative extracts styles at build time. For applications that depend on runtime theme switching and dynamic sx expressions, the migration boundary is not always clear. The documented trade-off is that build-time extraction removes runtime cost but sacrifices runtime theming and parts of the dynamic sx API. Because availability and packaging differ across major releases, teams must confirm whether their installed version treats Pigment CSS as experimental or production-ready before committing to an architecture.
Is there a supported pattern for selectively using zero-runtime CSS for static components while retaining the runtime engine for dynamic theming? What criteria determine whether a given sx expression is compatible with build-time extraction? Does the migration require a global switch, or can both engines coexist during incremental adoption?
35775 reputation · 17 Aug 2021, 10:34 UTC
Pigment CSS and Material UI's runtime styled engine can coexist during incremental adoption, but the pattern requires component-level implementation rather than a simple global toggle. For applications needing both zero-runtime CSS for static components and runtime theming for dynamic parts, the supported approach is to apply Pigment CSS only to specific components while continuing to use the default styled engine elsewhere.
The key is that Pigment CSS operates at the component level through Babel plugin transformation. When you configure the babel-plugin-pigment-css, it processes only the files where you explicitly import from @pigment-css or apply its styling patterns. Components using standard Material UI imports remain on the runtime engine.
// pigment-button.jsx - uses zero-runtime CSS
import { styled } from '@pigment-css/react';
const PigmentButton = styled('button')({ color: 'primary' });
// regular-button.jsx - uses runtime engine
import { styled } from '@mui/material/styles';
const RegularButton = styled('button')({ color: 'primary' });
The critical question is whether an sx expression can be resolved at build time. Pigment CSS can extract static values, but dynamic theme references require runtime evaluation:
theme.spacing(2) when theme is static)(theme) => theme.palette.primary.main), responsive values with runtime conditions, theme overrides that change at runtimeBecause Pigment CSS's production readiness varies significantly across Material UI versions, you must verify your specific version's capabilities:
sx expressions resolve correctly—expressions using theme callbacks will not work with PigmentThe migration does not require a global switch. You can incrementally adopt Pigment CSS by transforming components one at a time. However, each component must be evaluated independently for compatibility. If your application relies heavily on runtime theme switching or complex dynamic sx expressions, the benefits of Pigment CSS may be limited for those components.
One missing detail needed: What Material UI version are you using? The experimental vs. production-ready status of Pigment CSS features differs significantly across v5.10, v5.13, and later releases, which directly impacts the migration path and feature availability.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.