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

20 lines
536 B
JavaScript

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;
};