@hrnec06/react_utils 1.11.2 → 1.11.4

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.11.2",
3
+ "version": "1.11.4",
4
4
  "description": "React utilities",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -1,8 +1,8 @@
1
- import { Nullable, ReactUtils } from "@hrnec06/util";
1
+ import { Nullable, ReactUtils, Vector2 } from "@hrnec06/util";
2
2
  import useTransition from "../../../hooks/useTransition";
3
3
  import useSyncRef, { useSyncRefAuto } from "../../../hooks/useSyncRef";
4
4
  import clsx from "clsx";
5
- import { useRef } from "react";
5
+ import { useLayoutEffect, useRef, useState } from "react";
6
6
 
7
7
  type SmoothExpanderProps = ReactUtils.Props<{
8
8
  open?: boolean
@@ -14,17 +14,44 @@ export default function SmoothExpander({
14
14
  }: SmoothExpanderProps)
15
15
  {
16
16
  const ref = useRef<HTMLDivElement>(null);
17
+ const [dimensions, setDimensions] = useState<Vector2>([0, 0]);
18
+
19
+ useLayoutEffect(() => {
20
+ if (!ref.current || !open)
21
+ {
22
+ setDimensions([0, 0]);
23
+
24
+ return;
25
+ }
26
+
27
+ const observer = new ResizeObserver(() => {
28
+ if (!ref.current) return;
29
+
30
+ setDimensions([
31
+ ref.current.clientWidth,
32
+ ref.current.clientHeight
33
+ ]);
34
+ });
35
+
36
+ observer.observe(ref.current);
37
+
38
+ return () => {
39
+ observer.disconnect();
40
+ }
41
+ }, [ref.current, open]);
17
42
 
18
43
  return (
19
44
  <div
20
45
  {...props}
21
46
 
22
47
  className={clsx(
23
- "overflow-hidden transition-all relative",
48
+ "overflow-hidden relative",
49
+ props.className
24
50
  )}
25
51
 
26
52
  style={{
27
- height: open && ref.current ? ref.current.clientHeight : 0
53
+ width: dimensions[0],
54
+ height: dimensions[1],
28
55
  }}
29
56
  >
30
57
  <div
@@ -34,5 +61,5 @@ export default function SmoothExpander({
34
61
  {children}
35
62
  </div>
36
63
  </div>
37
- )
64
+ );
38
65
  }