@@ -10,13 +10,12 @@ import {
1010 FC ,
1111} from 'react'
1212import styled , { css } from 'styled-components'
13- import { useBoolean } from 'react-hooks-shareable'
13+ import { useBoolean , useClickOutside } from 'react-hooks-shareable'
1414
1515import { Typography , TypographyProps } from '../Typography'
1616import { PopOver , PopOverProps } from '../PopOver'
1717import { shape , spacing , componentSize } from '../designparams'
1818import { font } from '../theme'
19- import { useTouchScrollDistance } from './utils'
2019
2120/**
2221 * Tooltip
@@ -255,7 +254,8 @@ export const Tooltip: FC<TooltipProps | ExpandedTooltipProps> = ({
255254 const child = Children . only ( children ) as ReactElement
256255 const [ anchorEl , setAnchorEl ] = useState < HTMLElement | null > ( null )
257256 // State for click
258- const [ visibleByClick , showByClick ] = useState ( false )
257+ const [ visibleByClick , _showByClick , hideByClick , toggleByClick ] =
258+ useBoolean ( false )
259259 // Delayed state for pointer
260260 const [ visibleDelayed , showDelayed , hideDelayed ] = useBoolean ( false )
261261 // State for pointer
@@ -266,8 +266,6 @@ export const Tooltip: FC<TooltipProps | ExpandedTooltipProps> = ({
266266 // If tooltip should be shown
267267 const visible = visibleByClick || debouncedVisible
268268
269- const touchScrollDistance = useTouchScrollDistance ( )
270-
271269 const toggle = useCallback (
272270 ( event : PointerEvent ) => {
273271 // When using touch instead of mouse, we have to toggle the tooltip
@@ -276,24 +274,12 @@ export const Tooltip: FC<TooltipProps | ExpandedTooltipProps> = ({
276274 return
277275 }
278276
279- showByClick ( v => ! v )
277+ toggleByClick ( )
280278 } ,
281- [ showByClick ]
279+ [ toggleByClick ]
282280 )
283281
284- /**
285- * If the delta for any axis is larger than 150 pixels,
286- * remove the tooltip from the screen.
287- */
288- useLayoutEffect ( ( ) => {
289- if ( ! visible ) {
290- return
291- }
292- const { x, y } = touchScrollDistance
293- if ( Math . max ( Math . abs ( x ) , Math . abs ( y ) ) > 150 ) {
294- showByClick ( false )
295- }
296- } , [ touchScrollDistance ] )
282+ const handlePointerDown = useClickOutside ( hideByClick )
297283
298284 useEffect ( ( ) => {
299285 const delayVisible = ( ) => setDebouncedVisible ( visibleDelayed )
@@ -375,6 +361,7 @@ export const Tooltip: FC<TooltipProps | ExpandedTooltipProps> = ({
375361 < >
376362 { cloneElement ( child , {
377363 ref : setAnchorEl ,
364+ onPointerDown : handlePointerDown ,
378365 } ) }
379366 { visible ? (
380367 < PopOver anchorEl = { anchorEl } { ...alignments [ layout ] } { ...props } >
@@ -393,6 +380,7 @@ export const Tooltip: FC<TooltipProps | ExpandedTooltipProps> = ({
393380 < >
394381 { cloneElement ( child , {
395382 ref : setAnchorEl ,
383+ onPointerDown : handlePointerDown ,
396384 } ) }
397385 { visible ? (
398386 < >
0 commit comments