@hrnec06/react_utils 1.8.0 → 1.9.1

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.8.0",
3
+ "version": "1.9.1",
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,45 @@
1
+ import { Nullable, Optional, ReactUtils, RecordValues } from "@hrnec06/util";
2
+ import React, { useImperativeHandle, 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
+
11
+ const ShadowRoot = React.forwardRef<Optional<ShadowRoot>, ShadowRootProps>(({
12
+ children,
13
+ shadowConfig,
14
+ ...props
15
+ }, ref) => {
16
+ const hostRef = useRef<HTMLDivElement>(null);
17
+ const shadowAttached = useRef(false);
18
+
19
+ const [shadow, setShadow] = useState<Optional<ShadowRoot>>(undefined);
20
+
21
+ useImperativeHandle(ref, () => shadow, [shadow]);
22
+
23
+ useLayoutEffect(() => {
24
+ if (!hostRef.current || shadowAttached.current) return;
25
+
26
+ const shadow = hostRef.current.attachShadow(shadowConfig ?? { mode: 'open' });
27
+ setShadow(shadow);
28
+
29
+ shadowAttached.current = true;
30
+ }, []);
31
+
32
+ return (
33
+ <div
34
+ {...props}
35
+
36
+ ref={hostRef}
37
+ >
38
+ {
39
+ shadow ? createPortal(children, shadow) : null
40
+ }
41
+ </div>
42
+ )
43
+ });
44
+
45
+ export default ShadowRoot;
package/src/index.ts CHANGED
@@ -26,6 +26,8 @@ import Dialog from "./components/Dialog/Dialog";
26
26
 
27
27
  import ContextMenu from "./components/ContextMenu/ContextMenu";
28
28
 
29
+ import ShadowRoot from "./components/ShadowRoot/ShadowRoot";
30
+
29
31
  import * as util from './lib/utils';
30
32
  import ContextError from "./lib/errors/ContextError";
31
33
 
@@ -63,6 +65,9 @@ export {
63
65
  Dialog,
64
66
  ContextMenu,
65
67
 
68
+ // Shadow root
69
+ ShadowRoot,
70
+
66
71
  // Utilities
67
72
  util,
68
73
  ContextError