Skip to content

Commit

Permalink
refactor: useEffect 내부 함수들을 훅 외부로 이동 및 주석 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ImxYJL committed Jul 31, 2024
1 parent 0ece398 commit 0400c77
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions frontend/src/hooks/useModalClose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ const useModalClose = (closeModal: () => void, modalBackgroundRef: RefObject<HTM
return element instanceof HTMLElement;
};

// NOTE: esc 키를 눌렀을 때 햄버거 버튼이 포커싱되는 문제 해결을 위한 함수
const blurFocusing = () => {
const activeElement = document.activeElement;

if (!isHTMLElement(activeElement)) return;
if (typeof activeElement.blur === 'function') activeElement.blur();
};

useEffect(() => {
const handleBackgroundClick = (event: MouseEvent) => {
if (isNodeElement(event.target) && isModalBackground(event.target)) {
closeModal();
}
};
const handleBackgroundClick = (event: MouseEvent) => {
if (isNodeElement(event.target) && isModalBackground(event.target)) {
closeModal();
}
};

const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
event.preventDefault();
blurFocusing();
closeModal();
}
};
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
event.preventDefault();

blurFocusing();
closeModal();
}
};

useEffect(() => {
const modalBackgroundElement = modalBackgroundRef.current;

modalBackgroundElement?.addEventListener('click', handleBackgroundClick);
Expand Down

0 comments on commit 0400c77

Please sign in to comment.