-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
♻️ refactor: BottomSheet 컴포넌트 코드 최소화 리팩토링
- Loading branch information
Showing
5 changed files
with
74 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,38 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import { BottomSheetDim, ModalBox, HeaderModal } from './BottomSheetStyle'; | ||
import ListModal from './ListModal'; | ||
import BasicModal from './BasicModal'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { BottomSheetDim, BottomSheetWrapper, ModalBox, ModalHandle } from './BottomSheetStyle'; | ||
|
||
// type: basic, list | ||
function BottomSheet({ type = 'basic', isShow, setIsShow, children }) { | ||
const [animate, setAnimate] = useState(false); | ||
const [localVisible, setLocalVisible] = useState(isShow); | ||
const wrapperRef = useRef(null); | ||
const modalBoxRef = useRef(null); | ||
export default function BottomSheet({ isShow, onClick, children }) { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
useEffect(() => { | ||
setLocalVisible(isShow); | ||
}, [isShow]); | ||
let modalTimer; | ||
|
||
useEffect(() => { | ||
if (localVisible && !isShow) { | ||
setAnimate(true); | ||
setTimeout(() => setAnimate(false), 250); | ||
if (isShow) { | ||
setIsVisible(true); | ||
} else { | ||
modalTimer = setTimeout(() => setIsVisible(false), 250); | ||
} // 0.25초뒤에 없어짐 | ||
setLocalVisible(isShow); | ||
}, [localVisible, isShow]); | ||
|
||
// Dim 클릭했을 때, ModalBox부분을 제외한 영역이어야 동작하도록 | ||
const handleDimClick = (e) => { | ||
if (wrapperRef.current && modalBoxRef.current && !modalBoxRef.current.contains(e.target)) { | ||
setIsShow(false); | ||
} | ||
}; | ||
|
||
// ModalBox의 HeaderModal을 클릭했을 때 동작 | ||
const handleHeaderModalClick = () => { | ||
setIsShow(false); | ||
}; | ||
return () => { | ||
if (modalTimer !== undefined) { | ||
clearTimeout(modalTimer); | ||
} | ||
}; | ||
}, [isShow]); | ||
|
||
if (!localVisible && !animate) return null; | ||
if (!isVisible) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<BottomSheetDim onClick={handleDimClick} disappear={!isShow} ref={wrapperRef}> | ||
<ModalBox disappear={!isShow} ref={modalBoxRef}> | ||
<HeaderModal onClick={handleHeaderModalClick} /> | ||
{type === 'list' ? <ListModal /> : <BasicModal>{children}</BasicModal>} | ||
<BottomSheetWrapper> | ||
<ModalBox isShow={isShow}> | ||
{children} | ||
<ModalHandle onClick={onClick} aria-label='모달 닫기' /> | ||
</ModalBox> | ||
</BottomSheetDim> | ||
<BottomSheetDim isShow={isShow} onClick={onClick} /> | ||
</BottomSheetWrapper> | ||
</> | ||
); | ||
} | ||
|
||
export default BottomSheet; |
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