react-resizable-panels 2.0.9 → 2.0.11

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 (34) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/declarations/src/utils/assert.d.ts +1 -1
  3. package/dist/react-resizable-panels.browser.cjs.js +55 -57
  4. package/dist/react-resizable-panels.browser.development.cjs.js +57 -59
  5. package/dist/react-resizable-panels.browser.development.esm.js +57 -59
  6. package/dist/react-resizable-panels.browser.esm.js +55 -57
  7. package/dist/react-resizable-panels.cjs.js +55 -57
  8. package/dist/react-resizable-panels.development.cjs.js +57 -59
  9. package/dist/react-resizable-panels.development.esm.js +57 -59
  10. package/dist/react-resizable-panels.development.node.cjs.js +53 -55
  11. package/dist/react-resizable-panels.development.node.esm.js +53 -55
  12. package/dist/react-resizable-panels.esm.js +55 -57
  13. package/dist/react-resizable-panels.node.cjs.js +51 -53
  14. package/dist/react-resizable-panels.node.esm.js +51 -53
  15. package/package.json +1 -1
  16. package/src/Panel.test.tsx +23 -23
  17. package/src/PanelGroup.test.tsx +32 -3
  18. package/src/PanelGroup.ts +25 -7
  19. package/src/PanelResizeHandle.test.tsx +3 -3
  20. package/src/PanelResizeHandle.ts +1 -1
  21. package/src/hooks/useWindowSplitterBehavior.ts +5 -2
  22. package/src/hooks/useWindowSplitterPanelGroupBehavior.ts +5 -5
  23. package/src/utils/adjustLayoutByDelta.ts +47 -20
  24. package/src/utils/assert.ts +1 -1
  25. package/src/utils/calculateAriaValues.ts +1 -1
  26. package/src/utils/calculateDragOffsetPercentage.ts +6 -3
  27. package/src/utils/calculateUnsafeDefaultLayout.ts +2 -2
  28. package/src/utils/callPanelCallbacks.ts +1 -1
  29. package/src/utils/events/getResizeEventCoordinates.ts +5 -5
  30. package/src/utils/resizePanel.ts +4 -1
  31. package/src/utils/test-utils.ts +1 -1
  32. package/src/utils/validatePanelConstraints.ts +4 -1
  33. package/src/utils/validatePanelGroupLayout.ts +3 -3
  34. package/src/vendor/stacking-order.ts +5 -2
