react-resizable-panels 1.0.10 → 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.
@@ -174,6 +174,322 @@ const Panel = forwardRef((props, ref) => createElement(PanelWithForwardedRef, {
174
174
  PanelWithForwardedRef.displayName = "Panel";
175
175
  Panel.displayName = "forwardRef(Panel)";
176
176
 
177
+ let currentCursorStyle = null;
178
+ let styleElement = null;
179
+ function getCursorStyle(state, constraintFlags) {
180
+ if (constraintFlags) {
181
+ const horizontalMin = (constraintFlags & EXCEEDED_HORIZONTAL_MIN) !== 0;
182
+ const horizontalMax = (constraintFlags & EXCEEDED_HORIZONTAL_MAX) !== 0;
183
+ const verticalMin = (constraintFlags & EXCEEDED_VERTICAL_MIN) !== 0;
184
+ const verticalMax = (constraintFlags & EXCEEDED_VERTICAL_MAX) !== 0;
185
+ if (horizontalMin) {
186
+ if (verticalMin) {
187
+ return "se-resize";
188
+ } else if (verticalMax) {
189
+ return "ne-resize";
190
+ } else {
191
+ return "e-resize";
192
+ }
193
+ } else if (horizontalMax) {
194
+ if (verticalMin) {
195
+ return "sw-resize";
196
+ } else if (verticalMax) {
197
+ return "nw-resize";
198
+ } else {
199
+ return "w-resize";
200
+ }
201
+ } else if (verticalMin) {
202
+ return "s-resize";
203
+ } else if (verticalMax) {
204
+ return "n-resize";
205
+ }
206
+ }
207
+ switch (state) {
208
+ case "horizontal":
209
+ return "ew-resize";
210
+ case "intersection":
211
+ return "move";
212
+ case "vertical":
213
+ return "ns-resize";
214
+ }
215
+ }
216
+ function resetGlobalCursorStyle() {
217
+ if (styleElement !== null) {
218
+ document.head.removeChild(styleElement);
219
+ currentCursorStyle = null;
220
+ styleElement = null;
221
+ }
222
+ }
223
+ function setGlobalCursorStyle(state, constraintFlags) {
224
+ const style = getCursorStyle(state, constraintFlags);
225
+ if (currentCursorStyle === style) {
226
+ return;
227
+ }
228
+ currentCursorStyle = style;
229
+ if (styleElement === null) {
230
+ styleElement = document.createElement("style");
231
+ document.head.appendChild(styleElement);
232
+ }
233
+ styleElement.innerHTML = `*{cursor: ${style}!important;}`;
234
+ }
235
+
236
+ function isKeyDown(event) {
237
+ return event.type === "keydown";
238
+ }
239
+ function isMouseEvent(event) {
240
+ return event.type.startsWith("mouse");
241
+ }
242
+ function isTouchEvent(event) {
243
+ return event.type.startsWith("touch");
244
+ }
245
+
246
+ function getResizeEventCoordinates(event) {
247
+ if (isMouseEvent(event)) {
248
+ return {
249
+ x: event.pageX,
250
+ y: event.pageY
251
+ };
252
+ } else if (isTouchEvent(event)) {
253
+ const touch = event.touches[0];
254
+ if (touch && touch.pageX && touch.pageY) {
255
+ return {
256
+ x: touch.pageX,
257
+ y: touch.pageY
258
+ };
259
+ }
260
+ }
261
+ return {
262
+ x: Infinity,
263
+ y: Infinity
264
+ };
265
+ }
266
+
267
+ function getInputType() {
268
+ if (typeof matchMedia === "function") {
269
+ return matchMedia("(pointer:coarse)").matches ? "coarse" : "fine";
270
+ }
271
+ }
272
+
273
+ const EXCEEDED_HORIZONTAL_MIN = 0b0001;
274
+ const EXCEEDED_HORIZONTAL_MAX = 0b0010;
275
+ const EXCEEDED_VERTICAL_MIN = 0b0100;
276
+ const EXCEEDED_VERTICAL_MAX = 0b1000;
277
+ const isCoarsePointer = getInputType() === "coarse";
278
+ let intersectingHandles = [];
279
+ let isPointerDown = false;
280
+ let ownerDocumentCounts = new Map();
281
+ let panelConstraintFlags = new Map();
282
+ const registeredResizeHandlers = new Set();
283
+ function registerResizeHandle(resizeHandleId, element, direction, hitAreaMargins, setResizeHandlerState) {
284
+ var _ownerDocumentCounts$;
285
+ const {
286
+ ownerDocument
287
+ } = element;
288
+ const data = {
289
+ direction,
290
+ element,
291
+ hitAreaMargins,
292
+ setResizeHandlerState
293
+ };
294
+ const count = (_ownerDocumentCounts$ = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$ !== void 0 ? _ownerDocumentCounts$ : 0;
295
+ ownerDocumentCounts.set(ownerDocument, count + 1);
296
+ registeredResizeHandlers.add(data);
297
+ updateListeners();
298
+ return function unregisterResizeHandle() {
299
+ var _ownerDocumentCounts$2;
300
+ panelConstraintFlags.delete(resizeHandleId);
301
+ registeredResizeHandlers.delete(data);
302
+ const count = (_ownerDocumentCounts$2 = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$2 !== void 0 ? _ownerDocumentCounts$2 : 1;
303
+ ownerDocumentCounts.set(ownerDocument, count - 1);
304
+ updateListeners();
305
+ if (count === 1) {
306
+ ownerDocumentCounts.delete(ownerDocument);
307
+ }
308
+ };
309
+ }
310
+ function handlePointerDown(event) {
311
+ const {
312
+ x,
313
+ y
314
+ } = getResizeEventCoordinates(event);
315
+ isPointerDown = true;
316
+ updateResizeHandlerStates("down", event);
317
+ recalculateIntersectingHandles({
318
+ x,
319
+ y
320
+ });
321
+ updateListeners();
322
+ if (intersectingHandles.length > 0) {
323
+ event.preventDefault();
324
+ }
325
+ }
326
+ function handlePointerMove(event) {
327
+ const {
328
+ x,
329
+ y
330
+ } = getResizeEventCoordinates(event);
331
+ if (isPointerDown) {
332
+ intersectingHandles.forEach(data => {
333
+ const {
334
+ setResizeHandlerState
335
+ } = data;
336
+ setResizeHandlerState("move", "drag", event);
337
+ });
338
+
339
+ // Update cursor based on return value(s) from active handles
340
+ updateCursor();
341
+ } else {
342
+ recalculateIntersectingHandles({
343
+ x,
344
+ y
345
+ });
346
+ updateResizeHandlerStates("move", event);
347
+ updateCursor();
348
+ }
349
+ if (intersectingHandles.length > 0) {
350
+ event.preventDefault();
351
+ }
352
+ }
353
+ function handlePointerUp(event) {
354
+ const {
355
+ x,
356
+ y
357
+ } = getResizeEventCoordinates(event);
358
+ panelConstraintFlags.clear();
359
+ isPointerDown = false;
360
+ if (intersectingHandles.length > 0) {
361
+ event.preventDefault();
362
+ }
363
+ recalculateIntersectingHandles({
364
+ x,
365
+ y
366
+ });
367
+ updateResizeHandlerStates("up", event);
368
+ updateCursor();
369
+ updateListeners();
370
+ }
371
+ function recalculateIntersectingHandles({
372
+ x,
373
+ y
374
+ }) {
375
+ intersectingHandles.splice(0);
376
+ registeredResizeHandlers.forEach(data => {
377
+ const {
378
+ element,
379
+ hitAreaMargins
380
+ } = data;
381
+ const {
382
+ bottom,
383
+ left,
384
+ right,
385
+ top
386
+ } = element.getBoundingClientRect();
387
+ const margin = isCoarsePointer ? hitAreaMargins.coarse : hitAreaMargins.fine;
388
+ const intersects = x >= left - margin && x <= right + margin && y >= top - margin && y <= bottom + margin;
389
+ if (intersects) {
390
+ intersectingHandles.push(data);
391
+ }
392
+ });
393
+ }
394
+ function reportConstraintsViolation(resizeHandleId, flag) {
395
+ panelConstraintFlags.set(resizeHandleId, flag);
396
+ }
397
+ function updateCursor() {
398
+ let intersectsHorizontal = false;
399
+ let intersectsVertical = false;
400
+ intersectingHandles.forEach(data => {
401
+ const {
402
+ direction
403
+ } = data;
404
+ if (direction === "horizontal") {
405
+ intersectsHorizontal = true;
406
+ } else {
407
+ intersectsVertical = true;
408
+ }
409
+ });
410
+ let constraintFlags = 0;
411
+ panelConstraintFlags.forEach(flag => {
412
+ constraintFlags |= flag;
413
+ });
414
+ if (intersectsHorizontal && intersectsVertical) {
415
+ setGlobalCursorStyle("intersection", constraintFlags);
416
+ } else if (intersectsHorizontal) {
417
+ setGlobalCursorStyle("horizontal", constraintFlags);
418
+ } else if (intersectsVertical) {
419
+ setGlobalCursorStyle("vertical", constraintFlags);
420
+ } else {
421
+ resetGlobalCursorStyle();
422
+ }
423
+ }
424
+ function updateListeners() {
425
+ ownerDocumentCounts.forEach((_, ownerDocument) => {
426
+ const {
427
+ body
428
+ } = ownerDocument;
429
+ body.removeEventListener("contextmenu", handlePointerUp);
430
+ body.removeEventListener("mousedown", handlePointerDown);
431
+ body.removeEventListener("mouseleave", handlePointerMove);
432
+ body.removeEventListener("mousemove", handlePointerMove);
433
+ body.removeEventListener("touchmove", handlePointerMove);
434
+ body.removeEventListener("touchstart", handlePointerDown);
435
+ });
436
+ window.removeEventListener("mouseup", handlePointerUp);
437
+ window.removeEventListener("touchcancel", handlePointerUp);
438
+ window.removeEventListener("touchend", handlePointerUp);
439
+ if (registerResizeHandle.length > 0) {
440
+ if (isPointerDown) {
441
+ if (intersectingHandles.length > 0) {
442
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
443
+ const {
444
+ body
445
+ } = ownerDocument;
446
+ if (count > 0) {
447
+ body.addEventListener("contextmenu", handlePointerUp);
448
+ body.addEventListener("mouseleave", handlePointerMove);
449
+ body.addEventListener("mousemove", handlePointerMove);
450
+ body.addEventListener("touchmove", handlePointerMove, {
451
+ passive: false
452
+ });
453
+ }
454
+ });
455
+ }
456
+ window.addEventListener("mouseup", handlePointerUp);
457
+ window.addEventListener("touchcancel", handlePointerUp);
458
+ window.addEventListener("touchend", handlePointerUp);
459
+ } else {
460
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
461
+ const {
462
+ body
463
+ } = ownerDocument;
464
+ if (count > 0) {
465
+ body.addEventListener("mousedown", handlePointerDown);
466
+ body.addEventListener("mousemove", handlePointerMove);
467
+ body.addEventListener("touchmove", handlePointerMove, {
468
+ passive: false
469
+ });
470
+ body.addEventListener("touchstart", handlePointerDown);
471
+ }
472
+ });
473
+ }
474
+ }
475
+ }
476
+ function updateResizeHandlerStates(action, event) {
477
+ registeredResizeHandlers.forEach(data => {
478
+ const {
479
+ setResizeHandlerState
480
+ } = data;
481
+ if (intersectingHandles.includes(data)) {
482
+ if (isPointerDown) {
483
+ setResizeHandlerState(action, "drag", event);
484
+ } else {
485
+ setResizeHandlerState(action, "hover", event);
486
+ }
487
+ } else {
488
+ setResizeHandlerState(action, "inactive", event);
489
+ }
490
+ });
491
+ }
492
+
177
493
  function assert(expectedCondition, message = "Assertion failed!") {
178
494
  if (!expectedCondition) {
179
495
  console.error(message);
@@ -679,27 +995,13 @@ function areEqual(arrayA, arrayB) {
679
995
  return true;
680
996
  }
681
997
 
682
- function isKeyDown(event) {
683
- return event.type === "keydown";
684
- }
685
- function isMouseEvent(event) {
686
- return event.type.startsWith("mouse");
687
- }
688
- function isTouchEvent(event) {
689
- return event.type.startsWith("touch");
690
- }
691
-
692
998
  function getResizeEventCursorPosition(direction, event) {
693
999
  const isHorizontal = direction === "horizontal";
694
- if (isMouseEvent(event)) {
695
- return isHorizontal ? event.clientX : event.clientY;
696
- } else if (isTouchEvent(event)) {
697
- const firstTouch = event.touches[0];
698
- assert(firstTouch);
699
- return isHorizontal ? firstTouch.screenX : firstTouch.screenY;
700
- } else {
701
- throw Error(`Unsupported event type "${event.type}"`);
702
- }
1000
+ const {
1001
+ x,
1002
+ y
1003
+ } = getResizeEventCoordinates(event);
1004
+ return isHorizontal ? x : y;
703
1005
  }
704
1006
 
705
1007
  function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
@@ -889,44 +1191,6 @@ function computePanelFlexBoxStyle({
889
1191
  };
890
1192
  }
891
1193
 
892
- let currentState = null;
893
- let element = null;
894
- function getCursorStyle(state) {
895
- switch (state) {
896
- case "horizontal":
897
- return "ew-resize";
898
- case "horizontal-max":
899
- return "w-resize";
900
- case "horizontal-min":
901
- return "e-resize";
902
- case "vertical":
903
- return "ns-resize";
904
- case "vertical-max":
905
- return "n-resize";
906
- case "vertical-min":
907
- return "s-resize";
908
- }
909
- }
910
- function resetGlobalCursorStyle() {
911
- if (element !== null) {
912
- document.head.removeChild(element);
913
- currentState = null;
914
- element = null;
915
- }
916
- }
917
- function setGlobalCursorStyle(state) {
918
- if (currentState === state) {
919
- return;
920
- }
921
- currentState = state;
922
- const style = getCursorStyle(state);
923
- if (element === null) {
924
- element = document.createElement("style");
925
- document.head.appendChild(element);
926
- }
927
- element.innerHTML = `*{cursor: ${style}!important;}`;
928
- }
929
-
930
1194
  function debounce(callback, durationMs = 10) {
931
1195
  let timeoutId = null;
932
1196
  let callable = (...args) => {
@@ -1472,18 +1736,15 @@ function PanelGroupWithForwardedRef({
1472
1736
  if (prevDeltaRef.current != delta) {
1473
1737
  prevDeltaRef.current = delta;
1474
1738
  if (!layoutChanged) {
1475
- // If the pointer has moved too far to resize the panel any further,
1476
- // update the cursor style for a visual clue.
1739
+ // If the pointer has moved too far to resize the panel any further, note this so we can update the cursor.
1477
1740
  // This mimics VS Code behavior.
1478
-
1479
1741
  if (isHorizontal) {
1480
- setGlobalCursorStyle(delta < 0 ? "horizontal-min" : "horizontal-max");
1742
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_HORIZONTAL_MIN : EXCEEDED_HORIZONTAL_MAX);
1481
1743
  } else {
1482
- setGlobalCursorStyle(delta < 0 ? "vertical-min" : "vertical-max");
1744
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_VERTICAL_MIN : EXCEEDED_VERTICAL_MAX);
1483
1745
  }
1484
1746
  } else {
1485
- // Reset the cursor style to the the normal resize cursor.
1486
- setGlobalCursorStyle(isHorizontal ? "horizontal" : "vertical");
1747
+ reportConstraintsViolation(dragHandleId, 0);
1487
1748
  }
1488
1749
  }
1489
1750
  }
@@ -1581,7 +1842,6 @@ function PanelGroupWithForwardedRef({
1581
1842
  });
1582
1843
  }, []);
1583
1844
  const stopDragging = useCallback(() => {
1584
- resetGlobalCursorStyle();
1585
1845
  setDragState(null);
1586
1846
  }, []);
1587
1847
  const unregisterPanel = useCallback(panelData => {
@@ -1722,6 +1982,7 @@ function PanelResizeHandle({
1722
1982
  children = null,
1723
1983
  className: classNameFromProps = "",
1724
1984
  disabled = false,
1985
+ hitAreaMargins,
1725
1986
  id: idFromProps,
1726
1987
  onDragging,
1727
1988
  style: styleFromProps = {},
@@ -1744,67 +2005,60 @@ function PanelResizeHandle({
1744
2005
  }
1745
2006
  const {
1746
2007
  direction,
1747
- dragState,
1748
2008
  groupId,
1749
- registerResizeHandle,
2009
+ registerResizeHandle: registerResizeHandleWithParentGroup,
1750
2010
  startDragging,
1751
2011
  stopDragging,
1752
2012
  panelGroupElement
1753
2013
  } = panelGroupContext;
1754
2014
  const resizeHandleId = useUniqueId(idFromProps);
1755
- const isDragging = (dragState === null || dragState === void 0 ? void 0 : dragState.dragHandleId) === resizeHandleId;
2015
+ const [state, setState] = useState("inactive");
1756
2016
  const [isFocused, setIsFocused] = useState(false);
1757
2017
  const [resizeHandler, setResizeHandler] = useState(null);
1758
- const stopDraggingAndBlur = useCallback(() => {
1759
- // Clicking on the drag handle shouldn't leave it focused;
1760
- // That would cause the PanelGroup to think it was still active.
1761
- const element = elementRef.current;
1762
- assert(element);
1763
- element.blur();
1764
- stopDragging();
1765
- const {
1766
- onDragging
1767
- } = callbacksRef.current;
1768
- if (onDragging) {
1769
- onDragging(false);
1770
- }
1771
- }, [stopDragging]);
1772
2018
  useEffect(() => {
1773
2019
  if (disabled) {
1774
2020
  setResizeHandler(null);
1775
2021
  } else {
1776
- const resizeHandler = registerResizeHandle(resizeHandleId);
2022
+ const resizeHandler = registerResizeHandleWithParentGroup(resizeHandleId);
1777
2023
  setResizeHandler(() => resizeHandler);
1778
2024
  }
1779
- }, [disabled, resizeHandleId, registerResizeHandle]);
2025
+ }, [disabled, resizeHandleId, registerResizeHandleWithParentGroup]);
1780
2026
  useEffect(() => {
1781
- if (disabled || resizeHandler == null || !isDragging) {
2027
+ var _hitAreaMargins$coars, _hitAreaMargins$fine;
2028
+ if (disabled || resizeHandler == null) {
1782
2029
  return;
1783
2030
  }
1784
- const onMove = event => {
1785
- resizeHandler(event);
1786
- };
1787
- const onMouseLeave = event => {
1788
- resizeHandler(event);
1789
- };
1790
2031
  const element = elementRef.current;
1791
2032
  assert(element);
1792
- const targetDocument = element.ownerDocument;
1793
- targetDocument.body.addEventListener("contextmenu", stopDraggingAndBlur);
1794
- targetDocument.body.addEventListener("mousemove", onMove);
1795
- targetDocument.body.addEventListener("touchmove", onMove);
1796
- targetDocument.body.addEventListener("mouseleave", onMouseLeave);
1797
- window.addEventListener("mouseup", stopDraggingAndBlur);
1798
- window.addEventListener("touchend", stopDraggingAndBlur);
1799
- return () => {
1800
- targetDocument.body.removeEventListener("contextmenu", stopDraggingAndBlur);
1801
- targetDocument.body.removeEventListener("mousemove", onMove);
1802
- targetDocument.body.removeEventListener("touchmove", onMove);
1803
- targetDocument.body.removeEventListener("mouseleave", onMouseLeave);
1804
- window.removeEventListener("mouseup", stopDraggingAndBlur);
1805
- window.removeEventListener("touchend", stopDraggingAndBlur);
2033
+ const setResizeHandlerState = (action, state, event) => {
2034
+ setState(state);
2035
+ switch (action) {
2036
+ case "down":
2037
+ {
2038
+ startDragging(resizeHandleId, event);
2039
+ break;
2040
+ }
2041
+ case "up":
2042
+ {
2043
+ stopDragging();
2044
+ break;
2045
+ }
2046
+ }
2047
+ switch (state) {
2048
+ case "drag":
2049
+ {
2050
+ resizeHandler(event);
2051
+ break;
2052
+ }
2053
+ }
1806
2054
  };
1807
- }, [direction, disabled, isDragging, resizeHandler, stopDraggingAndBlur]);
2055
+ return registerResizeHandle(resizeHandleId, element, direction, {
2056
+ // Coarse inputs (e.g. finger/touch)
2057
+ coarse: (_hitAreaMargins$coars = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.coarse) !== null && _hitAreaMargins$coars !== void 0 ? _hitAreaMargins$coars : 15,
2058
+ // Fine inputs (e.g. mouse)
2059
+ fine: (_hitAreaMargins$fine = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.fine) !== null && _hitAreaMargins$fine !== void 0 ? _hitAreaMargins$fine : 5
2060
+ }, setResizeHandlerState);
2061
+ }, [direction, disabled, hitAreaMargins, registerResizeHandleWithParentGroup, resizeHandleId, resizeHandler, startDragging, stopDragging]);
1808
2062
  useWindowSplitterResizeHandlerBehavior({
1809
2063
  disabled,
1810
2064
  handleId: resizeHandleId,
@@ -1812,7 +2066,6 @@ function PanelResizeHandle({
1812
2066
  panelGroupElement
1813
2067
  });
1814
2068
  const style = {
1815
- cursor: getCursorStyle(direction),
1816
2069
  touchAction: "none",
1817
2070
  userSelect: "none"
1818
2071
  };
@@ -1822,31 +2075,6 @@ function PanelResizeHandle({
1822
2075
  className: classNameFromProps,
1823
2076
  onBlur: () => setIsFocused(false),
1824
2077
  onFocus: () => setIsFocused(true),
1825
- onMouseDown: event => {
1826
- startDragging(resizeHandleId, event.nativeEvent);
1827
- const callbacks = callbacksRef.current;
1828
- assert(callbacks);
1829
- const {
1830
- onDragging
1831
- } = callbacks;
1832
- if (onDragging) {
1833
- onDragging(true);
1834
- }
1835
- },
1836
- onMouseUp: stopDraggingAndBlur,
1837
- onTouchCancel: stopDraggingAndBlur,
1838
- onTouchEnd: stopDraggingAndBlur,
1839
- onTouchStart: event => {
1840
- startDragging(resizeHandleId, event.nativeEvent);
1841
- const callbacks = callbacksRef.current;
1842
- assert(callbacks);
1843
- const {
1844
- onDragging
1845
- } = callbacks;
1846
- if (onDragging) {
1847
- onDragging(true);
1848
- }
1849
- },
1850
2078
  ref: elementRef,
1851
2079
  role: "separator",
1852
2080
  style: {
@@ -1858,7 +2086,8 @@ function PanelResizeHandle({
1858
2086
  "data-panel-group-direction": direction,
1859
2087
  "data-panel-group-id": groupId,
1860
2088
  "data-resize-handle": "",
1861
- "data-resize-handle-active": isDragging ? "pointer" : isFocused ? "keyboard" : undefined,
2089
+ "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2090
+ "data-resize-handle-state": state,
1862
2091
  "data-panel-resize-handle-enabled": !disabled,
1863
2092
  "data-panel-resize-handle-id": resizeHandleId
1864
2093
  });