-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create modal context and wrap modal component
- Loading branch information
1 parent
cb84761
commit 3afcc45
Showing
4 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ModalContextState>({ 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 ( | ||
<ModalContext.Provider value={{ inModal }}> | ||
<Modal | ||
{...modalProps} | ||
onClose={() => { | ||
setInModal(false); | ||
modalProps.onClose?.(); | ||
}} | ||
/> | ||
</ModalContext.Provider> | ||
); | ||
} | ||
|
||
// Create a custom hook to conveniently access the modal context | ||
export function useModalContext(): ModalContextState { | ||
return useContext(ModalContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters