File tree Expand file tree Collapse file tree 2 files changed +58
-5
lines changed
packages/react/src/components/Toggletip Expand file tree Collapse file tree 2 files changed +58
-5
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { Information } from '@carbon/icons-react';
99import React , { useRef , useEffect } from 'react' ;
1010import { default as Button } from '../Button' ;
1111import { default as Link } from '../Link' ;
12+ import Modal from '../Modal' ;
1213import {
1314 ToggletipLabel ,
1415 Toggletip ,
@@ -154,3 +155,34 @@ Default.story = {
154155 ) ,
155156 ] ,
156157} ;
158+
159+ // I need to remove this
160+ export const OutsideClickTest = ( ) => {
161+ const [ open , setOpen ] = React . useState ( false ) ;
162+
163+ return (
164+ < div style = { { padding : '2rem' } } >
165+ < Button onClick = { ( ) => setOpen ( true ) } > Launch modal</ Button >
166+
167+ < Modal
168+ open = { open }
169+ onRequestClose = { ( ) => setOpen ( false ) }
170+ modalHeading = "Test Modal"
171+ primaryButtonText = "Close" >
172+ < p >
173+ Click the < strong > i</ strong > icon to open the Toggletip, then click
174+ anywhere in this modal body. The Toggletip should close.
175+ </ p >
176+
177+ < Toggletip align = "bottom" >
178+ < ToggletipButton label = "Show information" >
179+ < Information />
180+ </ ToggletipButton >
181+ < ToggletipContent >
182+ < p > Toggletip content</ p >
183+ </ ToggletipContent >
184+ </ Toggletip >
185+ </ Modal >
186+ </ div >
187+ ) ;
188+ } ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import React, {
1212 useContext ,
1313 useRef ,
1414 useState ,
15+ useEffect ,
1516 type ReactNode ,
1617 type ComponentProps ,
1718 type KeyboardEventHandler ,
@@ -173,11 +174,31 @@ export function Toggletip<E extends ElementType = 'span'>({
173174 }
174175 } ) ;
175176
176- useWindowEvent ( 'click' , ( { target } ) => {
177- if ( open && target instanceof Node && ! ref . current ?. contains ( target ) ) {
178- actions . close ( ) ;
179- }
180- } ) ;
177+ useEffect ( ( ) => {
178+ if ( ! ref . current ) return ;
179+
180+ const targetDocument = ref . current . ownerDocument || document ;
181+ const eventType : 'pointerdown' | 'mousedown' =
182+ 'PointerEvent' in window ? 'pointerdown' : 'mousedown' ;
183+
184+ const handleOutsideClick = ( event : MouseEvent | PointerEvent ) => {
185+ const node = event . target as Node | null ;
186+ if ( open && node && ! ref . current ! . contains ( node ) ) {
187+ setOpen ( false ) ;
188+ }
189+ } ;
190+
191+ const options = { capture : true } as const ;
192+
193+ targetDocument . addEventListener ( eventType , handleOutsideClick , options ) ;
194+ return ( ) => {
195+ targetDocument . removeEventListener (
196+ eventType ,
197+ handleOutsideClick ,
198+ options
199+ ) ;
200+ } ;
201+ } , [ open ] ) ;
181202
182203 return (
183204 < ToggletipContext . Provider value = { value } >
You can’t perform that action at this time.
0 commit comments