Skip to content

Commit

Permalink
Merge branch 'sompylasar-receive-classname-style-props'
Browse files Browse the repository at this point in the history
* sompylasar-receive-classname-style-props:
  Update `className` and `style` when new props arrive
  • Loading branch information
tajo committed Jun 16, 2016
2 parents a4725ea + 1de69e3 commit 8baf031
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
30 changes: 19 additions & 11 deletions lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,29 @@ export default class Portal extends React.Component {
}
}

applyClassNameAndStyle(props) {
if (props.className) {
this.node.className = props.className;
}
if (props.style) {
// React 15.1.0+ requires third parameter in debug mode
/* eslint-disable no-underscore-dangle */
CSSPropertyOperations.setValueForStyles(this.node,
props.style,
this._reactInternalInstance);
/* eslint-enable no-underscore-dangle */
}
}

renderPortal(props) {
if (!this.node) {
this.node = document.createElement('div');
if (props.className) {
this.node.className = props.className;
}
if (props.style) {
// React 15.1.0+ requires third parameter in debug mode
/* eslint-disable no-underscore-dangle */
CSSPropertyOperations.setValueForStyles(this.node,
props.style,
this._reactInternalInstance);
/* eslint-enable no-underscore-dangle */
}
// apply CSS before the node is added to the DOM to avoid needless reflows
this.applyClassNameAndStyle(props);
document.body.appendChild(this.node);
} else {
// update CSS when new props arrive
this.applyClassNameAndStyle(props);
}
this.portal = ReactDOM.unstable_renderSubtreeIntoContainer(
this,
Expand Down
14 changes: 13 additions & 1 deletion test/portal_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@ describe('react-portal', () => {
assert.equal(document.body.lastElementChild.className, 'some-class');
});

it('should add inline style to portal\'s wrapping node', () => {
it('should add inline style to the portal\'s wrapping node', () => {
mount(<Portal isOpened style={{ color: 'blue' }}><p>Hi</p></Portal>);
assert.equal(document.body.lastElementChild.style.color, 'blue');
});

it('should update className on the portal\'s wrapping node when props.className changes', () => {
const wrapper = mount(<Portal className="some-class" isOpened><p>Hi</p></Portal>);
wrapper.setProps({ className: 'some-other-class', children: <p>Hi</p> });
assert.equal(document.body.lastElementChild.className, 'some-other-class');
});

it('should update inline style on the portal\'s wrapping node when props.style changes', () => {
const wrapper = mount(<Portal isOpened style={{ color: 'blue' }}><p>Hi</p></Portal>);
wrapper.setProps({ style: { color: 'red' }, children: <p>Hi</p> });
assert.equal(document.body.lastElementChild.style.color, 'red');
});

describe('callbacks', () => {
it('should call props.beforeClose() if passed when calling Portal.closePortal()', () => {
const props = { isOpened: true, beforeClose: spy() };
Expand Down

0 comments on commit 8baf031

Please sign in to comment.