react-resizable-panels 1.0.9 → 2.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.
Files changed (28) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +29 -104
  3. package/dist/declarations/src/PanelResizeHandle.d.ts +3 -1
  4. package/dist/declarations/src/PanelResizeHandleRegistry.d.ts +20 -0
  5. package/dist/react-resizable-panels.browser.cjs.js +367 -140
  6. package/dist/react-resizable-panels.browser.development.cjs.js +367 -140
  7. package/dist/react-resizable-panels.browser.development.esm.js +367 -140
  8. package/dist/react-resizable-panels.browser.esm.js +367 -140
  9. package/dist/react-resizable-panels.cjs.js +367 -140
  10. package/dist/react-resizable-panels.development.cjs.js +367 -140
  11. package/dist/react-resizable-panels.development.esm.js +367 -140
  12. package/dist/react-resizable-panels.development.node.cjs.js +367 -140
  13. package/dist/react-resizable-panels.development.node.esm.js +367 -140
  14. package/dist/react-resizable-panels.esm.js +367 -140
  15. package/dist/react-resizable-panels.node.cjs.js +367 -140
  16. package/dist/react-resizable-panels.node.esm.js +367 -140
  17. package/package.json +1 -1
  18. package/src/Panel.test.tsx +52 -0
  19. package/src/PanelGroup.ts +23 -16
  20. package/src/PanelResizeHandle.ts +64 -82
  21. package/src/PanelResizeHandleRegistry.ts +263 -0
  22. package/src/utils/calculateDragOffsetPercentage.ts +1 -1
  23. package/src/utils/cursor.ts +63 -33
  24. package/src/utils/events/getResizeEventCoordinates.ts +24 -0
  25. package/src/utils/events/getResizeEventCursorPosition.ts +14 -0
  26. package/src/utils/{events.ts → events/index.ts} +1 -1
  27. package/src/utils/getInputType.ts +5 -0
  28. package/src/utils/getResizeEventCursorPosition.ts +0 -21
