@loomhq/lens 10.49.0 → 10.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/hooks/index.d.ts
CHANGED
package/dist/hooks/index.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
export default function useFocusedElement(ref) {
|
|
3
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
4
|
+
const handleFocus = useCallback(e => {
|
|
5
|
+
const currentRef = ref.current;
|
|
6
|
+
if (e.type === 'focusin' && e.target === currentRef) {
|
|
7
|
+
setIsFocused(true);
|
|
8
|
+
}
|
|
9
|
+
}, [ref]);
|
|
10
|
+
const handleBlur = useCallback(e => {
|
|
11
|
+
const currentRef = ref.current;
|
|
12
|
+
if (e.type === 'focusout' && e.target === currentRef) {
|
|
13
|
+
setIsFocused(false);
|
|
14
|
+
}
|
|
15
|
+
}, [ref]);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
// Note: `focusin` is sent whenever an element OR its children gain focus (or, it supports "event bubbling"). If you need to isolate it to only the parent, create a new function using 'focus' and 'blur' respectively.
|
|
18
|
+
document.addEventListener('focusin', handleFocus);
|
|
19
|
+
document.addEventListener('focusout', handleBlur);
|
|
20
|
+
return () => {
|
|
21
|
+
document.removeEventListener('focusin', handleFocus);
|
|
22
|
+
document.removeEventListener('focusout', handleBlur);
|
|
23
|
+
};
|
|
24
|
+
}, [handleFocus, handleBlur]);
|
|
25
|
+
return Boolean(isFocused);
|
|
26
|
+
}
|