forked from facebook/react
/
ModalDialog.js
160 lines (140 loc) · 3.87 KB
/
ModalDialog.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import {
createContext,
useCallback,
useContext,
useMemo,
useReducer,
useRef,
} from 'react';
import Button from './Button';
import {useModalDismissSignal} from './hooks';
import styles from './ModalDialog.css';
type DIALOG_ACTION_HIDE = {|
type: 'HIDE',
|};
type DIALOG_ACTION_SHOW = {|
type: 'SHOW',
canBeDismissed?: boolean,
content: React$Node,
title?: React$Node | null,
|};
type Action = DIALOG_ACTION_HIDE | DIALOG_ACTION_SHOW;
type Dispatch = (action: Action) => void;
type State = {|
canBeDismissed: boolean,
content: React$Node | null,
isVisible: boolean,
title: React$Node | null,
|};
type ModalDialogContextType = {|
...State,
dispatch: Dispatch,
|};
const ModalDialogContext = createContext<ModalDialogContextType>(
((null: any): ModalDialogContextType),
);
ModalDialogContext.displayName = 'ModalDialogContext';
function dialogReducer(state, action) {
switch (action.type) {
case 'HIDE':
return {
canBeDismissed: true,
content: null,
isVisible: false,
title: null,
};
case 'SHOW':
return {
canBeDismissed: action.canBeDismissed !== false,
content: action.content,
isVisible: true,
title: action.title || null,
};
default:
throw new Error(`Invalid action "${action.type}"`);
}
}
type Props = {|
children: React$Node,
|};
function ModalDialogContextController({children}: Props) {
const [state, dispatch] = useReducer<State, Action>(dialogReducer, {
canBeDismissed: true,
content: null,
isVisible: false,
title: null,
});
const value = useMemo<ModalDialogContextType>(
() => ({
canBeDismissed: state.canBeDismissed,
content: state.content,
isVisible: state.isVisible,
title: state.title,
dispatch,
}),
[state, dispatch],
);
return (
<ModalDialogContext.Provider value={value}>
{children}
</ModalDialogContext.Provider>
);
}
function ModalDialog(_: {||}) {
const {isVisible} = useContext(ModalDialogContext);
return isVisible ? <ModalDialogImpl /> : null;
}
function ModalDialogImpl(_: {||}) {
const {canBeDismissed, content, dispatch, title} = useContext(
ModalDialogContext,
);
const dismissModal = useCallback(() => {
if (canBeDismissed) {
dispatch({type: 'HIDE'});
}
}, [canBeDismissed, dispatch]);
const dialogRef = useRef<HTMLDivElement | null>(null);
// It's important to trap click events within the dialog,
// so the dismiss hook will use it for click hit detection.
// Because multiple tabs may be showing this ModalDialog,
// the normal `dialog.contains(target)` check would fail on a background tab.
useModalDismissSignal(dialogRef, dismissModal, false);
// Clicks on the dialog should not bubble.
// This way we can dismiss by listening to clicks on the background.
const handleDialogClick = (event: any) => {
event.stopPropagation();
// It is important that we don't also prevent default,
// or clicks within the dialog (e.g. on links) won't work.
};
return (
<div className={styles.Background} onClick={dismissModal}>
<div
ref={dialogRef}
className={styles.Dialog}
onClick={handleDialogClick}>
{title !== null && <div className={styles.Title}>{title}</div>}
{content}
{canBeDismissed && (
<div className={styles.Buttons}>
<Button
autoFocus={true}
className={styles.Button}
onClick={dismissModal}>
Okay
</Button>
</div>
)}
</div>
</div>
);
}
export {ModalDialog, ModalDialogContext, ModalDialogContextController};