diff --git a/src/components/BeamContext.tsx b/src/components/BeamContext.tsx index b45111dad..850acdc29 100644 --- a/src/components/BeamContext.tsx +++ b/src/components/BeamContext.tsx @@ -7,9 +7,10 @@ import { SnackbarProvider } from "src/components/Snackbar/SnackbarContext"; import { SuperDrawer } from "src/components/SuperDrawer/SuperDrawer"; import { ContentStack } from "src/components/SuperDrawer/useSuperDrawer"; import { CanCloseCheck, CheckFn } from "src/types"; -import { EmptyRef } from "src/utils/index"; +import { EmptyRef, isDefined } from "src/utils/index"; import { RightPaneProvider } from "./Layout"; import { ToastProvider } from "./Toast/ToastContext"; +import { ModalContext, ModalProvider } from "./Modal/ModalContext"; /** The internal state of our Beam context; see useModal and useSuperDrawer for the public APIs. */ export interface BeamContextState { @@ -100,7 +101,7 @@ export function BeamProvider({ children, ...presentationProps }: BeamProviderPro {children} - {modalRef.current && } + {modalRef.current && } diff --git a/src/components/Modal/Modal.stories.tsx b/src/components/Modal/Modal.stories.tsx index 4e0b9b10f..ee2afbe6a 100644 --- a/src/components/Modal/Modal.stories.tsx +++ b/src/components/Modal/Modal.stories.tsx @@ -125,7 +125,7 @@ export function InModalState() {
- Open: {inModal() ? "YES" : "NO"} + Open: {inModal ? "YES" : "NO"} ); } diff --git a/src/components/Modal/ModalContext.tsx b/src/components/Modal/ModalContext.tsx new file mode 100644 index 000000000..1dff40e92 --- /dev/null +++ b/src/components/Modal/ModalContext.tsx @@ -0,0 +1,37 @@ +// ModalContext.tsx + +import React, { createContext, useContext, useState } from "react"; +import { Modal, ModalProps } from "./Modal"; + +// Define the type for the modal context +interface ModalContextState { + inModal: boolean; +} + +// Create a context for the modal state +export const ModalContext = createContext({ inModal: false }); + +// Create a provider component to wrap your app and provide the modal state +interface ModalProviderProps { + modalProps: ModalProps; +} + +export function ModalProvider({ modalProps }: ModalProviderProps) { + const [inModal, setInModal] = useState(true); + return ( + + { + setInModal(false); + modalProps.onClose?.(); + }} + /> + + ); +} + +// Create a custom hook to conveniently access the modal context +export function useModalContext(): ModalContextState { + return useContext(ModalContext); +} diff --git a/src/components/Modal/useModal.tsx b/src/components/Modal/useModal.tsx index d9d2563f4..7f4bb41e0 100644 --- a/src/components/Modal/useModal.tsx +++ b/src/components/Modal/useModal.tsx @@ -3,17 +3,20 @@ import { useBeamContext } from "src/components/BeamContext"; import { CheckFn } from "src/types"; import { isDefined, maybeCall } from "src/utils"; import { ModalApi, ModalProps } from "./Modal"; +import { useModalContext } from "./ModalContext"; export interface UseModalHook { openModal: (props: ModalProps) => void; closeModal: VoidFunction; addCanClose: (canClose: CheckFn) => void; setSize: (size: ModalProps["size"]) => void; - inModal: () => boolean; + inModal: boolean; } export function useModal(): UseModalHook { const { modalState, modalCanCloseChecks } = useBeamContext(); + const { inModal } = useModalContext(); + console.log("useModal", { inModal }); const lastCanClose = useRef(); const api = useRef(); useEffect(() => { @@ -52,8 +55,8 @@ export function useModal(): UseModalHook { modalState.current.api.current.setSize(size); } }, - inModal: () => isDefined(modalState.current), + inModal, }), - [modalState, modalCanCloseChecks], + [inModal, modalState, modalCanCloseChecks], ); }