UNPKG

882 BPlain TextView Raw
1const modals: { element: HTMLDivElement; blockScroll: boolean }[] = [];
2
3/**
4 * Handle the order of the modals.
5 * Inspired by the material-ui implementation.
6 */
7export default {
8 /**
9 * Return the modals array
10 */
11 modals: () => modals,
12
13 /**
14 * Register a new modal
15 */
16 add: (newModal: HTMLDivElement, blockScroll: boolean) => {
17 if (modals.findIndex((modal) => modal.element === newModal) === -1) {
18 modals.push({ element: newModal, blockScroll });
19 }
20 },
21
22 /**
23 * Remove a modal
24 */
25 remove: (oldModal: HTMLDivElement) => {
26 const index = modals.findIndex((modal) => modal.element === oldModal);
27 if (index !== -1) {
28 modals.splice(index, 1);
29 }
30 },
31
32 /**
33 * Check if the modal is the first one on the screen
34 */
35 isTopModal: (modal: HTMLDivElement) =>
36 !!modals.length && modals[modals.length - 1]?.element === modal,
37};