@@ -247,15 +247,15 @@ function isTouchEvent(event) {
247
247
  function getResizeEventCoordinates(event) {
248
248
  if (isMouseEvent(event)) {
249
249
  return {
250
- x: event.pageX,
251
- y: event.pageY
250
+ x: event.clientX,
251
+ y: event.clientY
252
252
  };
253
253
  } else if (isTouchEvent(event)) {
254
254
  const touch = event.touches[0];
255
- if (touch && touch.pageX && touch.pageY) {
255
+ if (touch && touch.clientX && touch.clientY) {
256
256
  return {
257
- x: touch.pageX,
258
- y: touch.pageY
257
+ x: touch.clientX,
258
+ y: touch.clientY
259
259
  };
260
260
  }
261
261
  }
@@ -301,7 +301,7 @@ function compare(a, b) {
301
301
  b = ancestors.b.pop();
302
302
  common_ancestor = a;
303
303
  }
304
- assert(common_ancestor);
304
+ assert(common_ancestor, "Stacking order can only be calculated for elements with a common ancestor");
305
305
  const z_indexes = {
306
306
  a: get_z_index(find_stacking_context(ancestors.a)),
307
307
  b: get_z_index(find_stacking_context(ancestors.b))
@@ -359,7 +359,7 @@ function find_stacking_context(nodes) {
359
359
  let i = nodes.length;
360
360
  while (i--) {
361
361
  const node = nodes[i];
362
- assert(node);
362
+ assert(node, "Missing node");
363
363
  if (creates_stacking_context(node)) return node;
364
364
  }
365
365
  return null;
@@ -646,7 +646,7 @@ function updateResizeHandlerStates(action, event) {
646
646
  });
647
647
  }
648
648
 
649
- function assert(expectedCondition, message = "Assertion failed!") {
649
+ function assert(expectedCondition, message) {
650
650
  if (!expectedCondition) {
651
651
  console.error(message);
652
652
  throw Error(message);
@@ -677,7 +677,7 @@ function resizePanel({
677
677
  size
678
678
  }) {
679
679
  const panelConstraints = panelConstraintsArray[panelIndex];
680
- assert(panelConstraints != null);
680
+ assert(panelConstraints != null, `Panel constraints not found for index ${panelIndex}`);
681
681
  let {
682
682
  collapsedSize = 0,
683
683
  collapsible,
@@ -715,8 +715,8 @@ function adjustLayoutByDelta({
715
715
  }
716
716
  const nextLayout = [...prevLayout];
717
717
  const [firstPivotIndex, secondPivotIndex] = pivotIndices;
718
- assert(firstPivotIndex != null);
719
- assert(secondPivotIndex != null);
718
+ assert(firstPivotIndex != null, "Invalid first pivot index");
719
+ assert(secondPivotIndex != null, "Invalid second pivot index");
720
720
  let deltaApplied = 0;
721
721
 
722
722
  //const DEBUG = [];
@@ -742,19 +742,18 @@ function adjustLayoutByDelta({
742
742
  // Check if we should expand a collapsed panel
743
743
  const index = delta < 0 ? secondPivotIndex : firstPivotIndex;
744
744
  const panelConstraints = panelConstraintsArray[index];
745
- assert(panelConstraints);
745
+ assert(panelConstraints, `Panel constraints not found for index ${index}`);
746
+ const {
747
+ collapsedSize = 0,
748
+ collapsible,
749
+ minSize = 0
750
+ } = panelConstraints;
746
751
 
747
752
  //DEBUG.push(`edge case check 1: ${index}`);
748
753
  //DEBUG.push(` -> collapsible? ${constraints.collapsible}`);
749
- if (panelConstraints.collapsible) {
754
+ if (collapsible) {
750
755
  const prevSize = prevLayout[index];
751
- assert(prevSize != null);
752
- const panelConstraints = panelConstraintsArray[index];
753
- assert(panelConstraints);
754
- const {
755
- collapsedSize = 0,
756
- minSize = 0
757
- } = panelConstraints;
756
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
758
757
  if (fuzzyNumbersEqual(prevSize, collapsedSize)) {
759
758
  const localDelta = minSize - prevSize;
760
759
  //DEBUG.push(` -> expand delta: ${localDelta}`);
@@ -771,22 +770,18 @@ function adjustLayoutByDelta({
771
770
  // Check if we should collapse a panel at its minimum size
772
771
  const index = delta < 0 ? firstPivotIndex : secondPivotIndex;
773
772
  const panelConstraints = panelConstraintsArray[index];
774
- assert(panelConstraints);
773
+ assert(panelConstraints, `No panel constraints found for index ${index}`);
775
774
  const {
776
- collapsible
775
+ collapsedSize = 0,
776
+ collapsible,
777
+ minSize = 0
777
778
  } = panelConstraints;
778
779
 
779
780
  //DEBUG.push(`edge case check 2: ${index}`);
780
781
  //DEBUG.push(` -> collapsible? ${collapsible}`);
781
782
  if (collapsible) {
782
783
  const prevSize = prevLayout[index];
783
- assert(prevSize != null);
784
- const panelConstraints = panelConstraintsArray[index];
785
- assert(panelConstraints);
786
- const {
787
- collapsedSize = 0,
788
- minSize = 0
789
- } = panelConstraints;
784
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
790
785
  if (fuzzyNumbersEqual(prevSize, minSize)) {
791
786
  const localDelta = prevSize - collapsedSize;
792
787
  //DEBUG.push(` -> expand delta: ${localDelta}`);
@@ -816,7 +811,7 @@ function adjustLayoutByDelta({
816
811
  //DEBUG.push("pre calc...");
817
812
  while (true) {
818
813
  const prevSize = prevLayout[index];
819
- assert(prevSize != null);
814
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
820
815
  const maxSafeSize = resizePanel({
821
816
  panelConstraints: panelConstraintsArray,
822
817
  panelIndex: index,
@@ -847,7 +842,7 @@ function adjustLayoutByDelta({
847
842
  while (index >= 0 && index < panelConstraintsArray.length) {
848
843
  const deltaRemaining = Math.abs(delta) - Math.abs(deltaApplied);
849
844
  const prevSize = prevLayout[index];
850
- assert(prevSize != null);
845
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
851
846
  const unsafeSize = prevSize - deltaRemaining;
852
847
  const safeSize = resizePanel({
853
848
  panelConstraints: panelConstraintsArray,
@@ -884,7 +879,7 @@ function adjustLayoutByDelta({
884
879
  // Now distribute the applied delta to the panels in the other direction
885
880
  const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
886
881
  const prevSize = prevLayout[pivotIndex];
887
- assert(prevSize != null);
882
+ assert(prevSize != null, `Previous layout not found for panel index ${pivotIndex}`);
888
883
  const unsafeSize = prevSize + deltaApplied;
889
884
  const safeSize = resizePanel({
890
885
  panelConstraints: panelConstraintsArray,
@@ -902,7 +897,7 @@ function adjustLayoutByDelta({
902
897
  let index = pivotIndex;
903
898
  while (index >= 0 && index < panelConstraintsArray.length) {
904
899
  const prevSize = nextLayout[index];
905
- assert(prevSize != null);
900
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
906
901
  const unsafeSize = prevSize + deltaRemaining;
907
902
  const safeSize = resizePanel({
908
903
  panelConstraints: panelConstraintsArray,
@@ -948,7 +943,7 @@ function calculateAriaValues({
948
943
  let totalMinSize = 0;
949
944
  let totalMaxSize = 0;
950
945
  const firstIndex = pivotIndices[0];
951
- assert(firstIndex != null);
946
+ assert(firstIndex != null, "No pivot index found");
952
947
 
953
948
  // A panel's effective min/max sizes also need to account for other panel's sizes.
954
949
  panelsArray.forEach((panelData, index) => {
@@ -1057,7 +1052,7 @@ function useWindowSplitterPanelGroupBehavior({
1057
1052
  const resizeHandleElement = resizeHandleElements[index];
1058
1053
  if (resizeHandleElement == null) ; else {
1059
1054
  const panelData = panelDataArray[index];
1060
- assert(panelData);
1055
+ assert(panelData, `No panel data found for index "${index}"`);
1061
1056
  resizeHandleElement.setAttribute("aria-controls", panelData.id);
1062
1057
  resizeHandleElement.setAttribute("aria-valuemax", "" + Math.round(valueMax));
1063
1058
  resizeHandleElement.setAttribute("aria-valuemin", "" + Math.round(valueMin));
@@ -1078,17 +1073,17 @@ function useWindowSplitterPanelGroupBehavior({
1078
1073
  return;
1079
1074
  }
1080
1075
  const eagerValues = eagerValuesRef.current;
1081
- assert(eagerValues);
1076
+ assert(eagerValues, `Eager values not found`);
1082
1077
  const {
1083
1078
  panelDataArray
1084
1079
  } = eagerValues;
1085
1080
  const groupElement = getPanelGroupElement(groupId, panelGroupElement);
1086
1081
  assert(groupElement != null, `No group found for id "${groupId}"`);
1087
1082
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1088
- assert(handles);
1083
+ assert(handles, `No resize handles found for group id "${groupId}"`);
1089
1084
  const cleanupFunctions = handles.map(handle => {
1090
1085
  const handleId = handle.getAttribute("data-panel-resize-handle-id");
1091
- assert(handleId);
1086
+ assert(handleId, `Resize handle element has no handle id attribute`);
1092
1087
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1093
1088
  if (idBefore == null || idAfter == null) {
1094
1089
  return () => {};
@@ -1104,7 +1099,7 @@ function useWindowSplitterPanelGroupBehavior({
1104
1099
  const index = panelDataArray.findIndex(panelData => panelData.id === idBefore);
1105
1100
  if (index >= 0) {
1106
1101
  const panelData = panelDataArray[index];
1107
- assert(panelData);
1102
+ assert(panelData, `No panel data found for index ${index}`);
1108
1103
  const size = layout[index];
1109
1104
  const {
1110
1105
  collapsedSize = 0,
@@ -1163,15 +1158,15 @@ function getResizeEventCursorPosition(direction, event) {
1163
1158
  function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
1164
1159
  const isHorizontal = direction === "horizontal";
1165
1160
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1166
- assert(handleElement);
1161
+ assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1167
1162
  const groupId = handleElement.getAttribute("data-panel-group-id");
1168
- assert(groupId);
1163
+ assert(groupId, `Resize handle element has no group id attribute`);
1169
1164
  let {
1170
1165
  initialCursorPosition
1171
1166
  } = initialDragState;
1172
1167
  const cursorPosition = getResizeEventCursorPosition(direction, event);
1173
1168
  const groupElement = getPanelGroupElement(groupId, panelGroupElement);
1174
- assert(groupElement);
1169
+ assert(groupElement, `No group element found for id "${groupId}"`);
1175
1170
  const groupRect = groupElement.getBoundingClientRect();
1176
1171
  const groupSizeInPixels = isHorizontal ? groupRect.width : groupRect.height;
1177
1172
  const offsetPixels = cursorPosition - initialCursorPosition;
@@ -1232,7 +1227,7 @@ function calculateUnsafeDefaultLayout({
1232
1227
  // Distribute default sizes first
1233
1228
  for (let index = 0; index < panelDataArray.length; index++) {
1234
1229
  const panelConstraints = panelConstraintsArray[index];
1235
- assert(panelConstraints);
1230
+ assert(panelConstraints, `Panel constraints not found for index ${index}`);
1236
1231
  const {
1237
1232
  defaultSize
1238
1233
  } = panelConstraints;
@@ -1246,7 +1241,7 @@ function calculateUnsafeDefaultLayout({
1246
1241
  // Remaining size should be distributed evenly between panels without default sizes
1247
1242
  for (let index = 0; index < panelDataArray.length; index++) {
1248
1243
  const panelConstraints = panelConstraintsArray[index];
1249
- assert(panelConstraints);
1244
+ assert(panelConstraints, `Panel constraints not found for index ${index}`);
1250
1245
  const {
1251
1246
  defaultSize
1252
1247
  } = panelConstraints;
@@ -1266,7 +1261,7 @@ function calculateUnsafeDefaultLayout({
1266
1261
  function callPanelCallbacks(panelsArray, layout, panelIdToLastNotifiedSizeMap) {
1267
1262
  layout.forEach((size, index) => {
1268
1263
  const panelData = panelsArray[index];
1269
- assert(panelData);
1264
+ assert(panelData, `Panel data not found for index ${index}`);
1270
1265
  const {
1271
1266
  callbacks,
1272
1267
  constraints,
@@ -1456,7 +1451,7 @@ function validatePanelGroupLayout({
1456
1451
  } else if (!fuzzyNumbersEqual(nextLayoutTotalSize, 100)) {
1457
1452
  for (let index = 0; index < panelConstraints.length; index++) {
1458
1453
  const unsafeSize = nextLayout[index];
1459
- assert(unsafeSize != null);
1454
+ assert(unsafeSize != null, `No layout data found for index ${index}`);
1460
1455
  const safeSize = 100 / nextLayoutTotalSize * unsafeSize;
1461
1456
  nextLayout[index] = safeSize;
1462
1457
  }
@@ -1466,7 +1461,7 @@ function validatePanelGroupLayout({
1466
1461
  // First pass: Validate the proposed layout given each panel's constraints
1467
1462
  for (let index = 0; index < panelConstraints.length; index++) {
1468
1463
  const unsafeSize = nextLayout[index];
1469
- assert(unsafeSize != null);
1464
+ assert(unsafeSize != null, `No layout data found for index ${index}`);
1470
1465
  const safeSize = resizePanel({
1471
1466
  panelConstraints,
1472
1467
  panelIndex: index,
@@ -1483,7 +1478,7 @@ function validatePanelGroupLayout({
1483
1478
  if (!fuzzyNumbersEqual(remainingSize, 0)) {
1484
1479
  for (let index = 0; index < panelConstraints.length; index++) {
1485
1480
  const prevSize = nextLayout[index];
1486
- assert(prevSize != null);
1481
+ assert(prevSize != null, `No layout data found for index ${index}`);
1487
1482
  const unsafeSize = prevSize + remainingSize;
1488
1483
  const safeSize = resizePanel({
1489
1484
  panelConstraints,
@@ -1649,7 +1644,7 @@ function PanelGroupWithForwardedRef({
1649
1644
  panelSize,
1650
1645
  pivotIndices
1651
1646
  } = panelDataHelper(panelDataArray, panelData, prevLayout);
1652
- assert(panelSize != null);
1647
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1653
1648
  if (panelSize !== collapsedSize) {
1654
1649
  // Store size before collapse;
1655
1650
  // This is the size that gets restored if the expand() API is used.
@@ -1726,7 +1721,7 @@ function PanelGroupWithForwardedRef({
1726
1721
  const {
1727
1722
  panelSize
1728
1723
  } = panelDataHelper(panelDataArray, panelData, layout);
1729
- assert(panelSize != null);
1724
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1730
1725
  return panelSize;
1731
1726
  }, []);
1732
1727
 
@@ -1770,7 +1765,7 @@ function PanelGroupWithForwardedRef({
1770
1765
  collapsible,
1771
1766
  panelSize
1772
1767
  } = panelDataHelper(panelDataArray, panelData, layout);
1773
- assert(panelSize != null);
1768
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1774
1769
  return !collapsible || panelSize > collapsedSize;
1775
1770
  }, []);
1776
1771
  const registerPanel = useCallback(panelData => {
@@ -1937,7 +1932,7 @@ function PanelGroupWithForwardedRef({
1937
1932
  panelSize,
1938
1933
  pivotIndices
1939
1934
  } = panelDataHelper(panelDataArray, panelData, prevLayout);
1940
- assert(panelSize != null);
1935
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1941
1936
  const isLastPanel = findPanelDataIndex(panelDataArray, panelData) === panelDataArray.length - 1;
1942
1937
  const delta = isLastPanel ? panelSize - unsafePanelSize : unsafePanelSize - panelSize;
1943
1938
  const nextLayout = adjustLayoutByDelta({
@@ -1974,7 +1969,10 @@ function PanelGroupWithForwardedRef({
1974
1969
  const {
1975
1970
  panelSize: prevPanelSize
1976
1971
  } = panelDataHelper(panelDataArray, panelData, layout);
1977
- assert(prevPanelSize != null);
1972
+ if (prevPanelSize == null) {
1973
+ // It's possible that the panels in this group have changed since the last render
1974
+ return;
1975
+ }
1978
1976
  if (prevCollapsible && nextCollapsible && prevPanelSize === prevCollapsedSize) {
1979
1977
  if (prevCollapsedSize !== nextCollapsedSize) {
1980
1978
  resizePanel(panelData, nextCollapsedSize);
@@ -1996,7 +1994,7 @@ function PanelGroupWithForwardedRef({
1996
1994
  return;
1997
1995
  }
1998
1996
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElementRef.current);
1999
- assert(handleElement);
1997
+ assert(handleElement, `Drag handle element not found for id "${dragHandleId}"`);
2000
1998
  const initialCursorPosition = getResizeEventCursorPosition(direction, event);
2001
1999
  setDragState({
2002
2000
  dragHandleId,
@@ -2125,10 +2123,10 @@ function useWindowSplitterResizeHandlerBehavior({
2125
2123
  {
2126
2124
  event.preventDefault();
2127
2125
  const groupId = handleElement.getAttribute("data-panel-group-id");
2128
- assert(groupId);
2126
+ assert(groupId, `No group element found for id "${groupId}"`);
2129
2127
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2130
2128
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
2131
- assert(index !== null);
2129
+ assert(index !== null, `No resize element found for id "${handleId}"`);
2132
2130
  const nextIndex = event.shiftKey ? index > 0 ? index - 1 : handles.length - 1 : index + 1 < handles.length ? index + 1 : 0;
2133
2131
  const nextHandle = handles[nextIndex];
2134
2132
  nextHandle.focus();
@@ -2200,7 +2198,7 @@ function PanelResizeHandle({
2200
2198
  return;
2201
2199
  }
2202
2200
  const element = elementRef.current;
2203
- assert(element);
2201
+ assert(element, "Element ref not attached");
2204
2202
  const setResizeHandlerState = (action, isActive, event) => {
2205
2203
  if (isActive) {
2206
2204
  switch (action) {