Skip to content

Commit

Permalink
tooltip: attach global move handler only if trigger element is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
taifen committed Dec 24, 2020
1 parent fa92787 commit 0e7a14b
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/tooltip/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ function useTooltip<T extends HTMLElement>({
onFocus,
onBlur,
onKeyDown,
disabled,
ref: forwardedRef,
DEBUG_STYLE,
}: {
ref?: React.Ref<any>;
disabled?: boolean;
DEBUG_STYLE?: boolean;
} & React.HTMLAttributes<T> = {}): [TriggerParams, TooltipParams, boolean] {
let id = String(useId(idProp));
Expand Down Expand Up @@ -296,14 +298,12 @@ function useTooltip<T extends HTMLElement>({
Safari fires `pointerenter` but does not fire `pointerleave`
and `onPointerEventLeave` added to the trigger element will not work
*/

if (!("PointerEvent" in window)) return;
if (!("PointerEvent" in window) || !disabled) return;

let ownerDocument = getOwnerDocument(ownRef.current)!;

function listener(event: MouseEvent) {
// @ts-ignore `disabled` does not exist on HTMLDivElement but tooltips can be used with different elements
if (state !== VISIBLE || !ownRef.current?.disabled) return;
if (state !== VISIBLE) return;

if (
event.target instanceof Element &&
Expand All @@ -319,7 +319,7 @@ function useTooltip<T extends HTMLElement>({

ownerDocument.addEventListener("mousemove", listener);
return () => ownerDocument.removeEventListener("mousemove", listener);
}, []);
}, [disabled]);

function wrapMouseEvent<EventType extends React.SyntheticEvent | Event>(
theirHandler: ((event: EventType) => any) | undefined,
Expand Down Expand Up @@ -462,6 +462,7 @@ const Tooltip = forwardRefWithAs<TooltipProps, "div">(function (
onFocus: child.props.onFocus,
onBlur: child.props.onBlur,
onKeyDown: child.props.onKeyDown,
disabled: child.props.disabled,
ref: child.ref,
DEBUG_STYLE,
});
Expand Down

0 comments on commit 0e7a14b

Please sign in to comment.