@uxf/core-react 13.0.0 → 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/package.json
CHANGED
|
@@ -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
|
-
|
|
178
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
cleanup = () =>
|
|
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
|
-
//
|
|
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
|
|
293
|
-
const
|
|
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 (
|
|
312
|
-
|
|
308
|
+
if (props[defaultKey] === void 0) {
|
|
309
|
+
props[defaultKey] = defaultProps[defaultKey];
|
|
313
310
|
}
|
|
314
311
|
}
|
|
315
|
-
|
|
316
|
-
|
|
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
|
+
});
|