DEV Community

Discussion on: Forem: Keyboard shortcuts

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.