From 0dca9729bad82a1a1de7eb099624caa02fb4e7f0 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 31 Aug 2016 10:39:25 -0400 Subject: [PATCH] =?UTF-8?q?Bridge=20context=20into=20subtree.=20Fixes=20#1?= =?UTF-8?q?=20=F0=9F=8C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/preact-portal.js | 7 ++++++- test/preact-portal.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/preact-portal.js b/src/preact-portal.js index a31efae..3ec9258 100644 --- a/src/preact-portal.js +++ b/src/preact-portal.js @@ -38,7 +38,9 @@ export default class Portal extends Component { } this.remote = render(( - { show && this.props.children || null } + + { show && this.props.children || null } + ), this.into, this.remote); } } @@ -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; } diff --git a/test/preact-portal.js b/test/preact-portal.js index bb4c432..0cdcc71 100644 --- a/test/preact-portal.js +++ b/test/preact-portal.js @@ -36,4 +36,37 @@ describe('preact-portal', () => { expect(foo).to.have.property('innerHTML', '
hello
'); }); + + 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(
); + + let foo = document.createElement('div'); + foo.setAttribute('id', 'foo'); + scratch.appendChild(foo); + + let base = document.createElement('div'); + scratch.appendChild(base); + + render(( + + + + + + ), base); + + expect(Child) + .to.have.been.calledOnce + .and.calledWith({}, ctx); + }); });