DEV Community

Discussion on: Forem: Keyboard shortcuts

Collapse
 
link2twenty profile image
Andrew Bone • Edited

I like those maybe add message (M) too?

Twitter use chaining for certain actions like going to a certain page is g then another letter which is something we can theoretically do with the hook.

Collapse
 
reobin profile image
Robin Gagnon

Chaining sound super nice! vim-like 🌞

Thread Thread
 
link2twenty profile image
Andrew Bone

If that's a road we wanna go down it might be worth including chaining in the hook currently the only way to do it would be something like.

const [chaining, setChaining] = useState([]);
const [chainingQueue, setChainingQueue] = useState([]);

const pushChaining = useCallback((item)=>{
  setChaining([...chaining, item]);
},[chaining])

useEffect(()=>{ 
  let timeout;

  timeout = window.setTimeout(()=>{
    setChaining([]);
  }, 1500);

  if(!chainingQueue) return;
  pushChaining(chainingQueue);
  setChainingQueue(null)

  return ()=>clearTimeout(timeout);
},[chainingQueue, pushChaining]);

useKeyboardShortcut({
  KeyG: setChainingQueue('KeyG'),
  KeyH: (chaining === ['KeyG'] && function)
})
Enter fullscreen mode Exit fullscreen mode

Which is a bit convoluted to do over and over again.