@uxf/core-react 12.7.1 → 13.0.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/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.3",
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
  }
@@ -53,6 +53,8 @@ function rotateXYByAngle(pos, angle) {
53
53
  return [x, y];
54
54
  }
55
55
  function getHandlers(set, handlerProps) {
56
+ // The document listeners of the mouse drag in progress, removed together on mouseup or unmount.
57
+ let mouseDrag;
56
58
  const onStart = (event) => {
57
59
  const isTouch = "touches" in event;
58
60
  // if more than a single touch don't track, for now...
@@ -63,10 +65,12 @@ function getHandlers(set, handlerProps) {
63
65
  var _a;
64
66
  // setup mouse listeners on document to track swipe since swipe can leave container
65
67
  if (props.trackMouse && !isTouch) {
68
+ mouseDrag === null || mouseDrag === void 0 ? void 0 : mouseDrag.abort();
69
+ mouseDrag = new AbortController();
66
70
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
67
- document.addEventListener(mouseMove, onMove);
71
+ document.addEventListener(mouseMove, onMove, { signal: mouseDrag.signal });
68
72
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
69
- document.addEventListener(mouseUp, onUp);
73
+ document.addEventListener(mouseUp, onUp, { signal: mouseDrag.signal });
70
74
  }
71
75
  // eslint-disable-next-line no-restricted-syntax
72
76
  const { clientX, clientY } = isTouch ? event.touches[0] : event;
@@ -174,10 +178,8 @@ function getHandlers(set, handlerProps) {
174
178
  });
175
179
  };
176
180
  const cleanUpMouse = () => {
177
- // safe to just call removeEventListener
178
- document.removeEventListener(mouseMove, onMove);
179
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
180
- document.removeEventListener(mouseUp, onUp);
181
+ mouseDrag === null || mouseDrag === void 0 ? void 0 : mouseDrag.abort();
182
+ mouseDrag = undefined;
181
183
  };
182
184
  const onUp = (e) => {
183
185
  cleanUpMouse();
@@ -217,17 +219,16 @@ function getHandlers(set, handlerProps) {
217
219
  ],
218
220
  [touchEnd, onEnd, baseOptions],
219
221
  ];
220
- tls.forEach(([e, h, o]) => el.addEventListener(e, h, o));
221
- // return properly scoped cleanup method for removing listeners, options not required
222
- cleanup = () => tls.forEach(([e, h]) => el.removeEventListener(e, h));
222
+ const controller = new AbortController();
223
+ tls.forEach(([e, h, o]) => el.addEventListener(e, h, { ...o, signal: controller.signal }));
224
+ cleanup = () => controller.abort();
223
225
  }
224
226
  return cleanup;
225
227
  };
