react-grid-layout 0.16.4 → 0.17.1

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.
@@ -29,15 +29,16 @@ export type Position = {
29
29
  };
30
30
  export type ReactDraggableCallbackData = {
31
31
  node: HTMLElement,
32
- x: number,
33
- y: number,
32
+ x?: number,
33
+ y?: number,
34
34
  deltaX: number,
35
35
  deltaY: number,
36
- lastX: number,
37
- lastY: number
36
+ lastX?: number,
37
+ lastY?: number
38
38
  };
39
39
 
40
40
  export type PartialPosition = { left: number, top: number };
41
+ export type DroppingPosition = { x: number, y: number, e: Event };
41
42
  export type Size = { width: number, height: number };
42
43
  export type GridDragEvent = {
43
44
  e: Event,
@@ -45,6 +46,15 @@ export type GridDragEvent = {
45
46
  newPosition: PartialPosition
46
47
  };
47
48
  export type GridResizeEvent = { e: Event, node: HTMLElement, size: Size };
49
+ export type DragOverEvent = MouseEvent & {
50
+ nativeEvent: {
51
+ layerX: number,
52
+ layerY: number,
53
+ target: {
54
+ className: String
55
+ }
56
+ }
57
+ };
48
58
 
49
59
  type REl = ReactElement<any>;
50
60
  export type ReactChildren = ReactChildrenArray<REl>;
@@ -122,7 +132,7 @@ export function childrenEqual(a: ReactChildren, b: ReactChildren): boolean {
122
132
  * Given two layoutitems, check if they collide.
123
133
  */
124
134
  export function collides(l1: LayoutItem, l2: LayoutItem): boolean {
125
- if (l1 === l2) return false; // same element
135
+ if (l1.i === l2.i) return false; // same element
126
136
  if (l1.x + l1.w <= l2.x) return false; // l1 is left of l2
127
137
  if (l1.x >= l2.x + l2.w) return false; // l1 is right of l2
128
138
  if (l1.y + l1.h <= l2.y) return false; // l1 is above l2
@@ -185,15 +195,22 @@ function resolveCompactionCollision(
185
195
  ) {
186
196
  const sizeProp = heightWidth[axis];
187
197
  item[axis] += 1;
188
- const itemIndex = layout.indexOf(item);
198
+ const itemIndex = layout
199
+ .map(layoutItem => {
200
+ return layoutItem.i;
201
+ })
202
+ .indexOf(item.i);
189
203
 
190
204
  // Go through each item we collide with.
191
205
  for (let i = itemIndex + 1; i < layout.length; i++) {
192
206
  const otherItem = layout[i];
193
-
194
207
  // Ignore static items
195
208
  if (otherItem.static) continue;
196
209
 
210
+ // Optimization: we can break early if we know we're past this el
211
+ // We can do this b/c it's a sorted layout
212
+ if (otherItem.y > item.y + item.h) break;
213
+
197
214
  if (collides(item, otherItem)) {
198
215
  resolveCompactionCollision(
199
216
  layout,
@@ -342,25 +359,27 @@ export function getStatics(layout: Layout): Array<LayoutItem> {
342
359
  export function moveElement(
343
360
  layout: Layout,
344
361
  l: LayoutItem,
345
- x: number,
346
- y: number,
362
+ x: ?number,
363
+ y: ?number,
347
364
  isUserAction: ?boolean,
348
365
  preventCollision: ?boolean,
349
366
  compactType: CompactType,
350
367
  cols: number
351
368
  ): Layout {
352
369
  if (l.static) return layout;
353
- log(`Moving element ${l.i} to [${x},${y}] from [${l.x},${l.y}]`);
354
370
 
355
371
  // Short-circuit if nothing to do.
356
372
  if (l.y === y && l.x === x) return layout;
357
373
 
374
+ log(
375
+ `Moving element ${l.i} to [${String(x)},${String(y)}] from [${l.x},${l.y}]`
376
+ );
358
377
  const oldX = l.x;
359
378
  const oldY = l.y;
360
379
 
361
380
  // This is quite a bit faster than extending the object
362
- l.x = x;
363
- l.y = y;
381
+ if (typeof x === "number") l.x = x;
382
+ if (typeof y === "number") l.y = y;
364
383
  l.moved = true;
365
384
 
366
385
  // If this collides with anything, move it.
@@ -369,9 +388,11 @@ export function moveElement(
369
388
  // nearest collision.
370
389
  let sorted = sortLayoutItems(layout, compactType);
371
390
  const movingUp =
372
- compactType === "vertical"
391
+ compactType === "vertical" && typeof y === "number"
373
392
  ? oldY >= y
374
- : compactType === "horizontal" ? oldX >= x : false;
393
+ : compactType === "horizontal" && typeof x === "number"
394
+ ? oldX >= x
395
+ : false;
375
396
  if (movingUp) sorted = sorted.reverse();
376
397
  const collisions = getAllCollisions(sorted, l);
377
398
 
@@ -388,9 +409,7 @@ export function moveElement(
388
409
  for (let i = 0, len = collisions.length; i < len; i++) {
389
410
  const collision = collisions[i];
390
411
  log(
391
- `Resolving collision between ${l.i} at [${l.x},${l.y}] and ${
392
- collision.i
393
- } at [${collision.x},${collision.y}]`
412
+ `Resolving collision between ${l.i} at [${l.x},${l.y}] and ${collision.i} at [${collision.x},${collision.y}]`
394
413
  );
395
414
 
396
415
  // Short circuit so we can't infinite loop
@@ -438,43 +457,36 @@ export function moveElementAwayFromCollision(
438
457
  cols: number
439
458
  ): Layout {
440
459
  const compactH = compactType === "horizontal";
441
- const compactV = compactType === "vertical";
442
- const preventCollision = false; // we're already colliding
460
+ // Compact vertically if not set to horizontal
461
+ const compactV = compactType !== "horizontal";
462
+ const preventCollision = collidesWith.static; // we're already colliding (not for static items)
443
463
 
444
464
  // If there is enough space above the collision to put this element, move it there.
445
465
  // We only do this on the main collision as this can get funky in cascades and cause
446
466
  // unwanted swapping behavior.
447
467
  if (isUserAction) {
468
+ // Reset isUserAction flag because we're not in the main collision anymore.
469
+ isUserAction = false;
470
+
448
471
  // Make a mock item so we don't modify the item here, only modify in moveElement.
449
472
  const fakeItem: LayoutItem = {
450
473
  x: compactH ? Math.max(collidesWith.x - itemToMove.w, 0) : itemToMove.x,
451
- y: !compactH ? Math.max(collidesWith.y - itemToMove.h, 0) : itemToMove.y,
474
+ y: compactV ? Math.max(collidesWith.y - itemToMove.h, 0) : itemToMove.y,
452
475
  w: itemToMove.w,
453
476
  h: itemToMove.h,
454
477
  i: "-1"
455
478
  };
456
479
 
457
- // This makes it feel a bit more precise by waiting to swap for just a bit when moving up.
458
- const [axis, dimension] =
459
- compactType === "horizontal" ? ["x", "w"] : ["y", "h"];
460
- const shouldSkip =
461
- false &&
462
- // Our collision is below the item to move, and only encroaches by 25% of its dimension; ignore
463
- (collidesWith[axis] > itemToMove[axis] &&
464
- collidesWith[axis] - itemToMove[axis] > itemToMove[dimension] / 4);
465
-
466
480
  // No collision? If so, we can go up there; otherwise, we'll end up moving down as normal
467
- if (!shouldSkip && !getFirstCollision(layout, fakeItem)) {
481
+ if (!getFirstCollision(layout, fakeItem)) {
468
482
  log(
469
- `Doing reverse collision on ${itemToMove.i} up to [${fakeItem.x},${
470
- fakeItem.y
471
- }].`
483
+ `Doing reverse collision on ${itemToMove.i} up to [${fakeItem.x},${fakeItem.y}].`
472
484
  );
473
485
  return moveElement(
474
486
  layout,
475
487
  itemToMove,
476
- fakeItem.x,
477
- fakeItem.y,
488
+ compactH ? fakeItem.x : undefined,
489
+ compactV ? fakeItem.y : undefined,
478
490
  isUserAction,
479
491
  preventCollision,
480
492
  compactType,
@@ -486,8 +498,8 @@ export function moveElementAwayFromCollision(
486
498
  return moveElement(
487
499
  layout,
488
500
  itemToMove,
489
- compactH ? collidesWith.x + collidesWith.w : itemToMove.x,
490
- compactV ? collidesWith.y + collidesWith.h : itemToMove.y,
501
+ compactH ? itemToMove.x + 1 : undefined,
502
+ compactV ? itemToMove.y + 1 : undefined,
491
503
  isUserAction,
492
504
  preventCollision,
493
505
  compactType,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-grid-layout",
3
- "version": "0.16.4",
3
+ "version": "0.17.1",
4
4
  "description": "A draggable and resizable grid layout with responsive breakpoints, for React.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -38,48 +38,45 @@
38
38
  "dependencies": {
39
39
  "classnames": "2.x",
40
40
  "lodash.isequal": "^4.0.0",
41
- "prop-types": "15.x",
42
- "react-draggable": "3.x",
43
- "react-resizable": "1.x"
41
+ "prop-types": "^15.0.0",
42
+ "react-draggable": "^4.0.0",
43
+ "react-resizable": "^1.9.0"
44
44
  },
45
45
  "devDependencies": {
46
- "babel-cli": "^6.24.1",
47
- "babel-core": "^6.24.1",
48
- "babel-eslint": "^8.2.1",
49
- "babel-jest": "^22.2.2",
50
- "babel-loader": "^7.1.2",
51
- "babel-plugin-espower": "^2.4.0",
52
- "babel-plugin-react-transform": "^3.0.0",
53
- "babel-plugin-transform-react-constant-elements": "^6.5.0",
54
- "babel-plugin-transform-react-inline-elements": "^6.6.5",
55
- "babel-plugin-typecheck": "^3.6.1",
56
- "babel-preset-es2015": "^6.24.1",
57
- "babel-preset-react": "^6.24.1",
58
- "babel-preset-stage-1": "^6.24.1",
59
- "css-loader": "^0.28.9",
60
- "ejs": "^2.4.1",
61
- "eslint": "^4.17.0",
62
- "eslint-plugin-flowtype": "^2.44.0",
63
- "eslint-plugin-mocha": "^4.11.0",
64
- "eslint-plugin-react": "^7.6.1",
46
+ "@babel/cli": "^7.6.4",
47
+ "@babel/core": "^7.6.4",
48
+ "@babel/plugin-proposal-class-properties": "^7.5.5",
49
+ "@babel/plugin-transform-flow-comments": "^7.6.0",
50
+ "@babel/preset-env": "^7.6.0",
51
+ "@babel/preset-flow": "^7.0.0",
52
+ "@babel/preset-react": "^7.0.0",
53
+ "babel-eslint": "^10.0.3",
54
+ "babel-jest": "^24.9.0",
55
+ "babel-loader": "^8.0.6",
56
+ "babel-plugin-espower": "^3.0.1",
57
+ "css-loader": "^3.2.0",
58
+ "ejs": "^2.7.1",
59
+ "eslint": "^6.4.0",
60
+ "eslint-plugin-flowtype": "^4.3.0",
61
+ "eslint-plugin-mocha": "^6.2.0",
62
+ "eslint-plugin-react": "^7.14.3",
65
63
  "exports-loader": "^0.7.0",
66
- "flow-bin": "^0.65.0",
67
- "husky": "^0.14.3",
68
- "imports-loader": "^0.7.1",
69
- "jest-cli": "^22.3.0",
70
- "lint-staged": "^6.1.0",
64
+ "flow-bin": "^0.110.1",
65
+ "husky": "^3.0.9",
66
+ "imports-loader": "^0.8.0",
67
+ "jest-cli": "^24.9.0",
68
+ "lint-staged": "^9.2.5",
71
69
  "lodash": "^4.17.5",
72
70
  "opener": "^1.4.3",
73
- "power-assert": "^1.4.4",
74
- "prettier": "^1.10.2",
75
- "react": "^16.2.0",
76
- "react-dom": "^16.2.0",
77
- "react-hot-loader": "^3.1.3",
71
+ "prettier": "^1.18.2",
72
+ "react": "^16.11.0",
73
+ "react-dom": "^16.11.0",
74
+ "react-hot-loader": "^4.12.13",
78
75
  "react-transform-hmr": "^1.0.2",
79
- "style-loader": "^0.20.2",
80
- "valiquire": "^0.3.0",
81
- "webpack": "^3.11.0",
82
- "webpack-dev-server": "^2.11.1"
76
+ "style-loader": "^1.0.0",
77
+ "webpack": "^4.41.2",
78
+ "webpack-cli": "^3.3.9",
79
+ "webpack-dev-server": "^3.9.0"
83
80
  },
84
81
  "publishConfig": {
85
82
  "registry": "https://registry.npmjs.org"
@@ -87,14 +84,11 @@
87
84
  "jest": {
88
85
  "testMatch": [
89
86
  "<rootDir>/test/spec/*.js"
90
- ]
87
+ ],
88
+ "testURL": "http://localhost"
91
89
  },
92
90
  "lint-staged": {
93
- "*.{js,jsx}": [
94
- "prettier --write",
95
- "git add"
96
- ],
97
- "*.css": [
91
+ "*.{js,jsx,css}": [
98
92
  "prettier --write",
99
93
  "git add"
100
94
  ]
package/.babelrc DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "presets": [
3
- ["es2015", {"loose": true}],
4
- "stage-1",
5
- "react"
6
- ],
7
- "env": {
8
- "test": {
9
- "plugins": ["espower"]
10
- }
11
- }
12
- }
package/.jshintignore DELETED
@@ -1 +0,0 @@
1
- node_modules
package/.jshintrc DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "node": true,
3
-
4
- "curly": true,
5
- "latedef": true,
6
- "quotmark": true,
7
- "undef": true,
8
- "unused": true,
9
- "trailing": true
10
- }