From 87ac69d11004de9f407469887fda27a5fd867c0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Thu, 26 Nov 2026 17:15:02 +0100 Subject: [PATCH 1/2] Keep a flex item movable next to fixed items and across changed lines - an item whose walk along a line ended on a fixed item was not moved at all, it now takes the closest free slot - an item could not move to the last line when only fixed items followed its slot there - the place of the active item in its line is read from the layout, which can have one line less than expected once the item is taken out - an item wider than the container can be moved to a previous line, where it starts a line as in the layout --- .../updates/insert/index.test.tsx | 61 ++++++++++++++++--- .../updates/insert/index.ts | 32 +++++++--- .../updates/insert/utils.ts | 18 ++++-- 3 files changed, 89 insertions(+), 22 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index da32e588..08ee6929 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -8,6 +8,7 @@ import useInsertStrategy from '.'; const ITEM_HEIGHT = 50; type Options = { + columnGap?: number; fixedIndex?: number; rowGap?: number; }; @@ -17,7 +18,7 @@ type Options = { const setup = ( widths: Array, containerWidth: number, - { fixedIndex, rowGap }: Options = {} + { columnGap, fixedIndex, rowGap }: Options = {} ) => { let context!: ReturnType; let updater!: OrderUpdater; @@ -28,7 +29,11 @@ const setup = ( render( - + {widths.map((_, index) => ( { const [a, b, c, d] = keys; expect(drag(0, { x: 130, y: 75 })).toEqual([b, c, a, d]); - // The fixed item cannot be passed - expect(drag(0, { x: 240, y: 75 })).toEqual(keys); + // The fixed item cannot be passed, so the slot before it is taken + expect(drag(0, { x: 240, y: 75 })).toEqual([b, c, a, d]); }); -it('keeps a fixed last item when the target line does not exist', () => { - // a | b c | d | E: with b taken out, c joins a, so the layout has one line - // less than the strategy expects - const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { fixedIndex: 4 }); +it('moves an item to the next line when the pointer is over its fixed last item', () => { + // a b | c D -> a | c b D + const { drag, keys } = setup([54, 298, 270, 48], 352, { fixedIndex: 3 }); + const [a, b, c, d] = keys; + + expect(drag(1, { x: 203, y: 75 })).toEqual([a, c, b, d]); +}); + +it('moves an item to the last line when only a fixed item can follow it', () => { + // a b | c d | e F -> a b | c e | d F + const { drag, keys } = setup([233, 129, 160, 154, 146, 58], 380, { + fixedIndex: 5 + }); + const [a, b, c, d, e, f] = keys; + + expect(drag(3, { x: 263, y: 125 })).toEqual([a, b, c, e, d, f]); +}); + +describe('when taking the item out lets the next item join the previous line', () => { + it('moves the first item of a line to the next line', () => { + // a | b c d | e -> a c | d e | b + const { drag, keys } = setup([220, 100, 60, 110, 180], 300); + const [a, b, c, d, e] = keys; + + expect(drag(1, { x: 50, y: 125 })).toEqual([a, c, d, e, b]); + }); + + it('moves an item before a fixed last item', () => { + // a | b c | d | E -> a c | d | b E + const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { + fixedIndex: 4 + }); + const [a, b, c, d, e] = keys; + + expect(drag(1, { x: 150, y: 125 })).toEqual([a, c, d, b, e]); + }); +}); + +it('moves an item wider than the container to the previous line', () => { + // a | b | c d -> b | a c d + const { drag, keys } = setup([57, 368, 162, 35], 362); + const [a, b, c, d] = keys; - expect(drag(1, { x: 150, y: 125 })).toEqual(keys); + expect(drag(1, { x: 184, y: 40 })).toEqual([b, a, c, d]); }); it('moves an item wider than half of the line to the end of the line', () => { diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts index 615b20ca..8fa0bb8f 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts @@ -263,7 +263,15 @@ const useInsertStrategy: SortStrategyFactory = () => { // MAIN AXIS BOUNDS // currentGroup is the updated group after new layout calculation // that contains the active item - const currentGroup = currentLayout.itemGroups[groupIndex]; + let currentGroup = currentLayout.itemGroups[groupIndex]; + // Taking the active item out of its line can let the next item join the + // previous line, so the active item is one line before the expected one + if ( + !currentGroup?.includes(activeKey) && + currentLayout.itemGroups[groupIndex - 1]?.includes(activeKey) + ) { + currentGroup = currentLayout.itemGroups[--groupIndex]; + } if (!currentGroup) { const lastItemIndex = indexToKey.value.length - 1; if ( @@ -281,13 +289,10 @@ const useInsertStrategy: SortStrategyFactory = () => { } const mainAxisPosition = position[mainCoordinate]; - // Find the itemIndexInGroup of the active item if it is in the same group - if (groupIndex === activeGroupIndex.value) { - const firstItemKey = currentGroup[0]; - if (firstItemKey === undefined) return; - const firstItemIndex = keyToIndex.value[firstItemKey]; - if (firstItemIndex === undefined) return; - itemIndexInGroup = activeIndex - firstItemIndex; + // The layout knows where in its line the active item really is + const activeIndexInGroup = currentGroup.indexOf(activeKey); + if (activeIndexInGroup !== -1) { + itemIndexInGroup = activeIndexInGroup; } const initialItemIndexInGroup = itemIndexInGroup; @@ -465,10 +470,19 @@ const useInsertStrategy: SortStrategyFactory = () => { } } - const newIndex = Math.min( + let newIndex = Math.min( firstAvailableInGroupIndex + (itemIndexInGroup - initialItemIndexInGroup), indexToKey.value.length - 1 ); + // A fixed item cannot be replaced, so take the closest free slot on the + // way to it + const step = Math.sign(firstAvailableInGroupIndex - newIndex); + while ( + newIndex !== firstAvailableInGroupIndex && + fixedItemKeys?.value?.[indexToKey.value[newIndex]!] + ) { + newIndex += step; + } if ( newIndex === activeIndex || diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts index fde32012..99a83d86 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts @@ -121,7 +121,11 @@ const getIndexesWhenSwappedToGroupBefore = ({ if (itemKey === undefined) { return null; } - if (totalGroupSize + activeMainSize > groupSizeLimit) { + // The first item of a line never wraps (same rule as in the layout) + if ( + itemIndex > firstInGroupBeforeIndex && + totalGroupSize + activeMainSize > groupSizeLimit + ) { break; } @@ -196,7 +200,14 @@ const getIndexesWhenSwappedToGroupAfter = ({ totalGroupSize += itemMainSize + mainGap; }; - for (let i = activeItemIndex + 1; i < indexToKey.length; i++) { + // Fixed items at the end cannot be passed, so the slot before them is the + // last one the active item can take + let lastFreeIndex = indexToKey.length - 1; + while (fixedKeys?.[indexToKey[lastFreeIndex]!]) { + lastFreeIndex--; + } + + for (let i = activeItemIndex + 1; i <= lastFreeIndex; i++) { const key = indexToKey[i]!; if (fixedKeys?.[key]) { @@ -212,8 +223,7 @@ const getIndexesWhenSwappedToGroupAfter = ({ emptyIndex = i; if (doesNotFitInGroup(activeItemKey)) { - const nextItemKey = indexToKey[i + 1]; - if (nextItemKey === undefined || doesNotFitInGroup(nextItemKey)) { + if (i === lastFreeIndex || doesNotFitInGroup(indexToKey[i + 1]!)) { return { groupIndex: groupIndex + 1, itemIndex: i, From ba92d12ef4615d33fe0868d42ea25b6757d48cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Thu, 26 Nov 2026 17:22:19 +0100 Subject: [PATCH 2/2] Use the real line of the dragged item when it moves to a following line The expected line is one too far when the item that takes the place of the dragged one joins the previous line. The bounds of the next move were then taken from a line that does not exist, and the order could flip back and forth with the pointer at rest. --- .../updates/insert/index.test.tsx | 109 ++++++++++++++++-- .../updates/insert/index.ts | 33 +++++- 2 files changed, 131 insertions(+), 11 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index 08ee6929..3997138e 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -9,17 +9,21 @@ const ITEM_HEIGHT = 50; type Options = { columnGap?: number; - fixedIndex?: number; + fixedIndex?: Array | number; + heights?: Array; rowGap?: number; }; // Renders a wrapping row of items with the given widths and returns their keys -// with a function that drags one of them. +// with functions that drag one of them. const setup = ( widths: Array, containerWidth: number, - { columnGap, fixedIndex, rowGap }: Options = {} + { columnGap, fixedIndex = [], heights, rowGap }: Options = {} ) => { + const fixedIndexes = [fixedIndex].flat(); + const heightOf = (index: number) => heights?.[index] ?? ITEM_HEIGHT; + let context!: ReturnType; let updater!: OrderUpdater; const useStrategy = () => { @@ -37,7 +41,7 @@ const setup = ( {widths.map((_, index) => ( ))} @@ -47,7 +51,9 @@ const setup = ( const keys = context.indexToKey.value; act(() => { context.containerWidth.value = containerWidth; - context.itemHeights.value = ITEM_HEIGHT; + context.itemHeights.value = Object.fromEntries( + keys.map((key, index) => [key, heightOf(index)]) + ); context.itemWidths.value = Object.fromEntries( keys.map((key, index) => [key, widths[index]!]) ); @@ -65,13 +71,48 @@ const setup = ( updater({ activeIndex, activeKey, - dimensions: { height: ITEM_HEIGHT, width: widths[activeIndex]! }, + dimensions: { + height: heightOf(activeIndex), + width: widths[activeIndex]! + }, position }) ?? keys ); }; - return { context, drag, keys }; + const applyOrder = (order: Array) => + act(() => { + context.indexToKey.value = order; + jest.runAllTimers(); + }); + + // Keeps the item at the given position, applies every order the strategy + // returns (as when the finger moves by a pixel) and returns all of them + const hold = (activeIndex: number, position: Vector) => { + const activeKey = keys[activeIndex]!; + act(() => { + context.activeItemKey.value = activeKey; + jest.runAllTimers(); + }); + const orders: Array> = []; + for (let i = 0; i < 6; i++) { + const order = updater({ + activeIndex: context.keyToIndex.value[activeKey]!, + activeKey, + dimensions: { + height: heightOf(activeIndex), + width: widths[activeIndex]! + }, + position + }); + if (!order) break; + orders.push(order); + applyOrder(order); + } + return orders; + }; + + return { context, drag, hold, keys }; }; it('moves an item to the next line when rounded sizes overflow the line', () => { @@ -150,3 +191,57 @@ it('does not leave an empty line before a first item wider than the container', expect(context.itemLayoutPositions.value[keys[0]!]).toEqual({ x: 0, y: 0 }); }); + +describe('with the item held at one position', () => { + it('settles above a tall fixed line', () => { + // a b | C | d E, where C and E are fixed and C is 150 high + const { hold, keys } = setup([150, 100, 300, 200, 50], 300, { + fixedIndex: [2, 4], + heights: [50, 50, 150, 50, 50] + }); + const [a, b, c, d, e] = keys; + + expect(hold(3, { x: 100, y: 110 })).toEqual([ + [d, a, c, b, e], + [a, d, c, b, e] + ]); + }); + + it('settles above three fixed lines', () => { + // a b | C | D | E | f G, where C, D, E and G are fixed + const { hold, keys } = setup([150, 100, 300, 300, 300, 200, 50], 300, { + fixedIndex: [2, 3, 4, 6] + }); + const [a, b, c, d, e, f, g] = keys; + + expect(hold(5, { x: 100, y: 112 })).toEqual([ + [f, a, c, d, e, b, g], + [a, f, c, d, e, b, g] + ]); + }); + + it('settles when the item is wider than the container', () => { + // a | b c | D | e, where D is fixed and 150 high + const { hold, keys } = setup([200, 150, 100, 300, 320], 300, { + fixedIndex: 3, + heights: [50, 50, 50, 150, 50] + }); + const [a, b, c, d, e] = keys; + + expect(hold(4, { x: 150, y: 160 })).toEqual([ + [a, e, b, d, c], + [a, b, e, d, c] + ]); + }); + + it('ends on the line under the pointer when lines have gaps', () => { + // a | b | c | d | e | f g, the pointer is in the line of c + const { hold, keys } = setup([100, 120, 140, 160, 100, 140, 40], 200, { + columnGap: 16, + rowGap: 8 + }); + const [a, b, c, d, e, f, g] = keys; + + expect(hold(6, { x: 6, y: 150 }).at(-1)).toEqual([a, b, c, g, d, e, f]); + }); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts index 8fa0bb8f..8cf34082 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts @@ -29,6 +29,26 @@ import { getTotalGroupSize } from './utils'; +// The line the active item really is in after the swap. The predicted one is +// one too far when the item taking the place of the active item joins the +// previous line. +const getSwappedGroupIndex = ( + indexes: ItemGroupSwapResult, + layout: FlexLayout | null, + activeKey: string +) => { + 'worklet'; + const groups = layout?.itemGroups; + if ( + groups && + !groups[indexes.groupIndex]?.includes(activeKey) && + groups[indexes.groupIndex - 1]?.includes(activeKey) + ) { + return indexes.groupIndex - 1; + } + return indexes.groupIndex; +}; + const useInsertStrategy: SortStrategyFactory = () => { const { activeItemKey, indexToKey, itemHeights, itemWidths, keyToIndex } = useCommonValuesContext(); @@ -216,7 +236,7 @@ const useInsertStrategy: SortStrategyFactory = () => { if (swapGroupAfterBound !== -Infinity) { swappedAfterGroupsCount = swappedAfterLayout.value?.itemGroups.length ?? 0; - groupIndex = afterIndexes.groupIndex; + groupIndex = getSwappedGroupIndex(afterIndexes, afterLayout, activeKey); firstAvailableInGroupIndex = afterIndexes.itemIndex; itemIndexInGroup = afterIndexes.itemIndexInGroup; @@ -238,10 +258,15 @@ const useInsertStrategy: SortStrategyFactory = () => { swapGroupAfterOffset = currentGroupBeforeOffset + (currentLayout.crossAxisGroupSizes[groupIndex] ?? 0); + const afterGroupIndex = getSwappedGroupIndex( + afterIndexes, + afterLayout, + activeKey + ); const afterSwapGroupBeforeOffset = - afterLayout?.crossAxisGroupOffsets[afterIndexes.groupIndex] ?? 0; + afterLayout?.crossAxisGroupOffsets[afterGroupIndex] ?? 0; const afterSwapGroupSize = Math.max( - afterLayout?.crossAxisGroupSizes?.[afterIndexes.groupIndex] ?? 0, + afterLayout?.crossAxisGroupSizes?.[afterGroupIndex] ?? 0, activeItemDimensions[crossDimension] ); const swapOffset = afterSwapGroupBeforeOffset @@ -251,7 +276,7 @@ const useInsertStrategy: SortStrategyFactory = () => { 2 : swapGroupAfterOffset; const additionalSwapOffset = getAdditionalSwapOffset( - afterLayout?.crossAxisGroupSizes?.[afterIndexes.groupIndex] ?? 0 + afterLayout?.crossAxisGroupSizes?.[afterGroupIndex] ?? 0 ); swapGroupAfterBound = swapOffset + additionalSwapOffset; } while (