Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

#26 - Fix style prop #27

Merged
merged 5 commits into from
Jul 12, 2016
Merged

#26 - Fix style prop #27

merged 5 commits into from
Jul 12, 2016

Conversation

treshugart
Copy link
Contributor

Fixes #26.

@@ -55,6 +55,8 @@ export default function (CustomElement, opts) {

if (name.indexOf('on') === 0) {
syncEvent(node, name.substring(2), props[name]);
} else if (name === 'style') {
node.setAttribute('style', props[name]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, so React transforms style={{ display: 'block' }} to CSS?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care about ref and className, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradleyayers it seems so.

@stevemao className should work since we're passing it as a property, right? I'll write a test for that. ref should be a separate issue, I think. Can you raise it please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevemao I added a test for className to ensure that works.

@bradleyayers it seems React actually doesn't do that. I was getting a false positive. Instead I decided to just pass-through style but we needed to make sure it was being passed as the attrs argument to React.createElement() for the test to pass.

Copy link
Member

@bradleyayers bradleyayers Jul 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@treshugart so what does React actually do with a style object — how is that applied to the DOM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradleyayers Discussed IRL, but for posterity:

  • React enforces you pass a style object that maps to a CSSStyleDeclaration.
  • We use willReceiveProps() to set properties on the element. In this lifecycle callback, style is passed in as an object.
  • We must pass the style prop to React.createElement() as attributes in order for React to apply its special behaviour to it (i.e. converting to a string and numbers have px appended to them.

@joscha
Copy link
Member

joscha commented Jul 11, 2016

LGTM

… false positive.

Reordered tests so that built-in React props come first.

#26
@@ -61,7 +62,7 @@ export default function (CustomElement, opts) {
});
}
render() {
return React.createElement(tagName, null, this.props.children);
return React.createElement(tagName, { style: this.props.style }, this.props.children);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about {...this.props}?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be transpiled to a call that contains Array.from. One of our mobile browsers we need to support doesnt know that yet if I remember correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joscha that's for objects.

@stevemao It's still in TC39 stage 2. I'll add a test since it's common practice in React, but I'll note that there's no special behaviour necessary for this so we're essentially testing Babel here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@treshugart Yup sorry
What about {this.props}. Can we just pass all props down without explicitly listing everything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you can just do <Element {...this.props} /> and everything will get set through our willReceiveProps() callback (which is what we use to ensure props are set as properties on the web component).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean here instead of

return React.createElement(tagName, { style: this.props.style }, this.props.children);

Is there any downside to just simply

return React.createElement(tagName, { this.props }, this.props.children);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll set attributes instead of properties.

@treshugart
Copy link
Contributor Author

lgtm

@treshugart treshugart merged commit 44b867f into master Jul 12, 2016
@treshugart treshugart deleted the 26 branch July 12, 2016 02:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants