@micromag/screen-game-sort 0.4.55 → 0.4.57

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 (2) hide show
  1. package/es/index.js +68 -20
  2. package/package.json +2 -2
package/es/index.js CHANGED
@@ -8,7 +8,7 @@ import { useDrag } from '@use-gesture/react';
8
8
  import classNames from 'classnames';
9
9
  import isString from 'lodash/isString';
10
10
  import shuffle from 'lodash/shuffle';
11
- import { useMemo, useState, useRef, useEffect, useCallback } from 'react';
11
+ import { useMemo, useState, useRef, useEffect, useCallback, useLayoutEffect } from 'react';
12
12
  import { ScreenElement } from '@micromag/core/components';
13
13
  import { useScreenSize, useScreenRenderContext, useViewerContext, useViewerWebView, usePlaybackContext, usePlaybackMediaRef } from '@micromag/core/contexts';
14
14
  import { useDimensionObserver, useTrackScreenEvent } from '@micromag/core/hooks';
@@ -138,7 +138,7 @@ function GameSort(_ref) {
138
138
  _useState6 = _slicedToArray(_useState5, 2),
139
139
  validated = _useState6[0],
140
140
  setValidated = _useState6[1];
141
- var initialSortedItemsRef = useRef(sortedItems);
141
+ useRef(sortedItems);
142
142
  var _useState7 = useState(false),
143
143
  _useState8 = _slicedToArray(_useState7, 2),
144
144
  resultsVisible = _useState8[0],
@@ -169,7 +169,7 @@ function GameSort(_ref) {
169
169
  }, [sortedItems, getNewSortedItems]);
170
170
  var _useSprings = useSprings((items || []).length, function () {
171
171
  return {
172
- y: "0%",
172
+ y: 0,
173
173
  scale: 1
174
174
  };
175
175
  }, []),
@@ -211,7 +211,7 @@ function GameSort(_ref) {
211
211
  if (item === dragItem) {
212
212
  var _deltaY = dragY - currentY;
213
213
  return {
214
- y: "".concat(_deltaY / currentHeight * 100, "%"),
214
+ y: currentHeight !== 0 ? _deltaY / currentHeight * 100 : 0,
215
215
  scale: 1.02,
216
216
  immediate: true
217
217
  };
@@ -230,7 +230,7 @@ function GameSort(_ref) {
230
230
  }, 0);
231
231
  var deltaY = newY - currentY;
232
232
  return {
233
- y: "".concat(deltaY / currentHeight * 100, "%"),
233
+ y: currentHeight !== 0 ? deltaY / currentHeight * 100 : 0,
234
234
  scale: 1,
235
235
  immediate: initial
236
236
  };
@@ -239,14 +239,68 @@ function GameSort(_ref) {
239
239
  setInitialSorted(true);
240
240
  }
241
241
  }, [api]);
