bruno/packages/grafnode-run/src/providers/Hotkeys/index.js

37 lines
792 B
JavaScript
Raw Normal View History

2022-01-24 19:27:30 +01:00
import React, { useState, useEffect } from 'react';
import Mousetrap from 'mousetrap';
export const HotkeysContext = React.createContext();
export const HotkeysProvider = props => {
useEffect(() => {
Mousetrap.bind(['command+s', 'ctrl+s'], (e) => {
console.log("Save hotkey");
return false; // this stops the event bubbling
});
return () => {
Mousetrap.unbind(['command+s', 'ctrl+s']);
};
}, []);
return (
<HotkeysContext.Provider {...props}>
{props.children}
</HotkeysContext.Provider>
);
};
export const useHotkeys = () => {
const context = React.useContext(HotkeysContext);
if (!context) {
throw new Error(`useHotkeys must be used within a HotkeysProvider`);
}
return context;
}
export default HotkeysProvider;