bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/tooltip/component.jsx

144 lines
3.5 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Tippy from 'tippy.js';
import _ from 'lodash';
import cx from 'classnames';
import { ESCAPE } from '/imports/utils/keyCodes';
import Settings from '/imports/ui/services/settings';
2019-01-31 23:20:13 +08:00
const DEFAULT_ANIMATION = 'shift-away';
const ANIMATION_NONE = 'none';
const propTypes = {
title: PropTypes.string.isRequired,
position: PropTypes.oneOf(['bottom']),
children: PropTypes.element.isRequired,
2017-12-12 02:47:34 +08:00
className: PropTypes.string,
};
const defaultProps = {
position: 'bottom',
2017-12-12 02:47:34 +08:00
className: null,
};
class Tooltip extends Component {
static wait(tip, event) {
const tooltipTarget = event.target;
const expandedEl = tooltipTarget.parentElement.querySelector('[aria-expanded="true"]');
const isTarget = expandedEl === tooltipTarget;
if (expandedEl && !isTarget) return;
2018-12-29 07:11:05 +08:00
tip.set({ content: tooltipTarget.lastChild.innerText });
tip.show();
}
constructor(props) {
super(props);
this.tippySelectorId = _.uniqueId('tippy-');
this.onShow = this.onShow.bind(this);
this.onHide = this.onHide.bind(this);
this.handleEscapeHide = this.handleEscapeHide.bind(this);
2018-01-11 01:07:40 +08:00
this.delay = [150, 50];
this.state = {
enableAnimation: Settings.application.animations,
};
}
componentDidMount() {
const {
position,
2018-12-10 09:08:16 +08:00
title,
} = this.props;
const { enableAnimation } = this.state;
const options = {
placement: position,
performance: true,
2018-12-10 09:08:16 +08:00
content: title,
delay: this.delay,
onShow: this.onShow,
onHide: this.onHide,
wait: Tooltip.wait,
touchHold: true,
2018-12-19 22:27:48 +08:00
size: 'regular',
2019-01-31 19:15:14 +08:00
distance: (enableAnimation) ? 10 : 20,
2018-12-19 22:27:48 +08:00
arrow: true,
arrowType: 'sharp',
2019-01-31 23:20:13 +08:00
animation: (enableAnimation) ? DEFAULT_ANIMATION : ANIMATION_NONE,
};
this.tooltip = Tippy(`#${this.tippySelectorId}`, options);
}
componentDidUpdate() {
const { enableAnimation } = this.state;
const { animations } = Settings.application;
2019-01-31 01:43:11 +08:00
if (animations !== enableAnimation) {
const elements = document.querySelectorAll('[id^="tippy-"]');
2019-01-31 23:20:13 +08:00
Array.from(elements).filter((e) => {
const instance = e._tippy;
2019-01-31 23:20:13 +08:00
let anim;
if (instance && instance.props.animation === DEFAULT_ANIMATION) {
anim = true;
} else {
anim = false;
2019-01-31 19:15:14 +08:00
}
2019-01-31 23:20:13 +08:00
const validate = (instance
&& anim !== animations
);
return validate;
}).forEach((e) => {
const instance = e._tippy;
instance.set({
animation: animations
? DEFAULT_ANIMATION : ANIMATION_NONE,
distance: animations ? 10 : 20,
});
});
2019-01-31 01:43:11 +08:00
this.setEnableAnimation(animations);
}
}
onShow() {
document.addEventListener('keyup', this.handleEscapeHide);
}
onHide() {
document.removeEventListener('keyup', this.handleEscapeHide);
}
setEnableAnimation(enableAnimation) {
this.setState({ enableAnimation });
}
handleEscapeHide(e) {
if (e.keyCode !== ESCAPE) return;
this.tooltip.tooltips[0].hide();
}
render() {
const {
children,
className,
title,
...restProps
} = this.props;
const WrappedComponent = React.Children.only(children);
const WrappedComponentBound = React.cloneElement(WrappedComponent, {
...restProps,
id: this.tippySelectorId,
className: cx(children.props.className, className),
});
return WrappedComponentBound;
}
}
export default Tooltip;
Tooltip.defaultProps = defaultProps;
Tooltip.propTypes = propTypes;