@hrnec06/react_utils 1.7.4 → 1.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hrnec06/react_utils",
3
- "version": "1.7.4",
3
+ "version": "1.9.0",
4
4
  "description": "React utilities",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -26,7 +26,7 @@
26
26
  "typescript": "^5.8.3"
27
27
  },
28
28
  "dependencies": {
29
- "@hrnec06/util": "^1.6.1",
29
+ "@hrnec06/util": "^1.6.2",
30
30
  "clsx": "^2.1.1",
31
31
  "lucide-react": "^0.525.0",
32
32
  "react": "^19.1.0",
@@ -0,0 +1,41 @@
1
+ import { Nullable, ReactUtils, RecordValues } from "@hrnec06/util";
2
+ import React, { useLayoutEffect, useRef, useState } from "react";
3
+ import useDefaultValue from "../../hooks/useDefaultValue";
4
+ import { createPortal } from "react-dom";
5
+
6
+ type ShadowRootProps = ReactUtils.Props<{
7
+ children?: React.ReactNode,
8
+ shadowConfig?: ShadowRootInit,
9
+ }, HTMLDivElement>
10
+ export default function ShadowRoot({
11
+ children,
12
+ shadowConfig,
13
+ ...props
14
+ }: ShadowRootProps)
15
+ {
16
+ const ref = useRef<HTMLDivElement>(null);
17
+ const shadowAttached = useRef(false);
18
+
19
+ const [shadow, setShadow] = useState<Nullable<ShadowRoot>>(null);
20
+
21
+ useLayoutEffect(() => {
22
+ if (!ref.current || shadowAttached.current) return;
23
+
24
+ const shadow = ref.current.attachShadow(shadowConfig ?? { mode: 'open' });
25
+ setShadow(shadow);
26
+
27
+ shadowAttached.current = true;
28
+ }, []);
29
+
30
+ return (
31
+ <div
32
+ {...props}
33
+
34
+ ref={ref}
35
+ >
36
+ {
37
+ shadow ? createPortal(children, shadow) : null
38
+ }
39
+ </div>
40
+ )
41
+ }
@@ -0,0 +1,17 @@
1
+ import { Nullable } from "@hrnec06/util";
2
+ import useEvent from "./useEvent";
3
+ import useListener from "./useListener";
4
+
5
+ export default function useOutsideClick(
6
+ element: Nullable<HTMLElement>,
7
+ callback: (element: MouseEvent) => void
8
+ )
9
+ {
10
+ const cb = useEvent(callback);
11
+
12
+ useListener(document, 'mousedown', (event) => {
13
+ if (!element || !(event.target instanceof Node) || element.contains(event.target)) return;
14
+
15
+ cb(event);
16
+ }, [cb, element]);
17
+ }
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import useEfficientRef from "./hooks/useEfficientRef";
8
8
  import useUUID from "./hooks/useUUID";
9
9
  import useFlags from "./hooks/useFlags";
10
10
  import useNamespacedId from "./hooks/useNamespacedId";
11
+ import useOutsideClick from "./hooks/useOutsideClick";
11
12
  import useTransition, { transitionDataAttributes } from "./hooks/useTransition";
12
13
  import useLocalStorage from "./hooks/useLocalStorage";
13
14
  import useDebounce from "./hooks/useDebounce";
@@ -25,10 +26,11 @@ import Dialog from "./components/Dialog/Dialog";
25
26
 
26
27
  import ContextMenu from "./components/ContextMenu/ContextMenu";
27
28
 
29
+ import ShadowRoot from "./components/ShadowRoot/ShadowRoot";
30
+
28
31
  import * as util from './lib/utils';
29
32
  import ContextError from "./lib/errors/ContextError";
30
33
 
31
-
32
34
  export {
33
35
  // Hooks
34
36
  useDebounce,
@@ -42,6 +44,7 @@ export {
42
44
  useListener,
43
45
  useLocalStorage,
44
46
  useNamespacedId,
47
+ useOutsideClick,
45
48
  useSyncRef,
46
49
  useSyncRefAuto,
47
50
  useTransition,
@@ -62,6 +65,9 @@ export {
62
65
  Dialog,
63
66
  ContextMenu,
64
67
 
68
+ // Shadow root
69
+ ShadowRoot,
70
+
65
71
  // Utilities
66
72
  util,
67
73
  ContextError