Skip to content

Commit

Permalink
Bridge context into subtree. Fixes #1 🌈
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Aug 31, 2016
1 parent 7a7648b commit 0dca972
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/preact-portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export default class Portal extends Component {
}

this.remote = render((
<PortalProxy>{ show && this.props.children || null }</PortalProxy>
<PortalProxy context={this.context}>
{ show && this.props.children || null }
</PortalProxy>
), this.into, this.remote);
}
}
Expand All @@ -47,6 +49,9 @@ export default class Portal extends Component {
// high-order component that renders its first child if it exists.
// used as a conditional rendering proxy.
class PortalProxy extends Component {
getChildContext() {
return this.props.context;
}
render({ children }) {
return children && children[0] || null;
}
Expand Down
33 changes: 33 additions & 0 deletions test/preact-portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,37 @@ describe('preact-portal', () => {

expect(foo).to.have.property('innerHTML', '<div>hello</div>');
});

it('should preserve context', () => {
let ctx = { foo:'bar', baz:'bat' };
class Provider {
getChildContext() {
return ctx;
}
render({ children }) {
return children[0];
}
}

const Child = sinon.stub().returns(<div />);

let foo = document.createElement('div');
foo.setAttribute('id', 'foo');
scratch.appendChild(foo);

let base = document.createElement('div');
scratch.appendChild(base);

render((
<Provider>
<Portal into="#foo">
<Child />
</Portal>
</Provider>
), base);

expect(Child)
.to.have.been.calledOnce
.and.calledWith({}, ctx);
});
});

0 comments on commit 0dca972

Please sign in to comment.