-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathModal.tsx
More file actions
102 lines (88 loc) · 2.66 KB
/
Modal.tsx
File metadata and controls
102 lines (88 loc) · 2.66 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
97
98
99
100
101
102
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
import { useActions } from "@stackflow/react";
import { assignInlineVars } from "@vanilla-extract/dynamic";
import { useRef } from "react";
import { useLazy, useNullableActivity, useStyleEffect } from "../hooks";
import type { GlobalVars } from "../theme.css";
import { globalVars } from "../theme.css";
import { compactMap } from "../utils";
import * as css from "./Modal.css";
export type ModalProps = Partial<
Pick<GlobalVars, "backgroundColor" | "dimBackgroundColor">
> &
Partial<GlobalVars["modal"]> & {
onOutsideClick?: React.MouseEventHandler;
children: React.ReactNode;
};
const Modal = ({
backgroundColor,
dimBackgroundColor,
borderRadius = "1rem",
onOutsideClick,
children,
}: ModalProps) => {
const activity = useNullableActivity();
const { pop } = useActions();
const containerRef = useRef<HTMLDivElement>(null);
const paperRef = useRef<HTMLDivElement>(null);
useStyleEffect({
styleName: "hide",
refs: [containerRef],
});
useStyleEffect({
styleName: "offset",
refs: [paperRef],
});
useStyleEffect({
styleName: "swipe-back",
refs: [paperRef],
});
const popLock = useRef(false);
const onDimClick: React.MouseEventHandler = (e) => {
onOutsideClick?.(e);
if (e.defaultPrevented) {
return;
}
if (popLock.current) {
return;
}
popLock.current = true;
pop();
};
const onPaperClick: React.MouseEventHandler = (e) => {
e.stopPropagation();
};
const zIndexBase = (activity?.zIndex ?? 0) * 5 + 3;
const zIndexPaper = (activity?.zIndex ?? 0) * 5 + 4;
const transitionState = activity?.transitionState ?? "enter-done";
return (
<div
className={css.container({
transitionState: useLazy(transitionState),
})}
ref={containerRef}
style={assignInlineVars(
compactMap({
[globalVars.bottomSheet.borderRadius]: borderRadius,
[globalVars.backgroundColor]: backgroundColor,
[globalVars.dimBackgroundColor]: dimBackgroundColor,
[css.vars.zIndexes.dim]: `${zIndexBase}`,
[css.vars.zIndexes.paper]: `${zIndexPaper}`,
[css.vars.transitionDuration]:
transitionState === "enter-active" ||
transitionState === "exit-active"
? globalVars.transitionDuration
: "0ms",
}),
)}
>
<div className={css.dim} ref={paperRef} onClick={onDimClick}>
<div className={css.paper} onClick={onPaperClick}>
{children}
</div>
</div>
</div>
);
};
Modal.displayName = "Modal";
export default Modal;