overall updates
This commit is contained in:
26
src/components/nearle_components/useHotkeyFocus.js
Normal file
26
src/components/nearle_components/useHotkeyFocus.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export const useHotkeyFocus = (ref, hotkey = 'k') => {
|
||||
useEffect(() => {
|
||||
if (!ref?.current) return;
|
||||
|
||||
const handleKeyPress = (event) => {
|
||||
const isHotkey = event.key.toLowerCase() === hotkey.toLowerCase();
|
||||
|
||||
// CTRL + Hotkey
|
||||
if (isHotkey && (event.metaKey || event.ctrlKey)) {
|
||||
event.preventDefault();
|
||||
ref.current?.focus();
|
||||
}
|
||||
|
||||
// ESC to blur
|
||||
if (event.key === 'Escape' && document.activeElement === ref.current) {
|
||||
ref.current.blur();
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('keydown', handleKeyPress);
|
||||
|
||||
return () => document.removeEventListener('keydown', handleKeyPress);
|
||||
}, [ref, hotkey]);
|
||||
};
|
||||
Reference in New Issue
Block a user