Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 7c3a669

Browse files
HitomiWindanielkaxis
authored andcommitted
fix(tooltip): hide tooltip when touched outside of a component
Fixes: #276
1 parent 89c9afd commit 7c3a669

File tree

3 files changed

+23
-37
lines changed

3 files changed

+23
-37
lines changed

packages/core/src/Tooltip/__snapshots__/index.test.tsx.snap

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ exports[`Tooltip Default 1`] = `
3333
className="c1"
3434
id="layer-1"
3535
>
36-
<span>
36+
<span
37+
onPointerDown={[Function]}
38+
>
3739
Test
3840
</span>
3941
</div>
@@ -84,7 +86,9 @@ exports[`Tooltip Expanded 1`] = `
8486
className="c1"
8587
id="layer-3"
8688
>
87-
<span>
89+
<span
90+
onPointerDown={[Function]}
91+
>
8892
Test
8993
</span>
9094
</div>
@@ -135,7 +139,9 @@ exports[`Tooltip Expanded 2`] = `
135139
className="c1"
136140
id="layer-5"
137141
>
138-
<span>
142+
<span
143+
onPointerDown={[Function]}
144+
>
139145
Test
140146
</span>
141147
</div>
@@ -186,7 +192,9 @@ exports[`Tooltip Expanded 3`] = `
186192
className="c1"
187193
id="layer-7"
188194
>
189-
<span>
195+
<span
196+
onPointerDown={[Function]}
197+
>
190198
Test
191199
</span>
192200
</div>

packages/core/src/Tooltip/index.tsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import {
1010
FC,
1111
} from 'react'
1212
import styled, { css } from 'styled-components'
13-
import { useBoolean } from 'react-hooks-shareable'
13+
import { useBoolean, useClickOutside } from 'react-hooks-shareable'
1414

1515
import { Typography, TypographyProps } from '../Typography'
1616
import { PopOver, PopOverProps } from '../PopOver'
1717
import { shape, spacing, componentSize } from '../designparams'
1818
import { 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
<>

packages/ui-tests/src/coreComponents/Tooltip.spec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,15 @@ context('Tooltip mobile device', { testIsolation: false }, () => {
8585
cy.get(`[data-cy=${data.tooltipDataCy}]`).should('not.exist')
8686
})
8787

88-
it(`Tooltip ${data.tooltipDataCy} should hide when client touch move more than 150 pixels`, () => {
88+
it(`Tooltip ${data.tooltipDataCy} should hide when client touch the screen again`, () => {
8989
// Touch to show tooltip
9090
cy.get(`[data-cy=${data.textDataCy}]`).trigger('pointerdown')
9191
cy.get(`[data-cy=${data.tooltipDataCy}]`)
9292
.should('exist')
9393
.should('be.visible')
9494

95-
// Touch move 151 pixels and hide tooltip
96-
cy.get(`[data-cy=${data.textDataCy}]`)
97-
.parent()
98-
.trigger('touchstart', {
99-
touches: [{ clientX: 0, clientY: 0, identifier: 0 }],
100-
})
101-
102-
cy.get(`[data-cy=${data.textDataCy}]`)
103-
.parent()
104-
.trigger('touchmove', {
105-
changedTouches: [{ clientX: 151, clientY: 151, identifier: 0 }],
106-
})
95+
// Touch the screen again
96+
cy.get(`[data-cy=${data.textDataCy}]`).parent().trigger('pointerdown')
10797

10898
// Tooltip is not shown
10999
cy.get(`[data-cy=${data.tooltipDataCy}]`).should('not.exist')

0 commit comments

Comments
 (0)