react-grid-layout 1.4.0 → 1.4.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.4.2 (Sep 22, 2023)
4
+
5
+ ### Bugfixes
6
+
7
+ - Resizing in every directionnow obeys preventCollision restrictions [#1937](https://github.com/react-grid-layout/react-grid-layout/pull/1937)
8
+
9
+ ## 1.4.1 (Sep 12, 2023)
10
+
11
+ ### Bugfixes
12
+
13
+ - Fixed bug where height/width could not be resized if `h = 0` or `w = 0` and 0 `containerPadding`. [#1931](https://github.com/react-grid-layout/react-grid-layout/pull/1931)
14
+ - Revert `fast-equals` to @4. Fixes incompatibility with `Create-React-App@5`.
15
+
3
16
  ## 1.4.0 (Sep 11, 2023)
4
17
 
5
18
  Hey, it's been a long time! Taking a year and a half off is a pretty "open-source" thing to do, thanks for bearing with me.
package/build/GridItem.js CHANGED
@@ -22,14 +22,11 @@ function _toPrimitive(input, hint) { if (typeof input !== "object" || input ===
22
22
  GridDragEvent,
23
23
  GridResizeEvent,
24
24
  DroppingPosition,
25
- Position
25
+ Position,
26
+ ResizeHandleAxis
26
27
  } from "./utils";*/
27
28
  /*:: import type { PositionParams } from "./calculateUtils";*/
28
- /*:: import type {
29
- ResizeHandleAxis,
30
- ResizeHandle,
31
- ReactRef
32
- } from "./ReactGridLayoutPropTypes";*/
29
+ /*:: import type { ResizeHandle, ReactRef } from "./ReactGridLayoutPropTypes";*/
33
30
  /*:: type PartialPosition = { top: number, left: number };*/
34
31
  /*:: type GridItemCallback<Data: GridDragEvent | GridResizeEvent> = (
35
32
  i: string,
@@ -37,16 +34,16 @@ function _toPrimitive(input, hint) { if (typeof input !== "object" || input ===
37
34
  h: number,
38
35
  Data
39
36
  ) => void;*/
37
+ /*:: type ResizeCallbackData = {
38
+ node: HTMLElement,
39
+ size: Position,
40
+ handle: ResizeHandleAxis
41
+ };*/
40
42
  /*:: type GridItemResizeCallback = (
41
43
  e: Event,
42
44
  data: ResizeCallbackData,
43
45
  position: Position
44
46
  ) => void;*/
45
- /*:: type ResizeCallbackData = {
46
- node: HTMLElement,
47
- size: Position,
48
- handle: string
49
- };*/
50
47
  /*:: type State = {
51
48
  resizing: ?{ top: number, left: number, width: number, height: number },
52
49
  dragging: ?{ top: number, left: number },
@@ -401,8 +398,8 @@ class GridItem extends _react.default.Component /*:: <Props, State>*/{
401
398
  * Utility function to setup callback handler definitions for
402
399
  * similarily structured resize events.
403
400
  */
404
- curryResizeHandler(position /*: Position*/, handler /*: function*/) /*: function*/{
405
- return (e /*: Event*/, data /*: ResizeCallbackData*/) => /*: function*/handler(e, data, position);
401
+ curryResizeHandler(position /*: Position*/, handler /*: Function*/) /*: Function*/{
402
+ return (e /*: Event*/, data /*: ResizeCallbackData*/) => /*: Function*/handler(e, data, position);
406
403
  }
407
404
 
408
405
  /**
@@ -414,7 +411,6 @@ class GridItem extends _react.default.Component /*:: <Props, State>*/{
414
411
  mixinResizable(child /*: ReactElement<any>*/, position /*: Position*/, isResizable /*: boolean*/) /*: ReactElement<any>*/{
415
412
  const {
416
413
  cols,
417
- x,
418
414
  minW,
419
415
  minH,
420
416
  maxW,
@@ -453,11 +449,13 @@ class GridItem extends _react.default.Component /*:: <Props, State>*/{
453
449
  }, child);
454
450
  }
455
451
  /**
456
- * Wrapper around drag events to provide more useful data.
457
- * All drag events call the function with the given handler name,
458
- * with the signature (index, x, y).
452
+ * Wrapper around resize events to provide more useful data.
459
453
  */
460
- onResizeHandler(e /*: Event*/, _ref4 /*:: */, position /*: Position*/, handlerName /*: string*/) /*: void*/{
454
+ onResizeHandler(e /*: Event*/, _ref4 /*:: */,
455
+ // 'size' is updated position
456
+ position /*: Position*/,
457
+ // existing position
458
+ handlerName /*: string*/) /*: void*/{
461
459
  let {
462
460
  node,
463
461
  size,
@@ -470,122 +468,33 @@ class GridItem extends _react.default.Component /*:: <Props, State>*/{
470
468
  y,
471
469
  i,
472
470
  maxH,
473
- minH
471
+ minH,
472
+ containerWidth
474
473
  } = this.props;
475
- let {
474
+ const {
476
475
  minW,
477
476
  maxW
478
477
  } = this.props;
479
478
 
480
- // Get new XY
481
- let {
482
- w,
483
- h
484
- } = (0, _calculateUtils.calcWH)(this.getPositionParams(), size.width, size.height, x, y, handle);
485
-
486
- // minW should be at least 1 (TODO propTypes validation?)
487
- minW = Math.max(minW, 1);
488
-
489
- // Min/max capping
490
- w = (0, _calculateUtils.clamp)(w, minW, maxW);
491
- h = (0, _calculateUtils.clamp)(h, minH, maxH);
479
+ // Clamping of dimensions based on resize direction
492
480
  let updatedSize = size;
493
481
  if (node) {
494
- const currentLeft = position.left;
495
- const currentTop = position.top;
496
- const currentWidth = position.width;
497
- const currentHeight = position.height;
498
- const resizeNorth = _ref5 => {
499
- let {
500
- left,
501
- height,
502
- width
503
- } = _ref5;
504
- const top = currentTop - (height - currentHeight);
505
- return {
506
- top,
507
- left,
508
- width,
509
- height: this.constrainHeight(top, currentHeight, height),
510
- top: this.constrainTop(top)
511
- };
512
- };
513
- const resizeEast = _ref6 => {
514
- let {
515
- top,
516
- left,
517
- height,
518
- width
519
- } = _ref6;
520
- return {
521
- top,
522
- height,
523
- width: this.constrainWidth(currentLeft, currentWidth, width),
524
- left: this.constrainLeft(left)
525
- };
526
- };
527
- const resizeWest = _ref7 => {
528
- let {
529
- top,
530
- height,
531
- width
532
- } = _ref7;
533
- const left = currentLeft - (width - currentWidth);
534
- return {
535
- height,
536
- width: left <= 0 ? currentWidth : this.constrainWidth(currentLeft, currentWidth, width),
537
- top: this.constrainTop(top),
538
- left: this.constrainLeft(left)
539
- };
540
- };
541
- const resizeSouth = _ref8 => {
542
- let {
543
- top,
544
- left,
545
- height,
546
- width
547
- } = _ref8;
548
- return {
549
- width,
550
- left,
551
- height: this.constrainHeight(top, currentHeight, height),
552
- top: this.constrainTop(top)
553
- };
554
- };
555
- const resizeNorthEast = function () {
556
- return resizeNorth(resizeEast(...arguments));
557
- };
558
- const resizeNorthWest = function () {
559
- return resizeNorth(resizeWest(...arguments));
560
- };
561
- const resizeSouthEast = function () {
562
- return resizeSouth(resizeEast(...arguments));
563
- };
564
- const resizeSouthWest = function () {
565
- return resizeSouth(resizeWest(...arguments));
566
- };
567
- const ordinalResizeHandlerMap = {
568
- n: resizeNorth,
569
- ne: resizeNorthEast,
570
- e: resizeEast,
571
- se: resizeSouthEast,
572
- s: resizeSouth,
573
- sw: resizeSouthWest,
574
- w: resizeWest,
575
- nw: resizeNorthWest
576
- };
577
- const resizeHandler = ordinalResizeHandlerMap[handle];
578
- updatedSize = resizeHandler ? resizeHandler({
579
- top: currentTop,
580
- left: currentLeft,
581
- height: currentHeight,
582
- width: currentWidth,
583
- ...size
584
- }) : size;
482
+ updatedSize = (0, _utils.resizeItemInDirection)(handle, position, size, containerWidth);
585
483
  this.setState({
586
484
  resizing: handlerName === "onResizeStop" ? null : updatedSize
587
485
  });
588
486
  }
487
+
488
+ // Get new XY based on pixel size
489
+ let {
490
+ w,
491
+ h
492
+ } = (0, _calculateUtils.calcWH)(this.getPositionParams(), updatedSize.width, updatedSize.height, x, y, handle);
493
+
494
+ // Min/max capping.
495
+ // minW should be at least 1 (TODO propTypes validation?)
496
+ w = (0, _calculateUtils.clamp)(w, Math.max(minW, 1), maxW);
497
+ h = (0, _calculateUtils.clamp)(h, minH, maxH);
589
498
  handler.call(this, i, w, h, {
590
499
  e,
591
500
  node,
@@ -593,22 +502,6 @@ class GridItem extends _react.default.Component /*:: <Props, State>*/{
593
502
  handle
594
503
  });
595
504
  }
596
-
597
- /**
598
- * Helper functions to constrain dimensions of a GridItem
599
- */
600
- constrainWidth(left /*: number*/, currentWidth /*: number*/, newWidth /*: number*/) /*: number*/{
601
- return left + newWidth > this.props.containerWidth ? currentWidth : newWidth;
602
- }
603
- constrainHeight(top /*: number*/, currentHeight /*: number*/, newHeight /*: number*/) /*: number*/{
604
- return top <= 0 ? currentHeight : newHeight;
605
- }
606
- constrainLeft(left /*: number*/) /*: number*/{
607
- return Math.max(0, left);
608
- }
609
- constrainTop(top /*: number*/) /*: number*/{
610
- return Math.max(0, top);
611
- }
612
505
  render() /*: ReactNode*/{
613
506
  const {
614
507
  x,
@@ -230,59 +230,62 @@ class ReactGridLayout extends React.Component /*:: <Props, State>*/{
230
230
  preventCollision,
231
231
  allowOverlap
232
232
  } = this.props;
233
+ let shouldMoveItem = false;
234
+ let finalLayout;
235
+ let x;
236
+ let y;
233
237
  const [newLayout, l] = (0, _utils.withLayoutItem)(layout, i, l => {
238
+ let hasCollisions;
239
+ x = l.x;
240
+ y = l.y;
241
+ if (["sw", "w", "nw", "n", "ne"].indexOf(handle) !== -1) {
242
+ if (["sw", "nw", "w"].indexOf(handle) !== -1) {
243
+ x = l.x + (l.w - w);
244
+ w = l.x !== x && x < 0 ? l.w : w;
245
+ x = x < 0 ? 0 : x;
246
+ }
247
+ if (["ne", "n", "nw"].indexOf(handle) !== -1) {
248
+ y = l.y + (l.h - h);
249
+ h = l.y !== y && y < 0 ? l.h : h;
250
+ y = y < 0 ? 0 : y;
251
+ }
252
+ shouldMoveItem = true;
253
+ }
254
+
234
255
  // Something like quad tree should be used
235
256
  // to find collisions faster
236
- let hasCollisions;
237
257
  if (preventCollision && !allowOverlap) {
238
258
  const collisions = (0, _utils.getAllCollisions)(layout, {
239
259
  ...l,
240
260
  w,
241
- h
261
+ h,
262
+ x,
263
+ y
242
264
  }).filter(layoutItem => layoutItem.i !== l.i);
243
265
  hasCollisions = collisions.length > 0;
244
266
 
245
267
  // If we're colliding, we need adjust the placeholder.
246
268
  if (hasCollisions) {
247
- // adjust w && h to maximum allowed space
248
- let leastX = Infinity,
249
- leastY = Infinity;
250
- collisions.forEach(layoutItem => {
251
- if (layoutItem.x > l.x) leastX = Math.min(leastX, layoutItem.x);
252
- if (layoutItem.y > l.y) leastY = Math.min(leastY, layoutItem.y);
253
- });
254
- if (Number.isFinite(leastX)) l.w = leastX - l.x;
255
- if (Number.isFinite(leastY)) l.h = leastY - l.y;
269
+ // Reset layoutItem dimensions if there were collisions
270
+ y = l.y;
271
+ h = l.h;
272
+ x = l.x;
273
+ w = l.w;
274
+ shouldMoveItem = false;
256
275
  }
257
276
  }
277
+ l.w = w;
278
+ l.h = h;
258
279
  return l;
259
280
  });
260
281
 
261
282
  // Shouldn't ever happen, but typechecking makes it necessary
262
283
  if (!l) return;
263
- let finalLayout;
264
- if (["sw", "w", "nw", "n", "ne"].indexOf(handle) !== -1) {
265
- let x = l.x;
266
- let y = l.y;
267
- if (["sw", "nw", "w"].indexOf(handle) !== -1) {
268
- x = l.x + (l.w - w);
269
- w = l.x !== x && x < 0 ? l.w : w;
270
- x = x < 0 ? 0 : x;
271
- }
272
- if (["ne", "n", "nw"].indexOf(handle) !== -1) {
273
- y = l.y + (l.h - h);
274
- h = l.y !== y && y < 0 ? l.h : h;
275
- y = y < 0 ? 0 : y;
276
- }
277
- l.w = w;
278
- l.h = h;
284
+ finalLayout = newLayout;
285
+ if (shouldMoveItem) {
279
286
  // Move the element to the new position.
280
287
  const isUserAction = true;
281
288
  finalLayout = (0, _utils.moveElement)(newLayout, l, x, y, isUserAction, this.props.preventCollision, (0, _utils.compactType)(this.props), cols, allowOverlap);
282
- } else {
283
- l.w = w;
284
- l.h = h;
285
- finalLayout = newLayout;
286
289
  }
287
290
 
288
291
  // Create placeholder element (display only)
@@ -17,21 +17,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
17
17
  EventCallback,
18
18
  CompactType,
19
19
  Layout,
20
- LayoutItem
20
+ LayoutItem,
21
+ ResizeHandleAxis
21
22
  } from "./utils";*/
22
23
  /*:: export type ReactRef<T: HTMLElement> = {|
23
24
  +current: T | null
24
25
  |};*/
25
26
  // util
26
- /*:: export type ResizeHandleAxis =
27
- | "s"
28
- | "w"
29
- | "e"
30
- | "n"
31
- | "sw"
32
- | "nw"
33
- | "se"
34
- | "ne";*/
35
27
  /*:: export type ResizeHandle =
36
28
  | ReactElement<any>
37
29
  | ((
package/build/utils.js CHANGED
@@ -23,6 +23,7 @@ exports.moveElement = moveElement;
23
23
  exports.moveElementAwayFromCollision = moveElementAwayFromCollision;
24
24
  exports.noop = void 0;
25
25
  exports.perc = perc;
26
+ exports.resizeItemInDirection = resizeItemInDirection;
26
27
  exports.setTopLeft = setTopLeft;
27
28
  exports.setTransform = setTransform;
28
29
  exports.sortLayoutItems = sortLayoutItems;
@@ -38,6 +39,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
38
39
  ChildrenArray as ReactChildrenArray,
39
40
  Element as ReactElement
40
41
  } from "react";*/
42
+ /*:: export type ResizeHandleAxis =
43
+ | "s"
44
+ | "w"
45
+ | "e"
46
+ | "n"
47
+ | "sw"
48
+ | "nw"
49
+ | "se"
50
+ | "ne";*/
41
51
  /*:: export type LayoutItem = {
42
52
  w: number,
43
53
  h: number,
@@ -52,7 +62,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
52
62
  static?: boolean,
53
63
  isDraggable?: ?boolean,
54
64
  isResizable?: ?boolean,
55
- resizeHandles?: Array<"s" | "w" | "e" | "n" | "sw" | "nw" | "se" | "ne">,
65
+ resizeHandles?: Array<ResizeHandleAxis>,
56
66
  isBounded?: ?boolean
57
67
  };*/
58
68
  /*:: export type Layout = $ReadOnlyArray<LayoutItem>;*/
@@ -543,13 +553,116 @@ function moveElementAwayFromCollision(layout /*: Layout*/, collidesWith /*: Layo
543
553
  function perc(num /*: number*/) /*: string*/{
544
554
  return num * 100 + "%";
545
555
  }
546
- function setTransform(_ref /*:: */) /*: Object*/{
556
+
557
+ /**
558
+ * Helper functions to constrain dimensions of a GridItem
559
+ */
560
+ const constrainWidth = (left /*: number*/, currentWidth /*: number*/, newWidth /*: number*/, containerWidth /*: number*/) => {
561
+ return left + newWidth > containerWidth ? currentWidth : newWidth;
562
+ };
563
+ const constrainHeight = (top /*: number*/, currentHeight /*: number*/, newHeight /*: number*/) => {
564
+ return top < 0 ? currentHeight : newHeight;
565
+ };
566
+ const constrainLeft = (left /*: number*/) => Math.max(0, left);
567
+ const constrainTop = (top /*: number*/) => Math.max(0, top);
568
+ const resizeNorth = (currentSize, _ref, _containerWidth) => {
569
+ let {
570
+ left,
571
+ height,
572
+ width
573
+ } = _ref;
574
+ const top = currentSize.top - (height - currentSize.height);
575
+ return {
576
+ left,
577
+ width,
578
+ height: constrainHeight(top, currentSize.height, height),
579
+ top: constrainTop(top)
580
+ };
581
+ };
582
+ const resizeEast = (currentSize, _ref2, containerWidth) => {
583
+ let {
584
+ top,
585
+ left,
586
+ height,
587
+ width
588
+ } = _ref2;
589
+ return {
590
+ top,
591
+ height,
592
+ width: constrainWidth(currentSize.left, currentSize.width, width, containerWidth),
593
+ left: constrainLeft(left)
594
+ };
595
+ };
596
+ const resizeWest = (currentSize, _ref3, containerWidth) => {
597
+ let {
598
+ top,
599
+ height,
600
+ width
601
+ } = _ref3;
602
+ const left = currentSize.left - (width - currentSize.width);
603
+ return {
604
+ height,
605
+ width: left < 0 ? currentSize.width : constrainWidth(currentSize.left, currentSize.width, width, containerWidth),
606
+ top: constrainTop(top),
607
+ left: constrainLeft(left)
608
+ };
609
+ };
610
+ const resizeSouth = (currentSize, _ref4, containerWidth) => {
611
+ let {
612
+ top,
613
+ left,
614
+ height,
615
+ width
616
+ } = _ref4;
617
+ return {
618
+ width,
619
+ left,
620
+ height: constrainHeight(top, currentSize.height, height),
621
+ top: constrainTop(top)
622
+ };
623
+ };
624
+ const resizeNorthEast = function () {
625
+ return resizeNorth(arguments.length <= 0 ? undefined : arguments[0], resizeEast(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
626
+ };
627
+ const resizeNorthWest = function () {
628
+ return resizeNorth(arguments.length <= 0 ? undefined : arguments[0], resizeWest(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
629
+ };
630
+ const resizeSouthEast = function () {
631
+ return resizeSouth(arguments.length <= 0 ? undefined : arguments[0], resizeEast(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
632
+ };
633
+ const resizeSouthWest = function () {
634
+ return resizeSouth(arguments.length <= 0 ? undefined : arguments[0], resizeWest(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
635
+ };
636
+ const ordinalResizeHandlerMap = {
637
+ n: resizeNorth,
638
+ ne: resizeNorthEast,
639
+ e: resizeEast,
640
+ se: resizeSouthEast,
641
+ s: resizeSouth,
642
+ sw: resizeSouthWest,
643
+ w: resizeWest,
644
+ nw: resizeNorthWest
645
+ };
646
+
647
+ /**
648
+ * Helper for clamping width and position when resizing an item.
649
+ */
650
+ function resizeItemInDirection(direction /*: ResizeHandleAxis*/, currentSize /*: Position*/, newSize /*: Position*/, containerWidth /*: number*/) /*: Position*/{
651
+ const ordinalHandler = ordinalResizeHandlerMap[direction];
652
+ // Shouldn't be possible given types; that said, don't fail hard
653
+ if (!ordinalHandler) return newSize;
654
+ return ordinalHandler(currentSize, {
655
+ ...currentSize,
656
+ ...newSize
657
+ }, containerWidth);
658
+ }
659
+ function setTransform(_ref5 /*:: */) /*: Object*/{
547
660
  let {
548
661
  top,
549
662
  left,
550
663
  width,
551
664
  height
552
- } /*: Position*/ = _ref /*: Position*/;
665
+ } /*: Position*/ = _ref5 /*: Position*/;
553
666
  // Replace unitless items with px
554
667
  const translate = "translate(".concat(left, "px,").concat(top, "px)");
555
668
  return {
@@ -563,13 +676,13 @@ function setTransform(_ref /*:: */) /*: Object*/{
563
676
  position: "absolute"
564
677
  };
565
678
  }
566
- function setTopLeft(_ref2 /*:: */) /*: Object*/{
679
+ function setTopLeft(_ref6 /*:: */) /*: Object*/{
567
680
  let {
568
681
  top,
569
682
  left,
570
683
  width,
571
684
  height
572
- } /*: Position*/ = _ref2 /*: Position*/;
685
+ } /*: Position*/ = _ref6 /*: Position*/;
573
686
  return {
574
687
  top: "".concat(top, "px"),
575
688
  left: "".concat(left, "px"),