@@ -1,54 +1,84 @@
1
- type CursorState =
2
- | "horizontal"
3
- | "horizontal-max"
4
- | "horizontal-min"
5
- | "vertical"
6
- | "vertical-max"
7
- | "vertical-min";
8
-
9
- let currentState: CursorState | null = null;
10
- let element: HTMLStyleElement | null = null;
11
-
12
- export function getCursorStyle(state: CursorState): string {
1
+ import {
2
+ EXCEEDED_HORIZONTAL_MAX,
3
+ EXCEEDED_HORIZONTAL_MIN,
4
+ EXCEEDED_VERTICAL_MAX,
5
+ EXCEEDED_VERTICAL_MIN,
6
+ } from "../PanelResizeHandleRegistry";
7
+
8
+ type CursorState = "horizontal" | "intersection" | "vertical";
9
+
10
+ let currentCursorStyle: string | null = null;
11
+ let styleElement: HTMLStyleElement | null = null;
12
+
13
+ export function getCursorStyle(
14
+ state: CursorState,
15
+ constraintFlags: number
16
+ ): string {
17
+ if (constraintFlags) {
18
+ const horizontalMin = (constraintFlags & EXCEEDED_HORIZONTAL_MIN) !== 0;
19
+ const horizontalMax = (constraintFlags & EXCEEDED_HORIZONTAL_MAX) !== 0;
20
+ const verticalMin = (constraintFlags & EXCEEDED_VERTICAL_MIN) !== 0;
21
+ const verticalMax = (constraintFlags & EXCEEDED_VERTICAL_MAX) !== 0;
22
+
23
+ if (horizontalMin) {
24
+ if (verticalMin) {
25
+ return "se-resize";
26
+ } else if (verticalMax) {
27
+ return "ne-resize";
28
+ } else {
29
+ return "e-resize";
30
+ }
31
+ } else if (horizontalMax) {
32
+ if (verticalMin) {
33
+ return "sw-resize";
34
+ } else if (verticalMax) {
35
+ return "nw-resize";
36
+ } else {
37
+ return "w-resize";
38
+ }
39
+ } else if (verticalMin) {
40
+ return "s-resize";
41
+ } else if (verticalMax) {
42
+ return "n-resize";
43
+ }
44
+ }
45
+
13
46
  switch (state) {
14
47
  case "horizontal":
15
48
  return "ew-resize";
16
- case "horizontal-max":
17
- return "w-resize";
18
- case "horizontal-min":
19
- return "e-resize";
49
+ case "intersection":
50
+ return "move";
20
51
  case "vertical":
21
52
  return "ns-resize";
22
- case "vertical-max":
23
- return "n-resize";
24
- case "vertical-min":
25
- return "s-resize";
26
53
  }
27
54
  }
28
55
 
29
56
  export function resetGlobalCursorStyle() {
30
- if (element !== null) {
31
- document.head.removeChild(element);
57
+ if (styleElement !== null) {
58
+ document.head.removeChild(styleElement);
32
59
 
33
- currentState = null;
34
- element = null;
60
+ currentCursorStyle = null;
61
+ styleElement = null;
35
62
  }
36
63
  }
37
64
 
38
- export function setGlobalCursorStyle(state: CursorState) {
39
- if (currentState === state) {
65
+ export function setGlobalCursorStyle(
66
+ state: CursorState,
67
+ constraintFlags: number
68
+ ) {
69
+ const style = getCursorStyle(state, constraintFlags);
70
+
71
+ if (currentCursorStyle === style) {
40
72
  return;
41
73
  }
42
74
 
43
- currentState = state;
44
-
45
- const style = getCursorStyle(state);
75
+ currentCursorStyle = style;
46
76
 
47
- if (element === null) {
48
- element = document.createElement("style");
77
+ if (styleElement === null) {
78
+ styleElement = document.createElement("style");
49
79
 
50
- document.head.appendChild(element);
80
+ document.head.appendChild(styleElement);
51
81
  }
52
82
 
53
- element.innerHTML = `*{cursor: ${style}!important;}`;
83
+ styleElement.innerHTML = `*{cursor: ${style}!important;}`;
54
84
  }
@@ -0,0 +1,24 @@
1
+ import { ResizeEvent } from "../../types";
2
+ import { isMouseEvent, isTouchEvent } from ".";
3
+
4
+ export function getResizeEventCoordinates(event: ResizeEvent) {
5
+ if (isMouseEvent(event)) {
6
+ return {
7
+ x: event.pageX,
8
+ y: event.pageY,
9
+ };
10
+ } else if (isTouchEvent(event)) {
11
+ const touch = event.touches[0];
12
+ if (touch && touch.pageX && touch.pageY) {
13
+ return {
14
+ x: touch.pageX,
15
+ y: touch.pageY,
16
+ };
17
+ }
18
+ }
19
+
20
+ return {
21
+ x: Infinity,
22
+ y: Infinity,
23
+ };
24
+ }
@@ -0,0 +1,14 @@
1
+ import { ResizeEvent } from "../../PanelGroupContext";
2
+ import { Direction } from "../../types";
3
+ import { getResizeEventCoordinates } from "./getResizeEventCoordinates";
4
+
5
+ export function getResizeEventCursorPosition(
6
+ direction: Direction,
7
+ event: ResizeEvent
8
+ ): number {
9
+ const isHorizontal = direction === "horizontal";
10
+
11
+ const { x, y } = getResizeEventCoordinates(event);
12
+
13
+ return isHorizontal ? x : y;
14
+ }
@@ -1,4 +1,4 @@
1
- import { ResizeEvent } from "../PanelGroupContext";
1
+ import { ResizeEvent } from "../../PanelGroupContext";
2
2
 
3
3
  export function isKeyDown(event: ResizeEvent): event is KeyboardEvent {
4
4
  return event.type === "keydown";
@@ -0,0 +1,5 @@
1
+ export function getInputType(): "coarse" | "fine" | undefined {
2
+ if (typeof matchMedia === "function") {
3
+ return matchMedia("(pointer:coarse)").matches ? "coarse" : "fine";
4
+ }
5
+ }
@@ -1,21 +0,0 @@
1
- import { ResizeEvent } from "../PanelGroupContext";
2
- import { Direction } from "../types";
3
- import { assert } from "./assert";
4
- import { isMouseEvent, isTouchEvent } from "./events";
5
-
6
- export function getResizeEventCursorPosition(
7
- direction: Direction,
8
- event: ResizeEvent
9
- ): number {
10
- const isHorizontal = direction === "horizontal";
11
-
12
- if (isMouseEvent(event)) {
13
- return isHorizontal ? event.clientX : event.clientY;
14
- } else if (isTouchEvent(event)) {
15
- const firstTouch = event.touches[0];
16
- assert(firstTouch);
17
- return isHorizontal ? firstTouch.screenX : firstTouch.screenY;
18
- } else {
19
- throw Error(`Unsupported event type "${event.type}"`);
20
- }
21
- }