Skip to content

Commit

Permalink
feat: BottomSheet 모달 훅 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jieunpark247 committed Jul 7, 2023
1 parent b730778 commit 5e9723a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/components/BottomSheet/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useModalControl } from './useModalControl/useModalControl';
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState, useEffect, useRef } from 'react';
import { useClickOutside } from '@/hooks';

interface Props {
onClose: () => void;
}
const useModalControl = ({ onClose }: Props) => {
const [isOpen, setIsOpen] = useState(false);
const closeStatus = useRef(false);
const closeModalWithTransition = () => {
closeStatus.current = true;
setIsOpen(false);
};
const openModalWithTransition = () => {
setIsOpen(true);
};
const ref = useClickOutside<HTMLDivElement>({
onClickOutside: () => {
closeModalWithTransition();
},
});
const handleTransitionEnd = (event: TransitionEvent) => {
if (event.propertyName === 'transform' && closeStatus.current) {
onClose();
}
};
useEffect(() => {
openModalWithTransition();
const element = ref.current;
if (!element) return;
element.addEventListener('transitionend', handleTransitionEnd);
return () => {
element.removeEventListener('transitionend', handleTransitionEnd);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return { ref, isOpen, closeModalWithTransition };
};

export default useModalControl;

0 comments on commit 5e9723a

Please sign in to comment.