Global styles component
See original GitHub issueAPI
We will standardize on the @emotion/react API:
import { Global } from '@compiled/react';
<Global
styles={{
h1: { fontSize: 30 }
}}
/>
Assumptions
- Dynamic declarations can’t work with global styles
- Global styles will be extracted to an external stylesheet
Outstanding questions
- Do we force
@font-facedeclarations to go through this component? Currently they can’t be defined anywhere because it will blow up in prod mode - How can we conditionally set global styles when they have been extracted to an external stylesheet? If there is no good story for this we may need to just not support this when baked as well.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:5
Top Results From Across the Web
How to Create Global Styles with Styled Components
The first step is to create a file that contains all your global styles. Inside your src/ folder, add a file called globalStyles.js...
Read more >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 ...
Read more >How To Create Global Styles With Styled Components
Step 1: Create a global styles file · { createGlobalStyle } from 'styled-components' · export default createGlobalStyle` · body { · margin: 0;....
Read more >Styled Components - Creating Global Styles - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >Global Styles - Emotion
Sometimes you might want to insert global css like resets or font faces. You can use the Global component to do this. It...
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 Free
Top 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

I worked on a spike here for the global component https://github.com/atlassian-labs/compiled/pull/462 a while back. There are a few things that make it difficult because we want to be able to extract everything to an external style sheet.
If we want to be able to extract all CSS, how can we fundamentally support conditional global CSS? Aka, mount a component, styles are applied, unmount a component, styles are removed.
The solution is obvious if its purely a runtime one. It is complicated by extracting. Even more so that we want to support a runtime to extracted story over the lifecycle of compiled components. Fundamentally the answer could simply be - don’t have conditional global styles! Without thinking on names:
PermanentGlobalStylescomponent - for adding “always on” global stylesBrowserBodyStylescomponent - for adding styles that can be “conditional” that only works on the browser, which adds it to the body elementBut the downside is
PermanentGlobalStylesgoes against regular idiomatic React concepts. You could “unmount” the component and the styles wouldn’t be removed. And it’s even more problematic when you consider third party packages from NPM could set their own global styles, and they aren’t ever removed! Oh no.One thing we could have in our tool belt is the conditionally render style
linkelements, perhaps? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/linkdisabledattribute. However seemingly deprecated/not used now, so let’s not.What if we used
mediaattributes instead? Or re-write the CSS to only apply given certain media queries or selectors. We’d want something that doesn’t affect specificity though.The downside would be after extracting we’d need applications to have a provider or something to render these links when appropriate, as well as support SSR story. Realistically we’d need that anyway for critical CSS.
Assuming we can still support the conditional CSS once extracted, we’d probably aim to have two style sheets:
This is something that Compiled won’t currently be supporting. Closing to clean up the issue list.