@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.
@@ -1,2 +1,3 @@
1
1
  export { default as useMedia } from "./use-media.js";
2
2
  export { default as useOnClickOutside } from "./use-on-click-outside.js";
3
+ export { default as useFocusedElement } from "./use-focused-element";
@@ -1,2 +1,3 @@
1
1
  export { default as useMedia } from './use-media.js';
2
2
  export { default as useOnClickOutside } from './use-on-click-outside.js';
3
+ export { default as useFocusedElement } from './use-focused-element';
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export default function useFocusedElement<Element extends HTMLElement>(ref: React.RefObject<Element>): boolean;
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomhq/lens",
3
- "version": "10.49.0",
3
+ "version": "10.50.0",
4
4
  "scripts": {
5
5
  "dev": "next",
6
6
  "build:next": "next build",