lbry-desktop/ui/effects/use-active-element.js

21 lines
536 B
JavaScript
Raw Normal View History

2022-02-05 05:36:08 +01:00
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;
};