Skip to content

Commit

Permalink
refactor: modal 역할별 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
Zero-1016 committed May 16, 2024
1 parent 4ff2923 commit c9a626c
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 98 deletions.
2 changes: 1 addition & 1 deletion app/(site)/@modal/(.)i/image/[...info]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MovieImageModal } from '@/entities/modal'

type Props = {
params: { info: [string, 'poster' | 'backdrop', string] }
params: { info: [movieId: string, 'poster' | 'backdrop', imageUrl: string] }
}

export default function Page({ params }: Props) {
Expand Down
10 changes: 4 additions & 6 deletions src/entities/modal/MovieDetailModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ type Props = {

export function MovieDetailModal({ movieId }: Props) {
return (
<Modal>
<Modal.Content>
<Suspense fallback={<MovieDetailContentSkeleton />}>
<MovieDetailContent movieId={movieId} />
</Suspense>
</Modal.Content>
<Modal isClose>
<Suspense fallback={<MovieDetailContentSkeleton />}>
<MovieDetailContent movieId={movieId} />
</Suspense>
</Modal>
)
}
24 changes: 10 additions & 14 deletions src/entities/modal/MovieImageModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ export function MovieImageModal({ imageUrl, imageType, movieId }: Props) {
},
}

const imageSrc =
imageType === 'backdrop'
? getImageUrl(imageType, '/' + imageUrl, 'w1280')
: getImageUrl(imageType, '/' + imageUrl, 'w500')

console.log(imageSrc)

Check warning on line 31 in src/entities/modal/MovieImageModal.tsx

View workflow job for this annotation

GitHub Actions / qodana

ESLint

ESLint: Unexpected console statement. (no-console)

return (
<Modal>
<Modal.Content>
<div className={styles.container}>
<Image
src={
imageType === 'backdrop'
? getImageUrl('backdrop', '/' + imageUrl, 'w1280')
: getImageUrl('poster', '/' + imageUrl, 'w500')
}
alt={movieId + imageType}
width={size[imageType].width}
height={size[imageType].height}
/>
</div>
</Modal.Content>
<div className={styles.container}>
<Image src={imageSrc} alt={movieId + imageType} width={size[imageType].width} height={size[imageType].height} />
</div>
</Modal>
)
}
6 changes: 1 addition & 5 deletions src/entities/modal/SignInModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Modal } from '@/shared/ui'

export function SignInModal() {
return (
<Modal>
<Modal.Content></Modal.Content>
</Modal>
)
return <Modal></Modal>
}
6 changes: 1 addition & 5 deletions src/entities/modal/SignUpModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Modal } from '@/shared/ui'

export function SignUpModal() {
return (
<Modal>
<Modal.Content></Modal.Content>
</Modal>
)
return <Modal></Modal>
}
2 changes: 2 additions & 0 deletions src/shared/hook/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { useBlockScroll } from './use-block-scroll'
export { useDragHandler } from './use-drag-handler'
export { useHistoryBack } from './use-history-back'
2 changes: 2 additions & 0 deletions src/shared/hook/use-block-scroll.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { useEffect } from 'react'

