-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathOverlay.tsx
More file actions
96 lines (85 loc) · 2.67 KB
/
Copy pathOverlay.tsx
File metadata and controls
96 lines (85 loc) · 2.67 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, { useCallback, useRef } from 'react';
import { useMediaQuery } from 'react-responsive';
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useModalClose } from '../../../common/useModalClose';
import type { RootState } from '../../../reducers';
import ExitIcon from '../../../images/exit.svg';
type OverlayProps = {
children?: React.ReactNode;
actions?: React.ReactNode;
closeOverlay?: () => void;
title?: string;
ariaLabel?: string;
isFixedHeight?: boolean;
isCompactHeight?: boolean;
};
export const Overlay = ({
actions,
ariaLabel = 'modal',
children,
closeOverlay,
isCompactHeight = false,
isFixedHeight = false,
title = 'Modal'
}: OverlayProps) => {
const { t } = useTranslation();
const previousPath = useSelector(
(state: RootState) => state.ide.previousPath
);
const ref = useRef<HTMLElement>(null);
const browserHistory = useHistory();
const isDesktop = useMediaQuery({ minWidth: 770 });
const isMobile = useMediaQuery({ maxWidth: 769 });
const close = useCallback(() => {
const node = ref.current;
if (!node) return;
// Only close if it is the last (and therefore the topmost overlay)
const overlays = document.getElementsByClassName('overlay');
if (node.closest('.overlay') !== overlays[overlays.length - 1]) return;
if (!closeOverlay) {
browserHistory.push(previousPath);
} else {
closeOverlay();
}
}, [previousPath, closeOverlay, ref]);
useModalClose(close, ref);
return (
<div
className={`overlay ${isFixedHeight ? 'overlay--is-fixed-height' : ''} ${
isCompactHeight ? 'overlay--is-compact-height' : ''
}`}
>
<div className="overlay__content">
<section
role="main"
aria-label={ariaLabel}
ref={ref}
className="overlay__body"
>
<header className="overlay__header">
<h2 className="overlay__title">{title}</h2>
<div className="overlay__actions">
{isDesktop && actions}
<button
className="overlay__close-button"
onClick={(event) => {
event.stopPropagation();
close();
}}
aria-label={t('Overlay.AriaLabel', { title })}
>
<ExitIcon focusable="false" aria-hidden="true" />
</button>
</div>
</header>
{isMobile && actions && (
<div className="overlay__actions-mobile">{actions}</div>
)}
{children}
</section>
</div>
</div>
);
};