Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3526076
D3215.id9915.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D3215.id9915.diff
View Options
diff --git a/web/modals/modal-provider.react.js b/web/modals/modal-provider.react.js
new file mode 100644
--- /dev/null
+++ b/web/modals/modal-provider.react.js
@@ -0,0 +1,49 @@
+// @flow
+
+import invariant from 'invariant';
+import * as React from 'react';
+
+type Props = {
+ +children: React.Node,
+};
+type ModalContextType = {
+ +modal: ?React.Node,
+ +setModal: (?React.Node) => void,
+ +clearModal: () => void,
+};
+
+const ModalContext: React.Context<?ModalContextType> = React.createContext<?ModalContextType>(
+ {
+ modal: null,
+ setModal: () => {},
+ clearModal: () => {},
+ },
+);
+
+function ModalProvider(props: Props): React.Node {
+ const { children } = props;
+ const [modal, setModal] = React.useState(null);
+ const clearModal = React.useCallback(() => setModal(null), []);
+
+ const value = React.useMemo(
+ () => ({
+ modal,
+ setModal,
+ clearModal,
+ }),
+ [modal, clearModal],
+ );
+
+ return (
+ <ModalContext.Provider value={value}>{children}</ModalContext.Provider>
+ );
+}
+
+function useModalContext(): ModalContextType {
+ const context = React.useContext(ModalContext);
+ invariant(context, 'ModalContext not found');
+
+ return context;
+}
+
+export { ModalProvider, useModalContext };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Dec 24, 7:31 PM (8 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2700898
Default Alt Text
D3215.id9915.diff (1 KB)
Attached To
Mode
D3215: [web] [refactor] create modal context, use for setModal
Attached
Detach File
Event Timeline
Log In to Comment