-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Add support for the css prop #172
Conversation
I'd personally vote for yes, for the following reasons:
What do you think? |
This comment has been minimized.
This comment has been minimized.
/cc @satya164 does this look somewhat legit? Maybe useful for babel-plugin-css-prop too
This comment has been minimized.
This comment has been minimized.
A couple of areas where the babel plugin deviates from Emotion's CSS prop (as far as I can tell, i'm looking at the readme rather than the code):
|
I'm not sure about this, it would essentially transpile down to const Bla = styled.div`
${['margin-left: 1px']}
` which I think would work? |
Personally, I really like having the That said, I'm not really a fan of syntax being too tightly coupled with build tools. It's similar to why I don't recommend using things like CSS Modules: because the syntax you write only works when you're using webpack and the CSS Modules loader. This makes the code a little less portable. For the But, that's just my two cents, and I generally think this would be a great addition 👍 |
This is great. Only thing it doesn't cover is the scenario where you both a) don't want to create an extra component and b) you want to apply the same CSS to multiple repeated elements. That's a scenario that emotion handles wells, with That's ok though, can't have everything! Might be possible to add later if there's enough demand. |
withComponent hasn’t been removed yet, or you can use the new “as” prop
…On Wed, Nov 7, 2018 at 2:10 PM johnnysprinkles ***@***.***> wrote:
This is great. Only thing it doesn't cover is the scenario where you both
a) don't want to create an extra component and b) you want to apply the
same CSS to multiple repeated elements. That's a scenario that emotion
handles wells, with css tagged template literal declared outside the JSX
and used as many places as you need.
That's ok though, can't have everything! Might be possible to add later if
there's enough demand.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#172 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiy1kGOulkO4gKgyqes4Pl2o7gAQJ-Gks5usz4qgaJpZM4Xs4DD>
.
|
@johnnysprinkles This doesn't work? const bla = css``;
<div css={bla}>
<span css={bla}> If not we should defo make that work, /cc @satya164 |
@mxstbr it should work. let's add a test for it |
Oh cool, I didn't see any mention of using it that way in the new docs at styled-components/styled-components-website@9b6008a Would the import in that case be |
Yep @johnnysprinkles! |
This doesn't correctly add the styled import. Even code as simple as this throws a "styled is undefined" error: // @flow
import React from 'react';
import Card from '../../shared/components/Card';
import config from '../../../config';
export default () => (
<div
css={`
width: 35em;
`}
>
<Card>
<h1>Login or Sign Up</h1>
<p>
<a href={config.API_URI + '/auth/google'}>
Sign up or login with Google
</a>
</p>
</Card>
</div>
); Any ideas why @satya164? |
I'll take a look asap |
Just hit another bug:
With this code: // @flow
import React from 'react';
import styled from 'styled-components';
import Card from '../../shared/components/Card';
import Link from '../../shared/components/Link';
import { Button } from '../../shared/components/Button';
import config from '../../../config';
export default () => (
<div
css={`
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
`}
>
<div
css={`
max-width: 35em;
width: 100%;
margin: 0 auto;
`}
>
<Card>
<h1 css="margin-top: 0;">Sign Up or Log In</h1>
<Button.Ghost
css="display: block;"
href={config.API_URI + '/auth/google'}
>
Authenticate with Google
</Button.Ghost>
<p css="margin-bottom: 0;">
Your information is private and secure. By signing up, you agree to the{' '}
<Link href="/terms">Terms of Service</Link> and{' '}
<Link href="/privacy">Privacy Policy</Link>.
</p>
</Card>
</div>
</div>
); Specifically, it seems to be caused by the |
Hi and thanks for the amazing work you've done so far. May i know if the If this is a new API for adding css to components, it will reduce increase the code quality to css around our main component. It will reduce the amount of template literal written around it which is good thing as code will look nicer. May I know how this will handle the Thanks in advance, this feature is very exciting, is there a target release date yet? |
The specification is: put whatever you want in the
See the test suite in this PR for more in-depth examples, but it handles props and variables like any other styled component, with the added benefit of static interpolations: const Bla = (props) => (
<div css={css`
border-radius: ${props.borderRadius};
color: ${p => p.color};
background: ${p => p.theme.background};
`}
color="blue"
/>
)
<Bla borderRadius="3px" /> I hope that makes sense! No target release date yet, but you can test this already if you |
Thanks for finding the bugs @mxstbr. I'll take look over the weekend! |
@satya164 last two commits should fix those issues, mind giving them a double check? 🙏 |
Note: Should change the prefix of the generated display name to "Styled" rather than "CSS". |
@mxstbr thanks a lot for the insight, we'll test it soon. I believe we can't use it yet for distributing a module right? What is the plugin exactly for? Does this require a plugin to be working or will styled be able to handle css props by itself? |
You totally can, it just might be a bit buggy!
It only requires the plugin at build-time, the users of the distributed module do not need to use the plugin. Great question! |
Co-Authored-By: mxstbr <contact@mxstbr.com>
Going to ship this as per internal discussions! |
if (t.isJSXMemberExpression(node)) { | ||
return `${getName(node.object, t)}.${node.property.name}` | ||
} | ||
throw path.buildCodeFrameError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! This function needs to accept path
as an argument (or be defined inside the plugin closure to access path)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you submit a PR? Thanks! 🙏 (and let's hope nobody hits this anytime soon... 😉)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will try to do it soon!
This integrates
babel-plugin-css-prop
by @satya164 (shoutout to him for being a genius) into our own Babel plugin, thus making thecss
prop work for all existing users!Click here to read more about how this works under the hood
The way this works is incredible, requires 0 changes to core. This is the crux:
I've also moved the entire test suite into this repo, check that out to see some of the edge cases (inc interpolations, dynamic interpolations, global interpolations,...) this covers
The big question is: do we want to have native support for the
css
prop?/cc the core team (@kitten @probablyup @imbhargav5) and interested onlookers (@AndrewIngram @jxnblk).
Todos before shipping:
Potential future optimisations, probably not necessary for v1:
<p css={{ color: 'blue' }} />
) to not use a dynamic prop<p css={css\
color: blue;`} />`) to not use a dynamic prop