forked from ipfs/ipfs-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.tsx
More file actions
55 lines (48 loc) · 1.44 KB
/
Copy pathoverlay.tsx
File metadata and controls
55 lines (48 loc) · 1.44 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
import React, { useEffect } from 'react'
import { createPortal } from 'react-dom'
export interface OverlayProps {
show: boolean
onLeave: () => void
hidden?: boolean
className?: string
children?: React.ReactNode
}
const Overlay: React.FC<OverlayProps> = ({ children, show, onLeave, className = '', hidden }) => {
useEffect(() => {
if (!show) return
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
onLeave()
}
}
document.addEventListener('keydown', handleEscape)
return () => document.removeEventListener('keydown', handleEscape)
}, [show, onLeave])
if (!show) return null
const overlay = (
<>
<div
className='fixed top-0 left-0 right-0 bottom-0 bg-black o-50'
hidden={hidden}
onClick={onLeave}
onKeyDown={(e) => e.key === 'Enter' && onLeave()}
role="button"
tabIndex={0}
aria-label="Close modal"
style={{ zIndex: 9998 }}
/>
<div
className={`${className} fixed top-0 left-0 right-0 bottom-0 flex justify-center items-center`}
style={{ zIndex: 9999, pointerEvents: 'none', padding: '2rem' }}
role="dialog"
aria-modal="true"
>
<div style={{ pointerEvents: 'auto', maxWidth: '100%', maxHeight: '100%', overflow: 'auto' }}>
{children}
</div>
</div>
</>
)
return createPortal(overlay, document.body)
}
export default Overlay