Skip to content

Commit 75aa835

Browse files
authored
chore: repository structure (#107)
Updates the file names to index.ts in the src and tests folders
1 parent 962e049 commit 75aa835

File tree

14 files changed

+64
-139
lines changed

14 files changed

+64
-139
lines changed

.changeset/new-turtles-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@raddix/use-keyboard': minor
3+
---
4+
5+
Remove typescript types for keyboard
Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
1-
export { useKeyboard } from './use-keyboard';
1+
import type { KeyboardEvent as ReactKeyboardEvent } from 'react';
2+
import { useEffect } from 'react';
3+
4+
type Event = ReactKeyboardEvent | KeyboardEvent;
5+
type KeyboardHandler = (event: Event) => void;
6+
type KeyboardResult = (event: ReactKeyboardEvent) => void;
7+
8+
interface Options {
9+
globalEvent?: boolean;
10+
stopPropagation?: boolean;
11+
preventDefault?: boolean;
12+
checker?: 'key' | 'code';
13+
}
14+
15+
type UseKeyboard = (
16+
handler: KeyboardHandler,
17+
shortcut?: string[],
18+
options?: Options
19+
) => KeyboardResult;
20+
21+
export const useKeyboard: UseKeyboard = (handler, shortcut, options = {}) => {
22+
const {
23+
globalEvent = false,
24+
preventDefault = false,
25+
stopPropagation = true,
26+
checker = 'key'
27+
} = options;
28+
29+
const eventHandler = (event: Event) => {
30+
if (shortcut && shortcut.length > 0) {
31+
const match = shortcut.includes(event[checker]);
32+
33+
if (!match) return;
34+
}
35+
36+
if (stopPropagation) {
37+
event.stopPropagation();
38+
}
39+
40+
if (preventDefault) {
41+
event.preventDefault();
42+
}
43+
44+
handler(event);
45+
};
46+
47+
useEffect(() => {
48+
if (globalEvent) {
49+
document.addEventListener('keydown', eventHandler);
50+
}
51+
52+
return () => {
53+
document.removeEventListener('keydown', eventHandler);
54+
};
55+
// eslint-disable-next-line react-hooks/exhaustive-deps
56+
}, [globalEvent]);
57+
58+
return eventHandler;
59+
};

packages/hooks/use-keyboard/src/keys.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

packages/hooks/use-keyboard/src/use-keyboard.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)