Skip to content

Commit

Permalink
FEAT: ✨ esc key 눌리면 닫히도록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hoongding committed Sep 29, 2024
1 parent 67c5f4d commit c233d79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/TabList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useGesture } from '@use-gesture/react';
import { useRef, useState } from 'react';

import { ICategoryResponseDataType, useDeleteCategory } from '@/apis/category';
import useKeyEvent from '@/hooks/useKeyEvent';
import useOnClickOutside from '@/hooks/useOnClickOutside';
import useQueryString from '@/hooks/useQueyString';
import { cn } from '@/lib/utils';
Expand Down Expand Up @@ -102,8 +103,8 @@ const TabList = ({ tabs = [] }: ITabList) => {
},
);

useOnClickOutside([modalRef, selectRef, categoryEditRef], () => setIsControlModalOpen(false));

useOnClickOutside([modalRef, selectRef, categoryEditRef], () => handleCloseModal());
useKeyEvent({ targetKey: ['Escape'], callback: handleCloseModal });
return (
<div className='h-40 relative w-[calc(100%-100px)]'>
<ul
Expand Down
16 changes: 16 additions & 0 deletions src/hooks/useKeyEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import useEventListener from './useEventListener';

interface useKeyEventType {
targetKey: string[];
callback: () => void;
}

export default function useKeyEvent({ targetKey, callback }: useKeyEventType) {
const handleKeyDown = (e: KeyboardEvent) => {
if (targetKey.includes(e.key)) {
callback();
}
};

useEventListener('keydown', handleKeyDown);
}

0 comments on commit c233d79

Please sign in to comment.