@hrnec06/react_utils 1.11.0 → 1.11.2
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 +1 -1
- package/src/components/UI/DragNDrop/DragField.tsx +11 -0
- package/src/components/UI/DragNDrop/DragItem.tsx +10 -0
- package/src/components/UI/ErrorBoundary/ErrorBoundary.tsx +18 -2
- package/src/components/UI/SmoothExpander/SmoothExpander.tsx +38 -0
- package/src/index.ts +2 -0
- package/src/lib/createContext.ts +27 -0
package/package.json
CHANGED
|
@@ -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
|
+
}
|
|
@@ -3,8 +3,10 @@ import useSignal, { Signal } from "../../../hooks/useSignal";
|
|
|
3
3
|
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";
|
|
4
4
|
import React from "react";
|
|
5
5
|
|
|
6
|
+
type FallbackType = React.ReactNode | ((error: Error) => React.ReactNode);
|
|
7
|
+
|
|
6
8
|
type ErrorBoundaryProps = ReactUtils.Props<{
|
|
7
|
-
fallback:
|
|
9
|
+
fallback: FallbackType,
|
|
8
10
|
children: React.ReactNode
|
|
9
11
|
}>;
|
|
10
12
|
export default function ErrorBoundary({
|
|
@@ -28,7 +30,7 @@ export default function ErrorBoundary({
|
|
|
28
30
|
{children}
|
|
29
31
|
</ErrorCatcher>
|
|
30
32
|
) : (
|
|
31
|
-
fallback
|
|
33
|
+
<ErrorFallback error={error.value} fallback={fallback} />
|
|
32
34
|
)
|
|
33
35
|
}
|
|
34
36
|
</ErrorBoundaryContext.Provider>
|
|
@@ -63,4 +65,18 @@ class ErrorCatcher extends React.Component<ErrorCatcherProps>
|
|
|
63
65
|
{
|
|
64
66
|
return this.props.children;
|
|
65
67
|
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type ErrorFallback = ReactUtils.Props<{
|
|
71
|
+
fallback: FallbackType,
|
|
72
|
+
error: Error
|
|
73
|
+
}>;
|
|
74
|
+
function ErrorFallback({ error, fallback }: ErrorFallback)
|
|
75
|
+
{
|
|
76
|
+
if (typeof fallback === "function")
|
|
77
|
+
{
|
|
78
|
+
return fallback(error);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return fallback;
|
|
66
82
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Nullable, ReactUtils } from "@hrnec06/util";
|
|
2
|
+
import useTransition from "../../../hooks/useTransition";
|
|
3
|
+
import useSyncRef, { useSyncRefAuto } from "../../../hooks/useSyncRef";
|
|
4
|
+
import clsx from "clsx";
|
|
5
|
+
import { useRef } 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
|
+
|
|
18
|
+
return (
|
|
19
|
+
<div
|
|
20
|
+
{...props}
|
|
21
|
+
|
|
22
|
+
className={clsx(
|
|
23
|
+
"overflow-hidden transition-all relative",
|
|
24
|
+
)}
|
|
25
|
+
|
|
26
|
+
style={{
|
|
27
|
+
height: open && ref.current ? ref.current.clientHeight : 0
|
|
28
|
+
}}
|
|
29
|
+
>
|
|
30
|
+
<div
|
|
31
|
+
ref={ref}
|
|
32
|
+
className="absolute top-0 left-0"
|
|
33
|
+
>
|
|
34
|
+
{children}
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
)
|
|
38
|
+
}
|
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
|
+
}
|