@uxf/core-react 12.7.0 → 12.7.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/README.md CHANGED
@@ -382,6 +382,9 @@ const buttonProps = useClickableProps<Props>({
382
382
 
383
383
  Allow to scroll element by dragging mouse.
384
384
 
385
+ The click that ends a drag is swallowed, so releasing the mouse over a tab or a link inside the element does not
386
+ activate it, and the native drag of links and images is turned off. Clicks from the keyboard are never swallowed.
387
+
385
388
  ```tsx
386
389
  import { useMouseDragToScroll } from "@uxf/core-react/hooks/use-mouse-drag-to-scroll";
387
390
 
@@ -1,2 +1,9 @@
1
1
  import { CSSProperties, RefObject } from "react";
2
+ /**
3
+ * Scrolls the element while the mouse is dragged over it with the primary button.
4
+ *
5
+ * The click that ends a drag is swallowed, so releasing the mouse over a tab or a link does not activate it, and the
6
+ * browser's native drag of links and images is turned off, which would otherwise take over instead of scrolling.
7
+ * Clicks from the keyboard (Enter, Space) are never swallowed.
8
+ */
2
9
  export declare function useMouseDragToScroll<T extends HTMLElement>(containerRef: RefObject<T | null>): CSSProperties | undefined;
@@ -2,6 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useMouseDragToScroll = useMouseDragToScroll;
4
4
  const react_1 = require("react");
5
+ // Further than this from where the mouse was pressed is a drag, not a click.
6
+ const DRAG_THRESHOLD_PX = 5;
7
+ /**
8
+ * Scrolls the element while the mouse is dragged over it with the primary button.
9
+ *
10
+ * The click that ends a drag is swallowed, so releasing the mouse over a tab or a link does not activate it, and the
11
+ * browser's native drag of links and images is turned off, which would otherwise take over instead of scrolling.
12
+ * Clicks from the keyboard (Enter, Space) are never swallowed.
13
+ */
5
14
  function useMouseDragToScroll(containerRef) {
6
15
  const position = (0, react_1.useRef)({ left: 0, top: 0, x: 0, y: 0 });
7
16
  const [style, setStyle] = (0, react_1.useState)();
@@ -10,28 +19,62 @@ function useMouseDragToScroll(containerRef) {
10
19
  if (!node) {
11
20
  return;
12
21
  }
22
+ const controller = new AbortController();
23
+ // The document listeners live for one drag, from pressing the mouse to releasing it.
24
+ let dragController;
25
+ let hasDragged = false;
26
+ let isClickSwallowed = false;
27
+ let swallowTimeout;
13
28
  const mouseMoveHandler = (e) => {
14
29
  const dx = e.clientX - position.current.x;
15
30
  const dy = e.clientY - position.current.y;
31
+ if (Math.abs(dx) > DRAG_THRESHOLD_PX || Math.abs(dy) > DRAG_THRESHOLD_PX) {
32
+ hasDragged = true;
33
+ }
16
34
  node.scrollLeft = position.current.left - dx;
17
35
  node.scrollTop = position.current.top - dy;
18
36
  };
19
37
  const mouseUpHandler = () => {
20
38
  setStyle((prev) => ({ ...prev, cursor: "grab", userSelect: "unset" }));
21
- document.removeEventListener("mousemove", mouseMoveHandler);
22
- document.removeEventListener("mouseup", mouseUpHandler);
39
+ dragController === null || dragController === void 0 ? void 0 : dragController.abort();
40
+ if (hasDragged) {
41
+ // Browsers dispatch the click that ends a drag right after `mouseup`, in the same task, so only that
42
+ // click is swallowed. Whatever comes later, a click from the keyboard included, goes through. Telling
43
+ // a keyboard click apart by the event does not work: Firefox gives Enter on a link `detail` 1, Safari
44
+ // gives it the last mouse position as `clientX`.
45
+ isClickSwallowed = true;
46
+ swallowTimeout = setTimeout(() => {
47
+ isClickSwallowed = false;
48
+ });
49
+ }
23
50
  };
24
51
  const mouseDownHandler = (e) => {
52
+ if (e.button !== 0) {
53
+ return;
54
+ }
55
+ hasDragged = false;
25
56
  position.current = { left: node.scrollLeft, top: node.scrollTop, x: e.clientX, y: e.clientY };
26
57
  setStyle((prev) => ({ ...prev, cursor: "grabbing", userSelect: "none" }));
27
- document.addEventListener("mousemove", mouseMoveHandler);
28
- document.addEventListener("mouseup", mouseUpHandler);
58
+ dragController === null || dragController === void 0 ? void 0 : dragController.abort();
59
+ dragController = new AbortController();
60
+ document.addEventListener("mousemove", mouseMoveHandler, { signal: dragController.signal });
61
+ document.addEventListener("mouseup", mouseUpHandler, { signal: dragController.signal });
29
62
  };
30
- node.addEventListener("mousedown", mouseDownHandler);
63
+ const clickHandler = (e) => {
64
+ if (isClickSwallowed) {
65
+ isClickSwallowed = false;
66
+ e.preventDefault();
67
+ e.stopPropagation();
68
+ }
69
+ };
70
+ node.addEventListener("mousedown", mouseDownHandler, { signal: controller.signal });
71
+ // Capture phase, so the click is stopped before the tab's or the link's own handler sees it.
72
+ node.addEventListener("click", clickHandler, { capture: true, signal: controller.signal });
73
+ node.addEventListener("dragstart", (e) => e.preventDefault(), { signal: controller.signal });
31
74
  return () => {
32
- node.removeEventListener("mousedown", mouseDownHandler);
33
- document.removeEventListener("mousedown", mouseMoveHandler);
34
- document.removeEventListener("mousedown", mouseUpHandler);
75
+ controller.abort();
76
+ dragController === null || dragController === void 0 ? void 0 : dragController.abort();
77
+ clearTimeout(swallowTimeout);
35
78
  };
36
79
  }, [containerRef]);
37
80
  return style;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,174 @@
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_mouse_drag_to_scroll_1 = require("./use-mouse-drag-to-scroll");
39
+ function Row(props) {
40
+ const ref = (0, react_2.useRef)(null);
41
+ const style = (0, use_mouse_drag_to_scroll_1.useMouseDragToScroll)(ref);
42
+ return (react_2.default.createElement("div", { "data-testid": "row", ref: ref, style: style },
43
+ react_2.default.createElement("button", { onClick: props.onClick, type: "button" }, "Tab")));
44
+ }
45
+ function setup() {
46
+ const onClick = jest.fn();
47
+ const view = (0, react_1.render)(react_2.default.createElement(Row, { onClick: onClick }));
48
+ return { onClick, row: react_1.screen.getByTestId("row"), tab: react_1.screen.getByRole("button"), view };
49
+ }
50
+ // jsdom does not lay anything out, so its `scrollLeft` stays 0 whatever the hook sets.
51
+ function spyOnScrollLeft(element) {
52
+ const setScrollLeft = jest.fn();
53
+ Object.defineProperty(element, "scrollLeft", { configurable: true, get: () => 0, set: setScrollLeft });
54
+ return setScrollLeft;
55
+ }
56
+ /**
57
+ * A browser dispatches `mouseup` and the click it causes in one task, so `click()` follows `release()` synchronously,
58
+ * as it does in the browser. Anything a person does later (pressing Enter, clicking again) comes after `later()`.
59
+ */
60
+ function press(target, x) {
61
+ react_1.fireEvent.mouseDown(target, { button: 0, clientX: x, clientY: 0 });
62
+ }
63
+ function moveTo(x) {
64
+ react_1.fireEvent.mouseMove(document, { clientX: x, clientY: 0 });
65
+ }
66
+ function release(x) {
67
+ react_1.fireEvent.mouseUp(document, { clientX: x, clientY: 0 });
68
+ }
69
+ function click(target, x) {
70
+ react_1.fireEvent.click(target, { clientX: x, detail: 1 });
71
+ }
72
+ function later() {
73
+ jest.runAllTimers();
74
+ }
75
+ beforeEach(() => {
76
+ jest.useFakeTimers();
77
+ });
78
+ afterEach(() => {
79
+ jest.useRealTimers();
80
+ });
81
+ describe("useMouseDragToScroll", () => {
82
+ it("lets a click without a drag through", () => {
83
+ const { onClick, tab } = setup();
84
+ press(tab, 100);
85
+ moveTo(103);
86
+ release(103);
87
+ click(tab, 103);
88
+ expect(onClick).toHaveBeenCalledTimes(1);
89
+ });
90
+ it("swallows the click that ends a drag", () => {
91
+ const { onClick, tab } = setup();
92
+ press(tab, 100);
93
+ moveTo(40);
94
+ release(40);
95
+ click(tab, 40);
96
+ expect(onClick).not.toHaveBeenCalled();
97
+ });
98
+ it("swallows the click when the mouse was dragged away and back", () => {
99
+ const { onClick, tab } = setup();
100
+ press(tab, 100);
101
+ moveTo(20);
102
+ moveTo(100);
103
+ release(100);
104
+ click(tab, 100);
105
+ expect(onClick).not.toHaveBeenCalled();
106
+ });
107
+ it("never swallows a click from the keyboard after a drag", () => {
108
+ const { onClick, tab } = setup();
109
+ press(tab, 100);
110
+ moveTo(40);
111
+ release(40);
112
+ later();
113
+ // Firefox reports Enter on a link with `detail` 1, so the event itself cannot tell it apart from a mouse click.
114
+ click(tab, 0);
115
+ expect(onClick).toHaveBeenCalledTimes(1);
116
+ });
117
+ it("lets the next click through after a drag released outside the element", () => {
118
+ const { onClick, tab } = setup();
119
+ press(tab, 100);
120
+ moveTo(400);
121
+ release(400);
122
+ later();
123
+ press(tab, 40);
124
+ release(40);
125
+ click(tab, 40);
126
+ expect(onClick).toHaveBeenCalledTimes(1);
127
+ });
128
+ it("lets the click through on a child that does not let the press bubble", () => {
129
+ const { onClick, tab } = setup();
130
+ press(tab, 100);
131
+ moveTo(40);
132
+ release(40);
133
+ later();
134
+ tab.addEventListener("mousedown", (e) => e.stopPropagation());
135
+ press(tab, 40);
136
+ release(40);
137
+ click(tab, 40);
138
+ expect(onClick).toHaveBeenCalledTimes(1);
139
+ });
140
+ it("does not drag with other than the primary button", () => {
141
+ const { row, tab } = setup();
142
+ react_1.fireEvent.mouseDown(tab, { button: 2, clientX: 100, clientY: 0 });
143
+ expect(row.style.cursor).toBe("");
144
+ });
145
+ it("turns off the native drag of links and images", () => {
146
+ const { tab } = setup();
147
+ expect(react_1.fireEvent.dragStart(tab)).toBe(false);
148
+ });
149
+ it("scrolls the element by the distance dragged", () => {
150
+ const { row, tab } = setup();
151
+ const setScrollLeft = spyOnScrollLeft(row);
152
+ press(tab, 100);
153
+ moveTo(40);
154
+ expect(setScrollLeft).toHaveBeenLastCalledWith(60);
155
+ });
156
+ it("stops scrolling once the mouse is released", () => {
157
+ const { row, tab } = setup();
158
+ const setScrollLeft = spyOnScrollLeft(row);
159
+ press(tab, 100);
160
+ moveTo(40);
161
+ release(40);
162
+ setScrollLeft.mockClear();
163
+ moveTo(0);
164
+ expect(setScrollLeft).not.toHaveBeenCalled();
165
+ });
166
+ it("stops listening on the document when unmounted during a drag", () => {
167
+ const { row, tab, view } = setup();
168
+ const setScrollLeft = spyOnScrollLeft(row);
169
+ press(tab, 100);
170
+ view.unmount();
171
+ moveTo(40);
172
+ expect(setScrollLeft).not.toHaveBeenCalled();
173
+ });
174
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/core-react",
3
- "version": "12.7.0",
3
+ "version": "12.7.1",
4
4
  "description": "UXF Core",
5
5
  "author": "UX Fans s.r.o",
6
6
  "license": "MIT",