Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type {
import type { SharedProviderProps } from '../shared';
import { SharedProvider, useOrderUpdater, useStrategyKey } from '../shared';
import { ContextProviderComposer } from '../utils';
import type { FlexLayoutProviderProps } from './layout';
import { FLEX_STRATEGIES, FlexLayoutProvider } from './layout';
import type { FlexLayoutProviderProps } from './FlexLayoutProvider';
import { FLEX_STRATEGIES, FlexLayoutProvider } from './FlexLayoutProvider';

type FlexProviderProps = PropsWithChildren<
SharedProviderProps & {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './FlexLayoutProvider';
export { default as FlexProvider } from './FlexProvider';
export * from './layout';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { AdditionalCrossOffsetContextType } from '../../types';
import { calculateSnapOffset } from '../../utils';
import { useCommonValuesContext, useCustomHandleContext } from '../shared';
import { createProvider } from '../utils';
import { calculateActiveItemCrossOffset } from './layout/utils';
import { calculateActiveItemCrossOffset } from './GridLayoutProvider/utils';

type AdditionalCrossOffsetProviderProps = PropsWithChildren<{
isVertical: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PropsWithChildren } from 'react';
import type { PropsWithChildren } from 'react';
import type { SharedValue } from 'react-native-reanimated';
import { useAnimatedReaction } from 'react-native-reanimated';

Expand All @@ -8,7 +8,7 @@ import type { GridLayoutContextType } from '../../../types';
import { useCommonValuesContext, useMeasurementsContext } from '../../shared';
import { createProvider } from '../../utils';
import { useAdditionalCrossOffsetContext } from '../AdditionalCrossOffsetProvider';
import { calculateLayout } from './utils';
import { calculateLayout, shouldUpdateContainerDimensions } from './utils';

const DEBUG_COLORS = {
backgroundColor: '#ffa500',
Expand All @@ -35,6 +35,7 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(
rowHeight
}) => {
const {
containerHeight,
containerWidth,
indexToKey,
itemHeights,
Expand Down Expand Up @@ -115,8 +116,19 @@ const { GridLayoutProvider, useGridLayoutContext } = createProvider(

// Update item positions
itemPositions.value = layout.itemPositions;

// Update controlled container dimensions
applyControlledContainerDimensions(layout.controlledContainerDimensions); // TODO - adjust container height properly to prevent content jumps when items are collapsed
if (
shouldUpdateContainerDimensions(
isVertical ? containerHeight.value : containerWidth.value,
layout.containerCrossSize,
!!additionalCrossOffset?.value
)
) {
applyControlledContainerDimensions({
[isVertical ? 'height' : 'width']: layout.containerCrossSize
});
}

// On the web, animate layout only if parent container is not resized
// (e.g. skip animation when the browser window is resized)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'worklet';
import { areValuesDifferent } from '../../../../utils';

export const getMainIndex = (index: number, numGroups: number): number =>
+index % numGroups;

export const getCrossIndex = (index: number, numGroups: number): number =>
Math.floor(+index / numGroups);

export const shouldUpdateContainerDimensions = (
currentContainerCrossSize: null | number,
calculatedContainerCrossSize: number,
hasAdditionalCrossOffset: boolean
): boolean =>
!currentContainerCrossSize ||
(areValuesDifferent(
currentContainerCrossSize,
calculatedContainerCrossSize,
1
) &&
(!hasAdditionalCrossOffset ||
calculatedContainerCrossSize > currentContainerCrossSize));
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ export const calculateLayout = (
const lastCrossOffset = crossAxisOffsets[crossAxisOffsets.length - 1];

return {
controlledContainerDimensions: {
[isVertical ? 'height' : 'width']: lastCrossOffset
? Math.max(lastCrossOffset - gaps.cross, 0)
: 0
},
containerCrossSize: lastCrossOffset
? Math.max(lastCrossOffset - gaps.cross, 0)
: 0,
crossAxisOffsets,
itemPositions
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type { SharedProviderProps } from '../shared';
import { SharedProvider, useOrderUpdater, useStrategyKey } from '../shared';
import { ContextProviderComposer } from '../utils';
import { AdditionalCrossOffsetProvider } from './AdditionalCrossOffsetProvider';
import type { GridLayoutProviderProps } from './layout';
import { GRID_STRATEGIES, GridLayoutProvider } from './layout';
import type { GridLayoutProviderProps } from './GridLayoutProvider';
import { GRID_STRATEGIES, GridLayoutProvider } from './GridLayoutProvider';

type GridProviderProps = PropsWithChildren<
GridLayoutProviderProps &
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './GridLayoutProvider';
export { default as GridProvider } from './GridProvider';
export * from './layout';

This file was deleted.

4 changes: 2 additions & 2 deletions packages/react-native-sortables/src/types/layout/grid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coordinate, Dimensions, ItemSizes, Vector } from './shared';
import type { Coordinate, ItemSizes, Vector } from './shared';

export type GridLayoutProps = {
gaps: {
Expand All @@ -15,7 +15,7 @@ export type GridLayoutProps = {
export type GridLayout = {
itemPositions: Record<string, Vector>;
crossAxisOffsets: Array<number>;
controlledContainerDimensions: Partial<Dimensions>;
containerCrossSize: number;
};

export type AdditionalCrossOffsetProps = {
Expand Down
Loading