-
Notifications
You must be signed in to change notification settings - Fork 67
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
cloneElement support #1729
base: master
Are you sure you want to change the base?
cloneElement support #1729
Conversation
|
✅ Deploy Preview for compiled-css-in-js canceled.
|
}, | ||
}); | ||
|
||
// // Second pass to replace all usages of `style`. |
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.
TODO, makes sense to hook the style
prop up as well
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.
Does anything else ever use style
in Babel? style={css(…)}
isn't valid, right?
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.
I think the pass over the style element is intended for outputting CSS vars 🤔 or something of that nature.
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.
That's only when it's in runtime mode, but yeah, maybe right…really glad if you can learn this part of it because I have no clue how extraction or runtime actually work 🤣
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.
Neither hahah
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.
Without pulling it up locally, I'm not of much help reviewing the AST, but great like spike into Compiled, I hope it's not too far off and they'll like—it will drastically reduce some technical debt as the alternative is using <ClassNames>{({ css }) => cloneElement(…, { className: ax([css({…}), props.className]) })}</ClassNames>
or something.
import { css } from '@compiled/react'; | ||
|
||
const MyDiv = ({ children }) => { | ||
return cloneElement(children, { css: css({ fontSize: 12 }) }); |
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.
This is would be the full example of what we need this for—do you think it would work?
const styles = css({ fontSize: 12 });
const MyDiv = ({ children, className }) => {
return cloneElement(children, { css: cx(styles, props.className) });
}
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.
The other question I've got, probably for Compiled, is should this be props.css
or props.className
exclusively?
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.
I had the same question.
I'll add a test case for the example you provided! It should just work because that part of the algorithm gets handed back to compiled
}, | ||
}); | ||
|
||
// // Second pass to replace all usages of `style`. |
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.
Does anything else ever use style
in Babel? style={css(…)}
isn't valid, right?
// TODO: This is a temporary fix to prevent the plugin from crashing when the second argument of cloneElement is not an object expression. | ||
if (!t.isObjectExpression(props)) { | ||
throw new Error('Second argument of cloneElement must be an object expression.'); | ||
} |
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.
If someone is using a variable/spread/etc it'll probably be impossible to reliably fish out and modify the CSS prop.
Are there any standard approaches to this problem in use in the repo already? Do we throw/Log an err/try really hard to locate the variable?
@@ -295,6 +296,19 @@ export default declare<State>((api) => { | |||
path: NodePath<t.TaggedTemplateExpression> | NodePath<t.CallExpression>, | |||
state: State | |||
) { | |||
if ( |
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.
should this check that cloneElement
is imported from react
too?
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.
Yes definitely and also should check for aliased imports
import { cloneElement as foo } from 'react';
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.
Edit: I added support for alias and member expressions in the latest commit
a0a9506
to
b6b9736
Compare
What is this change?
Compiled now treats
react.cloneElement
as a stylable element.this can be extended to support
react.createElement
fairly easily.Why are we making this change?
To allow cases where a CSS prop is used like so:
How are we making this change?
cloneElement
CallExpression
css
propclassName
This might not be a direction the library wants to go in, which is understandable.
Alternatively, we could address this via
<ClassNames>
however Compiled currently does not pick up non-jsx elements as children of the<ClassNames>
API.PR checklist
Don't delete me!
I have...
website/