signal-desktop/ts/components/ContextMenu.tsx

357 lines
10 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 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';
import React, { useEffect, useRef, useState } from 'react';
2023-04-05 20:48:00 +00:00
import { createPortal } from 'react-dom';
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';
import { handleOutsideClick } from '../util/handleOutsideClick';
2021-12-01 02:14:25 +00:00
2022-10-18 17:12:02 +00:00
export type ContextMenuOptionType<T> = Readonly<{
description?: string;
icon?: string;
label: string;
group?: string;
onClick: (value?: T) => unknown;
value?: T;
}>;
2021-12-01 02:14:25 +00:00
2022-10-18 17:12:02 +00:00
type RenderButtonProps = Readonly<{
2022-11-04 13:22:07 +00:00
openMenu: (ev: React.MouseEvent) => void;
onKeyDown: (ev: KeyboardEvent) => void;
isMenuShowing: boolean;
ref: React.Ref<HTMLButtonElement> | null;
2022-11-04 13:22:07 +00:00
menuNode: ReactNode;
2022-10-18 17:12:02 +00:00
}>;
2021-12-01 02:14:25 +00:00
export type PropsType<T> = Readonly<{
ariaLabel?: string;
// contents of the button OR a function that will render the whole button
children?: ReactNode | ((props: RenderButtonProps) => JSX.Element);
i18n: LocalizerType;
menuOptions: ReadonlyArray<ContextMenuOptionType<T>>;
moduleClassName?: string;
button?: () => JSX.Element;
2022-11-04 13:22:07 +00:00
onClick?: (ev: React.MouseEvent) => unknown;
onMenuShowingChanged?: (value: boolean) => unknown;
popperOptions?: Pick<Options, 'placement' | 'strategy'>;
2023-04-05 20:48:00 +00:00
portalToRoot?: boolean;
theme?: Theme;
title?: string;
value?: T;
}>;
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: () => new DOMRect(x, y),
2022-08-19 18:36:47 +00:00
};
}
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,
2023-04-05 20:48:00 +00:00
portalToRoot,
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
// In Electron v23+, new elements added to the DOM may not trigger a recalculation of
// draggable regions, so if a ContextMenu is shown on top of a draggable region, its
// buttons may be unclickable. We add a class so that we can disable those draggable
// regions while the context menu is shown. It has the added benefit of ensuring that
// click events outside of the context menu onto an otherwise draggable region are
// propagated and trigger the menu to close.
useEffect(() => {
document.body.classList.toggle('context-menu-open', isMenuShowing);
}, [isMenuShowing]);
useEffect(() => {
// Remove it on unmount in case the component is unmounted when the menu is open
return () => document.body.classList.remove('context-menu-open');
}, []);
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;
}
return handleOutsideClick(
() => {
2022-07-25 18:55:44 +00:00
setIsMenuShowing(false);
2022-08-12 21:32:27 +00:00
closeCurrentOpenContextMenu = undefined;
return true;
},
2022-09-27 20:24:21 +00:00
{
containerElements: [referenceElement, popperElement],
name: 'ContextMenu',
}
);
}, [isMenuShowing, referenceElement, popperElement]);
2021-12-01 02:14:25 +00:00
2023-04-05 20:48:00 +00:00
const [portalNode, setPortalNode] = React.useState<HTMLElement | null>(null);
useEffect(() => {
if (!portalToRoot || !isMenuShowing) {
return noop;
}
const div = document.createElement('div');
document.body.appendChild(div);
setPortalNode(div);
return () => {
document.body.removeChild(div);
};
}, [portalToRoot, isMenuShowing]);
2021-12-01 02:14:25 +00:00
const handleKeyDown = (ev: KeyboardEvent) => {
if ((ev.key === 'Enter' || ev.key === 'Space') && !isMenuShowing) {
closeCurrentOpenContextMenu?.();
closeCurrentOpenContextMenu = () => setIsMenuShowing(false);
if (referenceElement) {
const box = referenceElement.getBoundingClientRect();
virtualElement.current = generateVirtualElement(box.x, box.y);
2021-12-01 02:14:25 +00:00
}
setIsMenuShowing(true);
setFocusedIndex(0);
ev.preventDefault();
ev.stopPropagation();
}
if (!isMenuShowing) {
2021-12-01 02:14:25 +00:00
return;
}
if (ev.key === 'ArrowDown' || ev.key === 'Tab') {
2021-12-01 02:14:25 +00:00
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-11-01 18:56:19 +00:00
if (ev.key === 'Escape') {
setIsMenuShowing(false);
closeCurrentOpenContextMenu = undefined;
ev.stopPropagation();
ev.preventDefault();
}
2021-12-01 02:14:25 +00:00
};
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
2022-10-18 17:12:02 +00:00
const optionElements = new Array<JSX.Element>();
const isAnyOptionSelected = typeof value !== 'undefined';
2022-10-18 17:12:02 +00:00
for (const [index, option] of menuOptions.entries()) {
const previous = menuOptions[index - 1];
const needsDivider = previous && previous.group !== option.group;
if (needsDivider) {
optionElements.push(
<div
className={getClassName('__divider')}
key={`${option.label}-divider`}
/>
);
}
// eslint-disable-next-line no-loop-func
const onElementClick = (ev: React.MouseEvent): void => {
ev.preventDefault();
ev.stopPropagation();
option.onClick(option.value);
setIsMenuShowing(false);
closeCurrentOpenContextMenu = undefined;
};
const isOptionSelected = isAnyOptionSelected && value === option.value;
2022-10-18 17:12:02 +00:00
optionElements.push(
<button
aria-label={option.label}
className={classNames(
getClassName('__option'),
focusedIndex === index ? getClassName('__option--focused') : undefined
)}
key={option.label}
type="button"
onClick={onElementClick}
>
<div
className={classNames(
getClassName('__option--container'),
isAnyOptionSelected
? getClassName('__option--container--with-selection')
: undefined,
isOptionSelected
? getClassName('__option--container--selected')
: undefined
)}
>
2022-10-18 17:12:02 +00:00
{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>
</button>
);
}
2022-11-04 13:22:07 +00:00
const menuNode = isMenuShowing ? (
<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>}
{optionElements}
</div>
</div>
) : undefined;
let buttonNode: JSX.Element;
if (typeof children === 'function') {
2023-08-09 00:53:06 +00:00
buttonNode = (
<>
{(children as (props: RenderButtonProps) => JSX.Element)({
openMenu: onClick || handleClick,
onKeyDown: handleKeyDown,
isMenuShowing,
ref: setReferenceElement,
menuNode,
})}
{portalNode ? createPortal(menuNode, portalNode) : menuNode}
</>
);
} else {
buttonNode = (
2022-11-04 13:22:07 +00:00
<div
2022-07-25 18:55:44 +00:00
className={classNames(
2022-11-04 13:22:07 +00:00
getClassName('__container'),
2022-11-04 13:22:07 +00:00
theme ? themeClassName(theme) : undefined
2022-07-25 18:55:44 +00:00
)}
>
2022-11-04 13:22:07 +00:00
<button
2023-03-30 00:03:25 +00:00
aria-label={ariaLabel || i18n('icu:ContextMenu--button')}
2022-11-04 13:22:07 +00:00
className={classNames(
getClassName('__button'),
isMenuShowing ? getClassName('__button--active') : undefined
)}
onClick={onClick || handleClick}
onContextMenu={handleClick}
onKeyDown={handleKeyDown}
ref={setReferenceElement}
type="button"
>
{children}
</button>
2023-04-05 20:48:00 +00:00
{portalNode ? createPortal(menuNode, portalNode) : menuNode}
2022-11-04 13:22:07 +00:00
</div>
);
}
2022-11-04 13:22:07 +00:00
return buttonNode;
2021-12-14 01:15:24 +00:00
}