Polyfill CSS Variables Support
See original GitHub issueAPI
The API for writing styles remains the same. For example:
const title = css`
color: var(--title-color);
`;
When using this class name in a component:
function MyComponent({ color }) {
return <h1 {...styles(title, { '--title-color': color })} />;
}
How to implement
- Write a Babel plugin which looks for
styles
calls with variables and assigns an unique ID for each. Something like:
This ID is necessary so that we can keep track of dynamic styles and avoid inserting same styles multiple times. The ID should be unique, but shouldn’t change between builds. The easiest way is to hash the current file path and the sequential number in the file.styles(title, { __id: 'xgfs65d', '--title-color': color });
- We will need to generate an AST from the CSS string. We can probably use the
ast
plugin forstylis
to generate theast
, but I had a brokenast
when I used nested media queries. We will probably need to fix the plugin. We will also need to write a simple serializer which takes the AST and serializes it to plain CSS string. - The
css
tagged template literal should parse the CSS string to an AST to determine if there are CSS variables in the string. It should generate 2 different CSS strings, one with properties using variables, and one without. The one without is inserted normally to thesheet
API, but the ones with variables is returned as a templates. It’s an array because there can be multiple rules. The returned object fromcss
can look like following:
Something to keep in mind is that if a property is redeclared after a variable use, we should discard the one with variable, i.e.{ toString() { return 'xgsf64f'; }, templates: ['.xgsf64f { color: var(--title-color); }'] }
color: var(--title-color); color: blue
should be considered ascolor: blue
. - The
names
function should be updated to look for these objects and usetoString
when necessary - The
styles
function should look for templates and do a search replace in them with the value of the variables, then insert it to the CSSOM with theinsertRule
API. If CSS custom properties are supported in the browser, it should avoid the search replace and use inline styles to set the variable values. - The babel presets should be separated to client and server presets, where the server preset excludes the
preval-extract
extract plugin and includes a plugin which just convertscss
tocss.named
calls. - Server rendering APIs and instructions should be updated to read the stylesheet from the string in memory instead of from a file.
- This should be configurable in the Babel preset options. If the polyfill behaviour is disabled, templates shouldn’t be generated and inline styles should be used to set the variables instead.
Advantages
- A consistent prop-based way to use dynamic styles with components
- Variables don’t cascade anymore, and results are more predictable
- No overhead for browsers which already support CSS variables
Disadvantages
- Some overhead for browsers which don’t support CSS variables
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:6 (5 by maintainers)
Top Results From Across the Web
IE11 - does a polyfill / script exist for CSS variables?
I'm developing a webpage in a mixed web browser environment (Chrome/IE11). IE11 doesn't support CSS variables, is there a polyfill or script ...
Read more >A basic polyfill for CSS Variables/custom-properties - GitHub
This is an attempt at a very basic CSS variables (custom properties) polyfil. In reality this is more of a partial polyfill as...
Read more >css-vars-ponyfill - npm
Client-side support for CSS custom properties (aka "CSS variables") in legacy and modern browsers. Latest version: 2.4.8, last published: 5 ...
Read more >CSS Variables Polyfill - CodePen
According to caniuse.com, of current browsers only IE, Edge and Opera Mini do not support CSS variables. This polyfil appears to work on...
Read more >css-vars-ponyfill - GitHub Pages
css-vars-ponyfill - Client-side support for CSS custom properties (aka "CSS variables") in legacy ... This library is offered as a ponyfill, not a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Crazy experiment
This is just an experiment and I don’t want to add this to linaria.
Babel plugin which converts
styled-component
like syntax to linaria syntax:becomes:
plugin implementation:
We are not going to support this.