diff --git a/package-lock.json b/package-lock.json index 31209b44..4ef887b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "reablocks", - "version": "7.4.8", + "version": "7.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "reablocks", - "version": "7.4.8", + "version": "7.5.0", "license": "Apache-2.0", "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", @@ -22,7 +23,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", @@ -4111,6 +4111,14 @@ } } }, + "node_modules/@reaviz/ctrl-keys": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@reaviz/ctrl-keys/-/ctrl-keys-1.0.0.tgz", + "integrity": "sha512-NYjTYVV7Mnd4uRnvQFrp8JskBzrvak1Owi2BAYlZQdBloIittg7lbz+T7CCFKjEYD69bwLckoI98rFrFO3a1Uw==", + "engines": { + "node": ">=10" + } + }, "node_modules/@reaviz/react-use-fuzzy": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/@reaviz/react-use-fuzzy/-/react-use-fuzzy-1.0.3.tgz", @@ -14555,11 +14563,6 @@ "node": ">=0.4.0" } }, - "node_modules/mousetrap": { - "version": "1.6.5", - "resolved": "https://registry.npmjs.org/mousetrap/-/mousetrap-1.6.5.tgz", - "integrity": "sha512-QNo4kEepaIBwiT8CDhP98umTetp+JNfQYBWvC1pc6/OAibuXtRcxZ58Qz8skvEHYvURne/7R8T5VoOI7rDsEUA==" - }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index cc411a8a..c46ec1fe 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/src/elements/CommandPalette/CommandPaletteInput/CommandPaletteInput.tsx b/src/elements/CommandPalette/CommandPaletteInput/CommandPaletteInput.tsx index 2543db03..ea50d6fb 100644 --- a/src/elements/CommandPalette/CommandPaletteInput/CommandPaletteInput.tsx +++ b/src/elements/CommandPalette/CommandPaletteInput/CommandPaletteInput.tsx @@ -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 { /** @@ -78,6 +78,8 @@ export const CommandPaletteInput: FC = ({ theme: customTheme }) => { const inputRef = useRef(null); + const handlerRef = useRef(keys()); + const keyMapRef = useRef>(new Map()); useLayoutEffect(() => { if (autoFocus) { @@ -87,17 +89,24 @@ export const CommandPaletteInput: FC = ({ }, [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(