-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.tsx
104 lines (92 loc) · 2.86 KB
/
index.tsx
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
97
98
99
100
101
102
103
104
import type { FC, MouseEvent } from "react";
import { useState, useEffect } from "react";
import usePortal from "react-cool-portal";
import GitHubCorner from "../GitHubCorner";
import styles from "./styles.module.scss";
const App: FC = () => {
const [isFadeOut, setIsFadeOut] = useState(false);
const { Portal, show, hide, isShow } = usePortal({
defaultShow: false,
escToHide: false,
});
const close = () => {
setIsFadeOut(true);
};
const handleClickBackdrop = (e: MouseEvent) => {
const { id } = e.target as HTMLDivElement;
if (id === "modal" || id === "modal-dialog") close();
};
const handleAnimEnd = () => {
if (!isFadeOut) return;
setIsFadeOut(false);
hide();
};
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (isShow && e.key === "Escape") close();
};
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [isShow]);
return (
<div className={styles.container}>
<GitHubCorner url="https://github.com/wellyshen/react-cool-portal" />
<h1 className={styles.title}>REACT COOL PORTAL</h1>
<p className={styles.subtitle}>
{
"React hook for Portals, which renders modals, dropdowns, tooltips etc. to <body> or else."
}
</p>
<button className={styles.btn} onClick={show} type="button">
OPEN MODAL
</button>
<Portal>
<div
id="modal"
className={`${styles.modal} ${
isFadeOut ? styles["modal-fade-out"] : ""
}`}
onClick={handleClickBackdrop}
onAnimationEnd={handleAnimEnd}
tabIndex={-1}
role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div
id="modal-dialog"
className={styles["modal-dialog"]}
role="document"
>
<div className={styles["modal-content"]}>
<div className={styles["modal-header"]}>
<h5 className={styles["modal-title"]} id="exampleModalLabel">
<span role="img" aria-label="Hello">
👋🏻
</span>{" "}
Hola
</h5>
<button
className={styles["modal-close"]}
onClick={close}
type="button"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
</div>
<div className={styles["modal-body"]}>
<p>
You can also close me by pressing the "ESC" key.
</p>
</div>
</div>
</div>
</div>
</Portal>
</div>
);
};
export default App;