@lynx-js/lynx-ui-sortable 3.139.0 → 3.140.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.
package/src/Sortable.tsx CHANGED
@@ -3,6 +3,7 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  import {
6
+ memo,
6
7
  runOnMainThread,
7
8
  useCallback,
8
9
  useContext,
@@ -11,7 +12,7 @@ import {
11
12
  useMemo,
12
13
  useState,
13
14
  } from '@lynx-js/react'
14
- import type { RefObject } from '@lynx-js/react'
15
+ import type { ReactNode, RefObject } from '@lynx-js/react'
15
16
 
16
17
  import type { Point } from '@lynx-js/lynx-ui-common'
17
18
  import { delayFrames, usePreCommit } from '@lynx-js/lynx-ui-common'
@@ -23,7 +24,9 @@ import {
23
24
  import type { DraggableRef } from '@lynx-js/lynx-ui-draggable'
24
25
  import type { MainThread, ScrollEvent } from '@lynx-js/types'
25
26
 
26
- import { SortableContext } from './SortableContext'
27
+ import { commitSortableOrder } from './commitSortableOrder'
28
+ import { SortableContext, SortableOrderContext } from './SortableContext'
29
+ import type { SortableContextType, SortableRect } from './SortableContext'
27
30
  import type {
28
31
  SortableData,
29
32
  SortableItemProps,
@@ -80,6 +83,41 @@ function getAutoScrollTriggerOverflow(
80
83
  const AUTO_SCROLL_MAX_STEP = 18
81
84
  const AUTO_SCROLL_VELOCITY_FACTOR = 0.35
82
85
 
86
+ interface SortableItemsProviderProps {
87
+ children: ReactNode
88
+ value: SortableContextType
89
+ }
90
+
91
+ const SortableItemsProvider = memo(function SortableItemsProvider(
92
+ props: SortableItemsProviderProps,
93
+ ) {
94
+ return (
95
+ <SortableContext.Provider value={props.value}>
96
+ {props.children}
97
+ </SortableContext.Provider>
98
+ )
99
+ })
100
+
101
+ // A reorder preserves the item and renderer identities, so the entire gesture
102
+ // subtree can be reused. Content/configuration changes still render normally.
103
+ function RenderSortableItem<T>(props: {
104
+ item: SortableData<T>
105
+ renderItem: (item: SortableData<T>) => ReactNode
106
+ }) {
107
+ return props.renderItem(props.item)
108
+ }
109
+ const SortableRenderedItem = memo(
110
+ RenderSortableItem,
111
+ ) as typeof RenderSortableItem
112
+
113
+ function SortableItemGeometryObserver(props: { refresh: () => void }) {
114
+ const orderSignature = useContext(SortableOrderContext)
115
+ useEffect(() => {
116
+ delayFrames(1, props.refresh)
117
+ }, [orderSignature, props.refresh])
118
+ return null
119
+ }
120
+
83
121
  function getNormalizedElementKey(sortingKey: string) {
84
122
  return String(sortingKey).replace(/[^\w-]/g, '-')
85
123
  }
@@ -110,14 +148,15 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
110
148
  enableSorting = true,
111
149
  } = props
112
150
  const scrollable = as === 'ScrollView'
113
- const [isDragging, setIsDragging] = useState(false)
114
- const handleInternalSortStart = useCallback(() => {
115
- setIsDragging(true)
151
+ const [activeDragKey, setActiveDragKey] = useState<string | null>(null)
152
+ const isDragging = activeDragKey !== null
153
+ const handleInternalSortStart = useCallback((sortingKey: string) => {
154
+ setActiveDragKey(sortingKey)
116
155
  onSortStart?.()
117
156
  }, [onSortStart])
118
157
  const handleInternalSortEnd = useCallback(
119
158
  (sortedData: SortableData<T>[]) => {
120
- setIsDragging(false)
159
+ setActiveDragKey(null)
121
160
  onSortEnd(sortedData)
122
161
  },
123
162
  [onSortEnd],
@@ -146,10 +185,36 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
146
185
  >(
147
186
  {},
148
187
  )
188
+ const dragOverlayActivatorRefMap = useMainThreadRef<
189
+ Record<string, (() => void) | null>
190
+ >(
191
+ {},
192
+ )
149
193
  const dirtyKeysRef = useMainThreadRef<Record<string, boolean>>({})
194
+ const resetItemVisualsRef = useMainThreadRef<
195
+ Record<string, (() => void) | undefined>
196
+ >({})
197
+ const keyArray = useMemo(
198
+ () => data?.map(item => item.getSortingKey()) ?? [],
199
+ [data],
200
+ )
201
+ const dataKeySignature = JSON.stringify(keyArray)
202
+ const orderRef = useMainThreadRef(keyArray)
203
+ usePreCommit(() => {
204
+ 'main thread'
205
+ commitSortableOrder(
206
+ orderRef,
207
+ keyArray,
208
+ dirtyKeysRef,
209
+ resetItemVisualsRef,
210
+ activeDragKey === null,
211
+ )
212
+ }, [dataKeySignature, activeDragKey])
150
213
  const disabledKeysRef = useMainThreadRef<Record<string, boolean>>({})
151
214
  const scrollableBoundaryUpperEdgeRef = useMainThreadRef(false)
152
215
  const scrollableBoundaryLowerEdgeRef = useMainThreadRef(false)
216
+ const scrollableBoundaryRectRef = useMainThreadRef<SortableRect | null>(null)
217
+ const scrollableBoundaryMeasurementVersion = useMainThreadRef(0)
153
218
  const scrollableScrollTopRef = useMainThreadRef(0)
154
219
  const updateItemSize = useCallback((sortingKey: string, size: number) => {
155
220
  'main thread'
@@ -181,6 +246,56 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
181
246
  scrollableBoundaryLowerEdgeRef.current = false
182
247
  }, [scrollableBoundaryLowerEdgeRef])
183
248
 
249
+ const measureScrollableBoundary = useCallback(() => {
250
+ 'main thread'
251
+ if (!scrollable) {
252
+ return
253
+ }
254
+
255
+ const measurementVersion = scrollableBoundaryMeasurementVersion.current + 1
256
+ scrollableBoundaryMeasurementVersion.current = measurementVersion
257
+ const measurement = lynx.querySelector(`#${scrollableElementId}`)
258
+ ?.invoke('boundingClientRect', {})
259
+ void measurement?.then((value) => {
260
+ 'main thread'
261
+ if (
262
+ scrollableBoundaryMeasurementVersion.current !== measurementVersion
263
+ ) {
264
+ return
265
+ }
266
+ scrollableBoundaryRectRef.current = value as SortableRect
267
+ })
268
+ }, [
269
+ scrollable,
270
+ scrollableBoundaryMeasurementVersion,
271
+ scrollableBoundaryRectRef,
272
+ scrollableElementId,
273
+ ])
274
+
275
+ const handleScrollableBoundaryLayoutChange = useCallback(() => {
276
+ 'main thread'
277
+ measureScrollableBoundary()
278
+ }, [measureScrollableBoundary])
279
+
280
+ const invalidateScrollableBoundaryMeasurement = useCallback(() => {
281
+ 'main thread'
282
+ scrollableBoundaryMeasurementVersion.current += 1
283
+ scrollableBoundaryRectRef.current = null
284
+ }, [scrollableBoundaryMeasurementVersion, scrollableBoundaryRectRef])
285
+
286
+ useEffect(() => {
287
+ if (scrollable) {
288
+ runOnMainThread(measureScrollableBoundary)()
289
+ }
290
+ return () => {
291
+ runOnMainThread(invalidateScrollableBoundaryMeasurement)()
292
+ }
293
+ }, [
294
+ invalidateScrollableBoundaryMeasurement,
295
+ measureScrollableBoundary,
296
+ scrollable,
297
+ ])
298
+
184
299
  const setChildrenRef = useCallback(
185
300
  (refI: RefObject<DraggableRef>, key: string) => {
186
301
  'main thread'
@@ -202,11 +317,21 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
202
317
  key: string,
203
318
  ) => {
204
319
  'main thread'
205
- dragOverlayRefMap.current[key] = refI.current
320
+ const overlay = refI.current
321
+ dragOverlayRefMap.current[key] = overlay
322
+ if (overlay) {
323
+ dragOverlayActivatorRefMap.current[key]?.()
324
+ }
325
+ }, [dragOverlayActivatorRefMap, dragOverlayRefMap])
326
+
327
+ const clearDragOverlayRef = useCallback((key: string) => {
328
+ 'main thread'
329
+ delete dragOverlayRefMap.current[key]
206
330
  }, [dragOverlayRefMap])
207
331
 
208
332
  const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({
209
333
  data: data,
334
+ orderRef,
210
335
  sizeMap: sizeMap,
211
336
  itemRefMap: childrenRefMap,
212
337
  itemMTSRefMap: childrenMTSRefMap,
@@ -217,9 +342,9 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
217
342
  debugLog,
218
343
  })
219
344
  const sortableContextValue = useMemo(() => ({
220
- data,
221
345
  isDragOverlay: false,
222
346
  debugLog,
347
+ resetItemVisualsRef,
223
348
  enableSorting,
224
349
  boundaryId: scrollable ? scrollableContentId : boundaryId,
225
350
  scrollableBoundaryId: scrollable
@@ -231,10 +356,16 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
231
356
  scrollableBoundaryLowerEdgeRef: scrollable
232
357
  ? scrollableBoundaryLowerEdgeRef
233
358
  : undefined,
359
+ scrollableBoundaryRectRef: scrollable
360
+ ? scrollableBoundaryRectRef
361
+ : undefined,
234
362
  scrollableScrollTopRef: scrollable
235
363
  ? scrollableScrollTopRef
236
364
  : undefined,
237
365
  dragOverlayRefMap: scrollable ? dragOverlayRefMap : undefined,
366
+ dragOverlayActivatorRefMap: scrollable
367
+ ? dragOverlayActivatorRefMap
368
+ : undefined,
238
369
  dirtyKeysRef,
239
370
  disabledKeysRef,
240
371
  scrollableStickyUpperOffset,
@@ -243,12 +374,13 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
243
374
  setChildrenRef,
244
375
  setChildrenMTSRef,
245
376
  setDragOverlayRef,
377
+ clearDragOverlayRef,
246
378
  handleDragEnd,
247
379
  handleDragMove,
248
380
  handleDragStart,
249
381
  }), [
250
- data,
251
382
  debugLog,
383
+ resetItemVisualsRef,
252
384
  enableSorting,
253
385
  boundaryId,
254
386
  scrollable,
@@ -256,9 +388,11 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
256
388
  scrollableElementId,
257
389
  scrollableBoundaryId,
258
390
  scrollableBoundaryLowerEdgeRef,
391
+ scrollableBoundaryRectRef,
259
392
  scrollableBoundaryUpperEdgeRef,
260
393
  scrollableScrollTopRef,
261
394
  dragOverlayRefMap,
395
+ dragOverlayActivatorRefMap,
262
396
  dirtyKeysRef,
263
397
  disabledKeysRef,
264
398
  scrollableStickyLowerOffset,
@@ -267,6 +401,7 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
267
401
  setChildrenRef,
268
402
  setChildrenMTSRef,
269
403
  setDragOverlayRef,
404
+ clearDragOverlayRef,
270
405
  handleDragEnd,
271
406
  handleDragMove,
272
407
  handleDragStart,
@@ -278,14 +413,27 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
278
413
  }), [sortableContextValue])
279
414
 
280
415
  const renderedChildren = useMemo(
281
- () => data?.map(item => children(item)),
416
+ () =>
417
+ data?.map(item => (
418
+ <SortableRenderedItem
419
+ key={item.getSortingKey()}
420
+ item={item}
421
+ renderItem={children}
422
+ />
423
+ )),
282
424
  [data, children],
283
425
  )
284
426
 
285
- const renderedDragOverlayChildren = useMemo(
286
- () => data?.map(item => children(item)),
287
- [data, children],
288
- )
427
+ const renderedDragOverlayChild = useMemo(() => {
428
+ if (activeDragKey === null) {
429
+ return null
430
+ }
431
+
432
+ const activeItem = data?.find(
433
+ item => item.getSortingKey() === activeDragKey,
434
+ )
435
+ return activeItem ? children(activeItem) : null
436
+ }, [activeDragKey, children, data])
289
437
 
290
438
  if (scrollable) {
291
439
  return (
@@ -295,6 +443,7 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
295
443
  className={scrollableClassName}
296
444
  enable-scroll={scrollableEnableScroll && !isDragging}
297
445
  scroll-orientation='vertical'
446
+ main-thread:bindlayoutchange={handleScrollableBoundaryLayoutChange}
298
447
  main-thread:bindscroll={handleScrollableBoundaryScroll}
299
448
  >
300
449
  <view
@@ -310,11 +459,11 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
310
459
  main-thread:binduiappear={handleScrollableBoundaryUpperExposure}
311
460
  main-thread:binduidisappear={handleScrollableBoundaryUpperDisexposure}
312
461
  />
313
- <SortableContext.Provider
462
+ <SortableItemsProvider
314
463
  value={sortableContextValue}
315
464
  >
316
465
  {renderedChildren}
317
- </SortableContext.Provider>
466
+ </SortableItemsProvider>
318
467
  <view
319
468
  style='display: flex; flex-direction: column; overflow:hidden; height: 1ppx; width: 100%;'
320
469
  exposure-scene={scrollableElementId}
@@ -328,30 +477,21 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
328
477
  <SortableContext.Provider
329
478
  value={sortableDragOverlayContextValue}
330
479
  >
331
- {renderedDragOverlayChildren}
480
+ {renderedDragOverlayChild}
332
481
  </SortableContext.Provider>
333
482
  </>
334
483
  )
335
484
  }
336
485
 
337
486
  return (
338
- <SortableContext.Provider
339
- value={sortableContextValue}
340
- >
341
- {renderedChildren}
342
- </SortableContext.Provider>
487
+ <SortableOrderContext.Provider value={dataKeySignature}>
488
+ <SortableItemsProvider value={sortableContextValue}>
489
+ {renderedChildren}
490
+ </SortableItemsProvider>
491
+ </SortableOrderContext.Provider>
343
492
  )
344
493
  }
345
494
 
346
- interface boundingClientRectRes {
347
- height: number
348
- width: number
349
- top: number
350
- left: number
351
- bottom: number
352
- right: number
353
- }
354
-
355
495
  export function SortableItem(props: SortableItemProps) {
356
496
  const { isDragOverlay } = useContext(SortableContext)
357
497
  if (isDragOverlay) {
@@ -363,7 +503,7 @@ export function SortableItem(props: SortableItemProps) {
363
503
 
364
504
  function SortableDragOverlayItem(props: SortableItemProps) {
365
505
  const { className, children, sortingKey } = props
366
- const { setDragOverlayRef } = useContext(SortableContext)
506
+ const { clearDragOverlayRef, setDragOverlayRef } = useContext(SortableContext)
367
507
  const overlayRef = useMainThreadRef<MainThread.Element | null>(null)
368
508
  const overlayElementId = useMemo(
369
509
  () => getSortableDragOverlayElementId(sortingKey),
@@ -383,7 +523,10 @@ function SortableDragOverlayItem(props: SortableItemProps) {
383
523
 
384
524
  useEffect(() => {
385
525
  runOnMainThread(setDragOverlayRef)(overlayRef, sortingKey)
386
- }, [overlayRef, setDragOverlayRef, sortingKey])
526
+ return () => {
527
+ runOnMainThread(clearDragOverlayRef)(sortingKey)
528
+ }
529
+ }, [clearDragOverlayRef, overlayRef, setDragOverlayRef, sortingKey])
387
530
 
388
531
  return (
389
532
  <view
@@ -407,20 +550,21 @@ function SortableInteractiveItem(props: SortableItemProps) {
407
550
  disabled = false,
408
551
  } = props
409
552
  const {
410
- data,
553
+ resetItemVisualsRef,
411
554
  enableSorting,
412
555
  boundaryId,
413
556
  scrollableBoundaryId,
414
557
  scrollableBoundaryUpperEdgeRef,
415
558
  scrollableBoundaryLowerEdgeRef,
559
+ scrollableBoundaryRectRef,
416
560
  scrollableScrollTopRef,
417
561
  dragOverlayRefMap,
562
+ dragOverlayActivatorRefMap,
418
563
  dirtyKeysRef,
419
564
  disabledKeysRef,
420
565
  scrollableStickyUpperOffset,
421
566
  scrollableStickyLowerOffset,
422
567
  updateItemSize,
423
- setChildrenRef,
424
568
  setChildrenMTSRef,
425
569
  handleDragStart,
426
570
  handleDragEnd,
@@ -428,7 +572,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
428
572
  } = useContext(SortableContext)
429
573
 
430
574
  const MTSRef = useMainThreadRef<DraggableRef>(null)
431
- const [itemRect, setItemRect] = useState<boundingClientRectRes>({
575
+ const [itemRect, setItemRect] = useState<SortableRect>({
432
576
  height: 0,
433
577
  width: 0,
434
578
  top: 0,
@@ -436,7 +580,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
436
580
  bottom: 0,
437
581
  right: 0,
438
582
  })
439
- const [boundaryRect, setBoundaryRect] = useState<boundingClientRectRes>({
583
+ const [boundaryRect, setBoundaryRect] = useState<SortableRect>({
440
584
  height: 0,
441
585
  width: 0,
442
586
  top: 0,
@@ -445,7 +589,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
445
589
  right: 0,
446
590
  })
447
591
  const [scrollableBoundaryRect, setScrollableBoundaryRect] = useState<
448
- boundingClientRectRes
592
+ SortableRect
449
593
  >({
450
594
  height: 0,
451
595
  width: 0,
@@ -465,6 +609,8 @@ function SortableInteractiveItem(props: SortableItemProps) {
465
609
  const autoScrollDirection = useMainThreadRef(0)
466
610
  const autoScrollStickyDirection = useMainThreadRef(0)
467
611
  const dragSourceHidden = useMainThreadRef(false)
612
+ const activeItemRectRef = useMainThreadRef<SortableRect | null>(null)
613
+ const activeRectMeasurementVersion = useMainThreadRef(0)
468
614
  const latestDragTranslate = useMainThreadRef<Point>({ x: 0, y: 0 })
469
615
  const latestDragEvent = useMainThreadRef<
470
616
  MainThread.MouseEvent | MainThread.TouchEvent | null
@@ -541,7 +687,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
541
687
  method: 'boundingClientRect',
542
688
  params: {},
543
689
  success: (res: unknown) => {
544
- const rect = res as boundingClientRectRes
690
+ const rect = res as SortableRect
545
691
  if (target === 'item') {
546
692
  setItemRect(rect)
547
693
  runOnMainThread(captureItemMeasuredScrollTop)()
@@ -558,6 +704,9 @@ function SortableInteractiveItem(props: SortableItemProps) {
558
704
  }, [captureItemMeasuredScrollTop])
559
705
 
560
706
  const refreshDraggingRects = useCallback(() => {
707
+ if (scrollableBoundaryRectRef) {
708
+ return
709
+ }
561
710
  measureRectById(itemElementId, 'item')
562
711
  measureRectById(boundaryId, 'boundary')
563
712
  measureRectById(scrollableBoundaryId, 'scrollableBoundary')
@@ -566,6 +715,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
566
715
  itemElementId,
567
716
  measureRectById,
568
717
  scrollableBoundaryId,
718
+ scrollableBoundaryRectRef,
569
719
  ])
570
720
 
571
721
  const scrollScrollableBoundaryBy = useCallback((
@@ -612,18 +762,37 @@ function SortableInteractiveItem(props: SortableItemProps) {
612
762
  return false
613
763
  }, [scrollableBoundaryLowerEdgeRef, scrollableBoundaryUpperEdgeRef])
614
764
 
765
+ const getCurrentItemRect = useCallback(() => {
766
+ 'main thread'
767
+ return scrollableBoundaryRectRef
768
+ ? activeItemRectRef.current
769
+ : itemRect
770
+ }, [activeItemRectRef, itemRect, scrollableBoundaryRectRef])
771
+
772
+ const getCurrentScrollableBoundaryRect = useCallback(() => {
773
+ 'main thread'
774
+ return scrollableBoundaryRectRef
775
+ ? scrollableBoundaryRectRef.current
776
+ : scrollableBoundaryRect
777
+ }, [scrollableBoundaryRect, scrollableBoundaryRectRef])
778
+
615
779
  const getDragStartItemBounds = useCallback((translateY: number) => {
616
780
  'main thread'
617
- const measuredTopAtDragStart = itemRect.top
781
+ const currentItemRect = getCurrentItemRect()
782
+ if (!currentItemRect) {
783
+ return null
784
+ }
785
+
786
+ const measuredTopAtDragStart = currentItemRect.top
618
787
  - dragStartScrollDeltaFromMeasuredRect.current
619
- const measuredBottomAtDragStart = itemRect.bottom
788
+ const measuredBottomAtDragStart = currentItemRect.bottom
620
789
  - dragStartScrollDeltaFromMeasuredRect.current
621
790
 
622
791
  return {
623
792
  top: measuredTopAtDragStart + translateY,
624
793
  bottom: measuredBottomAtDragStart + translateY,
625
794
  }
626
- }, [dragStartScrollDeltaFromMeasuredRect, itemRect])
795
+ }, [dragStartScrollDeltaFromMeasuredRect, getCurrentItemRect])
627
796
 
628
797
  const getAutoScrollOverflow = useCallback((
629
798
  distanceToTop: number,
@@ -653,24 +822,34 @@ function SortableInteractiveItem(props: SortableItemProps) {
653
822
  const getVisualTranslateY = useCallback(() => {
654
823
  'main thread'
655
824
  const scrollDelta = syncScrollDeltaForItemTranslateY()
656
- if (autoScrollStickyDirection.current > 0) {
657
- return scrollableBoundaryRect.bottom
658
- - itemRect.bottom
825
+ const currentItemRect = getCurrentItemRect()
826
+ const currentScrollableBoundaryRect = getCurrentScrollableBoundaryRect()
827
+ if (
828
+ autoScrollStickyDirection.current > 0
829
+ && currentItemRect
830
+ && currentScrollableBoundaryRect
831
+ ) {
832
+ return currentScrollableBoundaryRect.bottom
833
+ - currentItemRect.bottom
659
834
  - scrollableStickyLowerOffset
660
835
  + scrollDelta.measuredRectDeltaY
661
836
  }
662
- if (autoScrollStickyDirection.current < 0) {
663
- return scrollableBoundaryRect.top
664
- - itemRect.top
837
+ if (
838
+ autoScrollStickyDirection.current < 0
839
+ && currentItemRect
840
+ && currentScrollableBoundaryRect
841
+ ) {
842
+ return currentScrollableBoundaryRect.top
843
+ - currentItemRect.top
665
844
  + scrollableStickyUpperOffset
666
845
  + scrollDelta.measuredRectDeltaY
667
846
  }
668
847
  return latestDragTranslate.current.y + scrollDelta.dragDeltaY
669
848
  }, [
670
849
  autoScrollStickyDirection,
671
- itemRect,
850
+ getCurrentItemRect,
851
+ getCurrentScrollableBoundaryRect,
672
852
  latestDragTranslate,
673
- scrollableBoundaryRect,
674
853
  scrollableStickyLowerOffset,
675
854
  scrollableStickyUpperOffset,
676
855
  syncScrollDeltaForItemTranslateY,
@@ -678,16 +857,21 @@ function SortableInteractiveItem(props: SortableItemProps) {
678
857
 
679
858
  const getVisualBounds = useCallback((visualTranslateY: number) => {
680
859
  'main thread'
860
+ const currentItemRect = getCurrentItemRect()
861
+ if (!currentItemRect) {
862
+ return null
863
+ }
864
+
681
865
  const scrollDelta = syncScrollDeltaForItemTranslateY()
682
866
  return {
683
- top: itemRect.top - scrollDelta.measuredRectDeltaY
867
+ top: currentItemRect.top - scrollDelta.measuredRectDeltaY
684
868
  + visualTranslateY,
685
- left: itemRect.left + latestDragTranslate.current.x,
686
- width: itemRect.width,
687
- height: itemRect.height,
869
+ left: currentItemRect.left + latestDragTranslate.current.x,
870
+ width: currentItemRect.width,
871
+ height: currentItemRect.height,
688
872
  }
689
873
  }, [
690
- itemRect,
874
+ getCurrentItemRect,
691
875
  latestDragTranslate,
692
876
  syncScrollDeltaForItemTranslateY,
693
877
  ])
@@ -741,6 +925,9 @@ function SortableInteractiveItem(props: SortableItemProps) {
741
925
  }
742
926
 
743
927
  const bounds = getVisualBounds(visualTranslateY)
928
+ if (!bounds) {
929
+ return
930
+ }
744
931
  overlay.setStyleProperty(
745
932
  'transform',
746
933
  `translate(${bounds.left}px, ${bounds.top}px)`,
@@ -750,14 +937,12 @@ function SortableInteractiveItem(props: SortableItemProps) {
750
937
  getVisualBounds,
751
938
  ])
752
939
 
753
- const activateDragOverlay = useCallback(() => {
940
+ const tryActivateDragOverlay = useCallback(() => {
754
941
  'main thread'
755
- if (!scrollableBoundaryId) {
942
+ if (!scrollableBoundaryId || latestDragEvent.current === null) {
756
943
  return
757
944
  }
758
945
 
759
- hideDragSourceItem()
760
-
761
946
  const overlay = getDragOverlayElement()
762
947
  if (!overlay) {
763
948
  return
@@ -765,6 +950,12 @@ function SortableInteractiveItem(props: SortableItemProps) {
765
950
 
766
951
  const visualTranslateY = getVisualTranslateY()
767
952
  const bounds = getVisualBounds(visualTranslateY)
953
+ if (!bounds) {
954
+ return
955
+ }
956
+
957
+ MTSRef.current?.MTSSetTransform(0, 0)
958
+ hideDragSourceItem()
768
959
  overlay.setStyleProperties({
769
960
  position: 'fixed',
770
961
  top: '0px',
@@ -777,20 +968,90 @@ function SortableInteractiveItem(props: SortableItemProps) {
777
968
  transform: `translate(${bounds.left}px, ${bounds.top}px)`,
778
969
  })
779
970
  }, [
971
+ MTSRef,
780
972
  getDragOverlayElement,
781
973
  getVisualBounds,
782
974
  getVisualTranslateY,
783
975
  hideDragSourceItem,
976
+ latestDragEvent,
784
977
  scrollableBoundaryId,
785
978
  ])
786
979
 
980
+ const measureActiveItemAndTryOverlay = useCallback(() => {
981
+ 'main thread'
982
+ if (!scrollableBoundaryRectRef || latestDragEvent.current === null) {
983
+ return
984
+ }
985
+
986
+ const measurementVersion = activeRectMeasurementVersion.current + 1
987
+ activeRectMeasurementVersion.current = measurementVersion
988
+ activeItemRectRef.current = null
989
+ const measurement = lynx.querySelector(`#${itemElementId}`)
990
+ ?.invoke('boundingClientRect', {})
991
+ void measurement?.then((value) => {
992
+ 'main thread'
993
+ if (
994
+ activeRectMeasurementVersion.current !== measurementVersion
995
+ || latestDragEvent.current === null
996
+ ) {
997
+ return
998
+ }
999
+
1000
+ activeItemRectRef.current = value as SortableRect
1001
+ itemMeasuredScrollTop.current = scrollableScrollTopRef?.current ?? 0
1002
+
1003
+ const currentScrollableBoundaryRect = scrollableBoundaryRectRef.current
1004
+ const draggedItem = getDragStartItemBounds(
1005
+ latestDragTranslate.current.y,
1006
+ )
1007
+ if (
1008
+ currentScrollableBoundaryRect
1009
+ && currentScrollableBoundaryRect.height > 0
1010
+ && draggedItem
1011
+ ) {
1012
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(
1013
+ draggedItem,
1014
+ currentScrollableBoundaryRect,
1015
+ )
1016
+ const overflow = getAutoScrollOverflow(
1017
+ distanceToTop,
1018
+ distanceToBottom,
1019
+ )
1020
+ if (overflow !== 0) {
1021
+ autoScrollStickyDirection.current = overflow > 0 ? 1 : -1
1022
+ }
1023
+ }
1024
+
1025
+ tryActivateDragOverlay()
1026
+ })
1027
+ }, [
1028
+ activeItemRectRef,
1029
+ activeRectMeasurementVersion,
1030
+ autoScrollStickyDirection,
1031
+ getAutoScrollOverflow,
1032
+ getDragStartItemBounds,
1033
+ itemElementId,
1034
+ itemMeasuredScrollTop,
1035
+ latestDragEvent,
1036
+ latestDragTranslate,
1037
+ scrollableBoundaryRectRef,
1038
+ scrollableScrollTopRef,
1039
+ tryActivateDragOverlay,
1040
+ ])
1041
+
787
1042
  const applyVisualTranslate = useCallback(() => {
788
1043
  'main thread'
789
1044
  const visualTranslateY = getVisualTranslateY()
790
1045
  if (scrollableBoundaryId) {
791
- MTSRef.current?.MTSSetTransform(0, 0)
792
- console.info('reset local drag visual')
793
- updateDragOverlayTransform(visualTranslateY)
1046
+ if (dragSourceHidden.current && getDragOverlayElement()) {
1047
+ MTSRef.current?.MTSSetTransform(0, 0)
1048
+ updateDragOverlayTransform(visualTranslateY)
1049
+ } else {
1050
+ MTSRef.current?.MTSSetTransform(
1051
+ latestDragTranslate.current.x,
1052
+ visualTranslateY,
1053
+ )
1054
+ }
794
1055
  } else {
795
1056
  MTSRef.current?.MTSSetTransform(
796
1057
  latestDragTranslate.current.x,
@@ -801,6 +1062,8 @@ function SortableInteractiveItem(props: SortableItemProps) {
801
1062
  return visualTranslateY
802
1063
  }, [
803
1064
  MTSRef,
1065
+ dragSourceHidden,
1066
+ getDragOverlayElement,
804
1067
  getVisualTranslateY,
805
1068
  latestDragTranslate,
806
1069
  scrollableBoundaryId,
@@ -835,12 +1098,16 @@ function SortableInteractiveItem(props: SortableItemProps) {
835
1098
 
836
1099
  const finishAutoScrollFrame = useCallback(() => {
837
1100
  'main thread'
1101
+ if (!autoScrollingRef.current || latestDragEvent.current === null) {
1102
+ return
1103
+ }
838
1104
  const visualTranslateY = applyVisualTranslate()
839
1105
 
840
1106
  emitSortableDragMove(visualTranslateY, latestDragEvent.current)
841
1107
  scheduleNextAutoScrollFrame()
842
1108
  }, [
843
1109
  applyVisualTranslate,
1110
+ autoScrollingRef,
844
1111
  emitSortableDragMove,
845
1112
  latestDragEvent,
846
1113
  scheduleNextAutoScrollFrame,
@@ -917,13 +1184,31 @@ function SortableInteractiveItem(props: SortableItemProps) {
917
1184
  setAutoScrolling,
918
1185
  ])
919
1186
 
1187
+ const invalidateActiveItemMeasurement = useCallback(() => {
1188
+ 'main thread'
1189
+ activeRectMeasurementVersion.current += 1
1190
+ activeItemRectRef.current = null
1191
+ latestDragEvent.current = null
1192
+ if (dragOverlayActivatorRefMap) {
1193
+ dragOverlayActivatorRefMap.current[sortingKey] = null
1194
+ }
1195
+ }, [
1196
+ activeItemRectRef,
1197
+ activeRectMeasurementVersion,
1198
+ dragOverlayActivatorRefMap,
1199
+ latestDragEvent,
1200
+ sortingKey,
1201
+ ])
1202
+
920
1203
  useEffect(() => {
921
1204
  runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey)
922
- delayFrames(1, refreshDraggingRects)
1205
+ return () => {
1206
+ runOnMainThread(invalidateActiveItemMeasurement)()
1207
+ }
923
1208
  }, [
924
- data,
1209
+ invalidateActiveItemMeasurement,
925
1210
  refreshDraggingRects,
926
- setChildrenRef,
1211
+ scrollableBoundaryRectRef,
927
1212
  setChildrenMTSRef,
928
1213
  sortingKey,
929
1214
  ])
@@ -940,17 +1225,13 @@ function SortableInteractiveItem(props: SortableItemProps) {
940
1225
  [disabledKeysRef],
941
1226
  )
942
1227
 
943
- useEffect(() => {
944
- runOnMainThread(syncDisabledKey)(sortingKey, disabled)
945
- return () => {
946
- runOnMainThread(syncDisabledKey)(sortingKey, false)
947
- }
948
- }, [disabled, sortingKey, syncDisabledKey])
949
-
950
- const handleMTSLayoutChange = (e: MainThread.LayoutChangeEvent) => {
951
- 'main thread'
952
- updateItemSize(sortingKey, e.detail.height)
953
- }
1228
+ const handleMTSLayoutChange = useCallback(
1229
+ (e: MainThread.LayoutChangeEvent) => {
1230
+ 'main thread'
1231
+ updateItemSize(sortingKey, e.detail.height)
1232
+ },
1233
+ [sortingKey, updateItemSize],
1234
+ )
954
1235
 
955
1236
  const resetAutoScrollDragState = useCallback(() => {
956
1237
  'main thread'
@@ -971,7 +1252,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
971
1252
  latestDragTranslate,
972
1253
  ])
973
1254
 
974
- const itemDragStart = (
1255
+ const itemDragStart = useCallback((
975
1256
  pagePoint: Point,
976
1257
  event: MainThread.MouseEvent | MainThread.TouchEvent,
977
1258
  ) => {
@@ -979,40 +1260,85 @@ function SortableInteractiveItem(props: SortableItemProps) {
979
1260
  resetAutoScrollDragState()
980
1261
  const scrollTop = scrollableScrollTopRef?.current ?? 0
981
1262
  autoScrollStartScrollTop.current = scrollTop
982
- dragStartScrollDeltaFromMeasuredRect.current = scrollTop
983
- - itemMeasuredScrollTop.current
1263
+ dragStartScrollDeltaFromMeasuredRect.current = scrollableBoundaryRectRef
1264
+ ? 0
1265
+ : scrollTop - itemMeasuredScrollTop.current
984
1266
  latestDragEvent.current = event
985
- if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
1267
+ const currentScrollableBoundaryRect = getCurrentScrollableBoundaryRect()
1268
+ if (
1269
+ scrollableBoundaryId
1270
+ && currentScrollableBoundaryRect
1271
+ && currentScrollableBoundaryRect.height > 0
1272
+ ) {
986
1273
  const draggedItem = getDragStartItemBounds(0)
987
- const { distanceToTop, distanceToBottom } = getEdgeDistance(
988
- draggedItem,
989
- scrollableBoundaryRect,
990
- )
991
- const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom)
992
- if (overflow !== 0) {
993
- autoScrollStickyDirection.current = overflow > 0 ? 1 : -1
1274
+ if (draggedItem) {
1275
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(
1276
+ draggedItem,
1277
+ currentScrollableBoundaryRect,
1278
+ )
1279
+ const overflow = getAutoScrollOverflow(
1280
+ distanceToTop,
1281
+ distanceToBottom,
1282
+ )
1283
+ if (overflow !== 0) {
1284
+ autoScrollStickyDirection.current = overflow > 0 ? 1 : -1
1285
+ }
994
1286
  }
995
1287
  }
996
- activateDragOverlay()
1288
+ if (dragOverlayActivatorRefMap) {
1289
+ dragOverlayActivatorRefMap.current[sortingKey] = tryActivateDragOverlay
1290
+ }
1291
+ if (scrollableBoundaryRectRef) {
1292
+ measureActiveItemAndTryOverlay()
1293
+ }
997
1294
  handleDragStart?.(pagePoint, sortingKey, event)
998
1295
  if (autoScrollStickyDirection.current !== 0) {
999
1296
  const visualTranslateY = getVisualTranslateY()
1000
1297
  emitSortableDragMove(visualTranslateY, event)
1001
1298
  }
1002
- }
1299
+ }, [
1300
+ resetAutoScrollDragState,
1301
+ scrollableScrollTopRef,
1302
+ autoScrollStartScrollTop,
1303
+ dragStartScrollDeltaFromMeasuredRect,
1304
+ scrollableBoundaryRectRef,
1305
+ itemMeasuredScrollTop,
1306
+ latestDragEvent,
1307
+ getCurrentScrollableBoundaryRect,
1308
+ scrollableBoundaryId,
1309
+ getDragStartItemBounds,
1310
+ getAutoScrollOverflow,
1311
+ autoScrollStickyDirection,
1312
+ dragOverlayActivatorRefMap,
1313
+ sortingKey,
1314
+ tryActivateDragOverlay,
1315
+ measureActiveItemAndTryOverlay,
1316
+ handleDragStart,
1317
+ getVisualTranslateY,
1318
+ emitSortableDragMove,
1319
+ ])
1003
1320
 
1004
- const itemDragging = (
1321
+ const itemDragging = useCallback((
1005
1322
  translate: Point,
1006
1323
  event: MainThread.MouseEvent | MainThread.TouchEvent,
1007
1324
  ) => {
1008
1325
  'main thread'
1009
1326
  latestDragTranslate.current = translate
1010
1327
  latestDragEvent.current = event
1011
- if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
1328
+ const currentScrollableBoundaryRect = getCurrentScrollableBoundaryRect()
1329
+ if (
1330
+ scrollableBoundaryId
1331
+ && currentScrollableBoundaryRect
1332
+ && currentScrollableBoundaryRect.height > 0
1333
+ ) {
1012
1334
  const draggedItem = getDragStartItemBounds(translate.y)
1335
+ if (!draggedItem) {
1336
+ handleDragMove?.(translate, sortingKey, event)
1337
+ return
1338
+ }
1013
1339
  const { distanceToTop, distanceToBottom } = getEdgeDistance(
1014
1340
  draggedItem,
1015
- scrollableBoundaryRect,
1341
+ currentScrollableBoundaryRect,
1016
1342
  )
1017
1343
  const translateDeltaY = translate.y - lastAutoScrollTranslateY.current
1018
1344
  const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom)
@@ -1048,15 +1374,37 @@ function SortableInteractiveItem(props: SortableItemProps) {
1048
1374
  }
1049
1375
 
1050
1376
  handleDragMove?.(translate, sortingKey, event)
1051
- }
1377
+ }, [
1378
+ latestDragTranslate,
1379
+ latestDragEvent,
1380
+ getCurrentScrollableBoundaryRect,
1381
+ scrollableBoundaryId,
1382
+ getDragStartItemBounds,
1383
+ handleDragMove,
1384
+ sortingKey,
1385
+ lastAutoScrollTranslateY,
1386
+ getAutoScrollOverflow,
1387
+ stopAutoScrollLoop,
1388
+ applyVisualTranslate,
1389
+ emitSortableDragMove,
1390
+ getAutoScrollDirection,
1391
+ autoScrollOverflow,
1392
+ autoScrollStickyDirection,
1393
+ autoScrollDirection,
1394
+ isAutoScrollBlocked,
1395
+ setAutoScrolling,
1396
+ startAutoScrollLoop,
1397
+ ])
1052
1398
 
1053
- const resetLocalDragVisuals = () => {
1399
+ const resetLocalDragVisuals = useCallback(() => {
1054
1400
  'main thread'
1055
1401
  if (!dirtyKeysRef.current[sortingKey]) {
1056
1402
  return
1057
1403
  }
1058
- console.info('reset local drag visual2')
1404
+ invalidateActiveItemMeasurement()
1405
+ stopAutoScrollLoop()
1059
1406
  MTSRef.current?.MTSSetTransform(0, 0)
1407
+ MTSRef.current?.MTSResetInternalTranslateValues()
1060
1408
  const overlayWasActive = dragSourceHidden.current
1061
1409
  showDragSourceItem()
1062
1410
  if (overlayWasActive) {
@@ -1069,10 +1417,19 @@ function SortableInteractiveItem(props: SortableItemProps) {
1069
1417
  })
1070
1418
  }
1071
1419
  }
1072
- dirtyKeysRef.current[sortingKey] = false
1073
- }
1420
+ delete dirtyKeysRef.current[sortingKey]
1421
+ }, [
1422
+ dirtyKeysRef,
1423
+ sortingKey,
1424
+ MTSRef,
1425
+ dragSourceHidden,
1426
+ showDragSourceItem,
1427
+ getDragOverlayElement,
1428
+ invalidateActiveItemMeasurement,
1429
+ stopAutoScrollLoop,
1430
+ ])
1074
1431
 
1075
- const itemDragEnd = (
1432
+ const itemDragEnd = useCallback((
1076
1433
  _pagePoint: Point,
1077
1434
  event: MainThread.MouseEvent | MainThread.TouchEvent,
1078
1435
  ) => {
@@ -1080,25 +1437,67 @@ function SortableInteractiveItem(props: SortableItemProps) {
1080
1437
  resetAutoScrollDragState()
1081
1438
  autoScrollStartScrollTop.current = 0
1082
1439
  dragStartScrollDeltaFromMeasuredRect.current = 0
1083
- latestDragEvent.current = null
1440
+ invalidateActiveItemMeasurement()
1084
1441
  stopAutoScrollLoop()
1085
1442
  const orderChanged = handleDragEnd?.(sortingKey, event) ?? false
1086
1443
  if (!orderChanged) {
1087
1444
  // no data update -> no precommit will run -> reset locally now
1088
1445
  resetLocalDragVisuals()
1089
1446
  }
1090
- // if orderChanged: precommit will run resetLocalDragVisuals in the same frame as setData
1091
- }
1092
-
1093
- const dataKeyOrderSignature = useMemo(
1094
- () => (data ?? []).map(item => item.getSortingKey()).join('|'),
1095
- [data],
1096
- )
1447
+ // The root clears dirty items in the same commit as the new order.
1448
+ }, [
1449
+ resetAutoScrollDragState,
1450
+ autoScrollStartScrollTop,
1451
+ dragStartScrollDeltaFromMeasuredRect,
1452
+ invalidateActiveItemMeasurement,
1453
+ stopAutoScrollLoop,
1454
+ handleDragEnd,
1455
+ sortingKey,
1456
+ resetLocalDragVisuals,
1457
+ ])
1097
1458
 
1098
1459
  usePreCommit(() => {
1099
1460
  'main thread'
1100
- resetLocalDragVisuals()
1101
- }, [dataKeyOrderSignature])
1461
+ resetItemVisualsRef.current[sortingKey] = resetLocalDragVisuals
1462
+ syncDisabledKey(sortingKey, disabled)
1463
+ }, [
1464
+ resetItemVisualsRef,
1465
+ sortingKey,
1466
+ resetLocalDragVisuals,
1467
+ syncDisabledKey,
1468
+ disabled,
1469
+ ])
1470
+
1471
+ const unregisterItem = useCallback(() => {
1472
+ 'main thread'
1473
+ resetItemVisualsRef.current[sortingKey]?.()
1474
+ delete resetItemVisualsRef.current[sortingKey]
1475
+ delete dirtyKeysRef.current[sortingKey]
1476
+ delete disabledKeysRef.current[sortingKey]
1477
+ setChildrenMTSRef({ current: null }, sortingKey)
1478
+ }, [
1479
+ resetItemVisualsRef,
1480
+ dirtyKeysRef,
1481
+ disabledKeysRef,
1482
+ setChildrenMTSRef,
1483
+ sortingKey,
1484
+ ])
1485
+ useEffect(() => () => {
1486
+ runOnMainThread(unregisterItem)()
1487
+ }, [unregisterItem])
1488
+
1489
+ const draggableProps = useMemo(() => ({
1490
+ 'main-thread:bindlayoutchange': handleMTSLayoutChange,
1491
+ }), [handleMTSLayoutChange])
1492
+ const allowedDirection = useMemo(() => ['up', 'down'] as ['up', 'down'], [])
1493
+ const itemChildren = (
1494
+ <>
1495
+ {!scrollableBoundaryRectRef && (
1496
+ <SortableItemGeometryObserver refresh={refreshDraggingRects} />
1497
+ )}
1498
+ {children}
1499
+ </>
1500
+ )
1102
1501
 
1103
1502
  const itemDraggable = enableSorting && !disabled
1104
1503
 
@@ -1110,13 +1509,11 @@ function SortableInteractiveItem(props: SortableItemProps) {
1110
1509
  trigger='immediate'
1111
1510
  className={className}
1112
1511
  enableDragging={itemDraggable}
1113
- draggableProps={{
1114
- 'main-thread:bindlayoutchange': handleMTSLayoutChange,
1115
- }}
1512
+ draggableProps={draggableProps}
1116
1513
  onMTSDragStart={itemDragStart}
1117
1514
  onMTSDragEnd={itemDragEnd}
1118
1515
  onMTSDragging={itemDragging}
1119
- allowedDirection={['up', 'down']}
1516
+ allowedDirection={allowedDirection}
1120
1517
  {...(!scrollableBoundaryId && boundaryId
1121
1518
  && {
1122
1519
  minTranslateY: -(itemRect.top - boundaryRect.top),
@@ -1124,7 +1521,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
1124
1521
  - itemRect.bottom,
1125
1522
  })}
1126
1523
  >
1127
- {children}
1524
+ {itemChildren}
1128
1525
  </Draggable>
1129
1526
  )
1130
1527
  } else {
@@ -1134,9 +1531,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
1134
1531
  MTSRef={MTSRef}
1135
1532
  trigger='immediate'
1136
1533
  className={className}
1137
- draggableProps={{
1138
- 'main-thread:bindlayoutchange': handleMTSLayoutChange,
1139
- }}
1534
+ draggableProps={draggableProps}
1140
1535
  onMTSDragStart={itemDragStart}
1141
1536
  onMTSDragEnd={itemDragEnd}
1142
1537
  onMTSDragging={itemDragging}
@@ -1147,9 +1542,9 @@ function SortableInteractiveItem(props: SortableItemProps) {
1147
1542
  maxTranslateY: boundaryRect.bottom
1148
1543
  - itemRect.bottom,
1149
1544
  })}
1150
- allowedDirection={['up', 'down']}
1545
+ allowedDirection={allowedDirection}
1151
1546
  >
1152
- {children}
1547
+ {itemChildren}
1153
1548
  </DraggableRoot>
1154
1549
  )
1155
1550
  }