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);
@@ -606,27 +922,13 @@ function areEqual(arrayA, arrayB) {
606
922
  return true;
607
923
  }
608
924
 
609
- function isKeyDown(event) {
610
- return event.type === "keydown";
611
- }
612
- function isMouseEvent(event) {
613
- return event.type.startsWith("mouse");
614
- }
615
- function isTouchEvent(event) {
616
- return event.type.startsWith("touch");
617
- }
618
-
619
925
  function getResizeEventCursorPosition(direction, event) {
620
926
  const isHorizontal = direction === "horizontal";
621
- if (isMouseEvent(event)) {
622
- return isHorizontal ? event.clientX : event.clientY;
623
- } else if (isTouchEvent(event)) {
624
- const firstTouch = event.touches[0];
625
- assert(firstTouch);
626
- return isHorizontal ? firstTouch.screenX : firstTouch.screenY;
627
- } else {
628
- throw Error(`Unsupported event type "${event.type}"`);
629
- }
927
+ const {
928
+ x,
929
+ y
930
+ } = getResizeEventCoordinates(event);
931
+ return isHorizontal ? x : y;
630
932
  }
631
933
 
632
934
  function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
@@ -775,44 +1077,6 @@ function computePanelFlexBoxStyle({
775
1077
  };
776
1078
  }
777
1079
 
778
- let currentState = null;
779
- let element = null;
780
- function getCursorStyle(state) {
781
- switch (state) {
782
- case "horizontal":
783
- return "ew-resize";
784
- case "horizontal-max":
785
- return "w-resize";
786
- case "horizontal-min":
787
- return "e-resize";
788
- case "vertical":
789
- return "ns-resize";
790
- case "vertical-max":
791
- return "n-resize";
792
- case "vertical-min":
793
- return "s-resize";
794
- }
795
- }
796
- function resetGlobalCursorStyle() {
797
- if (element !== null) {
798
- document.head.removeChild(element);
799
- currentState = null;
800
- element = null;
801
- }
802
- }
803
- function setGlobalCursorStyle(state) {
804
- if (currentState === state) {
805
- return;
806
- }
807
- currentState = state;
808
- const style = getCursorStyle(state);
809
- if (element === null) {
810
- element = document.createElement("style");
811
- document.head.appendChild(element);
812
- }
813
- element.innerHTML = `*{cursor: ${style}!important;}`;
814
- }
815
-
816
1080
  function debounce(callback, durationMs = 10) {
817
1081
  let timeoutId = null;
818
1082
  let callable = (...args) => {
@@ -1386,18 +1650,15 @@ function PanelGroupWithForwardedRef({
1386
1650
  if (prevDeltaRef.current != delta) {
1387
1651
  prevDeltaRef.current = delta;
1388
1652
  if (!layoutChanged) {
1389
- // If the pointer has moved too far to resize the panel any further,
1390
- // update the cursor style for a visual clue.
1653
+ // If the pointer has moved too far to resize the panel any further, note this so we can update the cursor.
1391
1654
  // This mimics VS Code behavior.
1392
-
1393
1655
  if (isHorizontal) {
1394
- setGlobalCursorStyle(delta < 0 ? "horizontal-min" : "horizontal-max");
1656
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_HORIZONTAL_MIN : EXCEEDED_HORIZONTAL_MAX);
1395
1657
  } else {
1396
- setGlobalCursorStyle(delta < 0 ? "vertical-min" : "vertical-max");
1658
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_VERTICAL_MIN : EXCEEDED_VERTICAL_MAX);
1397
1659
  }
1398
1660
  } else {
1399
- // Reset the cursor style to the the normal resize cursor.
1400
- setGlobalCursorStyle(isHorizontal ? "horizontal" : "vertical");
1661
+ reportConstraintsViolation(dragHandleId, 0);
1401
1662
  }
1402
1663
  }
1403
1664
  }
@@ -1495,7 +1756,6 @@ function PanelGroupWithForwardedRef({
1495
1756
  });
1496
1757
  }, []);
