@uxf/core-react 12.7.1 → 13.0.0

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 CHANGED
@@ -432,6 +432,27 @@ useEffect(() => {
432
432
 
433
433
  ---
434
434
 
435
+ ### useComposedRefs
436
+
437
+ Puts several refs on one element. The composed ref keeps its identity until one of the refs changes, so React
438
+ does not detach and re-attach it on every render.
439
+
440
+ ```tsx
441
+ import { useComposedRefs } from "@uxf/core-react/hooks/use-composed-refs";
442
+
443
+ function Field(props: FieldProps & RefAttributes<HTMLInputElement>) {
444
+ const innerRef = useRef<HTMLInputElement>(null);
445
+ const ref = useComposedRefs(innerRef, props.ref);
446
+
447
+ return <input ref={ref} />;
448
+ }
449
+ ```
450
+
451
+ `composeRefs` from `@uxf/core-react/utils/compose-refs` does the same without the memoisation. It makes a new
452
+ callback on every call, so use it only outside a component.
453
+
454
+ ---
455
+
435
456
  ### usePrevious
436
457
 
437
458
  Returns previous value of given variable.
@@ -0,0 +1,6 @@
1
+ import { Ref, RefCallback } from "react";
2
+ /**
3
+ * `composeRefs` memoised on the refs themselves. A new callback on every render makes React detach and
4
+ * attach every composed ref on every commit, which re-runs a React 19 cleanup ref each time.
5
+ */
6
+ export declare function useComposedRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useComposedRefs = useComposedRefs;
4
+ const react_1 = require("react");
5
+ const compose_refs_1 = require("../utils/compose-refs");
6
+ /**
7
+ * `composeRefs` memoised on the refs themselves. A new callback on every render makes React detach and
8
+ * attach every composed ref on every commit, which re-runs a React 19 cleanup ref each time.
9
+ */
10
+ function useComposedRefs(...refs) {
11
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the refs are the dependencies
12
+ return (0, react_1.useMemo)(() => (0, compose_refs_1.composeRefs)(...refs), refs);
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const react_1 = require("@testing-library/react");
37
+ const react_2 = __importStar(require("react"));
38
+ const use_composed_refs_1 = require("./use-composed-refs");
39
+ function Probe(props) {
40
+ const ref = (0, use_composed_refs_1.useComposedRefs)(props.outerRef, props.innerRef);
41
+ return react_2.default.createElement("span", { ref: ref }, props.label);
42
+ }
43
+ test("keeps the same composed ref across re-renders, so a cleanup ref attaches once", () => {
44
+ var _a;
45
+ const calls = [];
46
+ const cleanupRef = (node) => {
47
+ calls.push(node ? "attach" : "null");
48
+ return () => {
49
+ calls.push("cleanup");
50
+ };
51
+ };
52
+ const objectRef = (0, react_2.createRef)();
53
+ const view = (0, react_1.render)(react_2.default.createElement(Probe, { innerRef: objectRef, label: "a", outerRef: cleanupRef }));
54
+ view.rerender(react_2.default.createElement(Probe, { innerRef: objectRef, label: "b", outerRef: cleanupRef }));
55
+ expect((_a = objectRef.current) === null || _a === void 0 ? void 0 : _a.textContent).toBe("b");
56
+ view.unmount();
57
+ expect(calls).toEqual(["attach", "cleanup"]);
58
+ expect(objectRef.current).toBeNull();
59
+ });
@@ -4,7 +4,7 @@ exports.useFocusReturn = useFocusReturn;
4
4
  const react_1 = require("react");
5
5
  const use_on_update_1 = require("./use-on-update");
6
6
  function useFocusReturn(opened, transitionDuration = 0, shouldReturnFocus = true) {
7
- const lastActiveElement = (0, react_1.useRef)();
7
+ const lastActiveElement = (0, react_1.useRef)(undefined);
8
8
  const returnFocus = () => {
9
9
  const node = lastActiveElement.current;
10
10
  if (node && "focus" in node && typeof node.focus === "function") {
@@ -86,9 +86,9 @@ function createAriaHider(containerNode, selector = "body > :not(script)") {
86
86
  };
87
87
  }
88
88
  function useFocusTrap(active = true) {
89
- const ref = (0, react_1.useRef)();
90
- const restoreAria = (0, react_1.useRef)();
91
- const timer = (0, react_1.useRef)();
89
+ const ref = (0, react_1.useRef)(null);
90
+ const restoreAria = (0, react_1.useRef)(undefined);
91
+ const timer = (0, react_1.useRef)(undefined);
92
92
  const setRef = (0, react_1.useCallback)((node) => {
93
93
  if (!active) {
94
94
  ref.current = null;
@@ -1 +1 @@
1
- export declare function useLatest<T>(current: T): import("react").MutableRefObject<T>;
1
+ export declare function useLatest<T>(current: T): import("react").RefObject<T>;
@@ -1 +1 @@
1
- export declare function usePrevious<T>(current: T): import("react").MutableRefObject<T>;
1
+ export declare function usePrevious<T>(current: T): import("react").RefObject<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/core-react",
3
- "version": "12.7.1",
3
+ "version": "13.0.0",
4
4
  "description": "UXF Core",
5
5
  "author": "UX Fans s.r.o",
6
6
  "license": "MIT",
@@ -12,15 +12,15 @@
12
12
  "typecheck": "tsc --noEmit"
13
13
  },
14
14
  "peerDependencies": {
15
- "@uxf/core": "12.7.0",
16
- "react": ">=18.2.0",
17
- "react-dom": ">=18.2.0"
15
+ "@uxf/core": "13.0.0",
16
+ "react": "^19.3.0",
17
+ "react-dom": "^19.3.0"
18
18
  },
19
19
  "devDependencies": {
20
- "@types/react": "18.3.31",
21
- "@types/react-dom": "18.3.7",
22
- "@uxf/core": "12.7.0",
23
- "react": "18.3.1",
24
- "react-dom": "18.3.1"
20
+ "@types/react": "19.3.0",
21
+ "@types/react-dom": "19.3.0",
22
+ "@uxf/core": "13.0.0",
23
+ "react": "19.3.0",
24
+ "react-dom": "19.3.0"
25
25
  }
26
26
  }
@@ -1,2 +1,6 @@
1
1
  import { Ref, RefCallback } from "react";
2
+ /**
3
+ * Returns a cleanup, so React 19 detaches by calling it instead of passing `null`: each ref's own
4
+ * cleanup runs, and only refs without one get `null`.
5
+ */
2
6
  export declare function composeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
@@ -1,15 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.composeRefs = composeRefs;
4
+ function attachRef(ref, value) {
5
+ if (!ref) {
6
+ return () => undefined;
7
+ }
8
+ if (typeof ref === "function") {
9
+ const cleanup = ref(value);
10
+ return typeof cleanup === "function" ? cleanup : () => ref(null);
11
+ }
12
+ ref.current = value;
13
+ return () => {
14
+ ref.current = null;
15
+ };
16
+ }
17
+ /**
18
+ * Returns a cleanup, so React 19 detaches by calling it instead of passing `null`: each ref's own
19
+ * cleanup runs, and only refs without one get `null`.
20
+ */
4
21
  function composeRefs(...refs) {
5
- return (value) => refs.forEach((ref) => {
6
- if (!ref) {
7
- return;
8
- }
9
- if (typeof ref === "function") {
10
- ref(value);
11
- return;
12
- }
13
- ref.current = value;
14
- });
22
+ return (value) => {
23
+ const cleanups = refs.map((ref) => attachRef(ref, value));
24
+ return () => cleanups.forEach((cleanup) => cleanup());
25
+ };
15
26
  }
@@ -39,3 +39,22 @@ describe("composeRefs", () => {
39
39
  expect(refObject2.current).toBe("test-value");
40
40
  });
41
41
  });
42
+ describe("composeRefs cleanup (React 19 ref semantics)", () => {
43
+ it("returns a cleanup that runs each callback ref's own cleanup instead of calling it with null", () => {
44
+ const cleanup = jest.fn();
45
+ const refCallback = jest.fn(() => cleanup);
46
+ const detach = (0, compose_refs_1.composeRefs)(refCallback)("node");
47
+ expect(typeof detach).toBe("function");
48
+ detach();
49
+ expect(cleanup).toHaveBeenCalledTimes(1);
50
+ expect(refCallback).not.toHaveBeenCalledWith(null);
51
+ });
52
+ it("calls a callback ref without a cleanup with null, and resets an object ref, on detach", () => {
53
+ const refCallback = jest.fn();
54
+ const refObject = { current: null };
55
+ const detach = (0, compose_refs_1.composeRefs)(refCallback, refObject)("node");
56
+ detach();
57
+ expect(refCallback).toHaveBeenLastCalledWith(null);
58
+ expect(refObject.current).toBeNull();
59
+ });
60
+ });