Skip to content

Commit b692a28

Browse files
committed
Refactor layout providers, remove main group size in grid
1 parent b97a3cf commit b692a28

File tree

16 files changed

+58
-65
lines changed

16 files changed

+58
-65
lines changed

packages/react-native-sortables/src/providers/flex/layout/updates/insert/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { useAnimatedReaction, useDerivedValue } from 'react-native-reanimated';
33

44
import { useMutableValue } from '../../../../../integrations/reanimated';
55
import type {
6-
ControlledSizes,
76
Coordinate,
87
Dimension,
98
FlexLayout,
9+
ItemSizes,
1010
SortStrategyFactory
1111
} from '../../../../../types';
1212
import {
@@ -53,7 +53,7 @@ const useInsertStrategy: SortStrategyFactory = () => {
5353
let mainDimension: Dimension;
5454
let crossDimension: Dimension;
5555
let mainGap: SharedValue<number>;
56-
let mainItemSizes: SharedValue<ControlledSizes>;
56+
let mainItemSizes: SharedValue<ItemSizes>;
5757

5858
if (isRow) {
5959
mainCoordinate = 'x';

packages/react-native-sortables/src/providers/flex/layout/updates/insert/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ControlledSizes } from '../../../../../types';
1+
import type { ItemSizes } from '../../../../../types';
22
import { reorderInsert, resolveDimension } from '../../../../../utils';
33

44
export type ItemGroupSwapProps = {
@@ -8,7 +8,7 @@ export type ItemGroupSwapProps = {
88
groupSizeLimit: number;
99
indexToKey: Array<string>;
1010
keyToIndex: Record<string, number>;
11-
mainItemSizes: ControlledSizes;
11+
mainItemSizes: ItemSizes;
1212
itemGroups: Array<Array<string>>;
1313
mainGap: number;
1414
fixedKeys: Record<string, boolean> | undefined;
@@ -35,7 +35,7 @@ const getGroupItemIndex = (
3535

3636
export const getTotalGroupSize = (
3737
group: Array<string>,
38-
mainItemSizes: ControlledSizes,
38+
mainItemSizes: ItemSizes,
3939
gap: number
4040
) => {
4141
'worklet';

packages/react-native-sortables/src/providers/flex/layout/utils/debug.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DebugRectUpdater } from '../../../../debug';
22
import type {
3-
ControlledSizes,
3+
ItemSizes,
44
FlexDirection,
55
FlexLayout,
66
Vector
@@ -17,8 +17,8 @@ export const updateLayoutDebugRects = (
1717
layout: FlexLayout,
1818
debugCrossAxisGapRects: Array<DebugRectUpdater>,
1919
debugMainAxisGapRects: Array<DebugRectUpdater>,
20-
itemWidths: ControlledSizes,
21-
itemHeights: ControlledSizes
20+
itemWidths: ItemSizes,
21+
itemHeights: ItemSizes
2222
) => {
2323
'worklet';
2424
const isRow = flexDirection.startsWith('row');

packages/react-native-sortables/src/providers/flex/layout/utils/layout.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IS_WEB } from '../../../../constants';
22
import type {
33
AlignContent,
44
AlignItems,
5-
ControlledSizes,
5+
ItemSizes,
66
Direction,
77
FlexAlignments,
88
FlexLayout,
@@ -16,8 +16,8 @@ type AxisDirections = { cross: Direction; main: Direction };
1616

1717
const createGroups = (
1818
indexToKey: Array<string>,
19-
mainItemSizes: ControlledSizes,
20-
crossItemSizes: ControlledSizes,
19+
mainItemSizes: ItemSizes,
20+
crossItemSizes: ItemSizes,
2121
gap: number,
2222
groupMainSizeLimit: number
2323
): null | {
@@ -141,8 +141,8 @@ const calculateAlignment = (
141141
const handleLayoutCalculation = (
142142
groups: Array<Array<string>>,
143143
crossAxisGroupSizes: Array<number>,
144-
mainItemSizes: ControlledSizes,
145-
crossItemSizes: ControlledSizes,
144+
mainItemSizes: ItemSizes,
145+
crossItemSizes: ItemSizes,
146146
gaps: FlexLayoutProps['gaps'],
147147
axisDirections: AxisDirections,
148148
{ alignContent, alignItems, justifyContent }: FlexAlignments,

packages/react-native-sortables/src/providers/grid/layout/GridLayoutProvider.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import { useAnimatedReaction } from 'react-native-reanimated';
44
import { IS_WEB } from '../../../constants';
55
import { useDebugContext } from '../../../debug';
66
import type { Animatable } from '../../../integrations/reanimated';
7-
import {
8-
useAnimatableValue,
9-
useMutableValue
10-
} from '../../../integrations/reanimated';
7+
import { useAnimatableValue } from '../../../integrations/reanimated';
118
import type { GridLayoutContextType } from '../../../types';
129
import { useCommonValuesContext, useMeasurementsContext } from '../../shared';
1310
import { createProvider } from '../../utils';
@@ -59,13 +56,6 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(
5956
const mainGap = isVertical ? columnGap : rowGap;
6057
const crossGap = isVertical ? rowGap : columnGap;
6158

62-
/**
63-
* Size of the group of items determined by the parent container size.
64-
* width - in vertical orientation (default) (columns are groups)
65-
* height - in horizontal orientation (rows are groups)
66-
*/
67-
const mainGroupSize = useMutableValue<null | number>(rowHeight ?? null);
68-
6959
// MAIN GROUP SIZE UPDATER
7060
useAnimatedReaction(
7161
() => {
@@ -84,7 +74,6 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(
8474
return;
8575
}
8676

87-
mainGroupSize.value = value;
8877
if (isVertical) {
8978
itemWidths.value = value;
9079
} else {
@@ -118,7 +107,6 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(
118107
isVertical,
119108
itemHeights: itemHeights.value,
120109
itemWidths: itemWidths.value,
121-
mainGroupSize: mainGroupSize.value,
122110
numGroups
123111
}),
124112
(props, previousProps) => {
@@ -128,10 +116,13 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(
128116
// (e.g. skip animation when the browser window is resized)
129117
shouldAnimateLayout.value =
130118
!IS_WEB ||
131-
!previousProps?.mainGroupSize ||
132-
props.mainGroupSize === previousProps.mainGroupSize;
119+
!previousProps?.itemHeights ||
120+
!previousProps?.itemWidths ||
121+
isVertical
122+
? props.itemWidths === previousProps?.itemWidths
123+
: props.itemHeights === previousProps?.itemHeights;
133124

134-
if (!layout || mainGroupSize.value === null) {
125+
if (!layout || !itemHeights.value || !itemWidths.value) {
135126
return;
136127
}
137128

@@ -160,7 +151,6 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(
160151
crossGap,
161152
isVertical,
162153
mainGap,
163-
mainGroupSize,
164154
numGroups
165155
}
166156
};

packages/react-native-sortables/src/providers/grid/layout/updates/common.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ export const createGridStrategy =
2828
itemHeights,
2929
itemWidths
3030
} = useCommonValuesContext();
31-
const { crossGap, isVertical, mainGap, mainGroupSize, numGroups } =
32-
useGridLayoutContext();
31+
const { crossGap, isVertical, mainGap, numGroups } = useGridLayoutContext();
3332
const { fixedItemKeys } = useCustomHandleContext() ?? {};
3433

3534
const othersIndexToKey = useInactiveIndexToKey();
@@ -43,7 +42,6 @@ export const createGridStrategy =
4342
isVertical,
4443
itemHeights: itemHeights.value,
4544
itemWidths: itemWidths.value,
46-
mainGroupSize: mainGroupSize.value,
4745
numGroups
4846
})
4947
);
@@ -71,11 +69,14 @@ export const createGridStrategy =
7169

7270
return ({ activeIndex, dimensions, position }) => {
7371
'worklet';
72+
const mainGroupSize = (isVertical ? itemWidths : itemHeights).value;
73+
7474
if (
7575
!othersLayout.value ||
7676
crossContainerSize.value === null ||
7777
mainContainerSize.value === null ||
78-
mainGroupSize.value === null
78+
mainGroupSize === null ||
79+
typeof mainGroupSize !== 'number'
7980
) {
8081
return;
8182
}
@@ -162,7 +163,7 @@ export const createGridStrategy =
162163
if (mainBeforeBound !== Infinity) {
163164
mainIndex--;
164165
}
165-
mainBeforeOffset = mainIndex * (mainGroupSize.value + mainGap.value);
166+
mainBeforeOffset = mainIndex * (mainGroupSize + mainGap.value);
166167
mainBeforeBound = mainBeforeOffset - additionalOffset;
167168
} while (
168169
mainBeforeBound > 0 &&
@@ -178,8 +179,7 @@ export const createGridStrategy =
178179
mainIndex++;
179180
}
180181
mainAfterOffset =
181-
mainIndex * (mainGroupSize.value + mainGap.value) +
182-
mainGroupSize.value;
182+
mainIndex * (mainGroupSize + mainGap.value) + mainGroupSize;
183183
mainAfterBound = mainAfterOffset + additionalOffset;
184184
} while (
185185
mainAfterBound < mainContainerSize.value &&

packages/react-native-sortables/src/providers/grid/layout/utils/layout.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ export const calculateLayout = ({
1313
isVertical,
1414
itemHeights,
1515
itemWidths,
16-
mainGroupSize,
1716
numGroups
1817
}: GridLayoutProps): GridLayout | null => {
1918
'worklet';
19+
const mainGroupSize = (isVertical ? itemWidths : itemHeights) as
20+
| null
21+
| number;
22+
2023
if (!mainGroupSize) {
2124
return null;
2225
}

packages/react-native-sortables/src/providers/shared/CommonValuesProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import type {
1212
ActiveItemSnapSettings,
1313
CommonValuesContextType,
1414
ControlledDimensions,
15-
ControlledSizes,
1615
Dimensions,
1716
ItemDragSettings,
17+
ItemSizes,
1818
ItemsLayoutTransitionMode,
1919
Vector
2020
} from '../../types';
@@ -76,10 +76,10 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
7676
// DIMENSIONS
7777
const containerWidth = useMutableValue<null | number>(null);
7878
const containerHeight = useMutableValue<null | number>(null);
79-
const itemWidths = useMutableValue<ControlledSizes>(
79+
const itemWidths = useMutableValue<ItemSizes>(
8080
controlledItemDimensions.width ? null : {}
8181
);
82-
const itemHeights = useMutableValue<ControlledSizes>(
82+
const itemHeights = useMutableValue<ItemSizes>(
8383
controlledItemDimensions.height ? null : {}
8484
);
8585
const activeItemDimensions = useMutableValue<Dimensions | null>(null);

packages/react-native-sortables/src/types/layout/flex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ViewStyle } from 'react-native';
22

33
import type { NoUndef } from '../../helperTypes';
4-
import type { ControlledSizes, Dimensions, Vector } from './shared';
4+
import type { ItemSizes, Dimensions, Vector } from './shared';
55

66
export type AlignContent = Exclude<
77
NoUndef<ViewStyle['alignContent']>,
@@ -37,8 +37,8 @@ export type FlexLayoutProps = {
3737
row: number;
3838
column: number;
3939
};
40-
itemWidths: ControlledSizes;
41-
itemHeights: ControlledSizes;
40+
itemWidths: ItemSizes;
41+
itemHeights: ItemSizes;
4242
indexToKey: Array<string>;
4343
flexDirection: FlexDirection;
4444
flexWrap: FlexWrap;

packages/react-native-sortables/src/types/layout/grid.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import type { ControlledSizes, Dimensions, Vector } from './shared';
1+
import type { Dimensions, ItemSizes, Vector } from './shared';
22

33
export type GridLayoutProps = {
4-
mainGroupSize: null | number;
54
gaps: {
65
main: number;
76
cross: number;
87
};
9-
itemHeights: ControlledSizes;
10-
itemWidths: ControlledSizes;
8+
itemHeights: ItemSizes;
9+
itemWidths: ItemSizes;
1110
indexToKey: Array<string>;
1211
isVertical: boolean;
1312
numGroups: number;

0 commit comments

Comments
 (0)