Page MenuHomePhabricator

D3215.id9908.diff
No OneTemporary

D3215.id9908.diff

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,51 @@
+// @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), [setModal]);
+
+ const contextSetModal = React.useCallback((component: ?React.Node) => {
+ setModal(component);
+ }, []);
+
+ const value = React.useMemo(() => {
+ return {
+ modal,
+ setModal: contextSetModal,
+ clearModal,
+ };
+ }, [contextSetModal, 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

Mime Type
text/plain
Expires
Wed, Dec 25, 10:14 AM (11 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2702505
Default Alt Text
D3215.id9908.diff (1 KB)

Event Timeline