@micromag/screen-game-sort 0.4.54 → 0.4.56

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 +57 -13
  2. package/package.json +13 -13
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,57 @@ function GameSort(_ref) {
239
239
  setInitialSorted(true);
240
240
  }
241
241
  }, [api]);
242
- useEffect(function () {
243
- if (!isView) {
242
+
243
+ // useLayoutEffect runs AFTER react-spring's internal layout effect which
244
+ // resets springs to {y:0, scale:1}. We api.stop() the reset, then immediately
245
+ // re-apply the correct shuffled positions before the browser paints.
246
+ var springsReady = useRef(false);
247
+ useLayoutEffect(function () {
248
+ if (!isView || itemsHeight === 0) {
244
249
  return;
245
250
  }
246
- updateSpring(sortedItems, {
247
- initial: initialSortedItemsRef.current === sortedItems
251
+ api.stop();
252
+ var refs = sortedItems.map(function (it) {
253
+ return elementsRef.current[it.id] || null;
254
+ });
255
+ var initialRefs = (items || []).map(function (it) {
256
+ return elementsRef.current[it.id] || null;
257
+ });
258
+ var initialHeights = initialRefs.map(function (it) {
259
+ var _it$getBoundingClient3;
260
+ return (it === null || it === void 0 || (_it$getBoundingClient3 = it.getBoundingClientRect()) === null || _it$getBoundingClient3 === void 0 ? void 0 : _it$getBoundingClient3.height) || 0;
261
+ });
262
+ var heights = refs.map(function (it) {
263
+ var _it$getBoundingClient4;
264
+ return (it === null || it === void 0 || (_it$getBoundingClient4 = it.getBoundingClientRect()) === null || _it$getBoundingClient4 === void 0 ? void 0 : _it$getBoundingClient4.height) || 0;
265
+ });
266
+ if (!heights.some(function (h) {
267
+ return h > 0;
268
+ })) return;
269
+ api.start(function (itemIndex) {
270
+ var item = (items || [])[itemIndex] || null;
271
+ var sortedIndex = sortedItems.findIndex(function (it) {
272
+ return it.id === item.id;
273
+ });
274
+ var currentHeight = initialHeights[itemIndex] || 0;
275
+ var currentY = initialHeights.slice(0, itemIndex).reduce(function (acc, h) {
276
+ return acc + h;
277
+ }, 0);
278
+ var newY = heights.slice(0, sortedIndex).reduce(function (acc, h) {
279
+ return acc + h;
280
+ }, 0);
281
+ var deltaY = newY - currentY;
282
+ return {
283
+ y: currentHeight !== 0 ? deltaY / currentHeight * 100 : 0,
284
+ scale: 1,
285
+ immediate: true
286
+ };
248
287
  });
249
- }, [sortedItems, itemsHeight]);
288
+ if (!springsReady.current) {
289
+ springsReady.current = true;
290
+ setInitialSorted(true);
291
+ }
292
+ });
250
293
  var bind = useDrag(function (_ref3) {
251
294
  var _ref3$args = _slicedToArray(_ref3.args, 1),
252
295
  itemIndex = _ref3$args[0],
@@ -279,8 +322,8 @@ function GameSort(_ref) {
279
322
  return elementsRef.current[it.id] || null;
280
323
  });
281
324
  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;
325
+ var _it$getBoundingClient5;
326
+ return (it === null || it === void 0 || (_it$getBoundingClient5 = it.getBoundingClientRect()) === null || _it$getBoundingClient5 === void 0 ? void 0 : _it$getBoundingClient5.height) || 0;
284
327
  });
285
328
  var ys = heights.map(function (itemHeight, heightIndex) {
286
329
  var endY = itemHeight + heights.slice(0, heightIndex).reduce(function (acc, h) {
@@ -413,6 +456,7 @@ function GameSort(_ref) {
413
456
  var y = _ref6.y,
414
457
  scale = _ref6.scale;
415
458
  var item = items[itemIndex] || {};
459
+ // console.log('item', item, itemIndex);
416
460
  var _ref7 = item || {},
417
461
  _ref7$id = _ref7.id,
418
462
  id = _ref7$id === void 0 ? null : _ref7$id,
@@ -441,7 +485,7 @@ function GameSort(_ref) {
441
485
  },
442
486
  style: _objectSpread(_objectSpread({
443
487
  transform: y.to(function (yValue) {
444
- return "translateY(".concat(yValue, ")");
488
+ return "translateY(".concat(yValue, "%)");
445
489
  })
446
490
  }, getStyleFromText(itemsTextStyle)), getStyleFromText(labelTextStyle))
447
491
  }, bind(itemIndex)), {}, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/screen-game-sort",
3
- "version": "0.4.54",
3
+ "version": "0.4.56",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -65,17 +65,17 @@
65
65
  },
66
66
  "dependencies": {
67
67
  "@babel/runtime": "^7.28.6",
68
- "@micromag/core": "^0.4.54",
69
- "@micromag/element-background": "^0.4.54",
70
- "@micromag/element-button": "^0.4.54",
71
- "@micromag/element-container": "^0.4.54",
72
- "@micromag/element-footer": "^0.4.54",
73
- "@micromag/element-header": "^0.4.54",
74
- "@micromag/element-heading": "^0.4.54",
75
- "@micromag/element-layout": "^0.4.54",
76
- "@micromag/element-text": "^0.4.54",
77
- "@micromag/element-visual": "^0.4.54",
78
- "@micromag/transforms": "^0.4.54",
68
+ "@micromag/core": "^0.4.55",
69
+ "@micromag/element-background": "^0.4.55",
70
+ "@micromag/element-button": "^0.4.55",
71
+ "@micromag/element-container": "^0.4.55",
72
+ "@micromag/element-footer": "^0.4.55",
73
+ "@micromag/element-header": "^0.4.55",
74
+ "@micromag/element-heading": "^0.4.55",
75
+ "@micromag/element-layout": "^0.4.55",
76
+ "@micromag/element-text": "^0.4.55",
77
+ "@micromag/element-visual": "^0.4.55",
78
+ "@micromag/transforms": "^0.4.55",
79
79
  "@react-spring/web": "^10.0.3",
80
80
  "@use-gesture/react": "^10.3.0",
81
81
  "classnames": "^2.2.6",
@@ -87,6 +87,6 @@
87
87
  "access": "public",
88
88
  "registry": "https://registry.npmjs.org/"
89
89
  },
90
- "gitHead": "b7aacdebb4a5c2179a58d21ab2aaea5bcdc43d69",
90
+ "gitHead": "8d1c7dca724abbc01da7cebbe6913c77ee27f0b7",
91
91
  "types": "es/index.d.ts"
92
92
  }