Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

} else {
node[name] = props[name];
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ describe('props', () => {
ReactDOM.render(<Comp test="test" />, window.fixture);
});

it('should set style as an attribute (object)', () => {
const Comp = createComponent('style', elem => expect(elem.style.display).to.equal('block'));
ReactDOM.render(<Comp style={{ display: 'block' }} />, window.fixture);
});

it('should set style as an attribute (string)', () => {
const Comp = createComponent('style', elem => expect(elem.style.display).to.equal('block'));
ReactDOM.render(<Comp style="style: block" />, window.fixture);
});

it('should set properties for anything else', () => {
const Comp = createComponent('test', (elem, value) => expect(value).to.equal('test'));
ReactDOM.render(<Comp test="test" />, window.fixture);
Expand Down