Skip to content

Commit

Permalink
feat: add keyboard shortcut for unselect button
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Apr 26, 2024
1 parent d3a2a12 commit 99f3a41
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/components/buttons/unselect/unselect.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useCallback } from 'react';
import { BiUndo, BiTrash } from 'react-icons/bi/index';
import { AnimatePresence, motion } from 'framer-motion';

Expand All @@ -20,6 +21,23 @@ export function UnselectButton() {
exit: { opacity: 0 },
};

const handleToggle = useCallback(() => {
if (hasHistory) restoreHistory();
else if (!noSelected) unselectAll(true);
}, [hasHistory, noSelected, unselectAll, restoreHistory]);

useEffect(() => {
const listener = (e: KeyboardEvent) => {
if (e.shiftKey && e.key === 'R') {
handleToggle();
}
};

document.addEventListener('keydown', listener);

return () => document.removeEventListener('keydown', listener);
}, [handleToggle]);

return (
<>
<AnimatePresence mode="wait">
Expand Down Expand Up @@ -49,10 +67,7 @@ export function UnselectButton() {
styles.unselectButton,
noSelected && !hasHistory && styles.disabled,
)}
onClick={() => {
if (hasHistory) restoreHistory();
else if (!noSelected) unselectAll(true);
}}
onClick={handleToggle}
>
{hasHistory ? <BiUndo /> : <BiTrash />}
</button>
Expand Down

0 comments on commit 99f3a41

Please sign in to comment.