1497
1758
  const stopDragging = useCallback(() => {
1498
- resetGlobalCursorStyle();
1499
1759
  setDragState(null);
1500
1760
  }, []);
1501
1761
  const unregisterPanel = useCallback(panelData => {
@@ -1636,6 +1896,7 @@ function PanelResizeHandle({
1636
1896
  children = null,
1637
1897
  className: classNameFromProps = "",
1638
1898
  disabled = false,
1899
+ hitAreaMargins,
1639
1900
  id: idFromProps,
1640
1901
  onDragging,
1641
1902
  style: styleFromProps = {},
@@ -1658,67 +1919,60 @@ function PanelResizeHandle({
1658
1919
  }
1659
1920
  const {
1660
1921
  direction,
1661
- dragState,
1662
1922
  groupId,
1663
- registerResizeHandle,
1923
+ registerResizeHandle: registerResizeHandleWithParentGroup,
1664
1924
  startDragging,
1665
1925
  stopDragging,
1666
1926
  panelGroupElement
1667
1927
  } = panelGroupContext;
1668
1928
  const resizeHandleId = useUniqueId(idFromProps);
1669
- const isDragging = (dragState === null || dragState === void 0 ? void 0 : dragState.dragHandleId) === resizeHandleId;
1929
+ const [state, setState] = useState("inactive");
1670
1930
  const [isFocused, setIsFocused] = useState(false);
1671
1931
  const [resizeHandler, setResizeHandler] = useState(null);
1672
- const stopDraggingAndBlur = useCallback(() => {
1673
- // Clicking on the drag handle shouldn't leave it focused;
1674
- // That would cause the PanelGroup to think it was still active.
1675
- const element = elementRef.current;
1676
- assert(element);
1677
- element.blur();
1678
- stopDragging();
1679
- const {
1680
- onDragging
1681
- } = callbacksRef.current;
1682
- if (onDragging) {
1683
- onDragging(false);
1684
- }
1685
- }, [stopDragging]);
1686
1932
  useEffect(() => {
1687
1933
  if (disabled) {
1688
1934
  setResizeHandler(null);
1689
1935
  } else {
1690
- const resizeHandler = registerResizeHandle(resizeHandleId);
1936
+ const resizeHandler = registerResizeHandleWithParentGroup(resizeHandleId);
1691
1937
  setResizeHandler(() => resizeHandler);
1692
1938
  }
1693
- }, [disabled, resizeHandleId, registerResizeHandle]);
1939
+ }, [disabled, resizeHandleId, registerResizeHandleWithParentGroup]);
1694
1940
  useEffect(() => {
1695
- if (disabled || resizeHandler == null || !isDragging) {
1941
+ var _hitAreaMargins$coars, _hitAreaMargins$fine;
1942
+ if (disabled || resizeHandler == null) {
1696
1943
  return;
1697
1944
  }
1698
- const onMove = event => {
1699
- resizeHandler(event);
1700
- };
1701
- const onMouseLeave = event => {
1702
- resizeHandler(event);
1703
- };
1704
1945
  const element = elementRef.current;
1705
1946
  assert(element);
1706
- const targetDocument = element.ownerDocument;
1707
- targetDocument.body.addEventListener("contextmenu", stopDraggingAndBlur);
1708
- targetDocument.body.addEventListener("mousemove", onMove);
1709
- targetDocument.body.addEventListener("touchmove", onMove);
1710
- targetDocument.body.addEventListener("mouseleave", onMouseLeave);
1711
- window.addEventListener("mouseup", stopDraggingAndBlur);
1712
- window.addEventListener("touchend", stopDraggingAndBlur);
1713
- return () => {
1714
- targetDocument.body.removeEventListener("contextmenu", stopDraggingAndBlur);
1715
- targetDocument.body.removeEventListener("mousemove", onMove);
1716
- targetDocument.body.removeEventListener("touchmove", onMove);
1717
- targetDocument.body.removeEventListener("mouseleave", onMouseLeave);
1718
- window.removeEventListener("mouseup", stopDraggingAndBlur);
1719
- window.removeEventListener("touchend", stopDraggingAndBlur);
1947
+ const setResizeHandlerState = (action, state, event) => {
1948
+ setState(state);
1949
+ switch (action) {
1950
+ case "down":
1951
+ {
1952
+ startDragging(resizeHandleId, event);
1953
+ break;
1954
+ }
1955
+ case "up":
1956
+ {
1957
+ stopDragging();
1958
+ break;
1959
+ }
1960
+ }
1961
+ switch (state) {
1962
+ case "drag":
1963
+ {
1964
+ resizeHandler(event);
1965
+ break;
1966
+ }
1967
+ }
1720
1968
  };
1721
- }, [direction, disabled, isDragging, resizeHandler, stopDraggingAndBlur]);
1969
+ return registerResizeHandle(resizeHandleId, element, direction, {
1970
+ // Coarse inputs (e.g. finger/touch)
1971
+ coarse: (_hitAreaMargins$coars = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.coarse) !== null && _hitAreaMargins$coars !== void 0 ? _hitAreaMargins$coars : 15,
1972
+ // Fine inputs (e.g. mouse)
1973
+ fine: (_hitAreaMargins$fine = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.fine) !== null && _hitAreaMargins$fine !== void 0 ? _hitAreaMargins$fine : 5
1974
+ }, setResizeHandlerState);
1975
+ }, [direction, disabled, hitAreaMargins, registerResizeHandleWithParentGroup, resizeHandleId, resizeHandler, startDragging, stopDragging]);
1722
1976
  useWindowSplitterResizeHandlerBehavior({
1723
1977
  disabled,
1724
1978
  handleId: resizeHandleId,
@@ -1726,7 +1980,6 @@ function PanelResizeHandle({
1726
1980
  panelGroupElement
1727
1981
  });
1728
1982
  const style = {
1729
- cursor: getCursorStyle(direction),
1730
1983
  touchAction: "none",
1731
1984
  userSelect: "none"
1732
1985
  };
@@ -1736,31 +1989,6 @@ function PanelResizeHandle({
1736
1989
  className: classNameFromProps,
1737
1990
  onBlur: () => setIsFocused(false),
1738
1991
  onFocus: () => setIsFocused(true),
1739
- onMouseDown: event => {
1740
- startDragging(resizeHandleId, event.nativeEvent);
1741
- const callbacks = callbacksRef.current;
1742
- assert(callbacks);
1743
- const {
1744
- onDragging
1745
- } = callbacks;
1746
- if (onDragging) {
1747
- onDragging(true);
1748
- }
1749
- },
1750
- onMouseUp: stopDraggingAndBlur,
1751
- onTouchCancel: stopDraggingAndBlur,
1752
- onTouchEnd: stopDraggingAndBlur,
1753
- onTouchStart: event => {
1754
- startDragging(resizeHandleId, event.nativeEvent);
1755
- const callbacks = callbacksRef.current;
1756
- assert(callbacks);
1757
- const {
1758
- onDragging
1759
- } = callbacks;
1760
- if (onDragging) {
1761
- onDragging(true);
1762
- }
1763
- },
1764
1992
  ref: elementRef,
1765
1993
  role: "separator",
1766
1994
  style: {
@@ -1772,7 +2000,8 @@ function PanelResizeHandle({
1772
2000
  "data-panel-group-direction": direction,
1773
2001
  "data-panel-group-id": groupId,
1774
2002
  "data-resize-handle": "",
1775
- "data-resize-handle-active": isDragging ? "pointer" : isFocused ? "keyboard" : undefined,
2003
+ "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2004
+ "data-resize-handle-state": state,
1776
2005
  "data-panel-resize-handle-enabled": !disabled,
1777
2006
  "data-panel-resize-handle-id": resizeHandleId
1778
2007
  });