-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature/FE] 모달 컴포넌트 구현 및 alert, confirm에 적용 #308
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,87 @@ | ||
import Modal from '@/components/common/Modal/Modal'; | ||
import { PropsWithChildren, useState } from 'react'; | ||
|
||
export default { | ||
component: Modal, | ||
title: 'Components/Modal', | ||
}; | ||
|
||
type Props = { | ||
showConfirm: boolean; | ||
}; | ||
|
||
const Template = ({ showConfirm, children }: PropsWithChildren<Props>) => { | ||
const [show, setShow] = useState(false); | ||
|
||
const handleClose = () => { | ||
setShow(false); | ||
}; | ||
const handleSubmit = () => { | ||
alert('제출됨'); | ||
handleClose(); | ||
}; | ||
return ( | ||
<> | ||
<button onClick={() => setShow(true)}>표시하기</button> | ||
{show && ( | ||
<Modal | ||
handleClose={handleClose} | ||
handleConfirm={showConfirm && handleSubmit} | ||
> | ||
{children} | ||
</Modal> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export const Default = () => ( | ||
<Template showConfirm={true}> | ||
<Modal.Title>제목</Modal.Title> | ||
<Modal.Body> | ||
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dolorum eveniet | ||
sint magnam nemo? Laboriosam animi non error veritatis, illo temporibus | ||
dolorum omnis alias repellat aperiam. | ||
</Modal.Body> | ||
</Template> | ||
); | ||
|
||
export const NoTitle = () => ( | ||
<Template showConfirm={true}> | ||
<Modal.Body> | ||
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dolorum eveniet | ||
sint magnam nemo? Laboriosam animi non error veritatis, illo temporibus | ||
dolorum omnis alias repellat aperiam. A illum doloremque voluptas modi | ||
eaque iste voluptate ullam corrupti quibusdam id fugiat, maiores | ||
reprehenderit labore ipsam nemo, aliquam cumque facere nostrum libero fuga | ||
unde. | ||
</Modal.Body> | ||
</Template> | ||
); | ||
|
||
export const NoConfirm = () => ( | ||
<Template showConfirm={false}> | ||
<Modal.Title>제목</Modal.Title> | ||
<Modal.Body> | ||
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dolorum eveniet | ||
sint magnam nemo? Laboriosam animi non error veritatis, illo temporibus | ||
dolorum omnis alias repellat aperiam. A illum doloremque voluptas modi | ||
eaque iste voluptate ullam corrupti quibusdam id fugiat, maiores | ||
reprehenderit labore ipsam nemo, aliquam cumque facere nostrum libero fuga | ||
unde. | ||
</Modal.Body> | ||
</Template> | ||
); | ||
|
||
export const NoTitleAndConfirm = () => ( | ||
<Template showConfirm={false}> | ||
<Modal.Body> | ||
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dolorum eveniet | ||
sint magnam nemo? Laboriosam animi non error veritatis, illo temporibus | ||
dolorum omnis alias repellat aperiam. A illum doloremque voluptas modi | ||
eaque iste voluptate ullam corrupti quibusdam id fugiat, maiores | ||
reprehenderit labore ipsam nemo, aliquam cumque facere nostrum libero fuga | ||
unde. | ||
</Modal.Body> | ||
</Template> | ||
); |
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,82 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Container = styled.section<{ scrollOffset: number }>` | ||
position: absolute; | ||
top: ${({ scrollOffset }) => scrollOffset}px; | ||
left: 0; | ||
width: 100vw; | ||
height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
`; | ||
|
||
export const Backdrop = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
z-index: 10; | ||
background-color: #00000033; | ||
height: 100%; | ||
`; | ||
|
||
export const Content = styled.section` | ||
width: 30rem; | ||
min-height: 10rem; | ||
padding: 1.5rem; | ||
position: relative; | ||
z-index: 15; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
gap: 1.5rem; | ||
border-radius: 0.5rem; | ||
filter: drop-shadow(2px 2px 5px rgba(0, 0, 0, 0.25)); | ||
background-color: ${({ theme }) => theme.colors.white}; | ||
`; | ||
|
||
export const Title = styled.h1` | ||
font-size: 1.5rem; | ||
`; | ||
|
||
export const Body = styled.div``; | ||
|
||
export const ButtonContainer = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
gap: 2rem; | ||
`; | ||
|
||
export const ActionButton = styled.button` | ||
padding: 0.5rem 1rem; | ||
border-radius: 0.3rem; | ||
border: none; | ||
filter: drop-shadow(1px 1px 2px rgba(0, 0, 0, 0.25)); | ||
&:hover { | ||
filter: drop-shadow(2px 2px 4px rgba(0, 0, 0, 0.25)); | ||
} | ||
`; | ||
|
||
export const ConfirmButton = styled(ActionButton)` | ||
background-color: ${({ theme }) => theme.colors.primary}; | ||
`; | ||
|
||
export const CloseButton = styled(ActionButton)` | ||
background-color: ${({ theme }) => theme.colors.secondary}; | ||
`; |
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,68 @@ | ||
import { createPortal } from 'react-dom'; | ||
import * as S from '@/components/common/Modal/Modal.style'; | ||
import { PropsWithChildren, useEffect, useState } from 'react'; | ||
|
||
type Props = { | ||
handleClose: () => void; | ||
handleConfirm?: () => void; | ||
}; | ||
|
||
function Modal({ | ||
handleClose, | ||
handleConfirm, | ||
children, | ||
}: PropsWithChildren<Props>) { | ||
const [scrollOffset, setScrollOffset] = useState(0); | ||
|
||
useEffect(() => { | ||
document.body.style.overflow = 'hidden'; | ||
setScrollOffset(window.pageYOffset); | ||
|
||
return () => { | ||
document.body.style.overflow = 'auto'; | ||
}; | ||
}, []); | ||
|
||
return createPortal( | ||
<S.Container scrollOffset={scrollOffset}> | ||
<S.Backdrop onClick={handleClose} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backdrop 👍🏻 |
||
<S.Content> | ||
{children} | ||
<ActionButtons | ||
handleClose={handleClose} | ||
handleConfirm={handleConfirm} | ||
/> | ||
</S.Content> | ||
</S.Container>, | ||
document.querySelector('#root') | ||
); | ||
} | ||
|
||
function Title({ children }: PropsWithChildren) { | ||
return <S.Title>{children}</S.Title>; | ||
} | ||
|
||
function Body({ children }: PropsWithChildren) { | ||
return <S.Body>{children}</S.Body>; | ||
} | ||
|
||
type ActionButtonProps = { | ||
handleClose: () => void; | ||
handleConfirm?: () => void; | ||
}; | ||
|
||
function ActionButtons({ handleClose, handleConfirm }: ActionButtonProps) { | ||
return ( | ||
<S.ButtonContainer> | ||
<S.CloseButton onClick={handleClose}>닫기</S.CloseButton> | ||
{handleConfirm && ( | ||
<S.ConfirmButton onClick={handleConfirm}>확인</S.ConfirmButton> | ||
)} | ||
</S.ButtonContainer> | ||
); | ||
} | ||
|
||
Modal.Title = Title; | ||
Modal.Body = Body; | ||
|
||
export default Modal; |
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,60 @@ | ||
import Modal from '@/components/common/Modal/Modal'; | ||
import { createContext, PropsWithChildren, useState } from 'react'; | ||
|
||
export const ShowAlertContext = createContext<(message: string) => void>(null); | ||
export const GetConfirmContext = | ||
createContext<(message: string) => Promise<boolean>>(null); | ||
|
||
let resolveConfirm: (value: boolean | PromiseLike<boolean>) => void; | ||
function ModalContextProvider({ children }: PropsWithChildren) { | ||
const [message, setMessage] = useState(''); | ||
const [alertOpen, setAlertOpen] = useState(false); | ||
const [confirmOpen, setConfirmOpen] = useState(false); | ||
|
||
const showAlert = (message: string) => { | ||
setMessage(message); | ||
setAlertOpen(true); | ||
}; | ||
const showConfirm = (message: string) => { | ||
setMessage(message); | ||
setConfirmOpen(true); | ||
}; | ||
|
||
const handleClose = () => { | ||
setAlertOpen(false); | ||
setConfirmOpen(false); | ||
}; | ||
|
||
const handleConfirm = () => { | ||
resolveConfirm(true); | ||
setConfirmOpen(false); | ||
}; | ||
|
||
const getConfirm: (message: string) => Promise<boolean> = (message) => { | ||
showConfirm(message); | ||
|
||
return new Promise((resolve) => { | ||
resolveConfirm = resolve; | ||
}); | ||
}; | ||
|
||
return ( | ||
<ShowAlertContext.Provider value={showAlert}> | ||
<GetConfirmContext.Provider value={getConfirm}> | ||
{children} | ||
{alertOpen && ( | ||
<Modal handleClose={handleClose}> | ||
<Modal.Body>{message}</Modal.Body> | ||
</Modal> | ||
)} | ||
{confirmOpen && ( | ||
<Modal handleClose={handleClose} handleConfirm={handleConfirm}> | ||
<Modal.Body>{message}</Modal.Body> | ||
</Modal> | ||
)} | ||
</GetConfirmContext.Provider> | ||
</ShowAlertContext.Provider> | ||
); | ||
} | ||
|
||
export default ModalContextProvider; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추후에 alert 메시지를 모아서 상수 처리하면 좋을 것 같습니다