2022-03-04 21:14:52 +00:00
|
|
|
// Copyright 2018-2022 Signal Messenger, LLC
|
2021-12-01 02:14:25 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
import type { KeyboardEvent, ReactNode } from 'react';
|
2022-08-19 18:36:47 +00:00
|
|
|
import type { Options, VirtualElement } from '@popperjs/core';
|
2022-03-04 21:14:52 +00:00
|
|
|
import FocusTrap from 'focus-trap-react';
|
2022-08-19 18:36:47 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2021-12-01 02:14:25 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { usePopper } from 'react-popper';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
|
|
|
|
import type { Theme } from '../util/theme';
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
2022-07-25 18:55:44 +00:00
|
|
|
import { getClassNamesFor } from '../util/getClassNamesFor';
|
2021-12-01 02:14:25 +00:00
|
|
|
import { themeClassName } from '../util/theme';
|
2022-09-15 01:58:35 +00:00
|
|
|
import { handleOutsideClick } from '../util/handleOutsideClick';
|
2021-12-01 02:14:25 +00:00
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
export type ContextMenuOptionType<T> = {
|
2022-03-04 21:14:52 +00:00
|
|
|
readonly description?: string;
|
2021-12-01 02:14:25 +00:00
|
|
|
readonly icon?: string;
|
|
|
|
readonly label: string;
|
2022-03-04 21:14:52 +00:00
|
|
|
readonly onClick: (value?: T) => unknown;
|
|
|
|
readonly value?: T;
|
2021-12-01 02:14:25 +00:00
|
|
|
};
|
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
export type PropsType<T> = {
|
2022-08-19 18:36:47 +00:00
|
|
|
readonly ariaLabel?: string;
|
2022-07-25 18:55:44 +00:00
|
|
|
readonly children?: ReactNode;
|
|
|
|
readonly i18n: LocalizerType;
|
|
|
|
readonly menuOptions: ReadonlyArray<ContextMenuOptionType<T>>;
|
|
|
|
readonly moduleClassName?: string;
|
|
|
|
readonly onClick?: () => unknown;
|
|
|
|
readonly onMenuShowingChanged?: (value: boolean) => unknown;
|
2022-03-04 21:14:52 +00:00
|
|
|
readonly popperOptions?: Pick<Options, 'placement' | 'strategy'>;
|
2021-12-01 02:14:25 +00:00
|
|
|
readonly theme?: Theme;
|
|
|
|
readonly title?: string;
|
2022-03-04 21:14:52 +00:00
|
|
|
readonly value?: T;
|
2021-12-01 02:14:25 +00:00
|
|
|
};
|
|
|
|
|
2022-08-12 21:32:27 +00:00
|
|
|
let closeCurrentOpenContextMenu: undefined | (() => unknown);
|
|
|
|
|
2022-08-19 18:36:47 +00:00
|
|
|
// https://popper.js.org/docs/v2/virtual-elements/
|
|
|
|
// Generating a virtual element here so that we can make the menu pop up
|
|
|
|
// right under the mouse cursor.
|
|
|
|
function generateVirtualElement(x: number, y: number): VirtualElement {
|
|
|
|
return {
|
|
|
|
getBoundingClientRect: () => ({
|
|
|
|
bottom: y,
|
|
|
|
height: 0,
|
|
|
|
left: x,
|
|
|
|
right: x,
|
|
|
|
toJSON: () => ({ x, y }),
|
|
|
|
top: y,
|
|
|
|
width: 0,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
export function ContextMenu<T>({
|
2022-08-19 18:36:47 +00:00
|
|
|
ariaLabel,
|
2022-07-25 18:55:44 +00:00
|
|
|
children,
|
|
|
|
i18n,
|
2022-03-04 21:14:52 +00:00
|
|
|
menuOptions,
|
2022-07-25 18:55:44 +00:00
|
|
|
moduleClassName,
|
|
|
|
onClick,
|
|
|
|
onMenuShowingChanged,
|
2022-03-04 21:14:52 +00:00
|
|
|
popperOptions,
|
2022-05-06 19:02:44 +00:00
|
|
|
theme,
|
2022-07-25 18:55:44 +00:00
|
|
|
title,
|
2022-03-04 21:14:52 +00:00
|
|
|
value,
|
2022-07-25 18:55:44 +00:00
|
|
|
}: PropsType<T>): JSX.Element {
|
|
|
|
const [isMenuShowing, setIsMenuShowing] = useState<boolean>(false);
|
|
|
|
const [focusedIndex, setFocusedIndex] = useState<number | undefined>(
|
|
|
|
undefined
|
|
|
|
);
|
2022-03-04 21:14:52 +00:00
|
|
|
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(
|
|
|
|
null
|
|
|
|
);
|
2022-08-19 18:36:47 +00:00
|
|
|
|
|
|
|
const virtualElement = useRef<VirtualElement>(generateVirtualElement(0, 0));
|
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
const [referenceElement, setReferenceElement] =
|
|
|
|
useState<HTMLButtonElement | null>(null);
|
|
|
|
|
2022-08-19 18:36:47 +00:00
|
|
|
const { styles, attributes } = usePopper(
|
|
|
|
virtualElement.current,
|
|
|
|
popperElement,
|
|
|
|
{
|
|
|
|
placement: 'top-start',
|
|
|
|
strategy: 'fixed',
|
|
|
|
...popperOptions,
|
|
|
|
}
|
|
|
|
);
|
2022-03-04 21:14:52 +00:00
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (onMenuShowingChanged) {
|
|
|
|
onMenuShowingChanged(isMenuShowing);
|
|
|
|
}
|
|
|
|
}, [isMenuShowing, onMenuShowingChanged]);
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isMenuShowing) {
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:58:35 +00:00
|
|
|
return handleOutsideClick(
|
|
|
|
() => {
|
2022-07-25 18:55:44 +00:00
|
|
|
setIsMenuShowing(false);
|
2022-08-12 21:32:27 +00:00
|
|
|
closeCurrentOpenContextMenu = undefined;
|
2022-09-15 01:58:35 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
{ containerElements: [referenceElement, popperElement] }
|
|
|
|
);
|
|
|
|
}, [isMenuShowing, referenceElement, popperElement]);
|
2021-12-01 02:14:25 +00:00
|
|
|
|
|
|
|
const handleKeyDown = (ev: KeyboardEvent) => {
|
2022-07-25 18:55:44 +00:00
|
|
|
if (!isMenuShowing) {
|
2021-12-01 02:14:25 +00:00
|
|
|
if (ev.key === 'Enter') {
|
|
|
|
setFocusedIndex(0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === 'ArrowDown') {
|
|
|
|
const currFocusedIndex = focusedIndex || 0;
|
|
|
|
const nextFocusedIndex =
|
|
|
|
currFocusedIndex >= menuOptions.length - 1 ? 0 : currFocusedIndex + 1;
|
|
|
|
setFocusedIndex(nextFocusedIndex);
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === 'ArrowUp') {
|
|
|
|
const currFocusedIndex = focusedIndex || 0;
|
|
|
|
const nextFocusedIndex =
|
|
|
|
currFocusedIndex === 0 ? menuOptions.length - 1 : currFocusedIndex - 1;
|
|
|
|
setFocusedIndex(nextFocusedIndex);
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === 'Enter') {
|
|
|
|
if (focusedIndex !== undefined) {
|
2022-03-04 21:14:52 +00:00
|
|
|
const focusedOption = menuOptions[focusedIndex];
|
|
|
|
focusedOption.onClick(focusedOption.value);
|
2021-12-01 02:14:25 +00:00
|
|
|
}
|
2022-07-25 18:55:44 +00:00
|
|
|
setIsMenuShowing(false);
|
2022-08-12 21:32:27 +00:00
|
|
|
closeCurrentOpenContextMenu = undefined;
|
2021-12-01 02:14:25 +00:00
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-19 18:36:47 +00:00
|
|
|
const handleClick = (ev: React.MouseEvent) => {
|
2022-08-12 21:32:27 +00:00
|
|
|
closeCurrentOpenContextMenu?.();
|
|
|
|
closeCurrentOpenContextMenu = () => setIsMenuShowing(false);
|
2022-08-19 18:36:47 +00:00
|
|
|
virtualElement.current = generateVirtualElement(ev.clientX, ev.clientY);
|
2022-07-25 18:55:44 +00:00
|
|
|
setIsMenuShowing(true);
|
2022-03-04 21:14:52 +00:00
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
};
|
2021-12-01 02:14:25 +00:00
|
|
|
|
2022-07-25 18:55:44 +00:00
|
|
|
const getClassName = getClassNamesFor('ContextMenu', moduleClassName);
|
2021-12-01 02:14:25 +00:00
|
|
|
|
|
|
|
return (
|
2022-08-03 20:54:02 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
getClassName('__container'),
|
|
|
|
theme ? themeClassName(theme) : undefined
|
|
|
|
)}
|
|
|
|
>
|
2021-12-01 02:14:25 +00:00
|
|
|
<button
|
2022-08-19 18:36:47 +00:00
|
|
|
aria-label={ariaLabel || i18n('ContextMenu--button')}
|
2022-07-25 18:55:44 +00:00
|
|
|
className={classNames(
|
|
|
|
getClassName('__button'),
|
|
|
|
isMenuShowing ? getClassName('__button--active') : undefined
|
|
|
|
)}
|
|
|
|
onClick={onClick || handleClick}
|
|
|
|
onContextMenu={handleClick}
|
2021-12-01 02:14:25 +00:00
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
2022-07-25 18:55:44 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</button>
|
|
|
|
{isMenuShowing && (
|
|
|
|
<FocusTrap
|
|
|
|
focusTrapOptions={{
|
|
|
|
allowOutsideClick: true,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className={theme ? themeClassName(theme) : undefined}>
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
getClassName('__popper'),
|
|
|
|
menuOptions.length === 1
|
|
|
|
? getClassName('__popper--single-item')
|
|
|
|
: undefined
|
|
|
|
)}
|
|
|
|
ref={setPopperElement}
|
|
|
|
style={styles.popper}
|
|
|
|
{...attributes.popper}
|
|
|
|
>
|
|
|
|
{title && <div className={getClassName('__title')}>{title}</div>}
|
|
|
|
{menuOptions.map((option, index) => (
|
|
|
|
<button
|
|
|
|
aria-label={option.label}
|
|
|
|
className={classNames(
|
|
|
|
getClassName('__option'),
|
|
|
|
focusedIndex === index
|
|
|
|
? getClassName('__option--focused')
|
|
|
|
: undefined
|
|
|
|
)}
|
|
|
|
key={option.label}
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
option.onClick(option.value);
|
|
|
|
setIsMenuShowing(false);
|
2022-08-12 21:32:27 +00:00
|
|
|
closeCurrentOpenContextMenu = undefined;
|
2022-07-25 18:55:44 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className={getClassName('__option--container')}>
|
|
|
|
{option.icon && (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
getClassName('__option--icon'),
|
|
|
|
option.icon
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<div>
|
|
|
|
<div className={getClassName('__option--title')}>
|
|
|
|
{option.label}
|
|
|
|
</div>
|
|
|
|
{option.description && (
|
|
|
|
<div className={getClassName('__option--description')}>
|
|
|
|
{option.description}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{typeof value !== 'undefined' &&
|
|
|
|
typeof option.value !== 'undefined' &&
|
|
|
|
value === option.value ? (
|
|
|
|
<div className={getClassName('__option--selected')} />
|
|
|
|
) : null}
|
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</FocusTrap>
|
2022-06-17 00:48:57 +00:00
|
|
|
)}
|
2021-12-01 02:14:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
2021-12-14 01:15:24 +00:00
|
|
|
}
|