-
Notifications
You must be signed in to change notification settings - Fork 12
/
AppealModal.tsx
208 lines (188 loc) · 5.26 KB
/
AppealModal.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, {
forwardRef,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import mergeRefs from 'react-merge-refs';
import { ButtonGroup as Group } from '../ButtonGroup';
import { CrossBold } from '../Icon';
import { IconButton } from '../IconButton';
import { SubtleButton } from '../SubtleButton';
type Size = 'large' | 'medium' | 'small';
interface AppealModalProps extends React.DialogHTMLAttributes<HTMLElement> {
children?: React.ReactNode;
size?: Size;
onCancel?: (event: React.BaseSyntheticEvent) => void;
onClose?: (event: React.BaseSyntheticEvent) => void;
}
export interface DialogHTMLElement extends HTMLElement {
close?: () => void;
showModal?: (returnValue?: string) => void;
open?: boolean;
}
const BLOCK_NAME = 'spui-AppealModal';
const FADE_OUT_ANIMATION = 'spui-AppealModal-fade-out';
const Frame = forwardRef<DialogHTMLElement, AppealModalProps>(
function AppealModal(
{ children, className, open, size = 'large', onClose, ...rest },
ref,
) {
const [closing, setClosing] = useState(false);
const dialogEl = useRef<DialogHTMLElement>(null);
const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault(); // To be closed with the open prop
onClose && onClose(event);
};
const handleDialogClose = (
event: React.SyntheticEvent<DialogHTMLElement>,
) => {
// Detect backdrop click and EscKey type
if (event.target === dialogEl.current) {
onClose && onClose(event);
setClosing(false);
}
};
const handleAnimationEnd = useCallback(
(event: AnimationEvent) => {
if (
dialogEl.current &&
event.animationName === FADE_OUT_ANIMATION &&
!event.pseudoElement // To exclude ::backdrop
) {
dialogEl.current.close && dialogEl.current.close();
}
},
[dialogEl],
);
useEffect(() => {
const dialog = dialogEl.current;
dialog?.addEventListener('animationend', handleAnimationEnd, false);
return () =>
dialog?.removeEventListener('animationend', handleAnimationEnd, false);
}, [dialogEl, handleAnimationEnd]);
useEffect(() => {
const dialog = dialogEl.current;
if (!dialog) {
return;
}
// Remove this when browsers support :has() pseudo-class
const classNameToStopScrollBehindDialog = `${BLOCK_NAME}--open`;
if (open) {
!dialog.open && dialog.showModal?.();
document.documentElement.classList.add(
classNameToStopScrollBehindDialog,
);
} else {
dialog?.open && setClosing(true);
// Always remove this class to avoid unexpected scroll stopping
document.documentElement.classList.remove(
classNameToStopScrollBehindDialog,
);
}
}, [open, dialogEl]);
return (
<dialog
className={[
BLOCK_NAME,
`${BLOCK_NAME}--${size}`,
closing && `${BLOCK_NAME}--closing`,
className,
]
.filter(Boolean)
.join(' ')
.trim()}
ref={mergeRefs([dialogEl, ref])}
onClick={handleDialogClose}
onClose={handleDialogClose}
{...rest}
>
<form
className={`${BLOCK_NAME}-frame`}
method="dialog"
onSubmit={handleFormSubmit}
>
<div className={`${BLOCK_NAME}-closeIconButton`}>
<IconButton aria-label="とじる" variant="neutral">
<CrossBold aria-hidden="true" />
</IconButton>
</div>
{children}
<div className={`${BLOCK_NAME}-closeTextButton`}>
<SubtleButton>とじる</SubtleButton>
</div>
</form>
</dialog>
);
},
);
const StyleOnly = ({
className,
children,
size = 'large',
...rest
}: React.ComponentProps<'div'> & { size?: Size }) => {
return (
<div
className={[BLOCK_NAME, `${BLOCK_NAME}--${size}`, className]
.filter(Boolean)
.join(' ')
.trim()}
{...rest}
>
<div className={`${BLOCK_NAME}-frame`}>
<div className={`${BLOCK_NAME}-closeIconButton`}>
<IconButton aria-label="閉じる" variant="neutral">
<CrossBold aria-hidden="true" />
</IconButton>
</div>
{children}
<div className={`${BLOCK_NAME}-closeTextButton`}>
<SubtleButton>閉じる</SubtleButton>
</div>
</div>
</div>
);
};
const Title = ({ children, ...rest }: React.ComponentProps<'p'>) => {
return (
<p className={`${BLOCK_NAME}-title`} {...rest}>
{children}
</p>
);
};
const Image = ({ children, ...rest }: React.ComponentProps<'div'>) => {
return (
<div className={`${BLOCK_NAME}-image`} {...rest}>
{children}
</div>
);
};
const Body = ({ children, ...rest }: React.ComponentProps<'p'>) => {
return (
<p className={`${BLOCK_NAME}-body`} {...rest}>
{children}
</p>
);
};
const ButtonGroup: typeof Group = ({ children, ...rest }) => {
return (
<Group
className={`${BLOCK_NAME}-buttonGroup`}
size="medium"
direction="column"
{...rest}
>
{children}
</Group>
);
};
export const AppealModal = {
Frame,
StyleOnly,
Title,
Image,
Body,
ButtonGroup,
};