From cae9bf93c4e04d0e7e12b14235134709dc2e7945 Mon Sep 17 00:00:00 2001 From: Alessandro Pagiaro Date: Thu, 3 Mar 2022 18:30:34 +0100 Subject: [PATCH] Test onClick --- src/index.tsx | 25 +++++++++++----- stories/html.stories.js | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 4035f69..b99ce4c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -144,6 +144,7 @@ class InPortal extends React.PureComponent { this.state = { nodeProps: this.props.node.getInitialPortalProps(), }; + this.onClick = this.onClick.bind(this); } addPropsChannel = () => { @@ -163,16 +164,26 @@ class InPortal extends React.PureComponent { this.addPropsChannel(); } + onClick(e:any){ + e.stopPropagation(); + console.log('click outer', this.props.node.element); + this.props.node.element.dispatchEvent(new Event('click', { bubbles: true })); + } + render() { const { children, node } = this.props; - return ReactDOM.createPortal( - React.Children.map(children, (child) => { - if (!React.isValidElement(child)) return child; - return React.cloneElement(child, this.state.nodeProps) - }), - node.element - ); + return
+ { + ReactDOM.createPortal( + React.Children.map(children, (child) => { + if (!React.isValidElement(child)) return child; + return React.cloneElement(child, this.state.nodeProps) + }), + node.element + ) + } +
} } diff --git a/stories/html.stories.js b/stories/html.stories.js index 1bf2986..3bc735f 100644 --- a/stories/html.stories.js +++ b/stories/html.stories.js @@ -349,5 +349,68 @@ storiesOf('Portals', module) ; } + return + }).add('Events bubbling from PortalOut', () => { + const MyExpensiveComponent = () =>
console.log('expensive')}>expensive!
; + + const MyComponent = (props) => { + const portalNode = React.useMemo(() => createHtmlPortalNode(), []); + + return
+ {/* + Create the content that you want to move around. + InPortals render as normal, but to detached DOM. + Until this is used MyExpensiveComponent will not + appear anywhere in the page. + */} + + + + + {/* ... The rest of your UI ... */} + + {/* Pass the node to whoever might want to show it: */} + { props.componentToShow === 'component-a' + ? + : } +
; + } + + const ComponentA = (props) => { + const alertEvent = () => alert('Div clicked') + return
+ {/* ... Some more UI ... */} + + A: + + +
; + } + + const ComponentB = (props) => { + const alertEvent = () => alert('Div clicked') + return
+ {/* ... Some more UI ... */} + + B: + + +
; + } + return });