summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components/KeyboardNavigation.tsx
blob: 3aefd7c68480dbed7fcd42efdbe396690f15bcd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import React, { useCallback, useEffect } from 'react';
import { useHistory } from 'react-router';
import History from '../lib/history';

interface IKeyboardNavigationProps {
  children: React.ReactElement;
}

export default function KeyboardNavigation(props: IKeyboardNavigationProps) {
  const history = useHistory() as History;

  const handleKeyDown = useCallback(
    (event: KeyboardEvent) => {
      if (event.key === 'Escape') {
        history.reset();
      }
    },
    [history.reset],
  );

  useEffect(() => {
    document.addEventListener('keydown', handleKeyDown);
    return () => document.removeEventListener('keydown', handleKeyDown);
  }, [handleKeyDown]);

  return props.children;
}