Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move off Mousetrap #163

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"homepage": "https://github.com/reaviz/reablocks#readme",
"dependencies": {
"@marko19907/string-to-color": "^1.0.0",
"@reaviz/ctrl-keys": "^1.0.0",
"@reaviz/react-use-fuzzy": "^1.0.3",
"body-scroll-lock-upgrade": "^1.1.0",
"chroma-js": "^2.4.2",
Expand All @@ -67,7 +68,6 @@
"framer-motion": "^10.16.16",
"fuse.js": "^6.6.2",
"human-format": "^1.2.0",
"mousetrap": "^1.6.5",
"name-initials": "^0.1.3",
"pluralize": "^8.0.0",
"popper.js": "^1.16.1",
Expand All @@ -83,13 +83,13 @@
"react-dom": ">=16"
},
"devDependencies": {
"@storybook/manager-api": "^8.0.8",
"@storybook/preview-api": "^8.0.8",
"@storybook/addon-docs": "^8.1.0-alpha.0",
"@storybook/addon-essentials": "^8.1.0-alpha.0",
"@storybook/addon-mdx-gfm": "^8.1.0-alpha.0",
"@storybook/addon-storysource": "^8.1.0-alpha.0",
"@storybook/addon-themes": "^8.1.0-alpha.0",
"@storybook/manager-api": "^8.0.8",
"@storybook/preview-api": "^8.0.8",
"@storybook/react": "^8.1.0-alpha.0",
"@storybook/react-vite": "^8.1.0-alpha.0",
"@storybook/theming": "^8.1.0-alpha.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import React, {
} from 'react';
import { SearchIcon } from './SearchIcon';
import { HotkeyIem } from '../useFlattenedTree';
import Mousetrap from 'mousetrap';
import { CommandPaletteTheme } from '../CommandPaletteTheme';
import { useComponentTheme } from '../../../utils';
import keys, { Handler } from '@reaviz/ctrl-keys';

export interface CommandPaletteInputProps {
/**
Expand Down Expand Up @@ -78,6 +78,8 @@ export const CommandPaletteInput: FC<CommandPaletteInputProps> = ({
theme: customTheme
}) => {
const inputRef = useRef<HTMLInputElement | null>(null);
const handlerRef = useRef<Handler>(keys());
const keyMapRef = useRef<Map<string, any>>(new Map());

useLayoutEffect(() => {
if (autoFocus) {
Expand All @@ -87,17 +89,24 @@ export const CommandPaletteInput: FC<CommandPaletteInputProps> = ({
}, [autoFocus]);

useEffect(() => {
if (inputRef.current) {
const mousetrap = new Mousetrap(inputRef.current);
if (typeof window !== 'undefined') {
const handler = handlerRef.current;
const keyMap = keyMapRef.current;

for (const hotkey of hotkeys) {
mousetrap.bind(hotkey.hotkey, () => onHotkey(hotkey));
const callback = () => onHotkey(hotkey);
handler.add(hotkey.hotkey, callback);
keyMap.set(hotkey.hotkey, callback);
}
}

return () => {
hotkeys.forEach(hotkey => Mousetrap.unbind(hotkey.hotkey));
};
window.addEventListener('keydown', handler.handle);

return () => {
[...keyMap].forEach(([key, cb]) => handler.remove(key, cb));
window.removeEventListener('keydown', handler.handle);
keyMapRef.current = new Map();
};
}
}, [onHotkey, hotkeys]);

const { input: inputTheme }: CommandPaletteTheme = useComponentTheme(
Expand Down
Loading