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.

Component-based API for global styling

See original GitHub issue

Via https://github.com/styled-components/styled-components/issues/730#issuecomment-320035959

It would be great to have a React component for global styles that does the actual injecting (rendering null), removing the CSS when it is unmounted.

So instead of

    injectGlobal`...`

you’d do

const GlobalStyle = styled.global`
… (including prop interpolation)
`
...
return (
  <App>
    <GlobalStyle darkBg={this.props.dark} />
    ...
  </App>
)

The only differences with regular styled components would be:

  • Renders null
  • Does not wrap the CSS with an own class

This way:

  • Global styles can be easily modified via props
  • It works without issues in SSR
  • It’s obvious what is happening

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:42
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

6reactions
Ky6ukcommented, Sep 18, 2018

@imbhargav5 after latest v3 update it just forces me with severity “error” to use createGlobalStyle, but I am not sure it’s possible to do in v3.

I want to say some kind of deprecations should provide a way to switch on the new feature and to remove that deprecation warning. But currently the deprecation warning doesn’t provide any alternatives to do that without upgrading to v4.

5reactions
probablyupcommented, Sep 18, 2018

Yeah… I think I’m just going to take it out. After more thought it doesn’t make sense to put in a non-actionable warning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference - styled-components
A helper function to generate a special StyledComponent that handles global styles. Normally, styled components are automatically scoped to a local CSS class ......
Read more >
Styled System
Styled System is a collection of utility functions that add style props to your React components and allows you to control styles based...
Read more >
Styled Components: Styling for component based application
Here in this piece of code we created a simple animation Loader component using keyframes and psuedo-classes. Styled-component export a keyframes helper ...
Read more >
Styles API | Mantine
You can also use Styles API in MantineProvider with styles prop. All styles defined there will be added to each component rendered inside...
Read more >
Styling-API Reinvented - Zalando Engineering Blog
This important step in CSS evolution was really necessary for UI components to be used with more confidence. No more global scope causing...
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