Skip to content

Commit

Permalink
disable keycode back in text area
Browse files Browse the repository at this point in the history
  • Loading branch information
jessopb committed Feb 7, 2022
1 parent 9a5f69f commit 629b928
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 20 additions & 0 deletions ui/effects/use-active-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect, useState } from 'react';

export const useActiveElement = () => {
const [active, setActive] = useState(document.activeElement);

const handleFocus = (e) => {
setActive(document.activeElement);
};

useEffect(() => {
document.addEventListener('focusin', handleFocus);
document.addEventListener('focusout', handleFocus);
return () => {
document.removeEventListener('focusin', handleFocus);
document.removeEventListener('focusout', handleFocus);
};
}, []);

return active;
};
11 changes: 7 additions & 4 deletions ui/effects/use-history-nav.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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 => {
const handleKeyPress = (e) => {
if ((e.metaKey || e.altKey) && !e.ctrlKey && !e.shiftKey) {
switch (e.code) {
case 'ArrowLeft':
Expand All @@ -19,7 +20,9 @@ export default function useHistoryNav(history) {
}
}
};
window.addEventListener('keydown', handleKeyPress);
if (!el.type || (el.type && !el.type.startsWith('text'))) {
window.addEventListener('keydown', handleKeyPress);
}
return () => window.removeEventListener('keydown', handleKeyPress);
}, []);
}, [el]);
}

0 comments on commit 629b928

Please sign in to comment.