@@ -349,5 +349,49 @@ storiesOf('Portals', module)
349
349
</ div > ;
350
350
}
351
351
352
+ return < MyComponent componentToShow = 'component-a' />
353
+ } ) . add ( 'Events bubbling from PortalOut' , ( ) => {
354
+ const MyExpensiveComponent = ( ) => < div onMouseDown = { ( ) => console . log ( 'expensive' ) } > expensive!</ div > ;
355
+
356
+ const MyComponent = ( ) => {
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
+ < ComponentA portalNode = { portalNode } />
377
+ </ div > ;
378
+ }
379
+
380
+ const ComponentA = ( props ) => {
381
+ return < div
382
+ onClick = { ( ) => alert ( 'Div clicked' ) }
383
+ onMouseDown = { ( ) => console . log ( 'Mouse Down' ) }
384
+ onMouseEnter = { ( ) => console . log ( 'Mouse enter' ) }
385
+ >
386
+ { /* ... Some more UI ... */ }
387
+
388
+ A:
389
+
390
+ < OutPortal
391
+ node = { props . portalNode } // Show the content from this node here
392
+ />
393
+ </ div > ;
394
+ }
395
+
352
396
return < MyComponent componentToShow = 'component-a' />
353
397
} ) ;
0 commit comments