2021-01-11 21:43:21 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-11-19 18:11:35 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-01-11 21:43:21 +00:00
|
|
|
import classNames from 'classnames';
|
2020-12-07 20:43:19 +00:00
|
|
|
import { noop } from 'lodash';
|
2020-11-19 18:11:35 +00:00
|
|
|
import { Manager, Reference, Popper } from 'react-popper';
|
2020-12-04 23:03:01 +00:00
|
|
|
import { Theme, themeClassName } from '../util/theme';
|
2021-03-08 22:31:19 +00:00
|
|
|
import { multiRef } from '../util/multiRef';
|
2021-08-03 17:04:49 +00:00
|
|
|
import { offsetDistanceModifier } from '../util/popperUtil';
|
2020-11-19 18:11:35 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type EventWrapperPropsType = {
|
2020-12-07 20:43:19 +00:00
|
|
|
children: React.ReactNode;
|
|
|
|
onHoverChanged: (_: boolean) => void;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-12-07 20:43:19 +00:00
|
|
|
|
|
|
|
// React doesn't reliably fire `onMouseLeave` or `onMouseOut` events if wrapping a
|
|
|
|
// disabled button. This uses native browser events to avoid that.
|
|
|
|
//
|
|
|
|
// See <https://lecstor.com/react-disabled-button-onmouseleave/>.
|
|
|
|
const TooltipEventWrapper = React.forwardRef<
|
|
|
|
HTMLSpanElement,
|
|
|
|
EventWrapperPropsType
|
|
|
|
>(({ onHoverChanged, children }, ref) => {
|
|
|
|
const wrapperRef = React.useRef<HTMLSpanElement | null>(null);
|
|
|
|
|
2021-03-01 20:08:37 +00:00
|
|
|
const on = React.useCallback(() => {
|
|
|
|
onHoverChanged(true);
|
|
|
|
}, [onHoverChanged]);
|
|
|
|
|
|
|
|
const off = React.useCallback(() => {
|
|
|
|
onHoverChanged(false);
|
|
|
|
}, [onHoverChanged]);
|
|
|
|
|
2020-12-07 20:43:19 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
const wrapperEl = wrapperRef.current;
|
|
|
|
|
|
|
|
if (!wrapperEl) {
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapperEl.addEventListener('mouseenter', on);
|
|
|
|
wrapperEl.addEventListener('mouseleave', off);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
wrapperEl.removeEventListener('mouseenter', on);
|
|
|
|
wrapperEl.removeEventListener('mouseleave', off);
|
|
|
|
};
|
2021-03-01 20:08:37 +00:00
|
|
|
}, [on, off]);
|
2020-12-07 20:43:19 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
2021-03-01 20:08:37 +00:00
|
|
|
onFocus={on}
|
|
|
|
onBlur={off}
|
2021-03-08 22:31:19 +00:00
|
|
|
ref={multiRef<HTMLSpanElement>(ref, wrapperRef)}
|
2020-12-07 20:43:19 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-11-19 18:11:35 +00:00
|
|
|
export enum TooltipPlacement {
|
|
|
|
Top = 'top',
|
|
|
|
Right = 'right',
|
|
|
|
Bottom = 'bottom',
|
|
|
|
Left = 'left',
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
content: string | JSX.Element;
|
|
|
|
direction?: TooltipPlacement;
|
|
|
|
sticky?: boolean;
|
2020-12-04 23:03:01 +00:00
|
|
|
theme?: Theme;
|
2020-11-19 18:11:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Tooltip: React.FC<PropsType> = ({
|
|
|
|
children,
|
|
|
|
content,
|
|
|
|
direction,
|
|
|
|
sticky,
|
2020-12-04 23:03:01 +00:00
|
|
|
theme,
|
2020-11-19 18:11:35 +00:00
|
|
|
}) => {
|
2020-12-07 20:43:19 +00:00
|
|
|
const [isHovering, setIsHovering] = React.useState(false);
|
|
|
|
|
|
|
|
const showTooltip = isHovering || Boolean(sticky);
|
2020-11-19 18:11:35 +00:00
|
|
|
|
2021-01-11 21:43:21 +00:00
|
|
|
const tooltipThemeClassName = theme
|
|
|
|
? `module-tooltip--${themeClassName(theme)}`
|
|
|
|
: undefined;
|
2020-11-19 23:38:59 +00:00
|
|
|
|
2020-11-19 18:11:35 +00:00
|
|
|
return (
|
|
|
|
<Manager>
|
|
|
|
<Reference>
|
|
|
|
{({ ref }) => (
|
2020-12-07 20:43:19 +00:00
|
|
|
<TooltipEventWrapper ref={ref} onHoverChanged={setIsHovering}>
|
2020-11-19 18:11:35 +00:00
|
|
|
{children}
|
2020-12-07 20:43:19 +00:00
|
|
|
</TooltipEventWrapper>
|
2020-11-19 18:11:35 +00:00
|
|
|
)}
|
|
|
|
</Reference>
|
2021-08-03 17:04:49 +00:00
|
|
|
<Popper placement={direction} modifiers={[offsetDistanceModifier(12)]}>
|
2020-11-19 18:11:35 +00:00
|
|
|
{({ arrowProps, placement, ref, style }) =>
|
|
|
|
showTooltip && (
|
2021-01-11 21:43:21 +00:00
|
|
|
<div
|
|
|
|
className={classNames('module-tooltip', tooltipThemeClassName)}
|
|
|
|
ref={ref}
|
|
|
|
style={style}
|
|
|
|
data-placement={placement}
|
|
|
|
>
|
|
|
|
{content}
|
2020-11-19 18:11:35 +00:00
|
|
|
<div
|
2021-01-11 21:43:21 +00:00
|
|
|
className="module-tooltip-arrow"
|
|
|
|
ref={arrowProps.ref}
|
|
|
|
style={arrowProps.style}
|
|
|
|
/>
|
2020-11-19 18:11:35 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Popper>
|
|
|
|
</Manager>
|
|
|
|
);
|
|
|
|
};
|