lbry-desktop/ui/effects/use-history-nav.js
2022-02-06 20:11:59 -05:00

28 lines
882 B
JavaScript

import { useEffect } from 'react';
import { useActiveElement } from './use-active-element';
export default function useHistoryNav(history) {
const el = useActiveElement(); // disable if we're in a textarea.
useEffect(() => {
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;
}
}
};
if (!el.type || (el.type && !el.type.startsWith('text'))) {
window.addEventListener('keydown', handleKeyPress);
}
return () => window.removeEventListener('keydown', handleKeyPress);
}, [el]);
}