@fibery/react 1.4.6 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/react",
3
- "version": "1.4.6",
3
+ "version": "1.5.0",
4
4
  "private": false,
5
5
  "license": "UNLICENSED",
6
6
  "files": [
@@ -11,11 +11,11 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@tanstack/react-query": "5.62.7",
14
- "@fibery/helpers": "1.3.4"
14
+ "@fibery/helpers": "1.5.0"
15
15
  },
16
16
  "devDependencies": {
17
- "@testing-library/react": "13.4.0",
18
- "@testing-library/react-hooks": "7.0.2",
17
+ "@testing-library/react": "16.3.1",
18
+ "@testing-library/react-hooks": "8.0.1",
19
19
  "@types/jest": "30.0.0",
20
20
  "@types/react": "18.3.12",
21
21
  "@types/react-dom": "18.3.1",
@@ -26,7 +26,7 @@
26
26
  "react-test-renderer": "18.3.1",
27
27
  "typescript": "5.9.3",
28
28
  "@fibery/babel-preset": "7.4.1",
29
- "@fibery/eslint-config": "8.6.2"
29
+ "@fibery/eslint-config": "9.0.0"
30
30
  },
31
31
  "jest": {
32
32
  "coveragePathIgnorePatterns": [
@@ -14,6 +14,5 @@ export const useCallbackRef = <T extends (...args: any[]) => any>(callback?: T):
14
14
  callbackRef.current = callback;
15
15
  });
16
16
 
17
- // eslint-disable-next-line react-hooks/exhaustive-deps
18
17
  return useCallback(((...args) => callbackRef.current?.(...args)) as T, []);
19
18
  };
@@ -1,6 +1,6 @@
1
1
  import React, {useCallback} from "react";
2
2
 
3
- const composeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => {
3
+ export const composeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => {
4
4
  return function setRefs(node: T) {
5
5
  refs.forEach((ref) => {
6
6
  if (!ref) {
@@ -0,0 +1,96 @@
1
+ import {useRef} from "react";
2
+
3
+ type Point = {
4
+ x: number;
5
+ y: number;
6
+ };
7
+
8
+ /**
9
+ * very simple hook for simple isolated drag-like operations.
10
+ * e.g. dragging border to resize, when you dont need drop targets, auto-scroll, etc.
11
+ *
12
+ * For complex operations you most likely want to use atlaskit/pragmatic-drag-and-drop
13
+ */
14
+ export const useDrag = ({
15
+ threshold = 0,
16
+ onDragStart,
17
+ onDrag,
18
+ onDragEnd,
19
+ }: {
20
+ /** Minimum movement before drag starts */
21
+ threshold?: number;
22
+ onDragStart?: () => void;
23
+ onDrag?: (translation: Point) => void;
24
+ onDragEnd?: (translation: Point) => void;
25
+ }) => {
26
+ const ref = useRef<HTMLDivElement>(null);
27
+ const state = useRef<"idle" | "press" | "drag" | "drag-end">("idle");
28
+
29
+ const lastPosition = useRef<Point>({x: 0, y: 0});
30
+ const translation = useRef<Point>({x: 0, y: 0});
31
+
32
+ function onClick(e: MouseEvent) {
33
+ if (state.current === "drag-end") {
34
+ e.preventDefault();
35
+ e.stopPropagation();
36
+ state.current = "idle";
37
+ ref.current?.removeEventListener("click", onClick);
38
+ }
39
+ }
40
+
41
+ function onPointerDown(e: React.PointerEvent) {
42
+ lastPosition.current = {x: e.clientX, y: e.clientY};
43
+ translation.current = {x: 0, y: 0};
44
+ state.current = "press";
45
+ ref.current?.setPointerCapture(e.pointerId);
46
+ ref.current?.addEventListener("click", onClick);
47
+ }
48
+
49
+ function onPointerMove(e: React.PointerEvent) {
50
+ if (state.current === "press") {
51
+ const dx = e.clientX - lastPosition.current.x;
52
+ const dy = e.clientY - lastPosition.current.y;
53
+ const distance = Math.sqrt(dx * dx + dy * dy);
54
+
55
+ if (distance >= threshold) {
56
+ state.current = "drag";
57
+ onDragStart?.();
58
+ }
59
+ }
60
+
61
+ if (state.current !== "drag") {
62
+ return;
63
+ }
64
+
65
+ const currentPosition = {x: e.clientX, y: e.clientY};
66
+
67
+ const dx = currentPosition.x - lastPosition.current.x;
68
+ const dy = currentPosition.y - lastPosition.current.y;
69
+ lastPosition.current = currentPosition;
70
+
71
+ translation.current = {
72
+ x: translation.current.x + dx,
73
+ y: translation.current.y + dy,
74
+ };
75
+
76
+ onDrag?.(translation.current);
77
+ }
78
+
79
+ function onPointerUp(e: React.PointerEvent) {
80
+ if (state.current === "drag") {
81
+ state.current = "drag-end";
82
+ } else {
83
+ state.current = "idle";
84
+ }
85
+
86
+ onDragEnd?.(translation.current);
87
+ ref.current?.releasePointerCapture(e.pointerId);
88
+ }
89
+
90
+ return {
91
+ ref,
92
+ onPointerDown,
93
+ onPointerMove,
94
+ onPointerUp,
95
+ };
96
+ };
@@ -21,7 +21,6 @@ export const useResizeObserver = <TargetElement extends HTMLElement>(
21
21
  let active = true;
22
22
  const element = getRefValue(target);
23
23
  if (!element) {
24
- // eslint-disable-next-line @typescript-eslint/no-empty-function
25
24
  return () => {};
26
25
  }
27
26