2017-12-05 03:43:08 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Tippy from 'tippy.js';
|
|
|
|
import _ from 'lodash';
|
|
|
|
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';
|
2017-12-05 03:43:08 +08:00
|
|
|
|
2019-01-31 23:20:13 +08:00
|
|
|
const DEFAULT_ANIMATION = 'shift-away';
|
|
|
|
const ANIMATION_NONE = 'none';
|
2019-02-05 01:36:59 +08:00
|
|
|
const ANIMATION_DURATION = 350;
|
|
|
|
const ANIMATION_DELAY = [150, 50];
|
2017-12-05 03:43:08 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2017-12-08 00:11:34 +08:00
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
position: PropTypes.oneOf(['bottom']),
|
|
|
|
children: PropTypes.element.isRequired,
|
2017-12-12 02:47:34 +08:00
|
|
|
className: PropTypes.string,
|
2019-03-09 09:06:45 +08:00
|
|
|
tooltipDistance: PropTypes.number,
|
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,
|
2019-03-09 09:06:45 +08:00
|
|
|
tooltipDistance: -1,
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Tooltip extends Component {
|
2018-11-06 23:41:11 +08:00
|
|
|
static wait(tip, event) {
|
2018-03-29 21:40:50 +08:00
|
|
|
const tooltipTarget = event.target;
|
|
|
|
const expandedEl = tooltipTarget.parentElement.querySelector('[aria-expanded="true"]');
|
|
|
|
const isTarget = expandedEl === tooltipTarget;
|
|
|
|
if (expandedEl && !isTarget) return;
|
2018-11-06 23:41:11 +08:00
|
|
|
tip.show();
|
2018-03-29 21:40:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-12-08 00:11:34 +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,
|
2019-03-08 03:00:01 +08:00
|
|
|
tooltipDistance,
|
2017-12-05 03:43:08 +08:00
|
|
|
} = this.props;
|
2019-04-14 05:22:47 +08:00
|
|
|
|
|
|
|
const { animations } = Settings.application;
|
2019-03-08 03:00:01 +08:00
|
|
|
|
|
|
|
let distance = 0;
|
2019-03-09 09:06:45 +08:00
|
|
|
if (tooltipDistance < 0) {
|
2019-04-14 05:22:47 +08:00
|
|
|
if (animations) distance = 10;
|
2019-03-09 09:06:45 +08:00
|
|
|
else distance = 20;
|
2019-03-08 03:00:01 +08:00
|
|
|
} else {
|
|
|
|
distance = tooltipDistance;
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
const options = {
|
2018-11-06 22:59:53 +08:00
|
|
|
placement: position,
|
|
|
|
performance: true,
|
2018-12-10 09:08:16 +08:00
|
|
|
content: title,
|
2019-04-14 05:22:47 +08:00
|
|
|
delay: animations ? ANIMATION_DELAY : [ANIMATION_DELAY[0], 0],
|
|
|
|
duration: animations ? ANIMATION_DURATION : 0,
|
2017-12-05 03:43:08 +08:00
|
|
|
onShow: this.onShow,
|
2018-01-12 01:05:32 +08:00
|
|
|
onHide: this.onHide,
|
2018-03-29 21:40:50 +08:00
|
|
|
wait: Tooltip.wait,
|
2018-01-11 03:45:12 +08:00
|
|
|
touchHold: true,
|
2018-12-19 22:27:48 +08:00
|
|
|
size: 'regular',
|
2019-03-08 03:00:01 +08:00
|
|
|
distance,
|
2018-12-19 22:27:48 +08:00
|
|
|
arrow: true,
|
|
|
|
arrowType: 'sharp',
|
2019-01-31 00:07:39 +08:00
|
|
|
aria: null,
|
2019-04-14 05:22:47 +08:00
|
|
|
animation: animations ? DEFAULT_ANIMATION : ANIMATION_NONE,
|
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;
|
2019-03-09 08:23:56 +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;
|
|
|
|
instance.set({
|
|
|
|
animation: animations
|
|
|
|
? DEFAULT_ANIMATION : ANIMATION_NONE,
|
|
|
|
distance: animations ? 10 : 20,
|
|
|
|
delay: animations ? ANIMATION_DELAY : [ANIMATION_DELAY[0], 0],
|
|
|
|
duration: animations ? ANIMATION_DURATION : 0,
|
2019-01-25 00:16:23 +08:00
|
|
|
});
|
2019-01-31 01:43:11 +08:00
|
|
|
|
2019-04-14 05:22:47 +08:00
|
|
|
// adjusts the distance for tooltips on the presentation toolbar
|
|
|
|
Object.entries(instance.reference.classList).reduce((acc, [key]) => {
|
|
|
|
if (!instance.reference.classList[key].match(/(presentationBtn)/)) return false;
|
|
|
|
instance.set({ distance: animations ? 35 : 45 });
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
2019-03-09 08:23:56 +08:00
|
|
|
|
|
|
|
const elem = document.getElementById(this.tippySelectorId);
|
|
|
|
if (elem._tippy) elem._tippy.set({ content: title });
|
2019-01-25 00:16:23 +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,
|
|
|
|
tooltipDistance,
|
2017-12-05 03:43:08 +08:00
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const WrappedComponent = React.Children.only(children);
|
|
|
|
|
|
|
|
const WrappedComponentBound = React.cloneElement(WrappedComponent, {
|
|
|
|
...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;
|