export function useBlockScroll() {
Expand Down
20 changes: 20 additions & 0 deletions src/shared/hook/use-history-back.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

import { useRouter } from 'next/navigation'

import { SITE_PATH } from '@/shared/constants'

export function useHistoryBack() {
const router = useRouter()
const history = typeof window !== 'undefined' ? window.history : []

const onClickBack = () => {
if (history.length) {
router.back()
} else {
router.push(SITE_PATH.home, { scroll: true })
}
}

return { onClickBack }
}
20 changes: 20 additions & 0 deletions src/shared/ui/CloseButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

import Image from 'next/image'

import { useHistoryBack } from '@/shared/hook'
import styles from '@/shared/ui/close-button.module.scss'

export function CloseButton() {
const { onClickBack } = useHistoryBack()
return (
<Image
onClick={onClickBack}
className={styles.closeButton}
width={50}
height={50}
src="/svg/close.svg"
alt="닫기 버튼"
/>
)
}
63 changes: 13 additions & 50 deletions src/shared/ui/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,23 @@
'use client'
import { PropsWithChildren } from 'react'

import Image from 'next/image'
import { useRouter } from 'next/navigation'
import { PropsWithChildren, useEffect } from 'react'

import { SITE_PATH } from '@/shared/constants'
import RQProvider from '@/shared/lib/react-query/RQProvider'
import { getScrollbarWidth } from '@/shared/lib/util'
import { CloseButton } from '@/shared/ui/CloseButton'
import { ModalBackGround } from '@/shared/ui/ModalBackGround'

import styles from './modal.module.scss'

function Content({ children }: PropsWithChildren) {
const onInnerClick = (e: React.MouseEvent) => {
e.stopPropagation()
}

return (
<div onClick={onInnerClick} className={styles.modalContent}>
{children}
</div>
)
type Props = {
isClose?: boolean
}

function ModalBackGround({ children }: Readonly<PropsWithChildren>) {
const router = useRouter()
const history = typeof window !== 'undefined' ? window.history : []

useEffect(() => {
const width = getScrollbarWidth()

if (Object.is(width, undefined)) return

document.body.style.overflow = 'hidden'
document.body.style.marginRight = `${width}px`
return () => {
document.body.style.overflow = 'auto'
document.body.style.marginRight = '0px'
}
}, [])

const onClickBack = () => {
if (history.length) {
router.back()
} else {
router.push(SITE_PATH.home, { scroll: true })
}
}

export function Modal({ children, isClose = false }: Readonly<PropsWithChildren<Props>>) {
return (
<RQProvider>
<button className={styles.container} onClick={onClickBack}>
<Image className={styles.closeButton} src="/public/svg/close.svg" alt="닫기 버튼" />
{children}
</button>
</RQProvider>
<section className={styles.container}>
<ModalBackGround />
{isClose && <CloseButton />}
<RQProvider>
<div className={styles.content}>{children}</div>
</RQProvider>
</section>
)
}

export const Modal = Object.assign(ModalBackGround, { Content })
11 changes: 11 additions & 0 deletions src/shared/ui/ModalBackGround.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client'

import { useBlockScroll, useHistoryBack } from '@/shared/hook'
import styles from '@/shared/ui/modal.module.scss'

export function ModalBackGround() {
useBlockScroll()
const { onClickBack } = useHistoryBack()

return <div onClick={onClickBack} className={styles.background} />
}
9 changes: 9 additions & 0 deletions src/shared/ui/close-button.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.closeButton {
cursor: pointer;
z-index: 9990;
width: 80px;
height: 80px;
position: absolute;
top: 5%;
right: 5%;
}
30 changes: 16 additions & 14 deletions src/shared/ui/modal.module.scss
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
.container {
position: fixed;
top: 0;
left: 0;
z-index: 9990;
width: 100%;
height: 100dvh;
}

.background {
cursor: pointer;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
z-index: 9000;
background: rgba(0, 0, 0, 0.5);
width: 100%;
height: 100dvh;
}

.modalContent {
.content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -60%);
z-index: 9991;
transform: translate(-50%, -50%);
width: 700px;
height: 400px;
height: fit-content;
min-height: 400px;
border-radius: 20px;
border: 2px solid #fffffe;
padding: 20px;
background: #101820;
}

.closeButton {
z-index: 9990;
width: 80px;
height: 80px;
position: absolute;
top: 5%;
right: 5%;
}
}
4 changes: 1 addition & 3 deletions src/widgets/detail/DetailBottomSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ type Props = {
export function DetailBottomSection({ movieId }: Props) {
return (
<section>
<Suspense>
<Suspense fallback={null}>
<MovieImagesSlide movieId={movieId} />
</Suspense>
<Suspense>
<MovieContentForm movieId={movieId} />
</Suspense>
</section>
Expand Down

0 comments on commit c9a626c

Please sign in to comment.