lbry-desktop/ui/effects/use-history-nav.js

29 lines
882 B
JavaScript
Raw Normal View History

import { useEffect } from 'react';
2022-02-05 05:36:08 +01:00
import { useActiveElement } from './use-active-element';
export default function useHistoryNav(history) {
2022-02-05 05:36:08 +01:00
const el = useActiveElement(); // disable if we're in a textarea.
useEffect(() => {
2022-02-05 05:36:08 +01:00
const handleKeyPress = (e) => {
if ((e.metaKey || e.altKey) && !e.ctrlKey && !e.shiftKey) {
switch (e.code) {
case 'ArrowLeft':
e.preventDefault();
history.goBack();
break;
case 'ArrowRight':
e.preventDefault();
history.goForward();
break;
default:
// Do nothing
break;
}
}
};
2022-02-05 05:36:08 +01:00
if (!el.type || (el.type && !el.type.startsWith('text'))) {
window.addEventListener('keydown', handleKeyPress);
}
return () => window.removeEventListener('keydown', handleKeyPress);
2022-02-05 05:36:08 +01:00
}, [el]);
}