2017-12-05 03:43:08 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import cx from 'classnames';
|
2018-01-12 01:05:32 +08:00
|
|
|
import { ESCAPE } from '/imports/utils/keyCodes';
|
2019-01-25 00:16:23 +08:00
|
|
|
import Settings from '/imports/ui/services/settings';
|
2022-02-22 21:56:43 +08:00
|
|
|
import Tippy, { roundArrow } from 'tippy.js';
|
2019-12-04 02:49:09 +08:00
|
|
|
import 'tippy.js/dist/svg-arrow.css';
|
|
|
|
import 'tippy.js/animations/shift-away.css';
|
|
|
|
import './bbbtip.css';
|
2022-02-15 04:20:50 +08:00
|
|
|
import BaseButton from '/imports/ui/components/common/button/base/component';
|
|
|
|
import ButtonEmoji from '/imports/ui/components/common/button/button-emoji/ButtonEmoji';
|
2023-02-23 22:23:51 +08:00
|
|
|
import { uniqueId } from '/imports/utils/string-utils';
|
2017-12-05 03:43:08 +08:00
|
|
|
|
2019-02-05 01:36:59 +08:00
|
|
|
const ANIMATION_DURATION = 350;
|
|
|
|
const ANIMATION_DELAY = [150, 50];
|
2019-12-04 02:49:09 +08:00
|
|
|
const DEFAULT_ANIMATION = 'shift-away';
|
|
|
|
const ANIMATION_NONE = 'none';
|
2023-11-13 21:51:03 +08:00
|
|
|
const TIP_OFFSET = [0, 10];
|
2017-12-05 03:43:08 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2020-07-23 06:08:55 +08:00
|
|
|
title: PropTypes.string,
|
2021-04-30 01:25:25 +08:00
|
|
|
position: PropTypes.oneOf(['bottom','top']),
|
2017-12-08 00:11:34 +08:00
|
|
|
children: PropTypes.element.isRequired,
|
2017-12-12 02:47:34 +08:00
|
|
|
className: PropTypes.string,
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
2017-12-08 00:11:34 +08:00
|
|
|
position: 'bottom',
|
2017-12-12 02:47:34 +08:00
|
|
|
className: null,
|
2020-07-23 06:08:55 +08:00
|
|
|
title: '',
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Tooltip extends Component {
|
2021-05-13 03:43:48 +08:00
|
|
|
static buttonComponentHasButtonEmoji(_component) {
|
|
|
|
return (
|
|
|
|
_component
|
|
|
|
&& (_component.type === BaseButton)
|
|
|
|
&& (_component.props)
|
|
|
|
&& (_component.props.children)
|
|
|
|
&& (typeof _component.props.children.find === 'function')
|
|
|
|
&& (!!_component.props.children.find((_child) => (
|
|
|
|
_child && _child.type === ButtonEmoji
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2023-02-23 22:23:51 +08:00
|
|
|
this.tippySelectorId = uniqueId('tippy-');
|
2017-12-05 03:43:08 +08:00
|
|
|
this.onShow = this.onShow.bind(this);
|
2018-01-12 01:05:32 +08:00
|
|
|
this.onHide = this.onHide.bind(this);
|
|
|
|
this.handleEscapeHide = this.handleEscapeHide.bind(this);
|
2017-12-05 03:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const {
|
2017-12-08 00:11:34 +08:00
|
|
|
position,
|
2018-12-10 09:08:16 +08:00
|
|
|
title,
|
2023-04-03 16:13:54 +08:00
|
|
|
delay,
|
|
|
|
placement,
|
2017-12-05 03:43:08 +08:00
|
|
|
} = this.props;
|
2019-04-14 05:22:47 +08:00
|
|
|
|
|
|
|
const { animations } = Settings.application;
|
2023-11-13 21:51:03 +08:00
|
|
|
|
2023-04-03 16:13:54 +08:00
|
|
|
const overridePlacement = placement ? placement : position;
|
|
|
|
let overrideDelay;
|
|
|
|
if (animations) {
|
|
|
|
overrideDelay = delay ? [delay, ANIMATION_DELAY[1]] : ANIMATION_DELAY;
|
|
|
|
} else {
|
|
|
|
overrideDelay = delay ? [delay, 0] : [ANIMATION_DELAY[0], 0];
|
|
|
|
}
|
2019-03-08 03:00:01 +08:00
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
const options = {
|
2019-12-04 02:49:09 +08:00
|
|
|
aria: null,
|
2021-05-31 20:59:15 +08:00
|
|
|
allowHTML: false,
|
2019-12-04 02:49:09 +08:00
|
|
|
animation: animations ? DEFAULT_ANIMATION : ANIMATION_NONE,
|
2022-11-11 01:42:19 +08:00
|
|
|
appendTo: document.body,
|
2019-12-04 02:49:09 +08:00
|
|
|
arrow: roundArrow,
|
|
|
|
boundary: 'window',
|
2018-12-10 09:08:16 +08:00
|
|
|
content: title,
|
2023-04-03 16:13:54 +08:00
|
|
|
delay: overrideDelay,
|
2019-04-14 05:22:47 +08:00
|
|
|
duration: animations ? ANIMATION_DURATION : 0,
|
2022-11-11 01:42:19 +08:00
|
|
|
interactive: true,
|
|
|
|
interactiveBorder: 10,
|
2017-12-05 03:43:08 +08:00
|
|
|
onShow: this.onShow,
|
2018-01-12 01:05:32 +08:00
|
|
|
onHide: this.onHide,
|
2019-12-04 02:49:09 +08:00
|
|
|
offset: TIP_OFFSET,
|
2023-04-03 16:13:54 +08:00
|
|
|
placement: overridePlacement,
|
2023-11-13 21:51:03 +08:00
|
|
|
touch: ['hold', 1000],
|
2019-12-04 02:49:09 +08:00
|
|
|
theme: 'bbbtip',
|
2021-05-31 20:59:15 +08:00
|
|
|
multiple: false,
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
2017-12-08 00:11:34 +08:00
|
|
|
this.tooltip = Tippy(`#${this.tippySelectorId}`, options);
|
2017-12-05 03:43:08 +08:00
|
|
|
}
|
2018-11-06 23:41:11 +08:00
|
|
|
|
2019-01-25 00:16:23 +08:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { animations } = Settings.application;
|
2022-11-10 22:28:23 +08:00
|
|
|
const { title } = this.props;
|
2019-04-14 05:22:47 +08:00
|
|
|
const elements = document.querySelectorAll('[id^="tippy-"]');
|
2019-01-31 01:43:11 +08:00
|
|
|
|
2019-04-14 05:22:47 +08:00
|
|
|
Array.from(elements).filter((e) => {
|
|
|
|
const instance = e._tippy;
|
2019-02-01 01:29:59 +08:00
|
|
|
|
2019-04-14 05:22:47 +08:00
|
|
|
if (!instance) return false;
|
2019-02-01 01:29:59 +08:00
|
|
|
|
2019-04-14 05:22:47 +08:00
|
|
|
const animation = animations ? DEFAULT_ANIMATION : ANIMATION_NONE;
|
2019-02-01 01:29:59 +08:00
|
|
|
|
2019-04-14 05:22:47 +08:00
|
|
|
if (animation === instance.props.animation) return false;
|
2019-02-01 01:29:59 +08:00
|
|
|
|
2019-04-14 05:22:47 +08:00
|
|
|
return true;
|
|
|
|
}).forEach((e) => {
|
|
|
|
const instance = e._tippy;
|
2023-04-04 10:03:51 +08:00
|
|
|
const newProps = {
|
2019-04-14 05:22:47 +08:00
|
|
|
animation: animations
|
|
|
|
? DEFAULT_ANIMATION : ANIMATION_NONE,
|
|
|
|
duration: animations ? ANIMATION_DURATION : 0,
|
2023-04-04 10:03:51 +08:00
|
|
|
};
|
|
|
|
if (!e.getAttribute("delay")) {
|
|
|
|
newProps["delay"] = animations ? ANIMATION_DELAY : [ANIMATION_DELAY[0], 0];
|
|
|
|
}
|
|
|
|
instance.setProps(newProps);
|
2019-04-14 05:22:47 +08:00
|
|
|
});
|
2019-03-09 08:23:56 +08:00
|
|
|
|
|
|
|
const elem = document.getElementById(this.tippySelectorId);
|
2022-11-10 22:28:23 +08:00
|
|
|
const opts = { content: title, appendTo: document.body };
|
2020-07-23 06:08:55 +08:00
|
|
|
if (elem && elem._tippy) elem._tippy.setProps(opts);
|
2019-01-25 00:16:23 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:25:11 +08:00
|
|
|
componentWillUnmount() {
|
2022-06-07 21:59:18 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
const tooltip = this.tooltip[0];
|
|
|
|
if (tooltip) tooltip.hide();
|
|
|
|
}, 150);
|
2021-08-31 20:25:11 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
onShow() {
|
2018-01-12 01:05:32 +08:00
|
|
|
document.addEventListener('keyup', this.handleEscapeHide);
|
2017-12-05 03:43:08 +08:00
|
|
|
}
|
|
|
|
|
2018-01-12 01:05:32 +08:00
|
|
|
onHide() {
|
|
|
|
document.removeEventListener('keyup', this.handleEscapeHide);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEscapeHide(e) {
|
2019-04-19 02:13:21 +08:00
|
|
|
if (this.tooltip
|
|
|
|
&& e.keyCode === ESCAPE
|
|
|
|
&& this.tooltip.tooltips
|
|
|
|
&& this.tooltip.tooltips[0]) {
|
|
|
|
this.tooltip.tooltips[0].hide();
|
|
|
|
}
|
2018-01-12 01:05:32 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
children,
|
|
|
|
className,
|
2019-04-19 03:00:26 +08:00
|
|
|
title,
|
2017-12-05 03:43:08 +08:00
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
|
2021-05-13 03:43:48 +08:00
|
|
|
let WrappedComponent;
|
|
|
|
let WrappedComponentBound;
|
|
|
|
|
|
|
|
if (Tooltip.buttonComponentHasButtonEmoji(children)) {
|
|
|
|
const { children: grandChildren } = children.props;
|
|
|
|
|
|
|
|
let otherChildren;
|
|
|
|
|
|
|
|
[WrappedComponent, ...otherChildren] = grandChildren;
|
|
|
|
|
|
|
|
WrappedComponentBound = React.cloneElement(WrappedComponent, {
|
|
|
|
id: this.tippySelectorId,
|
|
|
|
className: cx(WrappedComponent.props.className, className),
|
2021-06-29 03:26:33 +08:00
|
|
|
key: this.tippySelectorId,
|
2021-05-13 03:43:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const ParentComponent = React.Children.only(children);
|
2021-06-29 03:26:33 +08:00
|
|
|
const updatedChildren = [WrappedComponentBound, ...otherChildren];
|
2021-05-13 03:43:48 +08:00
|
|
|
|
2021-07-01 05:39:32 +08:00
|
|
|
return React.cloneElement(ParentComponent, null,
|
2021-05-13 03:43:48 +08:00
|
|
|
updatedChildren);
|
|
|
|
}
|
|
|
|
|
|
|
|
WrappedComponent = React.Children.only(children);
|
2017-12-05 03:43:08 +08:00
|
|
|
|
2021-05-13 03:43:48 +08:00
|
|
|
WrappedComponentBound = React.cloneElement(WrappedComponent, {
|
2017-12-05 03:43:08 +08:00
|
|
|
...restProps,
|
2017-12-08 00:11:34 +08:00
|
|
|
id: this.tippySelectorId,
|
2017-12-05 03:43:08 +08:00
|
|
|
className: cx(children.props.className, className),
|
|
|
|
});
|
|
|
|
|
|
|
|
return WrappedComponentBound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Tooltip;
|
|
|
|
|
|
|
|
Tooltip.defaultProps = defaultProps;
|
|
|
|
Tooltip.propTypes = propTypes;
|