2018-04-12 01:58:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-02-01 02:36:19 +03:00
|
|
|
Copyright 2019 New Vector Ltd
|
2019-07-31 14:19:29 +03:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2020-05-25 19:53:09 +03:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-07-17 19:09:43 +03:00
|
|
|
import React, {Component, CSSProperties} from 'react';
|
2018-06-12 17:22:45 +03:00
|
|
|
import ReactDOM from 'react-dom';
|
2018-04-12 01:58:04 +03:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const MIN_TOOLTIP_HEIGHT = 25;
|
|
|
|
|
2020-05-25 15:40:05 +03:00
|
|
|
interface IProps {
|
2018-04-12 01:58:04 +03:00
|
|
|
// Class applied to the element used to position the tooltip
|
2020-06-18 16:32:43 +03:00
|
|
|
className: string;
|
2018-04-12 01:58:04 +03:00
|
|
|
// Class applied to the tooltip itself
|
2020-06-18 16:32:43 +03:00
|
|
|
tooltipClassName?: string;
|
2019-04-25 13:10:35 +03:00
|
|
|
// Whether the tooltip is visible or hidden.
|
|
|
|
// The hidden state allows animating the tooltip away via CSS.
|
|
|
|
// Defaults to visible if unset.
|
2020-06-18 16:32:43 +03:00
|
|
|
visible?: boolean;
|
2019-02-01 02:36:19 +03:00
|
|
|
// the react element to put into the tooltip
|
2020-06-18 16:32:43 +03:00
|
|
|
label: React.ReactNode;
|
2020-05-25 15:40:05 +03:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:37:07 +03:00
|
|
|
export default class Tooltip extends React.Component<IProps> {
|
2020-05-25 15:40:05 +03:00
|
|
|
private tooltipContainer: HTMLElement;
|
|
|
|
private tooltip: void | Element | Component<Element, any, any>;
|
|
|
|
private parent: Element;
|
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-05-26 14:09:23 +03:00
|
|
|
public static readonly defaultProps = {
|
2020-05-25 15:40:05 +03:00
|
|
|
visible: true,
|
|
|
|
};
|
2019-04-25 13:10:35 +03:00
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
// Create a wrapper for the tooltip outside the parent and attach it to the body element
|
2020-05-26 14:09:23 +03:00
|
|
|
public componentDidMount() {
|
2018-04-12 01:58:04 +03:00
|
|
|
this.tooltipContainer = document.createElement("div");
|
2019-02-01 02:36:19 +03:00
|
|
|
this.tooltipContainer.className = "mx_Tooltip_wrapper";
|
2018-04-12 01:58:04 +03:00
|
|
|
document.body.appendChild(this.tooltipContainer);
|
2020-05-25 15:40:05 +03:00
|
|
|
window.addEventListener('scroll', this.renderTooltip, true);
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-05-25 15:40:05 +03:00
|
|
|
this.parent = ReactDOM.findDOMNode(this).parentNode as Element;
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-05-25 15:40:05 +03:00
|
|
|
this.renderTooltip();
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-05-26 14:09:23 +03:00
|
|
|
public componentDidUpdate() {
|
2020-05-25 15:40:05 +03:00
|
|
|
this.renderTooltip();
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
// Remove the wrapper element, as the tooltip has finished using it
|
2020-05-26 14:09:23 +03:00
|
|
|
public componentWillUnmount() {
|
2018-04-12 01:58:04 +03:00
|
|
|
ReactDOM.unmountComponentAtNode(this.tooltipContainer);
|
|
|
|
document.body.removeChild(this.tooltipContainer);
|
2020-05-25 15:40:05 +03:00
|
|
|
window.removeEventListener('scroll', this.renderTooltip, true);
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-07-17 19:09:43 +03:00
|
|
|
private updatePosition(style: CSSProperties) {
|
2018-04-12 01:58:04 +03:00
|
|
|
const parentBox = this.parent.getBoundingClientRect();
|
|
|
|
let offset = 0;
|
|
|
|
if (parentBox.height > MIN_TOOLTIP_HEIGHT) {
|
|
|
|
offset = Math.floor((parentBox.height - MIN_TOOLTIP_HEIGHT) / 2);
|
2019-05-14 02:16:40 +03:00
|
|
|
} else {
|
|
|
|
// The tooltip is larger than the parent height: figure out what offset
|
|
|
|
// we need so that we're still centered.
|
|
|
|
offset = Math.floor(parentBox.height - MIN_TOOLTIP_HEIGHT);
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
2020-07-17 19:09:43 +03:00
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
style.top = (parentBox.top - 2) + window.pageYOffset + offset;
|
2020-07-17 19:09:43 +03:00
|
|
|
if (parentBox.right > window.innerWidth / 2) {
|
|
|
|
style.right = window.innerWidth - parentBox.right - window.pageXOffset - 8;
|
|
|
|
} else {
|
|
|
|
style.left = parentBox.right + window.pageXOffset + 6;
|
|
|
|
}
|
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
return style;
|
2020-05-25 15:40:05 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-06-04 19:54:18 +03:00
|
|
|
private renderTooltip = () => {
|
2018-04-12 01:58:04 +03:00
|
|
|
// Add the parent's position to the tooltips, so it's correctly
|
|
|
|
// positioned, also taking into account any window zoom
|
|
|
|
// NOTE: The additional 6 pixels for the left position, is to take account of the
|
|
|
|
// tooltips chevron
|
2020-05-25 15:40:05 +03:00
|
|
|
const style = this.updatePosition({});
|
2019-11-25 06:52:22 +03:00
|
|
|
// Hide the entire container when not visible. This prevents flashing of the tooltip
|
|
|
|
// if it is not meant to be visible on first mount.
|
|
|
|
style.display = this.props.visible ? "block" : "none";
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-04-25 13:10:35 +03:00
|
|
|
const tooltipClasses = classNames("mx_Tooltip", this.props.tooltipClassName, {
|
|
|
|
"mx_Tooltip_visible": this.props.visible,
|
|
|
|
"mx_Tooltip_invisible": !this.props.visible,
|
|
|
|
});
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-06-12 17:22:45 +03:00
|
|
|
const tooltip = (
|
|
|
|
<div className={tooltipClasses} style={style}>
|
2019-02-01 02:36:19 +03:00
|
|
|
<div className="mx_Tooltip_chevron" />
|
2018-06-12 17:22:45 +03:00
|
|
|
{ this.props.label }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
// Render the tooltip manually, as we wish it not to be rendered within the parent
|
2020-05-25 15:40:05 +03:00
|
|
|
this.tooltip = ReactDOM.render<Element>(tooltip, this.tooltipContainer);
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-05-26 14:09:23 +03:00
|
|
|
public render() {
|
2018-04-12 01:58:04 +03:00
|
|
|
// Render a placeholder
|
|
|
|
return (
|
|
|
|
<div className={this.props.className} >
|
|
|
|
</div>
|
|
|
|
);
|
2020-05-25 15:40:05 +03:00
|
|
|
}
|
|
|
|
}
|