react-resizable-panels 1.0.10 → 2.0.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.
@@ -198,6 +198,322 @@ const Panel = forwardRef((props, ref) => createElement(PanelWithForwardedRef, {
198
198
  PanelWithForwardedRef.displayName = "Panel";
199
199
  Panel.displayName = "forwardRef(Panel)";
200
200
 
201
+ let currentCursorStyle = null;
202
+ let styleElement = null;
203
+ function getCursorStyle(state, constraintFlags) {
204
+ if (constraintFlags) {
205
+ const horizontalMin = (constraintFlags & EXCEEDED_HORIZONTAL_MIN) !== 0;
206
+ const horizontalMax = (constraintFlags & EXCEEDED_HORIZONTAL_MAX) !== 0;
207
+ const verticalMin = (constraintFlags & EXCEEDED_VERTICAL_MIN) !== 0;
208
+ const verticalMax = (constraintFlags & EXCEEDED_VERTICAL_MAX) !== 0;
209
+ if (horizontalMin) {
210
+ if (verticalMin) {
211
+ return "se-resize";
212
+ } else if (verticalMax) {
213
+ return "ne-resize";
214
+ } else {
215
+ return "e-resize";
216
+ }
217
+ } else if (horizontalMax) {
218
+ if (verticalMin) {
219
+ return "sw-resize";
220
+ } else if (verticalMax) {
221
+ return "nw-resize";
222
+ } else {
223
+ return "w-resize";
224
+ }
225
+ } else if (verticalMin) {
226
+ return "s-resize";
227
+ } else if (verticalMax) {
228
+ return "n-resize";
229
+ }
230
+ }
231
+ switch (state) {
232
+ case "horizontal":
233
+ return "ew-resize";
234
+ case "intersection":
235
+ return "move";
236
+ case "vertical":
237
+ return "ns-resize";
238
+ }
239
+ }
240
+ function resetGlobalCursorStyle() {
241
+ if (styleElement !== null) {
242
+ document.head.removeChild(styleElement);
243
+ currentCursorStyle = null;
244
+ styleElement = null;
245
+ }
246
+ }
247
+ function setGlobalCursorStyle(state, constraintFlags) {
248
+ const style = getCursorStyle(state, constraintFlags);
249
+ if (currentCursorStyle === style) {
250
+ return;
251
+ }
252
+ currentCursorStyle = style;
253
+ if (styleElement === null) {
254
+ styleElement = document.createElement("style");
255
+ document.head.appendChild(styleElement);
256
+ }
257
+ styleElement.innerHTML = `*{cursor: ${style}!important;}`;
258
+ }
259
+
260
+ function isKeyDown(event) {
261
+ return event.type === "keydown";
262
+ }
263
+ function isMouseEvent(event) {
264
+ return event.type.startsWith("mouse");
265
+ }
266
+ function isTouchEvent(event) {
267
+ return event.type.startsWith("touch");
268
+ }
269
+
270
+ function getResizeEventCoordinates(event) {
271
+ if (isMouseEvent(event)) {
272
+ return {
273
+ x: event.pageX,
274
+ y: event.pageY
275
+ };
276
+ } else if (isTouchEvent(event)) {
277
+ const touch = event.touches[0];
278
+ if (touch && touch.pageX && touch.pageY) {
279
+ return {
280
+ x: touch.pageX,
281
+ y: touch.pageY
282
+ };
283
+ }
284
+ }
285
+ return {
286
+ x: Infinity,
287
+ y: Infinity
288
+ };
289
+ }
290
+
291
+ function getInputType() {
292
+ if (typeof matchMedia === "function") {
293
+ return matchMedia("(pointer:coarse)").matches ? "coarse" : "fine";
294
+ }
295
+ }
296
+
297
+ const EXCEEDED_HORIZONTAL_MIN = 0b0001;
298
+ const EXCEEDED_HORIZONTAL_MAX = 0b0010;
299
+ const EXCEEDED_VERTICAL_MIN = 0b0100;
300
+ const EXCEEDED_VERTICAL_MAX = 0b1000;
301
+ const isCoarsePointer = getInputType() === "coarse";
302
+ let intersectingHandles = [];
303
+ let isPointerDown = false;
304
+ let ownerDocumentCounts = new Map();
305
+ let panelConstraintFlags = new Map();
306
+ const registeredResizeHandlers = new Set();
307
+ function registerResizeHandle(resizeHandleId, element, direction, hitAreaMargins, setResizeHandlerState) {
308
+ var _ownerDocumentCounts$;
309
+ const {
310
+ ownerDocument
311
+ } = element;
312
+ const data = {
313
+ direction,
314
+ element,
315
+ hitAreaMargins,
316
+ setResizeHandlerState
317
+ };
318
+ const count = (_ownerDocumentCounts$ = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$ !== void 0 ? _ownerDocumentCounts$ : 0;
319
+ ownerDocumentCounts.set(ownerDocument, count + 1);
320
+ registeredResizeHandlers.add(data);
321
+ updateListeners();
322
+ return function unregisterResizeHandle() {
323
+ var _ownerDocumentCounts$2;
324
+ panelConstraintFlags.delete(resizeHandleId);
325
+ registeredResizeHandlers.delete(data);
326
+ const count = (_ownerDocumentCounts$2 = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$2 !== void 0 ? _ownerDocumentCounts$2 : 1;
327
+ ownerDocumentCounts.set(ownerDocument, count - 1);
328
+ updateListeners();
329
+ if (count === 1) {
330
+ ownerDocumentCounts.delete(ownerDocument);
331
+ }
332
+ };
333
+ }
334
+ function handlePointerDown(event) {
335
+ const {
336
+ x,
337
+ y
338
+ } = getResizeEventCoordinates(event);
339
+ isPointerDown = true;
340
+ recalculateIntersectingHandles({
341
+ x,
342
+ y
343
+ });
344
+ updateListeners();
345
+ if (intersectingHandles.length > 0) {
346
+ updateResizeHandlerStates("down", event);
347
+ event.preventDefault();
348
+ }
349
+ }
350
+ function handlePointerMove(event) {
351
+ const {
352
+ x,
353
+ y
354
+ } = getResizeEventCoordinates(event);
355
+ if (isPointerDown) {
356
+ intersectingHandles.forEach(data => {
357
+ const {
358
+ setResizeHandlerState
359
+ } = data;
360
+ setResizeHandlerState("move", "drag", event);
361
+ });
362
+
363
+ // Update cursor based on return value(s) from active handles
364
+ updateCursor();
365
+ } else {
366
+ recalculateIntersectingHandles({
367
+ x,
368
+ y
369
+ });
370
+ updateResizeHandlerStates("move", event);
371
+ updateCursor();
372
+ }
373
+ if (intersectingHandles.length > 0) {
374
+ event.preventDefault();
375
+ }
376
+ }
377
+ function handlePointerUp(event) {
378
+ const {
379
+ x,
380
+ y
381
+ } = getResizeEventCoordinates(event);
382
+ panelConstraintFlags.clear();
383
+ isPointerDown = false;
384
+ if (intersectingHandles.length > 0) {
385
+ event.preventDefault();
386
+ }
387
+ recalculateIntersectingHandles({
388
+ x,
389
+ y
390
+ });
391
+ updateResizeHandlerStates("up", event);
392
+ updateCursor();
393
+ updateListeners();
394
+ }
395
+ function recalculateIntersectingHandles({
396
+ x,
397
+ y
398
+ }) {
399
+ intersectingHandles.splice(0);
400
+ registeredResizeHandlers.forEach(data => {
401
+ const {
402
+ element,
403
+ hitAreaMargins
404
+ } = data;
405
+ const {
406
+ bottom,
407
+ left,
408
+ right,
409
+ top
410
+ } = element.getBoundingClientRect();
411
+ const margin = isCoarsePointer ? hitAreaMargins.coarse : hitAreaMargins.fine;
412
+ const intersects = x >= left - margin && x <= right + margin && y >= top - margin && y <= bottom + margin;
413
+ if (intersects) {
414
+ intersectingHandles.push(data);
415
+ }
416
+ });
417
+ }
418
+ function reportConstraintsViolation(resizeHandleId, flag) {
419
+ panelConstraintFlags.set(resizeHandleId, flag);
420
+ }
421
+ function updateCursor() {
422
+ let intersectsHorizontal = false;
423
+ let intersectsVertical = false;
424
+ intersectingHandles.forEach(data => {
425
+ const {
426
+ direction
427
+ } = data;
428
+ if (direction === "horizontal") {
429
+ intersectsHorizontal = true;
430
+ } else {
431
+ intersectsVertical = true;
432
+ }
433
+ });
434
+ let constraintFlags = 0;
435
+ panelConstraintFlags.forEach(flag => {
436
+ constraintFlags |= flag;
437
+ });
438
+ if (intersectsHorizontal && intersectsVertical) {
439
+ setGlobalCursorStyle("intersection", constraintFlags);
440
+ } else if (intersectsHorizontal) {
441
+ setGlobalCursorStyle("horizontal", constraintFlags);
442
+ } else if (intersectsVertical) {
443
+ setGlobalCursorStyle("vertical", constraintFlags);
444
+ } else {
445
+ resetGlobalCursorStyle();
446
+ }
447
+ }
448
+ function updateListeners() {
449
+ ownerDocumentCounts.forEach((_, ownerDocument) => {
450
+ const {
451
+ body
452
+ } = ownerDocument;
453
+ body.removeEventListener("contextmenu", handlePointerUp);
454
+ body.removeEventListener("mousedown", handlePointerDown);
455
+ body.removeEventListener("mouseleave", handlePointerMove);
456
+ body.removeEventListener("mousemove", handlePointerMove);
457
+ body.removeEventListener("touchmove", handlePointerMove);
458
+ body.removeEventListener("touchstart", handlePointerDown);
459
+ });
460
+ window.removeEventListener("mouseup", handlePointerUp);
461
+ window.removeEventListener("touchcancel", handlePointerUp);
462
+ window.removeEventListener("touchend", handlePointerUp);
463
+ if (registerResizeHandle.length > 0) {
464
+ if (isPointerDown) {
465
+ if (intersectingHandles.length > 0) {
466
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
467
+ const {
468
+ body
469
+ } = ownerDocument;
470
+ if (count > 0) {
471
+ body.addEventListener("contextmenu", handlePointerUp);
472
+ body.addEventListener("mouseleave", handlePointerMove);
473
+ body.addEventListener("mousemove", handlePointerMove);
474
+ body.addEventListener("touchmove", handlePointerMove, {
475
+ passive: false
476
+ });
477
+ }
478
+ });
479
+ }
480
+ window.addEventListener("mouseup", handlePointerUp);
481
+ window.addEventListener("touchcancel", handlePointerUp);
482
+ window.addEventListener("touchend", handlePointerUp);
483
+ } else {
484
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
485
+ const {
486
+ body
487
+ } = ownerDocument;
488
+ if (count > 0) {
489
+ body.addEventListener("mousedown", handlePointerDown);
490
+ body.addEventListener("mousemove", handlePointerMove);
491
+ body.addEventListener("touchmove", handlePointerMove, {
492
+ passive: false
493
+ });
494
+ body.addEventListener("touchstart", handlePointerDown);
495
+ }
496
+ });
497
+ }
498
+ }
499
+ }
500
+ function updateResizeHandlerStates(action, event) {
501
+ registeredResizeHandlers.forEach(data => {
502
+ const {
503
+ setResizeHandlerState
504
+ } = data;
505
+ if (intersectingHandles.includes(data)) {
506
+ if (isPointerDown) {
507
+ setResizeHandlerState(action, "drag", event);
508
+ } else {
509
+ setResizeHandlerState(action, "hover", event);
510
+ }
511
+ } else {
512
+ setResizeHandlerState(action, "inactive", event);
513
+ }
514
+ });
515
+ }
516
+
201
517
  function assert(expectedCondition, message = "Assertion failed!") {
202
518
  if (!expectedCondition) {
203
519
  console.error(message);
@@ -703,27 +1019,13 @@ function areEqual(arrayA, arrayB) {
703
1019
  return true;
704
1020
  }
705
1021
 
706
- function isKeyDown(event) {
707
- return event.type === "keydown";
708
- }
709
- function isMouseEvent(event) {
710
- return event.type.startsWith("mouse");
711
- }
712
- function isTouchEvent(event) {
713
- return event.type.startsWith("touch");
714
- }
715
-
716
1022
  function getResizeEventCursorPosition(direction, event) {
717
1023
  const isHorizontal = direction === "horizontal";
718
- if (isMouseEvent(event)) {
719
- return isHorizontal ? event.clientX : event.clientY;
720
- } else if (isTouchEvent(event)) {
721
- const firstTouch = event.touches[0];
722
- assert(firstTouch);
723
- return isHorizontal ? firstTouch.screenX : firstTouch.screenY;
724
- } else {
725
- throw Error(`Unsupported event type "${event.type}"`);
726
- }
1024
+ const {
1025
+ x,
1026
+ y
1027
+ } = getResizeEventCoordinates(event);
1028
+ return isHorizontal ? x : y;
727
1029
  }
728
1030
 
729
1031
  function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
@@ -913,44 +1215,6 @@ function computePanelFlexBoxStyle({
913
1215
  };
914
1216
  }
915
1217
 
916
- let currentState = null;
917
- let element = null;
918
- function getCursorStyle(state) {
919
- switch (state) {
920
- case "horizontal":
921
- return "ew-resize";
922
- case "horizontal-max":
923
- return "w-resize";
924
- case "horizontal-min":
925
- return "e-resize";
926
- case "vertical":
927
- return "ns-resize";
928
- case "vertical-max":
929
- return "n-resize";
930
- case "vertical-min":
931
- return "s-resize";
932
- }
933
- }
934
- function resetGlobalCursorStyle() {
935
- if (element !== null) {
936
- document.head.removeChild(element);
937
- currentState = null;
938
- element = null;
939
- }
940
- }
941
- function setGlobalCursorStyle(state) {
942
- if (currentState === state) {
943
- return;
944
- }
945
- currentState = state;
946
- const style = getCursorStyle(state);
947
- if (element === null) {
948
- element = document.createElement("style");
949
- document.head.appendChild(element);
950
- }
951
- element.innerHTML = `*{cursor: ${style}!important;}`;
952
- }
953
-
954
1218
  function debounce(callback, durationMs = 10) {
955
1219
  let timeoutId = null;
956
1220
  let callable = (...args) => {
@@ -1496,18 +1760,15 @@ function PanelGroupWithForwardedRef({
1496
1760
  if (prevDeltaRef.current != delta) {
1497
1761
  prevDeltaRef.current = delta;
1498
1762
  if (!layoutChanged) {
1499
- // If the pointer has moved too far to resize the panel any further,
1500
- // update the cursor style for a visual clue.
1763
+ // If the pointer has moved too far to resize the panel any further, note this so we can update the cursor.
1501
1764
  // This mimics VS Code behavior.
1502
-
1503
1765
  if (isHorizontal) {
1504
- setGlobalCursorStyle(delta < 0 ? "horizontal-min" : "horizontal-max");
1766
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_HORIZONTAL_MIN : EXCEEDED_HORIZONTAL_MAX);
1505
1767
  } else {
1506
- setGlobalCursorStyle(delta < 0 ? "vertical-min" : "vertical-max");
1768
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_VERTICAL_MIN : EXCEEDED_VERTICAL_MAX);
1507
1769
  }
1508
1770
  } else {
1509
- // Reset the cursor style to the the normal resize cursor.
1510
- setGlobalCursorStyle(isHorizontal ? "horizontal" : "vertical");
1771
+ reportConstraintsViolation(dragHandleId, 0);
1511
1772
  }
1512
1773
  }
1513
1774
  }
@@ -1605,7 +1866,6 @@ function PanelGroupWithForwardedRef({
1605
1866
  });
1606
1867
  }, []);
1607
1868
  const stopDragging = useCallback(() => {
1608
- resetGlobalCursorStyle();
1609
1869
  setDragState(null);
1610
1870
  }, []);
1611
1871
  const unregisterPanel = useCallback(panelData => {
@@ -1746,6 +2006,7 @@ function PanelResizeHandle({
1746
2006
  children = null,
1747
2007
  className: classNameFromProps = "",
1748
2008
  disabled = false,
2009
+ hitAreaMargins,
1749
2010
  id: idFromProps,
1750
2011
  onDragging,
1751
2012
  style: styleFromProps = {},
@@ -1768,67 +2029,60 @@ function PanelResizeHandle({
1768
2029
  }
1769
2030
  const {
1770
2031
  direction,
1771
- dragState,
1772
2032
  groupId,
1773
- registerResizeHandle,
2033
+ registerResizeHandle: registerResizeHandleWithParentGroup,
1774
2034
  startDragging,
1775
2035
  stopDragging,
1776
2036
  panelGroupElement
1777
2037
  } = panelGroupContext;
1778
2038
  const resizeHandleId = useUniqueId(idFromProps);
1779
- const isDragging = (dragState === null || dragState === void 0 ? void 0 : dragState.dragHandleId) === resizeHandleId;
2039
+ const [state, setState] = useState("inactive");
1780
2040
  const [isFocused, setIsFocused] = useState(false);
1781
2041
  const [resizeHandler, setResizeHandler] = useState(null);
1782
- const stopDraggingAndBlur = useCallback(() => {
1783
- // Clicking on the drag handle shouldn't leave it focused;
1784
- // That would cause the PanelGroup to think it was still active.
1785
- const element = elementRef.current;
1786
- assert(element);
1787
- element.blur();
1788
- stopDragging();
1789
- const {
1790
- onDragging
1791
- } = callbacksRef.current;
1792
- if (onDragging) {
1793
- onDragging(false);
1794
- }
1795
- }, [stopDragging]);
1796
2042
  useEffect(() => {
1797
2043
  if (disabled) {
1798
2044
  setResizeHandler(null);
1799
2045
  } else {
1800
- const resizeHandler = registerResizeHandle(resizeHandleId);
2046
+ const resizeHandler = registerResizeHandleWithParentGroup(resizeHandleId);
1801
2047
  setResizeHandler(() => resizeHandler);
1802
2048
  }
1803
- }, [disabled, resizeHandleId, registerResizeHandle]);
2049
+ }, [disabled, resizeHandleId, registerResizeHandleWithParentGroup]);
1804
2050
  useEffect(() => {
1805
- if (disabled || resizeHandler == null || !isDragging) {
2051
+ var _hitAreaMargins$coars, _hitAreaMargins$fine;
2052
+ if (disabled || resizeHandler == null) {
1806
2053
  return;
1807
2054
  }
1808
- const onMove = event => {
1809
- resizeHandler(event);
1810
- };
1811
- const onMouseLeave = event => {
1812
- resizeHandler(event);
1813
- };
1814
2055
  const element = elementRef.current;
1815
2056
  assert(element);
1816
- const targetDocument = element.ownerDocument;
1817
- targetDocument.body.addEventListener("contextmenu", stopDraggingAndBlur);
1818
- targetDocument.body.addEventListener("mousemove", onMove);
1819
- targetDocument.body.addEventListener("touchmove", onMove);
1820
- targetDocument.body.addEventListener("mouseleave", onMouseLeave);
1821
- window.addEventListener("mouseup", stopDraggingAndBlur);
1822
- window.addEventListener("touchend", stopDraggingAndBlur);
1823
- return () => {
1824
- targetDocument.body.removeEventListener("contextmenu", stopDraggingAndBlur);
1825
- targetDocument.body.removeEventListener("mousemove", onMove);
1826
- targetDocument.body.removeEventListener("touchmove", onMove);
1827
- targetDocument.body.removeEventListener("mouseleave", onMouseLeave);
1828
- window.removeEventListener("mouseup", stopDraggingAndBlur);
1829
- window.removeEventListener("touchend", stopDraggingAndBlur);
2057
+ const setResizeHandlerState = (action, state, event) => {
2058
+ setState(state);
2059
+ switch (action) {
2060
+ case "down":
2061
+ {
2062
+ startDragging(resizeHandleId, event);
2063
+ break;
2064
+ }
2065
+ case "up":
2066
+ {
2067
+ stopDragging();
2068
+ break;
2069
+ }
2070
+ }
2071
+ switch (state) {
2072
+ case "drag":
2073
+ {
2074
+ resizeHandler(event);
2075
+ break;
2076
+ }
2077
+ }
1830
2078
  };
1831
- }, [direction, disabled, isDragging, resizeHandler, stopDraggingAndBlur]);
2079
+ return registerResizeHandle(resizeHandleId, element, direction, {
2080
+ // Coarse inputs (e.g. finger/touch)
2081
+ coarse: (_hitAreaMargins$coars = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.coarse) !== null && _hitAreaMargins$coars !== void 0 ? _hitAreaMargins$coars : 15,
2082
+ // Fine inputs (e.g. mouse)
2083
+ fine: (_hitAreaMargins$fine = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.fine) !== null && _hitAreaMargins$fine !== void 0 ? _hitAreaMargins$fine : 5
2084
+ }, setResizeHandlerState);
2085
+ }, [direction, disabled, hitAreaMargins, registerResizeHandleWithParentGroup, resizeHandleId, resizeHandler, startDragging, stopDragging]);
1832
2086
  useWindowSplitterResizeHandlerBehavior({
1833
2087
  disabled,
1834
2088
  handleId: resizeHandleId,
@@ -1836,7 +2090,6 @@ function PanelResizeHandle({
1836
2090
  panelGroupElement
1837
2091
  });
1838
2092
  const style = {
1839
- cursor: getCursorStyle(direction),
1840
2093
  touchAction: "none",
1841
2094
  userSelect: "none"
1842
2095
  };
@@ -1846,31 +2099,6 @@ function PanelResizeHandle({
1846
2099
  className: classNameFromProps,
1847
2100
  onBlur: () => setIsFocused(false),
1848
2101
  onFocus: () => setIsFocused(true),
1849
- onMouseDown: event => {
1850
- startDragging(resizeHandleId, event.nativeEvent);
1851
- const callbacks = callbacksRef.current;
1852
- assert(callbacks);
1853
- const {
1854
- onDragging
1855
- } = callbacks;
1856
- if (onDragging) {
1857
- onDragging(true);
1858
- }
1859
- },
1860
- onMouseUp: stopDraggingAndBlur,
1861
- onTouchCancel: stopDraggingAndBlur,
1862
- onTouchEnd: stopDraggingAndBlur,
1863
- onTouchStart: event => {
1864
- startDragging(resizeHandleId, event.nativeEvent);
1865
- const callbacks = callbacksRef.current;
1866
- assert(callbacks);
1867
- const {
1868
- onDragging
1869
- } = callbacks;
1870
- if (onDragging) {
1871
- onDragging(true);
1872
- }
1873
- },
1874
2102
  ref: elementRef,
1875
2103
  role: "separator",
1876
2104
  style: {
@@ -1882,7 +2110,8 @@ function PanelResizeHandle({
1882
2110
  "data-panel-group-direction": direction,
1883
2111
  "data-panel-group-id": groupId,
1884
2112
  "data-resize-handle": "",
1885
- "data-resize-handle-active": isDragging ? "pointer" : isFocused ? "keyboard" : undefined,
2113
+ "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2114
+ "data-resize-handle-state": state,
1886
2115
  "data-panel-resize-handle-enabled": !disabled,
1887
2116
  "data-panel-resize-handle-id": resizeHandleId
1888
2117
  });