242
- useEffect(function () {
243
- if (!isView) {
242
+
243
+ // Before :
244
+ // useEffect(() => {
245
+ // if (!isView) {
246
+ // return;
247
+ // }
248
+
249
+ // updateSpring(sortedItems, {
250
+ // initial: initialSortedItemsRef.current === sortedItems,
251
+ // });
252
+ // }, [sortedItems, itemsHeight]);
253
+
254
+ // useLayoutEffect runs AFTER react-spring's internal layout effect which
255
+ // resets springs to {y:0, scale:1}. We api.stop() the reset, then immediately
256
+ // re-apply the correct shuffled positions before the browser paints.
257
+ var springsReady = useRef(false);
258
+ useLayoutEffect(function () {
259
+ if (!isView || itemsHeight === 0) {
244
260
  return;
245
261
  }
246
- updateSpring(sortedItems, {
247
- initial: initialSortedItemsRef.current === sortedItems
262
+ api.stop();
263
+ var refs = sortedItems.map(function (it) {
264
+ return elementsRef.current[it.id] || null;
265
+ });
266
+ var initialRefs = (items || []).map(function (it) {
267
+ return elementsRef.current[it.id] || null;
268
+ });
269
+ var initialHeights = initialRefs.map(function (it) {
270
+ var _it$getBoundingClient3;
271
+ return (it === null || it === void 0 || (_it$getBoundingClient3 = it.getBoundingClientRect()) === null || _it$getBoundingClient3 === void 0 ? void 0 : _it$getBoundingClient3.height) || 0;
272
+ });
273
+ var heights = refs.map(function (it) {
274
+ var _it$getBoundingClient4;
275
+ return (it === null || it === void 0 || (_it$getBoundingClient4 = it.getBoundingClientRect()) === null || _it$getBoundingClient4 === void 0 ? void 0 : _it$getBoundingClient4.height) || 0;
276
+ });
277
+ if (!heights.some(function (h) {
278
+ return h > 0;
279
+ })) return;
280
+ api.start(function (itemIndex) {
281
+ var item = (items || [])[itemIndex] || null;
282
+ var sortedIndex = sortedItems.findIndex(function (it) {
283
+ return it.id === item.id;
284
+ });
285
+ var currentHeight = initialHeights[itemIndex] || 0;
286
+ var currentY = initialHeights.slice(0, itemIndex).reduce(function (acc, h) {
287
+ return acc + h;
288
+ }, 0);
289
+ var newY = heights.slice(0, sortedIndex).reduce(function (acc, h) {
290
+ return acc + h;
291
+ }, 0);
292
+ var deltaY = newY - currentY;
293
+ return {
294
+ y: currentHeight !== 0 ? deltaY / currentHeight * 100 : 0,
295
+ scale: 1,
296
+ immediate: true
297
+ };
248
298
  });
249
- }, [sortedItems, itemsHeight]);
299
+ if (!springsReady.current) {
300
+ springsReady.current = true;
301
+ setInitialSorted(true);
302
+ }
303
+ });
250
304
  var bind = useDrag(function (_ref3) {
251
305
  var _ref3$args = _slicedToArray(_ref3.args, 1),
252
306
  itemIndex = _ref3$args[0],
@@ -269,18 +323,12 @@ function GameSort(_ref) {
269
323
  var sortedIndex = sortedItems.findIndex(function (it) {
270
324
  return it === item;
271
325
  });
272
-
273
- // const heights = sortedItems.map((sortedItem) => {
274
- // const elementIndex = items.findIndex((it) => it === sortedItem);
275
- // return elementsRef.current[elementIndex]?.getBoundingClientRect()?.height || 0;
276
- // });
277
-
278
326
  var refs = sortedItems.map(function (it) {
279
327
  return elementsRef.current[it.id] || null;
280
328
  });
281
329
  var heights = refs.map(function (it) {
282
- var _it$getBoundingClient3;
283
- return (it === null || it === void 0 || (_it$getBoundingClient3 = it.getBoundingClientRect()) === null || _it$getBoundingClient3 === void 0 ? void 0 : _it$getBoundingClient3.height) || 0;
330
+ var _it$getBoundingClient5;
331
+ return (it === null || it === void 0 || (_it$getBoundingClient5 = it.getBoundingClientRect()) === null || _it$getBoundingClient5 === void 0 ? void 0 : _it$getBoundingClient5.height) || 0;
284
332
  });
285
333
  var ys = heights.map(function (itemHeight, heightIndex) {
286
334
  var endY = itemHeight + heights.slice(0, heightIndex).reduce(function (acc, h) {
@@ -313,7 +361,6 @@ function GameSort(_ref) {
313
361
  if (trackingEnabled) {
314
362
  trackEvent('drag', "item_".concat(itemIndex));
315
363
  }
316
- // updateIndex(item, newIndex !== -1 ? newIndex : sortedIndex);
317
364
  }
318
365
  }, {
319
366
  axis: 'y',
@@ -413,6 +460,7 @@ function GameSort(_ref) {
413
460
  var y = _ref6.y,
414
461
  scale = _ref6.scale;
415
462
  var item = items[itemIndex] || {};
463
+ // console.log('item', item, itemIndex);
416
464
  var _ref7 = item || {},
417
465
  _ref7$id = _ref7.id,
418
466
  id = _ref7$id === void 0 ? null : _ref7$id,
@@ -441,7 +489,7 @@ function GameSort(_ref) {
441
489
  },
442
490
  style: _objectSpread(_objectSpread({
443
491
  transform: y.to(function (yValue) {
444
- return "translateY(".concat(yValue, ")");
492
+ return "translateY(".concat(yValue, "%)");
445
493
  })
446
494
  }, getStyleFromText(itemsTextStyle)), getStyleFromText(labelTextStyle))
447
495
  }, bind(itemIndex)), {}, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/screen-game-sort",
3
- "version": "0.4.55",
3
+ "version": "0.4.57",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -87,6 +87,6 @@
87
87
  "access": "public",
88
88
  "registry": "https://registry.npmjs.org/"
89
89
  },
90
- "gitHead": "9708f0016de0cf18dcd6a6aa5bc70d7f00b86d9f",
90
+ "gitHead": "fbe8859ecf20634ed5591afb432ff4869eb37352",
91
91
  "types": "es/index.d.ts"
92
92
  }