@hrnec06/react_utils 1.7.0 → 1.7.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/README.md ADDED
@@ -0,0 +1 @@
1
+ A source only React Library for private use.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hrnec06/react_utils",
3
- "version": "1.7.0",
4
- "description": "A debugger component for react.",
3
+ "version": "1.7.2",
4
+ "description": "React utilities",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
@@ -20,7 +20,7 @@ import useDebounce from "../../../hooks/useDebounce";
20
20
  import { useCTXMListener } from "../../ContextMenu/ContextMenuCtx";
21
21
 
22
22
  export interface ParseValueRef {
23
- copyValue: string,
23
+ copyValue?: string,
24
24
  name?: string,
25
25
  menu?: CTXM.ContextMenuSection,
26
26
  }
@@ -116,13 +116,9 @@ export function DebugCTXMProvider({
116
116
  return path.join('.');
117
117
  }, [path]);
118
118
 
119
- const handleCopy = ref === null
119
+ const handleCopy = (ref === null || ref.copyValue === undefined)
120
120
  ? undefined
121
- : () => {
122
- if (!ref) return;
123
-
124
- navigator.clipboard.writeText(ref.copyValue);
125
- };
121
+ : () => navigator.clipboard.writeText(ref.copyValue!);
126
122
 
127
123
  return (
128
124
  <ContextMenu
@@ -35,17 +35,34 @@ const DebugTerminal = forwardRef<ParseValueRef, DebugTerminalProps>(({
35
35
  }, ref) => {
36
36
  const [showing, setShowing] = useState(!!defaultExpanded);
37
37
 
38
- useImperativeHandle(ref, () => ({
39
- name: `Terminal ${terminal.name}`,
40
- copyValue: terminal.getLog().reduce((prev, curr) => prev + `${curr.role === LogRole.Input ? '> ' : `${LogType[curr.type]}: `}${JSON.stringify(curr.value)}` + "\n", ""),
41
- menu: [
38
+ useImperativeHandle(ref, () => {
39
+ const logToString = (log: LogItem) => {
40
+ let value: string;
41
+
42
+ try
43
+ {
44
+ value = JSON.stringify(log.value);
45
+ }
46
+ catch (error)
42
47
  {
43
- id: "terminal-clear",
44
- label: "Clear",
45
- onClick: () => terminal.clear(),
48
+ value = `Error parsing value (${typeof log.value})`;
46
49
  }
47
- ]
48
- }), [terminal.getLog()]);
50
+
51
+ return `${log.role === LogRole.Input ? '> ' : `${LogType[log.type]}: `}${value}`;
52
+ }
53
+
54
+ return {
55
+ name: `Terminal ${terminal.name}`,
56
+ copyValue: terminal.getLog().reduce((prev, curr) => prev + logToString(curr) + "\n", ""),
57
+ menu: [
58
+ {
59
+ id: "terminal-clear",
60
+ label: "Clear",
61
+ onClick: () => terminal.clear(),
62
+ }
63
+ ]
64
+ };
65
+ }, [terminal.getLog()]);
49
66
 
50
67
  const handleToggle = (value: boolean) => {
51
68
  setShowing(value);
@@ -3,6 +3,7 @@ import useDebugger from "../DebuggerContext";
3
3
  import { Char_Bracket, Char_Comma, Chevron_Toggle } from "../DebuggerSymbols";
4
4
  import ParseValueSimple from "./DebugParserSimple";
5
5
  import ParseValue, { ParseValueRef } from "./DebugParser";
6
+ import { Optional } from "@hrnec06/util";
6
7
 
7
8
  interface ValueArrayProps {
8
9
  value: unknown[],
@@ -18,19 +19,27 @@ const ValueArray = forwardRef<ParseValueRef, ValueArrayProps>(({
18
19
 
19
20
  const [expanded, setExpanded] = useState(defaultExpanded);
20
21
 
21
- useImperativeHandle(ref, () => ({
22
- name: `Array (${value.length})`,
23
- copyValue: JSON.stringify(value),
24
- menu: [
25
- {
26
- id: "open-toggle",
27
- label: expanded ? "Collapse" : "Expand",
28
- onClick: () => {
29
- setExpanded(!expanded);
22
+ useImperativeHandle(ref, () => {
23
+ let copyValue: Optional<string> = undefined;
24
+ try
25
+ {
26
+ copyValue = JSON.stringify(value);
27
+ } catch {}
28
+
29
+ return {
30
+ name: `Array (${value.length})`,
31
+ copyValue: copyValue,
32
+ menu: [
33
+ {
34
+ id: "open-toggle",
35
+ label: expanded ? "Collapse" : "Expand",
36
+ onClick: () => {
37
+ setExpanded(!expanded);
38
+ }
30
39
  }
31
- }
32
- ]
33
- }), [value, expanded]);
40
+ ]
41
+ };
42
+ }, [value, expanded]);
34
43
 
35
44
  const children = useMemo(() => {
36
45
  const children: React.ReactNode[] = [];
@@ -1,6 +1,6 @@
1
1
  import React, { forwardRef, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
2
2
  import useDebugger from "../DebuggerContext";
3
- import { entries, keys, Nullable } from "@hrnec06/util";
3
+ import { entries, keys, Nullable, Optional } from "@hrnec06/util";
4
4
  import { Char_Bracket, Char_Colon, Char_Comma, Chevron_Toggle } from "../DebuggerSymbols";
5
5
  import ParseValue, { DebugCTXMProvider, ParseValueRef } from "./DebugParser";
6
6
  import useSyncRef from "../../../hooks/useSyncRef";
@@ -22,19 +22,27 @@ const ValueObject = forwardRef<ParseValueRef, ValueObjectProps>(({
22
22
 
23
23
  const objectEntries = entries(value);
24
24
 
25
- useImperativeHandle(ref, () => ({
26
- name: `Object {${objectEntries.length}}`,
27
- copyValue: JSON.stringify(value),
28
- menu: [
29
- {
30
- id: "open-toggle",
31
- label: expanded ? "Collapse" : "Expand",
32
- onClick: () => {
33
- setExpanded(!expanded);
25
+ useImperativeHandle(ref, () => {
26
+ let copyValue: Optional<string> = undefined;
27
+ try
28
+ {
29
+ copyValue = JSON.stringify(value);
30
+ } catch {}
31
+
32
+ return {
33
+ name: `Object {${objectEntries.length}}`,
34
+ copyValue: copyValue,
35
+ menu: [
36
+ {
37
+ id: "open-toggle",
38
+ label: expanded ? "Collapse" : "Expand",
39
+ onClick: () => {
40
+ setExpanded(!expanded);
41
+ }
34
42
  }
35
- }
36
- ]
37
- }), [value, expanded]);
43
+ ]
44
+ };
45
+ }, [value, expanded]);
38
46
 
39
47
  const collapsedPreview = useMemo(() => {
40
48
  const children: React.ReactNode[] = [];
@@ -2,6 +2,8 @@ import clsx from "clsx";
2
2
  import useDialog from "./DialogContext";
3
3
  import useTransition, { transitionDataAttributes } from "../../hooks/useTransition";
4
4
  import { useRef } from "react";
5
+ import { useSyncRefAuto } from "../../hooks/useSyncRef";
6
+ import { Nullable } from "@hrnec06/util";
5
7
 
6
8
  interface DialogBackdropProps extends Omit<React.HTMLProps<HTMLDivElement>, 'children'>
7
9
  {
@@ -15,9 +17,9 @@ export default function DialogBackdrop({
15
17
  {
16
18
  const dialog = useDialog();
17
19
 
18
- const divRef = useRef<HTMLDivElement>(null);
20
+ const { ref, value } = useSyncRefAuto<Nullable<HTMLDivElement>>(null);
19
21
 
20
- const [visible, transitionData] = useTransition($transition, divRef.current, dialog.open);
22
+ const [visible, transitionData] = useTransition($transition, value, dialog.open);
21
23
 
22
24
  if (!visible)
23
25
  return undefined;
@@ -33,7 +35,7 @@ export default function DialogBackdrop({
33
35
  {...props}
34
36
  {...transitionDataAttributes(transitionData)}
35
37
 
36
- ref={divRef}
38
+ ref={ref}
37
39
 
38
40
  onClick={handleClick}
39
41
 
@@ -1,7 +1,9 @@
1
1
  import clsx from "clsx";
2
2
  import useDialog from "./DialogContext";
3
3
  import useTransition, { transitionDataAttributes } from "../../hooks/useTransition";
4
- import { useRef } from "react";
4
+ import { useRef, useState } from "react";
5
+ import { Nullable } from "@hrnec06/util";
6
+ import useSyncRef, { useSyncRefAuto } from "../../hooks/useSyncRef";
5
7
 
6
8
  interface DialogPanelProps extends React.HTMLProps<HTMLDivElement> {
7
9
  $transition?: boolean
@@ -13,9 +15,9 @@ export default function DialogPanel({
13
15
  {
14
16
  const dialog = useDialog();
15
17
 
16
- const divRef = useRef<HTMLDivElement>(null);
18
+ const { value, ref } = useSyncRefAuto<Nullable<HTMLDivElement>>(null);
17
19
 
18
- const [visible, transitionData] = useTransition($transition, divRef.current, dialog.open);
20
+ const [visible, transitionData] = useTransition($transition, value, dialog.open);
19
21
 
20
22
  if (!visible)
21
23
  return undefined;
@@ -25,7 +27,7 @@ export default function DialogPanel({
25
27
  {...props}
26
28
  {...transitionDataAttributes(transitionData)}
27
29
 
28
- ref={divRef}
30
+ ref={ref}
29
31
 
30
32
  className={props.className}
31
33
  >
@@ -14,4 +14,13 @@ export default function useSyncRef<T>(
14
14
  });
15
15
 
16
16
  return syncRefs;
17
+ }
18
+
19
+ export function useSyncRefAuto<T>(value: T)
20
+ {
21
+ const ref = useRef(value);
22
+ const [localValue, setLocalValue] = useState(value);
23
+ const syncRef = useSyncRef(ref, setLocalValue);
24
+
25
+ return { value: localValue, ref: syncRef };
17
26
  }
package/src/index.ts CHANGED
@@ -10,6 +10,10 @@ import useFlags from "./hooks/useFlags";
10
10
  import useNamespacedId from "./hooks/useNamespacedId";
11
11
  import useTransition from "./hooks/useTransition";
12
12
  import useLocalStorage from "./hooks/useLocalStorage";
13
+ import useDebounce from "./hooks/useDebounce";
14
+ import useEvent from "./hooks/useEvent";
15
+ import useLatestRef from "./hooks/useLatestRef";
16
+ import useSyncRef, { useSyncRefAuto } from "./hooks/useSyncRef";
13
17
 
14
18
  import useSignal, { Signal } from "./hooks/useSignal";
15
19
  import useLazySignal, { LazySignal } from "./hooks/useLazySignal";
@@ -22,10 +26,6 @@ import Dialog from "./components/Dialog/Dialog";
22
26
  import ContextMenu from "./components/ContextMenu/ContextMenu";
23
27
 
24
28
  import * as util from './lib/utils';
25
- import useDebounce from "./hooks/useDebounce";
26
- import useEvent from "./hooks/useEvent";
27
- import useLatestRef from "./hooks/useLatestRef";
28
- import useSyncRef from "./hooks/useSyncRef";
29
29
 
30
30
 
31
31
  export {
@@ -42,6 +42,7 @@ export {
42
42
  useLocalStorage,
43
43
  useNamespacedId,
44
44
  useSyncRef,
45
+ useSyncRefAuto,
45
46
  useTransition,
46
47
  useUpdateEffect,
47
48
  useUUID,