@@ -349,5 +349,68 @@ storiesOf('Portals', module)
349349 </ div > ;
350350 }
351351
352+ return < MyComponent componentToShow = 'component-a' />
353+ } ) . add ( 'Events bubbling from PortalOut' , ( ) => {
354+ const MyExpensiveComponent = ( ) => < div onClick = { ( ) => console . log ( 'expensive' ) } > expensive!</ div > ;
355+
356+ const MyComponent = ( props ) => {
357+ const portalNode = React . useMemo ( ( ) => createHtmlPortalNode ( ) , [ ] ) ;
358+
359+ return < div >
360+ { /*
361+ Create the content that you want to move around.
362+ InPortals render as normal, but to detached DOM.
363+ Until this is used MyExpensiveComponent will not
364+ appear anywhere in the page.
365+ */ }
366+ < InPortal node = { portalNode } >
367+ < MyExpensiveComponent
368+ // Optionally provide props to use before this enters the DOM
369+ myProp = { "defaultValue" }
370+ />
371+ </ InPortal >
372+
373+ { /* ... The rest of your UI ... */ }
374+
375+ { /* Pass the node to whoever might want to show it: */ }
376+ { props . componentToShow === 'component-a'
377+ ? < ComponentA portalNode = { portalNode } />
378+ : < ComponentB portalNode = { portalNode } /> }
379+ </ div > ;
380+ }
381+
382+ const ComponentA = ( props ) => {
383+ const alertEvent = ( ) => alert ( 'Div clicked' )
384+ return < div onClick = { alertEvent } >
385+ { /* ... Some more UI ... */ }
386+
387+ A:
388+
389+ < OutPortal
390+ node = { props . portalNode } // Show the content from this node here
391+ />
392+ </ div > ;
393+ }
394+
395+ const ComponentB = ( props ) => {
396+ const alertEvent = ( ) => alert ( 'Div clicked' )
397+ return < div onClick = { alertEvent } >
398+ { /* ... Some more UI ... */ }
399+
400+ B:
401+
402+ < OutPortal
403+ node = { props . portalNode } // Pull in the content from this node
404+
405+ myProp = { "newValue" } // Optionally, override default values
406+ myOtherProp = { 123 } // Or add new props
407+
408+ // These props go back to the InPortal, and trigger a component
409+ // update (but on the same component instance) as if they had
410+ // been passed directly.
411+ />
412+ </ div > ;
413+ }
414+
352415 return < MyComponent componentToShow = 'component-a' />
353416 } ) ;
0 commit comments