-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace zustand with context for modals
- Loading branch information
Showing
13 changed files
with
189 additions
and
155 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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
"use client"; | ||
|
||
import { createContext, Dispatch, ReactNode, SetStateAction } from "react"; | ||
|
||
import { useSignInModal } from "@/components/modals//sign-in-modal"; | ||
|
||
export const ModalContext = createContext<{ | ||
setShowSignInModal: Dispatch<SetStateAction<boolean>>; | ||
}>({ | ||
setShowSignInModal: () => {}, | ||
}); | ||
|
||
export default function ModalProvider({ children }: { children: ReactNode }) { | ||
const { SignInModal, setShowSignInModal } = useSignInModal(); | ||
|
||
return ( | ||
<ModalContext.Provider | ||
value={{ | ||
setShowSignInModal, | ||
}} | ||
> | ||
<SignInModal /> | ||
{children} | ||
</ModalContext.Provider> | ||
); | ||
} |
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 was deleted.
Oops, something went wrong.
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,99 @@ | ||
"use client"; | ||
|
||
import { Dispatch, SetStateAction } from "react"; | ||
// import { useRouter } from "next/router"; | ||
import { Drawer } from "vaul"; | ||
|
||
import { Dialog, DialogContent } from "@/components/ui/dialog"; | ||
import { useMediaQuery } from "@/hooks/use-media-query"; | ||
import { cn } from "@/lib/utils"; | ||
|
||
interface ModalProps { | ||
children: React.ReactNode; | ||
className?: string; | ||
showModal?: boolean; | ||
setShowModal?: Dispatch<SetStateAction<boolean>>; | ||
onClose?: () => void; | ||
desktopOnly?: boolean; | ||
preventDefaultClose?: boolean; | ||
} | ||
|
||
export function Modal({ | ||
children, | ||
className, | ||
showModal, | ||
setShowModal, | ||
onClose, | ||
desktopOnly, | ||
preventDefaultClose, | ||
}: ModalProps) { | ||
// const router = useRouter(); | ||
|
||
const closeModal = ({ dragged }: { dragged?: boolean } = {}) => { | ||
if (preventDefaultClose && !dragged) { | ||
return; | ||
} | ||
// fire onClose event if provided | ||
onClose && onClose(); | ||
|
||
// if setShowModal is defined, use it to close modal | ||
if (setShowModal) { | ||
setShowModal(false); | ||
} | ||
// else, this is intercepting route @modal | ||
// else { | ||
// router.back(); | ||
// } | ||
}; | ||
const { isMobile } = useMediaQuery(); | ||
|
||
if (isMobile && !desktopOnly) { | ||
return ( | ||
<Drawer.Root | ||
open={setShowModal ? showModal : true} | ||
onOpenChange={(open) => { | ||
if (!open) { | ||
closeModal({ dragged: true }); | ||
} | ||
}} | ||
> | ||
<Drawer.Overlay className="fixed inset-0 z-40 bg-background/80 backdrop-blur-sm" /> | ||
<Drawer.Portal> | ||
<Drawer.Content | ||
className={cn( | ||
"fixed inset-x-0 bottom-0 z-50 mt-24 overflow-hidden rounded-t-[10px] border bg-background", | ||
className, | ||
)} | ||
> | ||
<div className="sticky top-0 z-20 flex w-full items-center justify-center bg-inherit"> | ||
<div className="my-3 h-1.5 w-16 rounded-full bg-muted-foreground/20" /> | ||
</div> | ||
{children} | ||
</Drawer.Content> | ||
<Drawer.Overlay /> | ||
</Drawer.Portal> | ||
</Drawer.Root> | ||
); | ||
} | ||
return ( | ||
<Dialog | ||
open={setShowModal ? showModal : true} | ||
onOpenChange={(open) => { | ||
if (!open) { | ||
closeModal(); | ||
} | ||
}} | ||
> | ||
<DialogContent | ||
onOpenAutoFocus={(e) => e.preventDefault()} | ||
onCloseAutoFocus={(e) => e.preventDefault()} | ||
className={cn( | ||
"overflow-hidden p-0 md:max-w-md md:rounded-2xl md:border", | ||
className, | ||
)} | ||
> | ||
{children} | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
Oops, something went wrong.