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
@@ -273,15 +273,15 @@ function isTouchEvent(event) {
273
273
  function getResizeEventCoordinates(event) {
274
274
  if (isMouseEvent(event)) {
275
275
  return {
276
- x: event.pageX,
277
- y: event.pageY
276
+ x: event.clientX,
277
+ y: event.clientY
278
278
  };
279
279
  } else if (isTouchEvent(event)) {
280
280
  const touch = event.touches[0];
281
- if (touch && touch.pageX && touch.pageY) {
281
+ if (touch && touch.clientX && touch.clientY) {
282
282
  return {
283
- x: touch.pageX,
284
- y: touch.pageY
283
+ x: touch.clientX,
284
+ y: touch.clientY
285
285
  };
286
286
  }
287
287
  }
@@ -327,7 +327,7 @@ function compare(a, b) {
327
327
  b = ancestors.b.pop();
328
328
  common_ancestor = a;
329
329
  }
330
- assert(common_ancestor);
330
+ assert(common_ancestor, "Stacking order can only be calculated for elements with a common ancestor");
331
331
  const z_indexes = {
332
332
  a: get_z_index(find_stacking_context(ancestors.a)),
333
333
  b: get_z_index(find_stacking_context(ancestors.b))
@@ -385,7 +385,7 @@ function find_stacking_context(nodes) {
385
385
  let i = nodes.length;
386
386
  while (i--) {
387
387
  const node = nodes[i];
388
- assert(node);
388
+ assert(node, "Missing node");
389
389
  if (creates_stacking_context(node)) return node;
390
390
  }
391
391
  return null;
@@ -672,7 +672,7 @@ function updateResizeHandlerStates(action, event) {
672
672
  });
673
673
  }
674
674
 
675
- function assert(expectedCondition, message = "Assertion failed!") {
675
+ function assert(expectedCondition, message) {
676
676
  if (!expectedCondition) {
677
677
  console.error(message);
678
678
  throw Error(message);
@@ -703,7 +703,7 @@ function resizePanel({
703
703
  size
704
704
  }) {
705
705
  const panelConstraints = panelConstraintsArray[panelIndex];
706
- assert(panelConstraints != null);
706
+ assert(panelConstraints != null, `Panel constraints not found for index ${panelIndex}`);
707
707
  let {
708
708
  collapsedSize = 0,
709
709
  collapsible,
@@ -741,8 +741,8 @@ function adjustLayoutByDelta({
741
741
  }
742
742
  const nextLayout = [...prevLayout];
743
743
  const [firstPivotIndex, secondPivotIndex] = pivotIndices;
744
- assert(firstPivotIndex != null);
745
- assert(secondPivotIndex != null);
744
+ assert(firstPivotIndex != null, "Invalid first pivot index");
745
+ assert(secondPivotIndex != null, "Invalid second pivot index");
746
746
  let deltaApplied = 0;
747
747
 
748
748
  //const DEBUG = [];
@@ -768,19 +768,18 @@ function adjustLayoutByDelta({
768
768
  // Check if we should expand a collapsed panel
769
769
  const index = delta < 0 ? secondPivotIndex : firstPivotIndex;
770
770
  const panelConstraints = panelConstraintsArray[index];
771
- assert(panelConstraints);
771
+ assert(panelConstraints, `Panel constraints not found for index ${index}`);
772
+ const {
773
+ collapsedSize = 0,
774
+ collapsible,
775
+ minSize = 0
776
+ } = panelConstraints;
772
777
 
773
778
  //DEBUG.push(`edge case check 1: ${index}`);
774
779
  //DEBUG.push(` -> collapsible? ${constraints.collapsible}`);
775
- if (panelConstraints.collapsible) {
780
+ if (collapsible) {
776
781
  const prevSize = prevLayout[index];
777
- assert(prevSize != null);
778
- const panelConstraints = panelConstraintsArray[index];
779
- assert(panelConstraints);
780
- const {
781
- collapsedSize = 0,
782
- minSize = 0
783
- } = panelConstraints;
782
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
784
783
  if (fuzzyNumbersEqual(prevSize, collapsedSize)) {
785
784
  const localDelta = minSize - prevSize;
786
785
  //DEBUG.push(` -> expand delta: ${localDelta}`);
@@ -797,22 +796,18 @@ function adjustLayoutByDelta({
797
796
  // Check if we should collapse a panel at its minimum size
798
797
  const index = delta < 0 ? firstPivotIndex : secondPivotIndex;
799
798
  const panelConstraints = panelConstraintsArray[index];
800
- assert(panelConstraints);
799
+ assert(panelConstraints, `No panel constraints found for index ${index}`);
801
800
  const {
802
- collapsible
801
+ collapsedSize = 0,
802
+ collapsible,
803
+ minSize = 0
803
804
  } = panelConstraints;
804
805
 
805
806
  //DEBUG.push(`edge case check 2: ${index}`);
806
807
  //DEBUG.push(` -> collapsible? ${collapsible}`);
807
808
  if (collapsible) {
808
809
  const prevSize = prevLayout[index];
809
- assert(prevSize != null);
810
- const panelConstraints = panelConstraintsArray[index];
811
- assert(panelConstraints);
812
- const {
813
- collapsedSize = 0,
814
- minSize = 0
815
- } = panelConstraints;
810
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
816
811
  if (fuzzyNumbersEqual(prevSize, minSize)) {
817
812
  const localDelta = prevSize - collapsedSize;
818
813
  //DEBUG.push(` -> expand delta: ${localDelta}`);
@@ -842,7 +837,7 @@ function adjustLayoutByDelta({
842
837
  //DEBUG.push("pre calc...");
843
838
  while (true) {
844
839
  const prevSize = prevLayout[index];
845
- assert(prevSize != null);
840
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
846
841
  const maxSafeSize = resizePanel({
847
842
  panelConstraints: panelConstraintsArray,
848
843
  panelIndex: index,
@@ -873,7 +868,7 @@ function adjustLayoutByDelta({
873
868
  while (index >= 0 && index < panelConstraintsArray.length) {
874
869
  const deltaRemaining = Math.abs(delta) - Math.abs(deltaApplied);
875
870
  const prevSize = prevLayout[index];
876
- assert(prevSize != null);
871
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
877
872
  const unsafeSize = prevSize - deltaRemaining;
878
873
  const safeSize = resizePanel({
879
874
  panelConstraints: panelConstraintsArray,
@@ -910,7 +905,7 @@ function adjustLayoutByDelta({
910
905
  // Now distribute the applied delta to the panels in the other direction
911
906
  const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
912
907
  const prevSize = prevLayout[pivotIndex];
913
- assert(prevSize != null);
908
+ assert(prevSize != null, `Previous layout not found for panel index ${pivotIndex}`);
914
909
  const unsafeSize = prevSize + deltaApplied;
915
910
  const safeSize = resizePanel({
916
911
  panelConstraints: panelConstraintsArray,
@@ -928,7 +923,7 @@ function adjustLayoutByDelta({
928
923
  let index = pivotIndex;
929
924
  while (index >= 0 && index < panelConstraintsArray.length) {
930
925
  const prevSize = nextLayout[index];
931
- assert(prevSize != null);
926
+ assert(prevSize != null, `Previous layout not found for panel index ${index}`);
932
927
  const unsafeSize = prevSize + deltaRemaining;
933
928
  const safeSize = resizePanel({
934
929
  panelConstraints: panelConstraintsArray,
@@ -974,7 +969,7 @@ function calculateAriaValues({
974
969
  let totalMinSize = 0;
975
970
  let totalMaxSize = 0;
976
971
  const firstIndex = pivotIndices[0];
977
- assert(firstIndex != null);
972
+ assert(firstIndex != null, "No pivot index found");
978
973
 
979
974
  // A panel's effective min/max sizes also need to account for other panel's sizes.
980
975
  panelsArray.forEach((panelData, index) => {
@@ -1083,7 +1078,7 @@ function useWindowSplitterPanelGroupBehavior({
1083
1078
  const resizeHandleElement = resizeHandleElements[index];
1084
1079
  if (resizeHandleElement == null) ; else {
1085
1080
  const panelData = panelDataArray[index];
1086
- assert(panelData);
1081
+ assert(panelData, `No panel data found for index "${index}"`);
1087
1082
  resizeHandleElement.setAttribute("aria-controls", panelData.id);
1088
1083
  resizeHandleElement.setAttribute("aria-valuemax", "" + Math.round(valueMax));
1089
1084
  resizeHandleElement.setAttribute("aria-valuemin", "" + Math.round(valueMin));
@@ -1104,17 +1099,17 @@ function useWindowSplitterPanelGroupBehavior({
1104
1099
  return;
1105
1100
  }
1106
1101
  const eagerValues = eagerValuesRef.current;
1107
- assert(eagerValues);
1102
+ assert(eagerValues, `Eager values not found`);
1108
1103
  const {
1109
1104
  panelDataArray
1110
1105
  } = eagerValues;
1111
1106
  const groupElement = getPanelGroupElement(groupId, panelGroupElement);
1112
1107
  assert(groupElement != null, `No group found for id "${groupId}"`);
1113
1108
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1114
- assert(handles);
1109
+ assert(handles, `No resize handles found for group id "${groupId}"`);
1115
1110
  const cleanupFunctions = handles.map(handle => {
1116
1111
  const handleId = handle.getAttribute("data-panel-resize-handle-id");
1117
- assert(handleId);
1112
+ assert(handleId, `Resize handle element has no handle id attribute`);
1118
1113
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1119
1114
  if (idBefore == null || idAfter == null) {
1120
1115
  return () => {};
@@ -1130,7 +1125,7 @@ function useWindowSplitterPanelGroupBehavior({
1130
1125
  const index = panelDataArray.findIndex(panelData => panelData.id === idBefore);
1131
1126
  if (index >= 0) {
1132
1127
  const panelData = panelDataArray[index];
1133
- assert(panelData);
1128
+ assert(panelData, `No panel data found for index ${index}`);
1134
1129
  const size = layout[index];
1135
1130
  const {
1136
1131
  collapsedSize = 0,
@@ -1189,15 +1184,15 @@ function getResizeEventCursorPosition(direction, event) {
1189
1184
  function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
1190
1185
  const isHorizontal = direction === "horizontal";
1191
1186
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1192
- assert(handleElement);
1187
+ assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1193
1188
  const groupId = handleElement.getAttribute("data-panel-group-id");
1194
- assert(groupId);
1189
+ assert(groupId, `Resize handle element has no group id attribute`);
1195
1190
  let {
1196
1191
  initialCursorPosition
1197
1192
  } = initialDragState;
1198
1193
  const cursorPosition = getResizeEventCursorPosition(direction, event);
1199
1194
  const groupElement = getPanelGroupElement(groupId, panelGroupElement);
1200
- assert(groupElement);
1195
+ assert(groupElement, `No group element found for id "${groupId}"`);
1201
1196
  const groupRect = groupElement.getBoundingClientRect();
1202
1197
  const groupSizeInPixels = isHorizontal ? groupRect.width : groupRect.height;
1203
1198
  const offsetPixels = cursorPosition - initialCursorPosition;
@@ -1258,7 +1253,7 @@ function calculateUnsafeDefaultLayout({
1258
1253
  // Distribute default sizes first
1259
1254
  for (let index = 0; index < panelDataArray.length; index++) {
1260
1255
  const panelConstraints = panelConstraintsArray[index];
1261
- assert(panelConstraints);
1256
+ assert(panelConstraints, `Panel constraints not found for index ${index}`);
1262
1257
  const {
1263
1258
  defaultSize
1264
1259
  } = panelConstraints;
@@ -1272,7 +1267,7 @@ function calculateUnsafeDefaultLayout({
1272
1267
  // Remaining size should be distributed evenly between panels without default sizes
1273
1268
  for (let index = 0; index < panelDataArray.length; index++) {
1274
1269
  const panelConstraints = panelConstraintsArray[index];
1275
- assert(panelConstraints);
1270
+ assert(panelConstraints, `Panel constraints not found for index ${index}`);
1276
1271
  const {
1277
1272
  defaultSize
1278
1273
  } = panelConstraints;
@@ -1292,7 +1287,7 @@ function calculateUnsafeDefaultLayout({
1292
1287
  function callPanelCallbacks(panelsArray, layout, panelIdToLastNotifiedSizeMap) {
1293
1288
  layout.forEach((size, index) => {
1294
1289
  const panelData = panelsArray[index];
1295
- assert(panelData);
1290
+ assert(panelData, `Panel data not found for index ${index}`);
1296
1291
  const {
1297
1292
  callbacks,
1298
1293
  constraints,
@@ -1482,7 +1477,7 @@ function validatePanelGroupLayout({
1482
1477
  } else if (!fuzzyNumbersEqual(nextLayoutTotalSize, 100)) {
1483
1478
  for (let index = 0; index < panelConstraints.length; index++) {
1484
1479
  const unsafeSize = nextLayout[index];
1485
- assert(unsafeSize != null);
1480
+ assert(unsafeSize != null, `No layout data found for index ${index}`);
1486
1481
  const safeSize = 100 / nextLayoutTotalSize * unsafeSize;
1487
1482
  nextLayout[index] = safeSize;
1488
1483
  }
@@ -1492,7 +1487,7 @@ function validatePanelGroupLayout({
1492
1487
  // First pass: Validate the proposed layout given each panel's constraints
1493
1488
  for (let index = 0; index < panelConstraints.length; index++) {
1494
1489
  const unsafeSize = nextLayout[index];
1495
- assert(unsafeSize != null);
1490
+ assert(unsafeSize != null, `No layout data found for index ${index}`);
1496
1491
  const safeSize = resizePanel({
1497
1492
  panelConstraints,
1498
1493
  panelIndex: index,
@@ -1509,7 +1504,7 @@ function validatePanelGroupLayout({
1509
1504
  if (!fuzzyNumbersEqual(remainingSize, 0)) {
1510
1505
  for (let index = 0; index < panelConstraints.length; index++) {
1511
1506
  const prevSize = nextLayout[index];
1512
- assert(prevSize != null);
1507
+ assert(prevSize != null, `No layout data found for index ${index}`);
1513
1508
  const unsafeSize = prevSize + remainingSize;
1514
1509
  const safeSize = resizePanel({
1515
1510
  panelConstraints,
@@ -1675,7 +1670,7 @@ function PanelGroupWithForwardedRef({
1675
1670
  panelSize,
1676
1671
  pivotIndices
1677
1672
  } = panelDataHelper(panelDataArray, panelData, prevLayout);
1678
- assert(panelSize != null);
1673
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1679
1674
  if (panelSize !== collapsedSize) {
1680
1675
  // Store size before collapse;
1681
1676
  // This is the size that gets restored if the expand() API is used.
@@ -1752,7 +1747,7 @@ function PanelGroupWithForwardedRef({
1752
1747
  const {
1753
1748
  panelSize
1754
1749
  } = panelDataHelper(panelDataArray, panelData, layout);
1755
- assert(panelSize != null);
1750
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1756
1751
  return panelSize;
1757
1752
  }, []);
1758
1753
 
@@ -1796,7 +1791,7 @@ function PanelGroupWithForwardedRef({
1796
1791
  collapsible,
1797
1792
  panelSize
1798
1793
  } = panelDataHelper(panelDataArray, panelData, layout);
1799
- assert(panelSize != null);
1794
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1800
1795
  return !collapsible || panelSize > collapsedSize;
1801
1796
  }, []);
1802
1797
  const registerPanel = useCallback(panelData => {
@@ -1963,7 +1958,7 @@ function PanelGroupWithForwardedRef({
1963
1958
  panelSize,
1964
1959
  pivotIndices
1965
1960
  } = panelDataHelper(panelDataArray, panelData, prevLayout);
1966
- assert(panelSize != null);
1961
+ assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1967
1962
  const isLastPanel = findPanelDataIndex(panelDataArray, panelData) === panelDataArray.length - 1;
1968
1963
  const delta = isLastPanel ? panelSize - unsafePanelSize : unsafePanelSize - panelSize;
1969
1964
  const nextLayout = adjustLayoutByDelta({
@@ -2000,7 +1995,10 @@ function PanelGroupWithForwardedRef({
2000
1995
  const {
2001
1996
  panelSize: prevPanelSize
2002
1997
  } = panelDataHelper(panelDataArray, panelData, layout);
2003
- assert(prevPanelSize != null);
1998
+ if (prevPanelSize == null) {
1999
+ // It's possible that the panels in this group have changed since the last render
2000
+ return;
2001
+ }
2004
2002
  if (prevCollapsible && nextCollapsible && prevPanelSize === prevCollapsedSize) {
2005
2003
  if (prevCollapsedSize !== nextCollapsedSize) {
2006
2004
  resizePanel(panelData, nextCollapsedSize);
@@ -2022,7 +2020,7 @@ function PanelGroupWithForwardedRef({
2022
2020
  return;
2023
2021
  }
2024
2022
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElementRef.current);
2025
- assert(handleElement);
2023
+ assert(handleElement, `Drag handle element not found for id "${dragHandleId}"`);
2026
2024
  const initialCursorPosition = getResizeEventCursorPosition(direction, event);
2027
2025
  setDragState({
2028
2026
  dragHandleId,
@@ -2151,10 +2149,10 @@ function useWindowSplitterResizeHandlerBehavior({
2151
2149
  {
2152
2150
  event.preventDefault();
2153
2151
  const groupId = handleElement.getAttribute("data-panel-group-id");
2154
- assert(groupId);
2152
+ assert(groupId, `No group element found for id "${groupId}"`);
2155
2153
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2156
2154
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
2157
- assert(index !== null);
2155
+ assert(index !== null, `No resize element found for id "${handleId}"`);
2158
2156
  const nextIndex = event.shiftKey ? index > 0 ? index - 1 : handles.length - 1 : index + 1 < handles.length ? index + 1 : 0;
2159
2157
  const nextHandle = handles[nextIndex];
2160
2158
  nextHandle.focus();
@@ -2226,7 +2224,7 @@ function PanelResizeHandle({
2226
2224
  return;
2227
2225
  }
2228
2226
  const element = elementRef.current;
2229
- assert(element);
2227
+ assert(element, "Element ref not attached");
2230
2228
  const setResizeHandlerState = (action, isActive, event) => {
2231
2229
  if (isActive) {
2232
2230
  switch (action) {