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