RFC - Variable name literals for keys that that start with "--"
The main change proposed is to opt out of automatic variable name generation when the object keys used within stylex.defineVars starts with --.
Motivations
1. Using variables across StyleX and global .css files
There is often a desire to use variables defined within StyleX to be usable within a raw CSS file to set global styles. However, since the actual generated variable names created by StyleX are unknown until compile time, this process isn't developer friendly.
The only escape hatch today is to fallback to strings for using variables within StyleX and CSS. This means losing all the benefits provided by StyleX's theming APIs, such as type-safety and guaranteed default values.
2. Migrating from CSS strings to StyleX theming APIs
There existing codebases that use CSS variable names as strings today and don't have a clear migration path to adopt StyleX's theming APIs. While it is easier to move to StyleX's APIs for new variables, it is usually a non-starter to migrate existing variables. This new feature provides an escape hatch to make migration possible.
A typical migration process might look like:
Define all existing CSS variables as literals with a stylex.defineVars call with the default value set to null
Update all usages of that variable to use the object created with stylex.defineVars
Define the default values for the variables within StyleX
Define any Themes within StyleX
Delete the old patterns for defining the variables to make StyleX the exclusive way to define variables.
Optionally, if no uses of the variables exist outside of StyleX anymore, rename the keys to drop the --.
Caveat
There is one major caveat or risk of this proposal. Unlike variables generated by StyleX itself, there is no way to guarantee that a variable name isn't accidentally duplicated. If the same variable name is used in two separate stylex.defineVars calls, it will result in the same variable being defined twice in the generated CSS file.
Ideally, in a follow-up, we should be able to detect such a conflict at compile time.
RFC - Variable name literals for keys that that start with "--"
The main change proposed is to opt out of automatic variable name generation when the object keys used within
stylex.defineVars
starts with--
.Motivations
1. Using variables across StyleX and global
.css
filesThere is often a desire to use variables defined within StyleX to be usable within a raw CSS file to set global styles. However, since the actual generated variable names created by StyleX are unknown until compile time, this process isn't developer friendly.
The only escape hatch today is to fallback to strings for using variables within StyleX and CSS. This means losing all the benefits provided by StyleX's theming APIs, such as type-safety and guaranteed default values.
2. Migrating from CSS strings to StyleX theming APIs
There existing codebases that use CSS variable names as strings today and don't have a clear migration path to adopt StyleX's theming APIs. While it is easier to move to StyleX's APIs for new variables, it is usually a non-starter to migrate existing variables. This new feature provides an escape hatch to make migration possible.
A typical migration process might look like:
stylex.defineVars
call with the default value set tonull
stylex.defineVars
--
.Caveat
There is one major caveat or risk of this proposal. Unlike variables generated by StyleX itself, there is no way to guarantee that a variable name isn't accidentally duplicated. If the same variable name is used in two separate
stylex.defineVars
calls, it will result in the same variable being defined twice in the generated CSS file.Ideally, in a follow-up, we should be able to detect such a conflict at compile time.