226
228
  const onRef = (el) => {
227
- // "inline" ref functions are called twice on render, once with null then again with DOM element
228
- // ignore null here
229
+ // With the cleanup returned below, React 19 does not call the ref with null any more.
229
230
  if (el === null) {
230
- return;
231
+ return undefined;
231
232
  }
232
233
  set((state, props) => {
233
234
  // if the same DOM el as previous just return state
@@ -247,6 +248,15 @@ function getHandlers(set, handlerProps) {
247
248
  // store event attached DOM el for comparison, clean up, and re-attachment
248
249
  return { ...state, el, ...addState };
249
250
  });
251
+ // The element is detached (removed, swapped, or the component unmounts): stop listening on it.
252
+ return () => set((state) => {
253
+ var _a;
254
+ if (state.el !== el) {
255
+ return state;
256
+ }
257
+ (_a = state.cleanUpTouch) === null || _a === void 0 ? void 0 : _a.call(state);
258
+ return { ...state, el: undefined, cleanUpTouch: undefined };
259
+ });
250
260
  };
251
261
  // set ref callback to attach touch event listeners
252
262
  const output = {
@@ -256,7 +266,7 @@ function getHandlers(set, handlerProps) {
256
266
  if (handlerProps.trackMouse) {
257
267
  output.onMouseDown = onStart;
258
268
  }
259
- return [output, attachTouch];
269
+ return [output, attachTouch, cleanUpMouse];
260
270
  }
261
271
  function updateTransientState(state, props, previousProps, attachTouch) {
262
272
  // if trackTouch is off or there is no el, then remove handlers if necessary and exit
@@ -289,30 +299,35 @@ function updateTransientState(state, props, previousProps, attachTouch) {
289
299
  }
290
300
  return state;
291
301
  }
292
- function useSwipeable(options) {
293
- const transientState = (0, react_1.useRef)({ ...initialState });
294
- const transientProps = (0, react_1.useRef)({
295
- ...defaultProps,
296
- });
297
- // track previous rendered props
298
- const previousProps = (0, react_1.useRef)({
299
- ...transientProps.current,
300
- });
301
- previousProps.current = { ...transientProps.current };
302
- // update current render props & defaults
303
- transientProps.current = {
304
- ...defaultProps,
305
- ...options,
306
- };
302
+ function withDefaults(options) {
303
+ const props = { ...defaultProps, ...options };
307
304
  // Force defaults for config properties
308
305
  let defaultKey;
309
306
  for (defaultKey in defaultProps) {
310
307
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
311
- if (transientProps.current[defaultKey] === void 0) {
312
- transientProps.current[defaultKey] = defaultProps[defaultKey];
308
+ if (props[defaultKey] === void 0) {
309
+ props[defaultKey] = defaultProps[defaultKey];
313
310
  }
314
311
  }
315
- const [handlers, attachTouch] = (0, react_1.useMemo)(() => getHandlers((stateSetter) => (transientState.current = stateSetter(transientState.current, transientProps.current)), { trackMouse: options.trackMouse }), [options.trackMouse]);
316
- transientState.current = updateTransientState(transientState.current, transientProps.current, previousProps.current, attachTouch);
312
+ return props;
313
+ }
314
+ function useSwipeable(options) {
315
+ const transientState = (0, react_1.useRef)({ ...initialState });
316
+ // The props of the last commit, read by the handlers when an event fires. Written after commit, never during
317
+ // render, together with attaching or detaching the touch listeners: a render React discards must leave
318
+ // neither its props nor its listeners behind.
319
+ // Starts with this render's props: the ref callback attaches the touch listeners during the first commit,
320
+ // before any effect, and must already see them.
321
+ const [initialProps] = (0, react_1.useState)(() => withDefaults(options));
322
+ const transientProps = (0, react_1.useRef)(initialProps);
323
+ const [handlers, attachTouch, cleanUpMouse] = (0, react_1.useMemo)(() => getHandlers((stateSetter) => (transientState.current = stateSetter(transientState.current, transientProps.current)), { trackMouse: options.trackMouse }), [options.trackMouse]);
324
+ (0, react_1.useLayoutEffect)(() => {
325
+ const previousProps = transientProps.current;
326
+ transientProps.current = withDefaults(options);
327
+ transientState.current = updateTransientState(transientState.current, transientProps.current, previousProps, attachTouch);
328
+ });
329
+ // On unmount, in the middle of a mouse drag: the listeners on the document. The element's own touch listeners
330
+ // go with the ref's cleanup.
331
+ (0, react_1.useLayoutEffect)(() => cleanUpMouse, [cleanUpMouse]);
317
332
  return handlers;
318
333
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = require("@testing-library/react");
7
+ const react_2 = __importDefault(require("react"));
8
+ const use_swipeable_1 = require("./use-swipeable");
9
+ function Swipeable(props) {
10
+ const handlers = (0, use_swipeable_1.useSwipeable)(props);
11
+ return props.isShown === false ? null : react_2.default.createElement("div", { ...handlers }, "swipe");
12
+ }
13
+ function swipeLeft(element) {
14
+ const touch = (clientX) => ({ touches: [{ clientX, clientY: 0 }], timeStamp: 1 });
15
+ react_1.fireEvent.touchStart(element, touch(200));
16
+ react_1.fireEvent.touchMove(element, touch(100));
17
+ react_1.fireEvent.touchMove(element, touch(0));
18
+ react_1.fireEvent.touchEnd(element, { touches: [] });
19
+ }
20
+ test("a swipe reaches the latest callback", () => {
21
+ const first = jest.fn();
22
+ const latest = jest.fn();
23
+ const { getByText, rerender } = (0, react_1.render)(react_2.default.createElement(Swipeable, { onSwipedLeft: first }));
24
+ rerender(react_2.default.createElement(Swipeable, { onSwipedLeft: latest }));
25
+ swipeLeft(getByText("swipe"));
26
+ expect(first).not.toHaveBeenCalled();
27
+ expect(latest).toHaveBeenCalledTimes(1);
28
+ });
29
+ test("unmounting in the middle of a mouse drag stops tracking the mouse on the document", () => {
30
+ const onSwiping = jest.fn();
31
+ const { getByText, unmount } = (0, react_1.render)(react_2.default.createElement(Swipeable, { onSwiping: onSwiping, trackMouse: true }));
32
+ react_1.fireEvent.mouseDown(getByText("swipe"), { clientX: 0, clientY: 0 });
33
+ unmount();
34
+ react_1.fireEvent.mouseMove(document, { clientX: 100, clientY: 0 });
35
+ expect(onSwiping).not.toHaveBeenCalled();
36
+ });
37
+ test("after unmounting, touches on the element no longer count", () => {
38
+ const onSwiped = jest.fn();
39
+ const { getByText, unmount } = (0, react_1.render)(react_2.default.createElement(Swipeable, { onSwiped: onSwiped }));
40
+ const element = getByText("swipe");
41
+ unmount();
42
+ swipeLeft(element);
43
+ expect(onSwiped).not.toHaveBeenCalled();
44
+ });
45
+ test("an element removed while the hook stays mounted stops counting touches", () => {
46
+ const onSwiped = jest.fn();
47
+ const { getByText, rerender } = (0, react_1.render)(react_2.default.createElement(Swipeable, { onSwiped: onSwiped }));
48
+ const element = getByText("swipe");
49
+ rerender(react_2.default.createElement(Swipeable, { isShown: false, onSwiped: onSwiped }));
50
+ swipeLeft(element);
51
+ expect(onSwiped).not.toHaveBeenCalled();
52
+ });
53
+ test("switching trackTouch off after mount stops counting touches", () => {
54
+ const onSwiped = jest.fn();
55
+ const { getByText, rerender } = (0, react_1.render)(react_2.default.createElement(Swipeable, { onSwiped: onSwiped }));
56
+ rerender(react_2.default.createElement(Swipeable, { onSwiped: onSwiped, trackTouch: false }));
57
+ swipeLeft(getByText("swipe"));
58
+ expect(onSwiped).not.toHaveBeenCalled();
59
+ });
@@ -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
+ });