2022-09-15 01:58:35 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { RefObject } from 'react';
|
2023-06-21 23:28:31 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-09-15 01:58:35 +00:00
|
|
|
|
2023-06-21 23:28:31 +00:00
|
|
|
export type HandlerType = (target: Node) => boolean;
|
|
|
|
export type HandlersType = {
|
|
|
|
name: string;
|
|
|
|
handleClick: HandlerType;
|
|
|
|
handlePointerDown: HandlerType;
|
|
|
|
};
|
2022-09-15 01:58:35 +00:00
|
|
|
export type ContainerElementType = Node | RefObject<Node> | null | undefined;
|
|
|
|
|
|
|
|
// TODO(indutny): DESKTOP-4177
|
|
|
|
// A stack of handlers. Handlers are executed from the top to the bottom
|
2023-06-21 23:28:31 +00:00
|
|
|
const fakeHandlers = new Array<HandlersType>();
|
|
|
|
|
|
|
|
export type HandleOutsideClickOptionsType = Readonly<{
|
2022-09-29 20:13:45 +00:00
|
|
|
name: string;
|
2023-06-21 23:28:31 +00:00
|
|
|
containerElements: ReadonlyArray<ContainerElementType>;
|
|
|
|
}>;
|
2022-09-15 01:58:35 +00:00
|
|
|
|
2023-06-21 23:28:31 +00:00
|
|
|
function handleGlobalPointerDown(event: MouseEvent) {
|
|
|
|
for (const handlers of fakeHandlers) {
|
|
|
|
// continue even if handled, so that we can detect if the click was inside
|
|
|
|
handlers.handlePointerDown(event.target as Node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleGlobalClick(event: MouseEvent) {
|
|
|
|
for (const handlers of fakeHandlers.slice().reverse()) {
|
|
|
|
const handled = handlers.handleClick(event.target as Node);
|
|
|
|
if (handled) {
|
|
|
|
log.info(`handleOutsideClick: ${handlers.name} handled click`);
|
2022-09-15 01:58:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:28:31 +00:00
|
|
|
const eventOptions = { capture: true };
|
2022-09-15 01:58:35 +00:00
|
|
|
|
|
|
|
export const handleOutsideClick = (
|
2023-06-21 23:28:31 +00:00
|
|
|
handler: HandlerType,
|
2022-09-29 20:13:45 +00:00
|
|
|
{ name, containerElements }: HandleOutsideClickOptionsType
|
2022-09-15 01:58:35 +00:00
|
|
|
): (() => void) => {
|
2023-06-21 23:28:31 +00:00
|
|
|
function isInside(target: Node) {
|
|
|
|
return containerElements.some(elem => {
|
2022-09-15 01:58:35 +00:00
|
|
|
if (!elem) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (elem instanceof Node) {
|
|
|
|
return elem.contains(target);
|
|
|
|
}
|
|
|
|
return elem.current?.contains(target);
|
|
|
|
});
|
2023-06-21 23:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let startedInside = false;
|
2022-09-15 01:58:35 +00:00
|
|
|
|
2023-06-21 23:28:31 +00:00
|
|
|
function handlePointerDown(target: Node) {
|
|
|
|
startedInside = isInside(target);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleClick(target: Node) {
|
|
|
|
const endedInside = isInside(target);
|
2022-09-27 20:24:21 +00:00
|
|
|
// Clicked inside of one of container elements - stop processing
|
2023-06-21 23:28:31 +00:00
|
|
|
if (startedInside || endedInside) {
|
|
|
|
return false;
|
2022-09-15 01:58:35 +00:00
|
|
|
}
|
2022-09-27 20:24:21 +00:00
|
|
|
// Stop processing if requested by handler function
|
|
|
|
return handler(target);
|
2023-06-21 23:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fakeHandler = {
|
|
|
|
name,
|
|
|
|
handleClick,
|
|
|
|
handlePointerDown,
|
2022-09-15 01:58:35 +00:00
|
|
|
};
|
|
|
|
|
2023-06-21 23:28:31 +00:00
|
|
|
fakeHandlers.push(fakeHandler);
|
|
|
|
|
|
|
|
if (fakeHandlers.length === 1) {
|
|
|
|
document.addEventListener(
|
|
|
|
'pointerdown',
|
|
|
|
handleGlobalPointerDown,
|
|
|
|
eventOptions
|
|
|
|
);
|
|
|
|
document.addEventListener('click', handleGlobalClick, eventOptions);
|
2022-09-15 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
2023-06-21 23:28:31 +00:00
|
|
|
const index = fakeHandlers.indexOf(fakeHandler);
|
|
|
|
fakeHandlers.splice(index, 1);
|
2022-09-15 01:58:35 +00:00
|
|
|
|
2023-06-21 23:28:31 +00:00
|
|
|
if (fakeHandlers.length === 0) {
|
|
|
|
document.removeEventListener(
|
|
|
|
'pointerdown',
|
|
|
|
handleGlobalPointerDown,
|
|
|
|
eventOptions
|
|
|
|
);
|
|
|
|
document.removeEventListener('click', handleGlobalClick, eventOptions);
|
2022-09-15 01:58:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|