forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.ts
More file actions
36 lines (32 loc) · 1.14 KB
/
functions.ts
File metadata and controls
36 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { PREVENT_SCROLL } from "utils/constants";
export const START_BUTTON_TITLE = "Start";
export const SEARCH_BUTTON_TITLE = "Type here to search";
export const maybeCloseTaskbarMenu = (
{ relatedTarget: focusedElement }: React.FocusEvent<HTMLElement>,
menuElement: HTMLElement | null,
toggleMenu: (toggle: boolean) => void,
focusElement?: HTMLElement | null,
buttonTitle?: string,
closeOnTaskbarEntries = false
): void => {
const focusedInsideMenu =
focusedElement && menuElement?.contains(focusedElement);
if (!focusedInsideMenu) {
const taskbarElement = menuElement?.nextSibling;
const focusedTaskbarEntries = focusedElement === taskbarElement;
const focusedTaskbarButton =
focusedElement?.parentElement === taskbarElement;
const focusedOnSelfButton =
(focusedElement as HTMLElement)?.title === buttonTitle;
if (
focusedElement &&
((closeOnTaskbarEntries && focusedTaskbarEntries) ||
(!focusedTaskbarEntries &&
(!focusedTaskbarButton || !focusedOnSelfButton)))
) {
toggleMenu(false);
} else {
(focusElement || menuElement)?.focus(PREVENT_SCROLL);
}
}
};