2022-10-24 09:51:18 +00:00
|
|
|
import React, { Component, ReactNode } from 'react';
|
2017-08-22 19:22:06 +00:00
|
|
|
|
2022-10-24 09:51:18 +00:00
|
|
|
import { IReduxState } from '../../../app/types';
|
|
|
|
// eslint-disable-next-line lines-around-comment
|
|
|
|
// @ts-ignore
|
2022-09-27 07:10:28 +00:00
|
|
|
import { DialogPortal, Drawer, JitsiPortal } from '../../../toolbox/components/web';
|
2021-10-06 09:53:27 +00:00
|
|
|
import { isMobileBrowser } from '../../environment/utils';
|
2022-10-24 09:51:18 +00:00
|
|
|
import { connect } from '../../redux/functions';
|
2021-10-06 09:53:27 +00:00
|
|
|
import { getContextMenuStyle } from '../functions.web';
|
2017-08-22 19:22:06 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link Popover}.
|
|
|
|
*/
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A child React Element to use as the trigger for showing the dialog.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
children: ReactNode;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional CSS classnames to apply to the root of the {@code Popover}
|
|
|
|
* component.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
className?: string;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ReactElement to display within the dialog.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
content: ReactNode;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether displaying of the popover should be prevented.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
disablePopover?: boolean;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An id attribute to apply to the root of the {@code Popover}
|
|
|
|
* component.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
id?: string;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
2021-06-30 16:12:12 +00:00
|
|
|
/**
|
|
|
|
* Callback to invoke when the popover has closed.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
onPopoverClose: Function;
|
2021-06-30 16:12:12 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* Callback to invoke when the popover has opened.
|
|
|
|
*/
|
2022-10-25 13:11:55 +00:00
|
|
|
onPopoverOpen?: Function;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
2021-01-04 13:30:23 +00:00
|
|
|
/**
|
|
|
|
* Whether to display the Popover as a drawer.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
overflowDrawer?: boolean;
|
2021-01-04 13:30:23 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
2022-10-25 13:11:55 +00:00
|
|
|
* Where should the popover content be placed.
|
2018-10-30 05:02:23 +00:00
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
position: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the trigger for open/ close should be click or hover.
|
|
|
|
*/
|
|
|
|
trigger?: 'hover' | 'click';
|
2021-11-01 09:39:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the popover is visible or not.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
visible: boolean;
|
2018-10-30 05:02:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} state of {@link Popover}.
|
|
|
|
*/
|
|
|
|
type State = {
|
|
|
|
|
2021-10-06 09:53:27 +00:00
|
|
|
/**
|
|
|
|
* The style to apply to the context menu in order to position it correctly.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
contextMenuStyle?: {
|
|
|
|
bottom?: string;
|
|
|
|
left?: string;
|
|
|
|
position: string;
|
|
|
|
top?: string;
|
|
|
|
} | null;
|
2018-10-30 05:02:23 +00:00
|
|
|
};
|
|
|
|
|
2017-08-22 19:22:06 +00:00
|
|
|
/**
|
2022-10-25 13:11:55 +00:00
|
|
|
* Implements a React {@code Component} for showing an {@code Popover} on
|
2017-08-22 19:22:06 +00:00
|
|
|
* mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments Component
|
2017-08-22 19:22:06 +00:00
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
class Popover extends Component<Props, State> {
|
2017-08-22 19:22:06 +00:00
|
|
|
/**
|
|
|
|
* Default values for {@code Popover} component's properties.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
2022-10-24 09:51:18 +00:00
|
|
|
id: '',
|
|
|
|
trigger: 'hover'
|
2017-08-22 19:22:06 +00:00
|
|
|
};
|
|
|
|
|
2021-09-14 07:43:52 +00:00
|
|
|
/**
|
|
|
|
* Reference to the dialog container.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_containerRef: React.RefObject<HTMLDivElement>;
|
2021-09-14 07:43:52 +00:00
|
|
|
|
2021-10-06 09:53:27 +00:00
|
|
|
_contextMenuRef: HTMLElement;
|
2021-09-14 07:43:52 +00:00
|
|
|
|
2017-08-22 19:22:06 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code Popover} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
constructor(props: Props) {
|
2017-08-22 19:22:06 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2021-10-06 09:53:27 +00:00
|
|
|
contextMenuStyle: null
|
2017-08-22 19:22:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
|
|
|
this._onHideDialog = this._onHideDialog.bind(this);
|
|
|
|
this._onShowDialog = this._onShowDialog.bind(this);
|
2021-06-10 12:48:44 +00:00
|
|
|
this._onKeyPress = this._onKeyPress.bind(this);
|
2021-09-14 07:43:52 +00:00
|
|
|
this._containerRef = React.createRef();
|
2021-06-10 12:48:44 +00:00
|
|
|
this._onEscKey = this._onEscKey.bind(this);
|
2022-10-24 09:51:18 +00:00
|
|
|
this._onClick = this._onClick.bind(this);
|
2021-09-14 07:43:52 +00:00
|
|
|
this._onTouchStart = this._onTouchStart.bind(this);
|
2021-10-06 09:53:27 +00:00
|
|
|
this._setContextMenuRef = this._setContextMenuRef.bind(this);
|
|
|
|
this._setContextMenuStyle = this._setContextMenuStyle.bind(this);
|
|
|
|
this._getCustomDialogStyle = this._getCustomDialogStyle.bind(this);
|
2022-10-24 09:51:18 +00:00
|
|
|
this._onOutsideClick = this._onOutsideClick.bind(this);
|
2021-01-04 13:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 07:43:52 +00:00
|
|
|
/**
|
|
|
|
* Sets up a touch event listener to attach.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('touchstart', this._onTouchStart);
|
2022-10-24 09:51:18 +00:00
|
|
|
if (this.props.trigger === 'click') {
|
|
|
|
// @ts-ignore
|
|
|
|
window.addEventListener('click', this._onOutsideClick);
|
|
|
|
}
|
2021-09-14 07:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the listener set up in the {@code componentDidMount} method.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('touchstart', this._onTouchStart);
|
2022-10-24 09:51:18 +00:00
|
|
|
if (this.props.trigger === 'click') {
|
|
|
|
// @ts-ignore
|
|
|
|
window.removeEventListener('click', this._onOutsideClick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles click outside the popover.
|
|
|
|
*
|
|
|
|
* @param {MouseEvent} e - The click event.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onOutsideClick(e: React.MouseEvent) { // @ts-ignore
|
|
|
|
if (!this._containerRef?.current?.contains(e.target) && this.props.visible) {
|
|
|
|
this._onHideDialog();
|
|
|
|
}
|
2021-06-30 16:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:22:06 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2022-10-24 09:51:18 +00:00
|
|
|
const { children, className, content, id, overflowDrawer, visible, trigger } = this.props;
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
if (overflowDrawer) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className = { className }
|
|
|
|
id = { id }
|
2021-09-10 12:17:57 +00:00
|
|
|
onClick = { this._onShowDialog }>
|
2021-01-04 13:30:23 +00:00
|
|
|
{ children }
|
2021-10-04 14:07:05 +00:00
|
|
|
<JitsiPortal>
|
2021-01-04 13:30:23 +00:00
|
|
|
<Drawer
|
2021-11-01 09:39:19 +00:00
|
|
|
isOpen = { visible }
|
2021-01-04 13:30:23 +00:00
|
|
|
onClose = { this._onHideDialog }>
|
|
|
|
{ content }
|
|
|
|
</Drawer>
|
2021-10-04 14:07:05 +00:00
|
|
|
</JitsiPortal>
|
2021-01-04 13:30:23 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:22:06 +00:00
|
|
|
return (
|
|
|
|
<div
|
2021-01-04 13:30:23 +00:00
|
|
|
className = { className }
|
|
|
|
id = { id }
|
2022-10-24 09:51:18 +00:00
|
|
|
onClick = { this._onClick }
|
2021-06-10 12:48:44 +00:00
|
|
|
onKeyPress = { this._onKeyPress }
|
2022-10-24 09:51:18 +00:00
|
|
|
{ ...(trigger === 'hover' ? {
|
|
|
|
onMouseEnter: this._onShowDialog,
|
|
|
|
onMouseLeave: this._onHideDialog
|
|
|
|
} : {}) }
|
2021-09-14 07:43:52 +00:00
|
|
|
ref = { this._containerRef }>
|
2021-11-01 09:39:19 +00:00
|
|
|
{ visible && (
|
2021-10-06 09:53:27 +00:00
|
|
|
<DialogPortal
|
|
|
|
getRef = { this._setContextMenuRef }
|
|
|
|
setSize = { this._setContextMenuStyle }
|
|
|
|
style = { this.state.contextMenuStyle }>
|
|
|
|
{this._renderContent()}
|
|
|
|
</DialogPortal>
|
|
|
|
)}
|
|
|
|
{ children }
|
2017-08-22 19:22:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-06 09:53:27 +00:00
|
|
|
/**
|
|
|
|
* Sets the context menu dialog style for positioning it on screen.
|
|
|
|
*
|
|
|
|
* @param {DOMRectReadOnly} size -The size info of the current context menu.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_setContextMenuStyle(size: DOMRectReadOnly) {
|
2021-10-06 09:53:27 +00:00
|
|
|
const style = this._getCustomDialogStyle(size);
|
|
|
|
|
|
|
|
this.setState({ contextMenuStyle: style });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the context menu's ref.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} elem -The html element of the context menu.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_setContextMenuRef(elem: HTMLElement) {
|
2021-10-06 09:53:27 +00:00
|
|
|
this._contextMenuRef = elem;
|
|
|
|
}
|
|
|
|
|
2021-09-14 07:43:52 +00:00
|
|
|
/**
|
|
|
|
* Hide dialog on touch outside of the context menu.
|
|
|
|
*
|
|
|
|
* @param {TouchEvent} event - The touch event.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_onTouchStart(event: TouchEvent) {
|
2021-11-01 09:39:19 +00:00
|
|
|
if (this.props.visible
|
2021-09-14 07:43:52 +00:00
|
|
|
&& !this.props.overflowDrawer
|
2021-10-06 09:53:27 +00:00
|
|
|
&& this._contextMenuRef
|
2022-10-24 09:51:18 +00:00
|
|
|
&& this._contextMenuRef.contains // @ts-ignore
|
2021-10-06 09:53:27 +00:00
|
|
|
&& !this._contextMenuRef.contains(event.target)) {
|
2021-09-14 07:43:52 +00:00
|
|
|
this._onHideDialog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:22:06 +00:00
|
|
|
/**
|
2022-10-25 13:11:55 +00:00
|
|
|
* Stops displaying the {@code Popover}.
|
2017-08-22 19:22:06 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onHideDialog() {
|
2021-10-06 09:53:27 +00:00
|
|
|
this.setState({
|
|
|
|
contextMenuStyle: null
|
|
|
|
});
|
2021-06-30 16:12:12 +00:00
|
|
|
|
|
|
|
if (this.props.onPopoverClose) {
|
|
|
|
this.props.onPopoverClose();
|
|
|
|
}
|
2017-08-22 19:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-25 13:11:55 +00:00
|
|
|
* Displays the {@code Popover} and calls any registered onPopoverOpen
|
2017-08-22 19:22:06 +00:00
|
|
|
* callbacks.
|
|
|
|
*
|
2021-06-10 12:48:44 +00:00
|
|
|
* @param {Object} event - The mouse event or the keypress event to intercept.
|
2017-08-22 19:22:06 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_onShowDialog(event?: React.MouseEvent | React.KeyboardEvent) {
|
|
|
|
event?.stopPropagation();
|
2017-08-22 19:22:06 +00:00
|
|
|
|
2021-11-01 09:39:19 +00:00
|
|
|
if (!this.props.disablePopover) {
|
2022-10-25 13:11:55 +00:00
|
|
|
this.props.onPopoverOpen?.();
|
2017-08-22 19:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:17:57 +00:00
|
|
|
/**
|
|
|
|
* Prevents switching from tile view to stage view on accidentally clicking
|
|
|
|
* the popover thumbs.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The mouse event or the keypress event to intercept.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_onClick(event: React.MouseEvent) {
|
|
|
|
const { trigger, visible } = this.props;
|
|
|
|
|
2021-09-10 12:17:57 +00:00
|
|
|
event.stopPropagation();
|
2022-10-24 09:51:18 +00:00
|
|
|
if (trigger === 'click') {
|
|
|
|
if (visible) {
|
|
|
|
this._onHideDialog();
|
|
|
|
} else {
|
|
|
|
this._onShowDialog();
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 12:17:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
/**
|
|
|
|
* KeyPress handler for accessibility.
|
|
|
|
*
|
|
|
|
* @param {Object} e - The key event to handle.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_onKeyPress(e: React.KeyboardEvent) {
|
2021-06-10 12:48:44 +00:00
|
|
|
if (e.key === ' ' || e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
2021-11-01 09:39:19 +00:00
|
|
|
if (this.props.visible) {
|
2021-06-10 12:48:44 +00:00
|
|
|
this._onHideDialog();
|
|
|
|
} else {
|
|
|
|
this._onShowDialog(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* KeyPress handler for accessibility.
|
|
|
|
*
|
|
|
|
* @param {Object} e - The key event to handle.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_onEscKey(e: React.KeyboardEvent) {
|
2021-06-10 12:48:44 +00:00
|
|
|
if (e.key === 'Escape') {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-11-01 09:39:19 +00:00
|
|
|
if (this.props.visible) {
|
2021-06-10 12:48:44 +00:00
|
|
|
this._onHideDialog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 09:53:27 +00:00
|
|
|
/**
|
|
|
|
* Gets style for positioning the context menu on screen in regards to the trigger's
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* @param {DOMRectReadOnly} size -The current context menu's size info.
|
|
|
|
*
|
|
|
|
* @returns {Object} - The new style of the context menu.
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
_getCustomDialogStyle(size: DOMRectReadOnly) {
|
|
|
|
if (this._containerRef?.current) {
|
2021-10-06 09:53:27 +00:00
|
|
|
const bounds = this._containerRef.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
return getContextMenuStyle(bounds, size, this.props.position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:22:06 +00:00
|
|
|
/**
|
2022-10-25 13:11:55 +00:00
|
|
|
* Renders the React Element to be displayed in the {@code Popover}.
|
2017-08-22 19:22:06 +00:00
|
|
|
* Also adds padding to support moving the mouse from the trigger to the
|
|
|
|
* dialog to prevent mouseleave events.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderContent() {
|
2021-12-15 13:18:41 +00:00
|
|
|
const { content } = this.props;
|
2017-08-22 19:22:06 +00:00
|
|
|
|
|
|
|
return (
|
2021-06-10 12:48:44 +00:00
|
|
|
<div
|
2021-12-15 13:18:41 +00:00
|
|
|
className = 'popover'
|
2021-06-10 12:48:44 +00:00
|
|
|
onKeyDown = { this._onEscKey }>
|
2017-08-22 19:22:06 +00:00
|
|
|
{ content }
|
2021-10-06 09:53:27 +00:00
|
|
|
{!isMobileBrowser() && (
|
|
|
|
<>
|
|
|
|
<div className = 'popover-mousemove-padding-top' />
|
|
|
|
<div className = 'popover-mousemove-padding-right' />
|
|
|
|
<div className = 'popover-mousemove-padding-left' />
|
|
|
|
<div className = 'popover-mousemove-padding-bottom' />
|
|
|
|
</>)}
|
2017-08-22 19:22:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:39:19 +00:00
|
|
|
/**
|
|
|
|
* Maps (parts of) the Redux state to the associated {@code Popover}'s props.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @param {Object} ownProps - The own props of the component.
|
|
|
|
* @private
|
|
|
|
* @returns {Props}
|
|
|
|
*/
|
2022-10-24 09:51:18 +00:00
|
|
|
function _mapStateToProps(state: IReduxState) {
|
2021-11-01 09:39:19 +00:00
|
|
|
return {
|
|
|
|
overflowDrawer: state['features/toolbox'].overflowDrawer
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(_mapStateToProps)(Popover);
|