-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: modal provider * feat: add body size
- Loading branch information
Showing
11 changed files
with
177 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
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,30 @@ | ||
* { | ||
box-sizing: border-box; | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
html { | ||
overflow-x: hidden; | ||
background-color: #696E77; | ||
|
||
} | ||
|
||
body { | ||
position: relative; | ||
max-width: 390px; | ||
min-height: 100svh; | ||
margin: 0 auto; | ||
overflow: hidden; | ||
|
||
} | ||
|
||
@media (min-width: 391px) { | ||
body { | ||
margin: 20px auto; | ||
border-radius: 20px; | ||
min-height: calc(100svh - 40px); | ||
overflow: hidden; | ||
|
||
} | ||
} |
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,14 @@ | ||
import { Button } from "@radix-ui/themes"; | ||
import Modal2 from "../Modal2"; | ||
import { useModal } from "@/providers/ModalProvider"; | ||
|
||
export default function Modal1() { | ||
const { open } = useModal(); | ||
|
||
return ( | ||
<div> | ||
Modal1 | ||
<Button onClick={() => open(<Modal2 />)}>SHOW MODAL 2</Button> | ||
</div> | ||
); | ||
} |
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,3 @@ | ||
export default function Modal2() { | ||
return <div>Modal2</div>; | ||
} |
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,116 @@ | ||
"use client"; | ||
|
||
import React, { useContext, useEffect, useState } from "react"; | ||
import { Portal } from "@radix-ui/themes"; | ||
import styled from "@emotion/styled"; | ||
|
||
const PortalContainer = styled(Portal)<{ isOpen: Boolean }>` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100svh; | ||
@media (min-width: 391px) { | ||
height: calc(100vh - 40px); | ||
} | ||
pointer-events: ${({ isOpen }) => (isOpen ? "auto" : "none")}; | ||
`; | ||
|
||
const Modal = styled.div<{ isOpen: Boolean }>` | ||
position: absolute; | ||
bottom: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 80svh; | ||
border: 5px solid var(--accent-9); | ||
border-radius: 10px 10px 0 0; | ||
background-color: white; | ||
transform: ${({ isOpen }) => (isOpen ? "translate3d(0, 0, 0)" : "translate3d(0, 100svh, 0)")}; | ||
transition: transform 0.1s ease-in-out; | ||
color: black; | ||
`; | ||
|
||
const Overlay = styled.div<{ isOpen: Boolean }>` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
opacity: ${({ isOpen }) => (isOpen ? 1 : 0)}; | ||
backdrop-filter: blur(3px); | ||
// background-color: rgba(var(--color-selection-root), 0.5); | ||
width: 100%; | ||
height: 100svh; | ||
@media (min-width: 391px) { | ||
height: calc(100vh - 40px); | ||
} | ||
visibility: ${({ isOpen }) => (isOpen ? "visible" : "hidden")}; | ||
transition: opacity 0.1s ease-in-out; | ||
`; | ||
|
||
function useModalHook() { | ||
const [content, setContent] = useState<React.ReactNode>(null); | ||
const [isOpen, setIsOpen] = useState<Boolean>(false); | ||
const [isBackdrop, setIsBackdrop] = useState<Boolean>(false); | ||
|
||
function open(content: React.ReactNode) { | ||
if (isOpen) { | ||
setContent(null); | ||
setIsOpen(false); | ||
setTimeout(() => { | ||
setContent(content); | ||
setIsOpen(true); | ||
}, 150); | ||
return; | ||
} | ||
|
||
setContent(content); | ||
setIsOpen(true); | ||
setIsBackdrop(true); | ||
} | ||
|
||
function close() { | ||
setContent(null); | ||
setIsOpen(false); | ||
setIsBackdrop(false); | ||
} | ||
|
||
return { | ||
content, | ||
isOpen, | ||
isBackdrop, | ||
open, | ||
close, | ||
}; | ||
} | ||
|
||
type UseModalHook = ReturnType<typeof useModalHook>; | ||
const ModalContext = React.createContext<UseModalHook | null>(null); | ||
|
||
export const useModal = (): UseModalHook => { | ||
const context = useContext(ModalContext); | ||
if (!context) { | ||
throw new Error("useModalHook must be used within a ModalHoolProvider"); | ||
} | ||
return context; | ||
}; | ||
|
||
export function ModalProvider({ children }: { children: React.ReactNode }) { | ||
const modalValue = useModalHook(); | ||
const [isPortalMounted, setIsPortalMounted] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsPortalMounted(true); | ||
}, []); | ||
|
||
const radixElement = document.getElementsByClassName("radix-themes")[0]; | ||
return ( | ||
<ModalContext.Provider value={modalValue}> | ||
{children} | ||
{isPortalMounted && ( | ||
<PortalContainer isOpen={modalValue.isOpen} container={radixElement as HTMLElement}> | ||
<Overlay isOpen={modalValue.isBackdrop} onClick={() => modalValue.close()} /> | ||
<Modal isOpen={modalValue.isOpen}>{modalValue.content}</Modal> | ||
</PortalContainer> | ||
)} | ||
</ModalContext.Provider> | ||
); | ||
} |
File renamed without changes.
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