Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Tooltip): Tooltip trigger外に移動する際にtooltipを非表示にする #5219

Closed
wants to merge 9 commits into from
27 changes: 26 additions & 1 deletion packages/smarthr-ui/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
ReactElement,
ReactNode,
useCallback,
useEffect,
useId,
useMemo,
useRef,
Expand Down Expand Up @@ -110,6 +111,13 @@ export const Tooltip: FC<Props & ElementProps> = ({
return
}

setRect(ref.current.getBoundingClientRect())

// Tooltipのtriggerの他の要素(Dropwdown menu buttonで開いたmenu contentとか)に移動されたらtooltipを表示しない
if (!ref.current?.contains(document.activeElement)) {
return
}

if (ellipsisOnly) {
const outerWidth = parseInt(
window
Expand All @@ -125,7 +133,6 @@ export const Tooltip: FC<Props & ElementProps> = ({
}
}

setRect(ref.current.getBoundingClientRect())
setIsVisible(true)
},
[ellipsisOnly],
Expand Down Expand Up @@ -155,6 +162,24 @@ export const Tooltip: FC<Props & ElementProps> = ({
[children, isInnerTarget, messageId],
)

// Tooltipのtriggerの他の要素(Dropwdown menu buttonで開いたmenu contentとか)にpointerで移動されたときのtooltipの表示・非表示の処理
useEffect(() => {
const pointerHandler = (e: PointerEvent) => {
if (!(e.target instanceof HTMLElement) || !ref.current) return

if (!ref.current.contains(document.activeElement)) {
setIsVisible(false)
}

if (ref.current.contains(e.target)) {
setIsVisible(true)
}
}

document.addEventListener('pointerenter', pointerHandler, true)
return () => document.removeEventListener('pointerenter', pointerHandler, true)
}, [])

return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions,smarthr/a11y-delegate-element-has-role-presentation
<span
Expand Down
Loading