@hrnec06/react_utils 1.10.2 → 1.11.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.10.2",
3
+ "version": "1.11.1",
4
4
  "description": "React utilities",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -0,0 +1,82 @@
1
+ import { Nullable, ReactUtils } from "@hrnec06/util";
2
+ import useSignal, { Signal } from "../../../hooks/useSignal";
3
+ import { ErrorBoundaryContext } from "./ErrorBoundaryContext";
4
+ import React from "react";
5
+
6
+ type FallbackType = React.ReactNode | ((error: Error) => React.ReactNode);
7
+
8
+ type ErrorBoundaryProps = ReactUtils.Props<{
9
+ fallback: FallbackType,
10
+ children: React.ReactNode
11
+ }>;
12
+ export default function ErrorBoundary({
13
+ fallback,
14
+ children
15
+ }: ErrorBoundaryProps)
16
+ {
17
+ const error = useSignal<Nullable<Error>>(null);
18
+
19
+ return (
20
+ <ErrorBoundaryContext.Provider
21
+ value={{
22
+ error: error
23
+ }}
24
+ >
25
+ {
26
+ error.value === null ? (
27
+ <ErrorCatcher
28
+ error={error}
29
+ >
30
+ {children}
31
+ </ErrorCatcher>
32
+ ) : (
33
+ <ErrorFallback error={error.value} fallback={fallback} />
34
+ )
35
+ }
36
+ </ErrorBoundaryContext.Provider>
37
+ )
38
+ }
39
+
40
+ type ErrorCatcherProps = ReactUtils.Props<{
41
+ error: Signal<Nullable<Error>>,
42
+ children: React.ReactNode
43
+ }>;
44
+ class ErrorCatcher extends React.Component<ErrorCatcherProps>
45
+ {
46
+ componentDidCatch(error: unknown, info: unknown)
47
+ {
48
+ if (error instanceof Error)
49
+ {
50
+ this.props.error.value = error;
51
+ }
52
+ else if (typeof error === "string")
53
+ {
54
+ this.props.error.value = new Error(error);
55
+ }
56
+ else
57
+ {
58
+ console.warn(`An unknown error has occured: `, error);
59
+
60
+ this.props.error.value = new Error("Unknown error.");
61
+ }
62
+ }
63
+
64
+ render()
65
+ {
66
+ return this.props.children;
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;
82
+ }
@@ -0,0 +1,20 @@
1
+ import { Nullable, Optional } from "@hrnec06/util";
2
+ import { createContext, useContext } from "react";
3
+ import { Signal } from "../../../hooks/useSignal";
4
+ import ContextError from "../../../lib/errors/ContextError";
5
+
6
+ interface ErrorBoundaryContext {
7
+ error: Signal<Nullable<Error>>
8
+ }
9
+
10
+ export const ErrorBoundaryContext = createContext<Optional<ErrorBoundaryContext>>(undefined);
11
+
12
+ export default function useErrorBoundary()
13
+ {
14
+ const ctx = useContext(ErrorBoundaryContext);
15
+
16
+ if (!ctx)
17
+ throw new ContextError("ErrorBoundary");
18
+
19
+ return ctx;
20
+ }
@@ -2,7 +2,7 @@ import { Nullable, ReactUtils, Vector2 } from "@hrnec06/util";
2
2
  import useSignal from "../../../hooks/useSignal";
3
3
  import { useEffect, useState } from "react";
4
4
  import useListener from "../../../hooks/useListener";
5
- import { Debugger, useLatestRef } from "../../..";
5
+ import { useLatestRef } from "../../..";
6
6
 
7
7
  type TooltipProps = ReactUtils.Props<{
8
8
  $tooltip: React.ReactNode,
@@ -91,8 +91,6 @@ function Tooltip({
91
91
  </div>
92
92
  )
93
93
  }
94
-
95
- <Debugger.Debug value={position} />
96
94
  </>
97
95
  );
98
96
  }
package/src/index.ts CHANGED
@@ -31,6 +31,7 @@ import ContextMenu from "./components/ContextMenu/ContextMenu";
31
31
  import ShadowRoot from "./components/ShadowRoot/ShadowRoot";
32
32
 
33
33
  import Tooltip from "./components/UI/Tooltip/Tooltip";
34
+ import ErrorBoundary from "./components/UI/ErrorBoundary/ErrorBoundary";
34
35
 
35
36
  import * as util from './lib/utils';
36
37
  import ContextError from "./lib/errors/ContextError";
@@ -74,6 +75,7 @@ export {
74
75
 
75
76
  // UI
76
77
  Tooltip,
78
+ ErrorBoundary,
77
79
 
78
80
  // Shadow root
79
81
  ShadowRoot,