@hrnec06/react_utils 1.11.1 → 1.11.3

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.1",
3
+ "version": "1.11.3",
4
4
  "description": "React utilities",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -0,0 +1,11 @@
1
+ import { ReactUtils } from "@hrnec06/util";
2
+
3
+ type DragFieldProps = ReactUtils.Props<{
4
+ children?: React.ReactNode,
5
+ onHover?: (item: string) => void,
6
+ onDrop?: (item: string) => void,
7
+ }>;
8
+ export default function DragField({}: DragFieldProps)
9
+ {
10
+
11
+ }
@@ -0,0 +1,10 @@
1
+ import { ReactUtils } from "@hrnec06/util";
2
+
3
+ type DragItemProps = ReactUtils.Props<{
4
+ children?: React.ReactNode,
5
+ id: string
6
+ }>;
7
+ export default function DragItem({}: DragItemProps)
8
+ {
9
+
10
+ }
@@ -0,0 +1,64 @@
1
+ import { Nullable, ReactUtils, Vector2 } from "@hrnec06/util";
2
+ import useTransition from "../../../hooks/useTransition";
3
+ import useSyncRef, { useSyncRefAuto } from "../../../hooks/useSyncRef";
4
+ import clsx from "clsx";
5
+ import { useLayoutEffect, useRef, useState } from "react";
6
+
7
+ type SmoothExpanderProps = ReactUtils.Props<{
8
+ open?: boolean
9
+ }, HTMLDivElement>;
10
+ export default function SmoothExpander({
11
+ open = false,
12
+ children,
13
+ ...props
14
+ }: SmoothExpanderProps)
15
+ {
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]);
42
+
43
+ return (
44
+ <div
45
+ {...props}
46
+
47
+ className={clsx(
48
+ "overflow-hidden relative",
49
+ )}
50
+
51
+ style={{
52
+ width: dimensions[0],
53
+ height: dimensions[1],
54
+ }}
55
+ >
56
+ <div
57
+ ref={ref}
58
+ className="absolute top-0 left-0"
59
+ >
60
+ {children}
61
+ </div>
62
+ </div>
63
+ );
64
+ }
package/src/index.ts CHANGED
@@ -32,6 +32,7 @@ import ShadowRoot from "./components/ShadowRoot/ShadowRoot";
32
32
 
33
33
  import Tooltip from "./components/UI/Tooltip/Tooltip";
34
34
  import ErrorBoundary from "./components/UI/ErrorBoundary/ErrorBoundary";
35
+ import SmoothExpander from "./components/UI/SmoothExpander/SmoothExpander";
35
36
 
36
37
  import * as util from './lib/utils';
37
38
  import ContextError from "./lib/errors/ContextError";
@@ -76,6 +77,7 @@ export {
76
77
  // UI
77
78
  Tooltip,
78
79
  ErrorBoundary,
80
+ SmoothExpander,
79
81
 
80
82
  // Shadow root
81
83
  ShadowRoot,
@@ -0,0 +1,27 @@
1
+ import { Optional } from "@hrnec06/util";
2
+ import { Context, createContext, useContext } from "react";
3
+ import ContextError from "./errors/ContextError";
4
+
5
+ interface ContextReturn<T> {
6
+ context: Context<Optional<T>>,
7
+ useContext: () => T
8
+ }
9
+ export default function createOptionalContext<T>(name: string): ContextReturn<T>
10
+ {
11
+ const context = createContext<Optional<T>>(undefined);
12
+
13
+ function useCtx(): T
14
+ {
15
+ const ctx: Optional<T> = useContext(context);
16
+
17
+ if (!ctx)
18
+ throw new ContextError(name);
19
+
20
+ return ctx;
21
+ }
22
+
23
+ return {
24
+ context: context,
25
+ useContext: useCtx
26
+ }
27
+ }