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.
@@ -211,6 +211,322 @@ const Panel = forwardRef((props, ref) => createElement(PanelWithForwardedRef, {
211
211
  PanelWithForwardedRef.displayName = "Panel";
212
212
  Panel.displayName = "forwardRef(Panel)";
213
213
 
214
+ let currentCursorStyle = null;
215
+ let styleElement = null;
216
+ function getCursorStyle(state, constraintFlags) {
217
+ if (constraintFlags) {
218
+ const horizontalMin = (constraintFlags & EXCEEDED_HORIZONTAL_MIN) !== 0;
219
+ const horizontalMax = (constraintFlags & EXCEEDED_HORIZONTAL_MAX) !== 0;
220
+ const verticalMin = (constraintFlags & EXCEEDED_VERTICAL_MIN) !== 0;
221
+ const verticalMax = (constraintFlags & EXCEEDED_VERTICAL_MAX) !== 0;
222
+ if (horizontalMin) {
223
+ if (verticalMin) {
224
+ return "se-resize";
225
+ } else if (verticalMax) {
226
+ return "ne-resize";
227
+ } else {
228
+ return "e-resize";
229
+ }
230
+ } else if (horizontalMax) {
231
+ if (verticalMin) {
232
+ return "sw-resize";
233
+ } else if (verticalMax) {
234
+ return "nw-resize";
235
+ } else {
236
+ return "w-resize";
237
+ }
238
+ } else if (verticalMin) {
239
+ return "s-resize";
240
+ } else if (verticalMax) {
241
+ return "n-resize";
242
+ }
243
+ }
244
+ switch (state) {
245
+ case "horizontal":
246
+ return "ew-resize";
247
+ case "intersection":
248
+ return "move";
249
+ case "vertical":
250
+ return "ns-resize";
251
+ }
252
+ }
253
+ function resetGlobalCursorStyle() {
254
+ if (styleElement !== null) {
255
+ document.head.removeChild(styleElement);
256
+ currentCursorStyle = null;
257
+ styleElement = null;
258
+ }
259
+ }
260
+ function setGlobalCursorStyle(state, constraintFlags) {
261
+ const style = getCursorStyle(state, constraintFlags);
262
+ if (currentCursorStyle === style) {
263
+ return;
264
+ }
265
+ currentCursorStyle = style;
266
+ if (styleElement === null) {
267
+ styleElement = document.createElement("style");
268
+ document.head.appendChild(styleElement);
269
+ }
270
+ styleElement.innerHTML = `*{cursor: ${style}!important;}`;
271
+ }
272
+
273
+ function isKeyDown(event) {
274
+ return event.type === "keydown";
275
+ }
276
+ function isMouseEvent(event) {
277
+ return event.type.startsWith("mouse");
278
+ }
279
+ function isTouchEvent(event) {
280
+ return event.type.startsWith("touch");
281
+ }
282
+
283
+ function getResizeEventCoordinates(event) {
284
+ if (isMouseEvent(event)) {
285
+ return {
286
+ x: event.pageX,
287
+ y: event.pageY
288
+ };
289
+ } else if (isTouchEvent(event)) {
290
+ const touch = event.touches[0];
291
+ if (touch && touch.pageX && touch.pageY) {
292
+ return {
293
+ x: touch.pageX,
294
+ y: touch.pageY
295
+ };
296
+ }
297
+ }
298
+ return {
299
+ x: Infinity,
300
+ y: Infinity
301
+ };
302
+ }
303
+
304
+ function getInputType() {
305
+ if (typeof matchMedia === "function") {
306
+ return matchMedia("(pointer:coarse)").matches ? "coarse" : "fine";
307
+ }
308
+ }
309
+
310
+ const EXCEEDED_HORIZONTAL_MIN = 0b0001;
311
+ const EXCEEDED_HORIZONTAL_MAX = 0b0010;
312
+ const EXCEEDED_VERTICAL_MIN = 0b0100;
313
+ const EXCEEDED_VERTICAL_MAX = 0b1000;
314
+ const isCoarsePointer = getInputType() === "coarse";
315
+ let intersectingHandles = [];
316
+ let isPointerDown = false;
317
+ let ownerDocumentCounts = new Map();
318
+ let panelConstraintFlags = new Map();
319
+ const registeredResizeHandlers = new Set();
320
+ function registerResizeHandle(resizeHandleId, element, direction, hitAreaMargins, setResizeHandlerState) {
321
+ var _ownerDocumentCounts$;
322
+ const {
323
+ ownerDocument
324
+ } = element;
325
+ const data = {
326
+ direction,
327
+ element,
328
+ hitAreaMargins,
329
+ setResizeHandlerState
330
+ };
331
+ const count = (_ownerDocumentCounts$ = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$ !== void 0 ? _ownerDocumentCounts$ : 0;
332
+ ownerDocumentCounts.set(ownerDocument, count + 1);
333
+ registeredResizeHandlers.add(data);
334
+ updateListeners();
335
+ return function unregisterResizeHandle() {
336
+ var _ownerDocumentCounts$2;
337
+ panelConstraintFlags.delete(resizeHandleId);
338
+ registeredResizeHandlers.delete(data);
339
+ const count = (_ownerDocumentCounts$2 = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$2 !== void 0 ? _ownerDocumentCounts$2 : 1;
340
+ ownerDocumentCounts.set(ownerDocument, count - 1);
341
+ updateListeners();
342
+ if (count === 1) {
343
+ ownerDocumentCounts.delete(ownerDocument);
344
+ }
345
+ };
346
+ }
347
+ function handlePointerDown(event) {
348
+ const {
349
+ x,
350
+ y
351
+ } = getResizeEventCoordinates(event);
352
+ isPointerDown = true;
353
+ updateResizeHandlerStates("down", event);
354
+ recalculateIntersectingHandles({
355
+ x,
356
+ y
357
+ });
358
+ updateListeners();
359
+ if (intersectingHandles.length > 0) {
360
+ event.preventDefault();
361
+ }
362
+ }
363
+ function handlePointerMove(event) {
364
+ const {
365
+ x,
366
+ y
367
+ } = getResizeEventCoordinates(event);
368
+ if (isPointerDown) {
369
+ intersectingHandles.forEach(data => {
370
+ const {
371
+ setResizeHandlerState
372
+ } = data;
373
+ setResizeHandlerState("move", "drag", event);
374
+ });
375
+
376
+ // Update cursor based on return value(s) from active handles
377
+ updateCursor();
378
+ } else {
379
+ recalculateIntersectingHandles({
380
+ x,
381
+ y
382
+ });
383
+ updateResizeHandlerStates("move", event);
384
+ updateCursor();
385
+ }
386
+ if (intersectingHandles.length > 0) {
387
+ event.preventDefault();
388
+ }
389
+ }
390
+ function handlePointerUp(event) {
391
+ const {
392
+ x,
393
+ y
394
+ } = getResizeEventCoordinates(event);
395
+ panelConstraintFlags.clear();
396
+ isPointerDown = false;
397
+ if (intersectingHandles.length > 0) {
398
+ event.preventDefault();
399
+ }
400
+ recalculateIntersectingHandles({
401
+ x,
402
+ y
403
+ });
404
+ updateResizeHandlerStates("up", event);
405
+ updateCursor();
406
+ updateListeners();
407
+ }
408
+ function recalculateIntersectingHandles({
409
+ x,
410
+ y
411
+ }) {
412
+ intersectingHandles.splice(0);
413
+ registeredResizeHandlers.forEach(data => {
414
+ const {
415
+ element,
416
+ hitAreaMargins
417
+ } = data;
418
+ const {
419
+ bottom,
420
+ left,
421
+ right,
422
+ top
423
+ } = element.getBoundingClientRect();
424
+ const margin = isCoarsePointer ? hitAreaMargins.coarse : hitAreaMargins.fine;
425
+ const intersects = x >= left - margin && x <= right + margin && y >= top - margin && y <= bottom + margin;
426
+ if (intersects) {
427
+ intersectingHandles.push(data);
428
+ }
429
+ });
430
+ }
431
+ function reportConstraintsViolation(resizeHandleId, flag) {
432
+ panelConstraintFlags.set(resizeHandleId, flag);
433
+ }
434
+ function updateCursor() {
435
+ let intersectsHorizontal = false;
436
+ let intersectsVertical = false;
437
+ intersectingHandles.forEach(data => {
438
+ const {
439
+ direction
440
+ } = data;
441
+ if (direction === "horizontal") {
442
+ intersectsHorizontal = true;
443
+ } else {
444
+ intersectsVertical = true;
445
+ }
446
+ });
447
+ let constraintFlags = 0;
448
+ panelConstraintFlags.forEach(flag => {
449
+ constraintFlags |= flag;
450
+ });
451
+ if (intersectsHorizontal && intersectsVertical) {
452
+ setGlobalCursorStyle("intersection", constraintFlags);
453
+ } else if (intersectsHorizontal) {
454
+ setGlobalCursorStyle("horizontal", constraintFlags);
455
+ } else if (intersectsVertical) {
456
+ setGlobalCursorStyle("vertical", constraintFlags);
457
+ } else {
458
+ resetGlobalCursorStyle();
459
+ }
460
+ }
461
+ function updateListeners() {
462
+ ownerDocumentCounts.forEach((_, ownerDocument) => {
463
+ const {
464
+ body
465
+ } = ownerDocument;
466
+ body.removeEventListener("contextmenu", handlePointerUp);
467
+ body.removeEventListener("mousedown", handlePointerDown);
468
+ body.removeEventListener("mouseleave", handlePointerMove);
469
+ body.removeEventListener("mousemove", handlePointerMove);
470
+ body.removeEventListener("touchmove", handlePointerMove);
471
+ body.removeEventListener("touchstart", handlePointerDown);
472
+ });
473
+ window.removeEventListener("mouseup", handlePointerUp);
474
+ window.removeEventListener("touchcancel", handlePointerUp);
475
+ window.removeEventListener("touchend", handlePointerUp);
476
+ if (registerResizeHandle.length > 0) {
477
+ if (isPointerDown) {
478
+ if (intersectingHandles.length > 0) {
479
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
480
+ const {
481
+ body
482
+ } = ownerDocument;
483
+ if (count > 0) {
484
+ body.addEventListener("contextmenu", handlePointerUp);
485
+ body.addEventListener("mouseleave", handlePointerMove);
486
+ body.addEventListener("mousemove", handlePointerMove);
487
+ body.addEventListener("touchmove", handlePointerMove, {
488
+ passive: false
489
+ });
490
+ }
491
+ });
492
+ }
493
+ window.addEventListener("mouseup", handlePointerUp);
494
+ window.addEventListener("touchcancel", handlePointerUp);
495
+ window.addEventListener("touchend", handlePointerUp);
496
+ } else {
497
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
498
+ const {
499
+ body
500
+ } = ownerDocument;
501
+ if (count > 0) {
502
+ body.addEventListener("mousedown", handlePointerDown);
503
+ body.addEventListener("mousemove", handlePointerMove);
504
+ body.addEventListener("touchmove", handlePointerMove, {
505
+ passive: false
506
+ });
507
+ body.addEventListener("touchstart", handlePointerDown);
508
+ }
509
+ });
510
+ }
511
+ }
512
+ }
513
+ function updateResizeHandlerStates(action, event) {
514
+ registeredResizeHandlers.forEach(data => {
515
+ const {
516
+ setResizeHandlerState
517
+ } = data;
518
+ if (intersectingHandles.includes(data)) {
519
+ if (isPointerDown) {
520
+ setResizeHandlerState(action, "drag", event);
521
+ } else {
522
+ setResizeHandlerState(action, "hover", event);
523
+ }
524
+ } else {
525
+ setResizeHandlerState(action, "inactive", event);
526
+ }
527
+ });
528
+ }
529
+
214
530
  function assert(expectedCondition, message = "Assertion failed!") {
215
531
  if (!expectedCondition) {
216
532
  console.error(message);
@@ -726,27 +1042,13 @@ function areEqual(arrayA, arrayB) {
726
1042
  return true;
727
1043
  }
728
1044
 
729
- function isKeyDown(event) {
730
- return event.type === "keydown";
731
- }
732
- function isMouseEvent(event) {
733
- return event.type.startsWith("mouse");
734
- }
735
- function isTouchEvent(event) {
736
- return event.type.startsWith("touch");
737
- }
738
-
739
1045
  function getResizeEventCursorPosition(direction, event) {
740
1046
  const isHorizontal = direction === "horizontal";
741
- if (isMouseEvent(event)) {
742
- return isHorizontal ? event.clientX : event.clientY;
743
- } else if (isTouchEvent(event)) {
744
- const firstTouch = event.touches[0];
745
- assert(firstTouch);
746
- return isHorizontal ? firstTouch.screenX : firstTouch.screenY;
747
- } else {
748
- throw Error(`Unsupported event type "${event.type}"`);
749
- }
1047
+ const {
1048
+ x,
1049
+ y
1050
+ } = getResizeEventCoordinates(event);
1051
+ return isHorizontal ? x : y;
750
1052
  }
751
1053
 
752
1054
  function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
@@ -936,44 +1238,6 @@ function computePanelFlexBoxStyle({
936
1238
  };
937
1239
  }
938
1240
 
939
- let currentState = null;
940
- let element = null;
941
- function getCursorStyle(state) {
942
- switch (state) {
943
- case "horizontal":
944
- return "ew-resize";
945
- case "horizontal-max":
946
- return "w-resize";
947
- case "horizontal-min":
948
- return "e-resize";
949
- case "vertical":
950
- return "ns-resize";
951
- case "vertical-max":
952
- return "n-resize";
953
- case "vertical-min":
954
- return "s-resize";
955
- }
956
- }
957
- function resetGlobalCursorStyle() {
958
- if (element !== null) {
959
- document.head.removeChild(element);
960
- currentState = null;
961
- element = null;
962
- }
963
- }
964
- function setGlobalCursorStyle(state) {
965
- if (currentState === state) {
966
- return;
967
- }
968
- currentState = state;
969
- const style = getCursorStyle(state);
970
- if (element === null) {
971
- element = document.createElement("style");
972
- document.head.appendChild(element);
973
- }
974
- element.innerHTML = `*{cursor: ${style}!important;}`;
975
- }
976
-
977
1241
  function debounce(callback, durationMs = 10) {
978
1242
  let timeoutId = null;
979
1243
  let callable = (...args) => {
@@ -1609,18 +1873,15 @@ function PanelGroupWithForwardedRef({
1609
1873
  if (prevDeltaRef.current != delta) {
1610
1874
  prevDeltaRef.current = delta;
1611
1875
  if (!layoutChanged) {
1612
- // If the pointer has moved too far to resize the panel any further,
1613
- // update the cursor style for a visual clue.
1876
+ // If the pointer has moved too far to resize the panel any further, note this so we can update the cursor.
1614
1877
  // This mimics VS Code behavior.
1615
-
1616
1878
  if (isHorizontal) {
1617
- setGlobalCursorStyle(delta < 0 ? "horizontal-min" : "horizontal-max");
1879
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_HORIZONTAL_MIN : EXCEEDED_HORIZONTAL_MAX);
1618
1880
  } else {
1619
- setGlobalCursorStyle(delta < 0 ? "vertical-min" : "vertical-max");
1881
+ reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_VERTICAL_MIN : EXCEEDED_VERTICAL_MAX);
1620
1882
  }
1621
1883
  } else {
1622
- // Reset the cursor style to the the normal resize cursor.
1623
- setGlobalCursorStyle(isHorizontal ? "horizontal" : "vertical");
1884
+ reportConstraintsViolation(dragHandleId, 0);
1624
1885
  }
1625
1886
  }
1626
1887
  }
@@ -1718,7 +1979,6 @@ function PanelGroupWithForwardedRef({
1718
1979
  });
1719
1980
  }, []);
1720
1981
  const stopDragging = useCallback(() => {
1721
- resetGlobalCursorStyle();
1722
1982
  setDragState(null);
1723
1983
  }, []);
1724
1984
  const unregisterPanel = useCallback(panelData => {
@@ -1859,6 +2119,7 @@ function PanelResizeHandle({
1859
2119
  children = null,
1860
2120
  className: classNameFromProps = "",
1861
2121
  disabled = false,
2122
+ hitAreaMargins,
1862
2123
  id: idFromProps,
1863
2124
  onDragging,
1864
2125
  style: styleFromProps = {},
@@ -1881,67 +2142,60 @@ function PanelResizeHandle({
1881
2142
  }
1882
2143
  const {
1883
2144
  direction,
1884
- dragState,
1885
2145
  groupId,
1886
- registerResizeHandle,
2146
+ registerResizeHandle: registerResizeHandleWithParentGroup,
1887
2147
  startDragging,
1888
2148
  stopDragging,
1889
2149
  panelGroupElement
1890
2150
  } = panelGroupContext;
1891
2151
  const resizeHandleId = useUniqueId(idFromProps);
1892
- const isDragging = (dragState === null || dragState === void 0 ? void 0 : dragState.dragHandleId) === resizeHandleId;
2152
+ const [state, setState] = useState("inactive");
1893
2153
  const [isFocused, setIsFocused] = useState(false);
1894
2154
  const [resizeHandler, setResizeHandler] = useState(null);
1895
- const stopDraggingAndBlur = useCallback(() => {
1896
- // Clicking on the drag handle shouldn't leave it focused;
1897
- // That would cause the PanelGroup to think it was still active.
1898
- const element = elementRef.current;
1899
- assert(element);
1900
- element.blur();
1901
- stopDragging();
1902
- const {
1903
- onDragging
1904
- } = callbacksRef.current;
1905
- if (onDragging) {
1906
- onDragging(false);
1907
- }
1908
- }, [stopDragging]);
1909
2155
  useEffect(() => {
1910
2156
  if (disabled) {
1911
2157
  setResizeHandler(null);
1912
2158
  } else {
1913
- const resizeHandler = registerResizeHandle(resizeHandleId);
2159
+ const resizeHandler = registerResizeHandleWithParentGroup(resizeHandleId);
1914
2160
  setResizeHandler(() => resizeHandler);
1915
2161
  }
1916
- }, [disabled, resizeHandleId, registerResizeHandle]);
2162
+ }, [disabled, resizeHandleId, registerResizeHandleWithParentGroup]);
1917
2163
  useEffect(() => {
1918
- if (disabled || resizeHandler == null || !isDragging) {
2164
+ var _hitAreaMargins$coars, _hitAreaMargins$fine;
2165
+ if (disabled || resizeHandler == null) {
1919
2166
  return;
1920
2167
  }
1921
- const onMove = event => {
1922
- resizeHandler(event);
1923
- };
1924
- const onMouseLeave = event => {
1925
- resizeHandler(event);
1926
- };
1927
2168
  const element = elementRef.current;
1928
2169
  assert(element);
1929
- const targetDocument = element.ownerDocument;
1930
- targetDocument.body.addEventListener("contextmenu", stopDraggingAndBlur);
1931
- targetDocument.body.addEventListener("mousemove", onMove);
1932
- targetDocument.body.addEventListener("touchmove", onMove);
1933
- targetDocument.body.addEventListener("mouseleave", onMouseLeave);
1934
- window.addEventListener("mouseup", stopDraggingAndBlur);
1935
- window.addEventListener("touchend", stopDraggingAndBlur);
1936
- return () => {
1937
- targetDocument.body.removeEventListener("contextmenu", stopDraggingAndBlur);
1938
- targetDocument.body.removeEventListener("mousemove", onMove);
1939
- targetDocument.body.removeEventListener("touchmove", onMove);
1940
- targetDocument.body.removeEventListener("mouseleave", onMouseLeave);
1941
- window.removeEventListener("mouseup", stopDraggingAndBlur);
1942
- window.removeEventListener("touchend", stopDraggingAndBlur);
2170
+ const setResizeHandlerState = (action, state, event) => {
2171
+ setState(state);
2172
+ switch (action) {
2173
+ case "down":
2174
+ {
2175
+ startDragging(resizeHandleId, event);
2176
+ break;
2177
+ }
2178
+ case "up":
2179
+ {
2180
+ stopDragging();
2181
+ break;
2182
+ }
2183
+ }
2184
+ switch (state) {
2185
+ case "drag":
2186
+ {
2187
+ resizeHandler(event);
2188
+ break;
2189
+ }
2190
+ }
1943
2191
  };
1944
- }, [direction, disabled, isDragging, resizeHandler, stopDraggingAndBlur]);
2192
+ return registerResizeHandle(resizeHandleId, element, direction, {
2193
+ // Coarse inputs (e.g. finger/touch)
2194
+ coarse: (_hitAreaMargins$coars = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.coarse) !== null && _hitAreaMargins$coars !== void 0 ? _hitAreaMargins$coars : 15,
2195
+ // Fine inputs (e.g. mouse)
2196
+ fine: (_hitAreaMargins$fine = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.fine) !== null && _hitAreaMargins$fine !== void 0 ? _hitAreaMargins$fine : 5
2197
+ }, setResizeHandlerState);
2198
+ }, [direction, disabled, hitAreaMargins, registerResizeHandleWithParentGroup, resizeHandleId, resizeHandler, startDragging, stopDragging]);
1945
2199
  useWindowSplitterResizeHandlerBehavior({
1946
2200
  disabled,
1947
2201
  handleId: resizeHandleId,
@@ -1949,7 +2203,6 @@ function PanelResizeHandle({
1949
2203
  panelGroupElement
1950
2204
  });
1951
2205
  const style = {
1952
- cursor: getCursorStyle(direction),
1953
2206
  touchAction: "none",
1954
2207
  userSelect: "none"
1955
2208
  };
@@ -1959,31 +2212,6 @@ function PanelResizeHandle({
1959
2212
  className: classNameFromProps,
1960
2213
  onBlur: () => setIsFocused(false),
1961
2214
  onFocus: () => setIsFocused(true),
1962
- onMouseDown: event => {
1963
- startDragging(resizeHandleId, event.nativeEvent);
1964
- const callbacks = callbacksRef.current;
1965
- assert(callbacks);
1966
- const {
1967
- onDragging
1968
- } = callbacks;
1969
- if (onDragging) {
1970
- onDragging(true);
1971
- }
1972
- },
1973
- onMouseUp: stopDraggingAndBlur,
1974
- onTouchCancel: stopDraggingAndBlur,
1975
- onTouchEnd: stopDraggingAndBlur,
1976
- onTouchStart: event => {
1977
- startDragging(resizeHandleId, event.nativeEvent);
1978
- const callbacks = callbacksRef.current;
1979
- assert(callbacks);
1980
- const {
1981
- onDragging
1982
- } = callbacks;
1983
- if (onDragging) {
1984
- onDragging(true);
1985
- }
1986
- },
1987
2215
  ref: elementRef,
1988
2216
  role: "separator",
1989
2217
  style: {
@@ -1995,7 +2223,8 @@ function PanelResizeHandle({
1995
2223
  "data-panel-group-direction": direction,
1996
2224
  "data-panel-group-id": groupId,
1997
2225
  "data-resize-handle": "",
1998
- "data-resize-handle-active": isDragging ? "pointer" : isFocused ? "keyboard" : undefined,
2226
+ "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2227
+ "data-resize-handle-state": state,
1999
2228
  "data-panel-resize-handle-enabled": !disabled,
2000
2229
  "data-panel-resize-handle-id": resizeHandleId
2001
2230
  });