question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API Consequences for Static CSS Extraction

See original GitHub issue

Phil’s excellent work on the babel plugin means that we can extract all non-dynamic styles into a static CSS stylesheet.

However, we need to think about the effects this has on the project. Take this code,

const Button = styled.button`
  background: ${props => props.primary ? 'palevioletred' : 'white'};
  ${props => props.large && css`
    font-size: 2em;
  `}
`

While it is theoretically possible for this use-case to extracted into static CSS, we don’t currently have this optimisation. Further more, we won’t be able to optimise every case.

We can work around this.

const Button = styled.button`
  background: white;

  &.primary {
    background: palevioletred;
  }

  &.large {
    font-size: 2em;
  }
`

<Button className="primary large">It mostly works like before</Button>

This can now be completely statically extracted.

However, this is no longer idiomatic styled-components. But we can guarantee people will do this for the sake of optimisation.

We need to continue static style extraction. But we also need to consider what we can change to have an idiomatic styled-components that better supports CSS extraction. Even if that changes what ‘idiomatic styled-components’ is.

Pinging @mxstbr @geelen @philpl

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kittencommented, Apr 23, 2017

@jacobp100 it requires more effort, but it should be possible. Mixins are not as simple though, since they might be external. This means that we’d need an internal method that adds more classnames to a styled component and need to generate our own ones for them.

Even then, I believe that we can enhance this as we go

0reactions
jacobp100commented, Apr 23, 2017

When interpolations are used

That’s sort of what I’m getting at. Interpolations are used for mixins, variables, passing arbitrary props, conditionally applying styles, and more. Can we do any of these in a way that can be statically analysed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Roll Your Own CSS-in-JS Library (4) - Static Extraction - yyjhao
In order to statically generate the css styles, the extractor needs to look at at least one other file called “helpers”.
Read more >
A Thorough Analysis of CSS-in-JS
A consequence of using CSS classes instead of inline styles is that ... Then, extracting static .css files may probably be a better...
Read more >
Why We're Breaking Up with CSS-in-JS - DEV Community ‍ ‍
CSS -in-JS clutters the React DevTools. Static extracted styles will replace your styles with classNames and/or CSS variables. No clutter (not ...
Read more >
The evolution of scalable CSS, Part 7: CSS-in-JS
Trivial variables sharing between styles and JS code;; Effortless code navigation using editor tooling;; Static CSS extraction as regular .css ...
Read more >
Lessons Learned: Emotion Library Maintainer Explains Why ...
When using runtime CSS-in-JS libraries, developers define the style of components together with the component markup and logic. It becomes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found