css prop signature and css style signature is inconsistent when theme is used
See original GitHub issueCurrent behavior: If you define styles which expect the theme property, it can not be shared for css prop and styled components.
const colorStyles = ({ theme }) => css`
color: ${theme.colors.primary};
`;
const Title = styled.h1`
${colorStyles};
font-size: 30px;
`;
If you try to apply the color styles defined above to the css prop.
<h2 css={colorStyles}>Start editing to see some magic happen!</h2>
An error will be thrown.
Cannot read property 'colors' of undefined
To fix that, you have to do
<h2 css={theme => colorStyles({theme})}>Start editing to see some magic happen!</h2>
The reason is because the inconsistent signature.
const colorStylesToBeUsedInStyledComponents = ({ theme }) => css`
color: ${theme.colors.primary};
`;
const colorStylesToBeUsedInCssProp = theme => css`
color: ${theme.colors.primary};
`;
In styled-components they are consistent.
To reproduce:
https://codesandbox.io/s/emotion-issue-p6qhf
Expected behavior:
Styles depending on the theme should be able to be shared regardless where they are used. The inconsistency is causing confusion.
Environment information:
react
version: 16.8.6emotion
version: 10.0.15
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (4 by maintainers)
Top Results From Across the Web
css - HTML email signature rendering differently/incorrectly on ...
The main problem occurring is that between different email clients (Gmail, iOS Mail, etc) the text color changes to black sometimes, and is...
Read more >Inconsistent expansion of `&` · Issue #3265 · styled ... - GitHub
TL;DR: & sometimes expands to the static component class name, sometimes to the dynamic props-based class name.
Read more >Best format for email signatures
Another common error is that the signature text (especially where the disclaimer is placed) appears overlapped. This is because of wrongly used ...
Read more >What's the best way to make an email signature for Office 365?
Usually the safest way to make things appear consistent in email is to pretend it's still the 90s and arrange things in tables,...
Read more >Signature Font Options - CodePen
URL Extension Required. When linking another Pen as a resource, make sure you use a URL Extension of the type of code you...
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
I believe that @mitchellhamilton just meant that
theme
for a css prop would have to be, somewhat artificially, wrapped in an object (if we would implement this). So internally instead of roughly doing this:we’d have to do this
The given example is just impossible to implement.
css
prop lacks the context of ColorCard props, it’s completely unaware of them. It could potentially be aware of thatdiv
’s props but it’s not something that we want to support because often you are usingcss
prop on host elements (likediv
) but your “styling props” are not what you actually want to pass onto that div (it would show up in the DOM and React would warn you against that). We could filter them out like in case of the styled API but we refuse to do this because it forces us to maintain a props whitelist and this is something thatcss
prop was designed to avoid.Your example can be rewritten easily with just minor changes to:
@Andarist thanks for explaining! I thought css prop and styled components are two interchangeable ways to work with predefined/imported styles. Now I realize they are designed with a little difference and constraints. Good to know!