Comments (12)
I thought about this a bit longer because I stumbled over the same need to have all hotkeys in one place and I came to the conclusion that this shouldn't be a feature at all.
Because this feature request reverses the source of truth.
You need to know the hotkey combinations and their functions beforehand anyways. So instead of returning the assigned hotkeys from the hook, you should have a hotkey map and use the values to assign the useHotkeys hooks.
Pseudocode wise it would look something like this:
// Somewhere in your project.
const hotkeyMap = {
"PRINT": "Shift+F12",
// ...
};
// ...
// in your component
function PrintComponent() {
useHotkeys(hotkeyMap['PRINT'], printHandler);
return (
// ...
);
}
This way you can achieve exactly what you want and the hook stays clean. You could also have multiple functionalities for the same hotkey stroke and still differentiate between them which you cannot when the hook would have to keep track of it.
from react-hotkeys-hook.
@amcdnl This hook is just a wrapper around the hotkeys package. So that would be a feature to request there.
from react-hotkeys-hook.
Actually you could just handle this in your registration hook. Heres a use case I'm using react-hotkeys for:
import React, { useState } from 'react';
import { Dialog } from 'shared/Dialog';
import { GlobalHotKeys, getApplicationKeyMap } from 'react-hotkeys';
import { startCase } from 'lodash-es';
import css from './HotkeyDialog.module.scss';
const KEY_MAP = {
SHOW: 'shift+?'
};
export const HotkeyDialog = ({ children }) => {
const [visible, setVisible] = useState(false);
const keys = getApplicationKeyMap() || [];
return (
<GlobalHotKeys
keyMap={KEY_MAP}
handlers={{
SHOW: () => setVisible(true)
}}
>
<Dialog
size="550px"
header="Hotkeys"
open={visible}
onClose={() => setVisible(false)}
>
{visible && (
<ul className={css.list}>
{Object.keys(keys).map(k => (
<li key={k} className={css.listItem}>
<strong>{startCase(k)}</strong>:{' '}
{keys[k].sequences.map(kk => (
<span key={kk.sequence} className={css.keyCombo}>
{kk.sequence}
</span>
))}
</li>
))}
</ul>
)}
</Dialog>
{children}
</GlobalHotKeys>
);
};
from react-hotkeys-hook.
I'll think about it. Generally I don't like to add global state to a modular hook. Although you could argue that the hotkeys package itself is acting globally. I know that this would be a useful feature, but I still think it would be better to add it to the core hotkeys package.
from react-hotkeys-hook.
Ya - I don't like how he did that implementation. I would do something more like context or something like this: https://github.com/streamich/react-use/blob/master/docs/createGlobalState.md
from react-hotkeys-hook.
@amcdnl Regarding #273 we have to come up with some kind of api that returns the values.
I was thinking about something like this:
const [hotkeysRef, getRegisteredHotkeys] = useHotkeys(....);
but I am not sure if it wouldn't be better to have an object returning. Thing is the naming of the return objects, I am not quite sure about them.
Have you some thoughts on that?
from react-hotkeys-hook.
I think the thats a fine api.
from react-hotkeys-hook.
The what?
from react-hotkeys-hook.
I changed my mind on this.
Using 2.3.0 the hook returns an RefObject that can be set to any element taking a ref. Then the callback will only get executed when the reffed object is focused.
But retrieving all registered hotkeys will be a different hook, since its more of a meta information.
@amcdnl What do you want to retrieve? Just the registered keys? Because the actual textual information what it does isn't saved currently. Would you use the hotkeys scopes for that?
from react-hotkeys-hook.
So with:
const [hotkeysRef, getRegisteredHotkeys] = useHotkeys(....);
I would expect getRegisteredHotkeys
to return all the hotkeys that are in the current state of the document. Might be helpful to return in that object the context of what they are attached to but probably not deal breaker.
from react-hotkeys-hook.
Hmh...
I was thinking about something like a useRegisteredHotkeys
hook that just gives you all registered hotkeys. Not quite sure how we could save the context. Do you mean the ref that they are attached to?
from react-hotkeys-hook.
I also stumbled across another edge case.
What about multiple assigned hotkeys?
If an app for example has multiple hotkeys ctrl+m
in different contexts, how should we return that?
from react-hotkeys-hook.
Related Issues (20)
- [BUG] Attaching resulting ref to a different element stops the shortcuts/hotkeys from working HOT 6
- [BUG] `option` Key No Longer Registers in v4? HOT 2
- [BUG] cannot trigger hot key when pressing "," (comma). HOT 1
- slash, backslash, and bracketright are not defined correctly in parseHotkeys.ts HOT 9
- [BUG] meta on Mac does not capture cmd like the docs say HOT 3
- What is v4.5.0? HOT 1
- [BUG] Working locally, but not in production HOT 6
- [BUG]The keyboard combination doesn't work in focused input HOT 2
- [PSA] You probably want `equal` instead of `+` (plus/bracketright) HOT 1
- The useRecordHotkeys hook should allow us to programatically set the keys HOT 1
- [BUG] Does not handle the 'Alt Graph' modifier key HOT 1
- Match a key pressed by character (optionally maybe) instead of keyCode HOT 1
- [BUG] Hotkeys stuck on `contextmenu` event. HOT 1
- Several entries in mappedKeys look suspicious HOT 2
- [BUG] preventDefault seems not work when use `command + s ` HOT 2
- [BUG] Why is # key mapped to Backslash HOT 1
- [BUG] Callback called on each matched key in array HOT 1
- [BUG] hotKeys trigger from input inside WebComponent HOT 2
- [BUG] isHotkeyPressed requires ALL buttons to be pressed, not just at least one
- [BUG] v5.0.0-1, "useKey" option also seems to (partially) activate "ignoreModifiers" HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hotkeys-hook.