-
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.
- Loading branch information
Showing
5 changed files
with
87 additions
and
15 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,66 @@ | ||
'use client'; | ||
|
||
import { | ||
ReactElement, | ||
ReactNode, | ||
createContext, | ||
useContext, | ||
useState, | ||
} from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { Modal } from '..'; | ||
|
||
const contextDefaultValue = { | ||
isModalOpen: false, | ||
handleOpenModal: () => {}, | ||
handleCloseModal: () => {}, | ||
}; | ||
//popover 안에서 공유할 값을 context를 통해서 공유한다. | ||
const PopoverContext = createContext<{ | ||
isModalOpen: boolean; | ||
handleOpenModal: () => void; | ||
handleCloseModal: () => void; | ||
}>(contextDefaultValue); | ||
|
||
const Main = ({ children }: { children: ReactNode }) => { | ||
const [isModalOpen, setIsModalOpen] = useState<boolean>(false); | ||
const handleOpenModal = () => setIsModalOpen(true); | ||
const handleCloseModal = () => setIsModalOpen(false); | ||
return ( | ||
<PopoverContext.Provider | ||
value={{ | ||
isModalOpen, | ||
handleOpenModal, | ||
handleCloseModal, | ||
}}> | ||
{children} | ||
</PopoverContext.Provider> | ||
); | ||
}; | ||
//popover의 trigger역할 | ||
const Trigger = ({ children }: { children: ReactNode }) => { | ||
const contextValue = useContext(PopoverContext); | ||
|
||
return <div onClick={contextValue.handleOpenModal}>{children}</div>; | ||
}; | ||
//실제 모달이 들어올 컴포넌트 현재 modal 구현상 render함수를 받고 있다. | ||
const ModalContent = ({ | ||
container, | ||
children, | ||
}: { | ||
container: HTMLDivElement | null; | ||
children: (onClickNo: () => void) => ReactElement; | ||
}) => { | ||
const contextValue = useContext(PopoverContext); | ||
return ( | ||
<> | ||
{contextValue.isModalOpen && | ||
createPortal( | ||
<Modal>{children(contextValue.handleCloseModal)}</Modal>, | ||
container!, | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export { ModalContent, Trigger, Main }; |
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 @@ | ||
export * as Popover from './Popover'; |
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