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 @@ -19,6 +19,7 @@ import type {
SortableCallbacks
} from '../types';
import {
ActiveItemValuesProvider,
AutoScrollProvider,
CommonValuesProvider,
CustomHandleProvider,
Expand Down Expand Up @@ -75,6 +76,8 @@ export default function SharedProvider({
<LayerProvider />,
// Provider used for layout debugging (can be used only in dev mode)
__DEV__ && debug && <DebugProvider />,
// Provider used for active item values
<ActiveItemValuesProvider />,
// Provider used for shared values between all providers below
<CommonValuesProvider
customHandle={customHandle}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { ReactNode } from 'react';
import { useSharedValue } from 'react-native-reanimated';

import type {
ActiveItemValuesContextType,
Dimensions,
Vector
} from '../../types';
import { DragActivationState } from '../../types';
import { createProvider } from '../utils';

type ActiveItemValuesProviderProps = {
children?: ReactNode;
};

const { ActiveItemValuesProvider, useActiveItemValuesContext } = createProvider(
'ActiveItemValues'
)<ActiveItemValuesProviderProps, ActiveItemValuesContextType>(() => {
// POSITIONS
const touchPosition = useSharedValue<Vector | null>(null);
const activeItemPosition = useSharedValue<Vector | null>(null);

// DIMENSIONS
const activeItemDimensions = useSharedValue<Dimensions | null>(null);

// DRAG STATE
const activeItemKey = useSharedValue<null | string>(null);
const prevActiveItemKey = useSharedValue<null | string>(null);
const activationState = useSharedValue(DragActivationState.INACTIVE);
const activeAnimationProgress = useSharedValue(0);
const inactiveAnimationProgress = useSharedValue(0);
const activeItemDropped = useSharedValue(true);

return {
value: {
activationState,
activeAnimationProgress,
activeItemDimensions,
activeItemDropped,
activeItemKey,
activeItemPosition,
inactiveAnimationProgress,
prevActiveItemKey,
touchPosition
}
};
});

export { ActiveItemValuesProvider, useActiveItemValuesContext };
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import type {
Maybe,
Vector
} from '../../types';
import { AbsoluteLayoutState, DragActivationState } from '../../types';
import { AbsoluteLayoutState } from '../../types';
import { areArraysDifferent } from '../../utils';
import { createProvider } from '../utils';
import { useActiveItemValuesContext } from './ActiveItemValuesProvider';

let nextId = 0;

Expand Down Expand Up @@ -75,8 +76,6 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =

// POSITIONS
const itemPositions = useSharedValue<Record<string, Vector>>({});
const touchPosition = useSharedValue<Vector | null>(null);
const activeItemPosition = useSharedValue<Vector | null>(null);

// DIMENSIONS
// measured dimensions via onLayout used to calculate containerWidth and containerHeight
Expand All @@ -88,20 +87,11 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
// (containerWidth and containerHeight should be used in most cases)
const containerWidth = useSharedValue<null | number>(null);
const containerHeight = useSharedValue<null | number>(null);
const activeItemDimensions = useSharedValue<Dimensions | null>(null);
const itemDimensions = useSharedValue<Record<string, Dimensions>>({});
const itemsStyleOverride = useSharedValue<Maybe<ViewStyle>>(
initialItemsStyleOverride
);

// DRAG STATE
const activeItemKey = useSharedValue<null | string>(null);
const prevActiveItemKey = useSharedValue<null | string>(null);
const activationState = useSharedValue(DragActivationState.INACTIVE);
const activeAnimationProgress = useSharedValue(0);
const inactiveAnimationProgress = useSharedValue(0);
const activeItemDropped = useSharedValue(true);

// ITEM ACTIVATION SETTINGS
const dragActivationDelay = useAnimatableValue(_dragActivationDelay);
const activationAnimationDuration = useAnimatableValue(
Expand Down Expand Up @@ -149,15 +139,10 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =

return {
value: {
...useActiveItemValuesContext(),
absoluteLayoutState,
activationAnimationDuration,
activationState,
activeAnimationProgress,
activeItemDimensions,
activeItemDropped,
activeItemKey,
activeItemOpacity,
activeItemPosition,
activeItemScale,
activeItemShadowOpacity,
animateLayoutOnReorderOnly,
Expand All @@ -171,7 +156,6 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
dragActivationFailOffset,
dropAnimationDuration,
enableActiveItemSnap,
inactiveAnimationProgress,
inactiveItemOpacity,
inactiveItemScale,
indexToKey,
Expand All @@ -183,12 +167,10 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
keyToIndex,
measuredContainerHeight,
measuredContainerWidth,
prevActiveItemKey,
shouldAnimateLayout,
snapOffsetX,
snapOffsetY,
sortEnabled,
touchPosition
sortEnabled
}
};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { ActiveItemValuesProvider } from './ActiveItemValuesProvider';
export { AutoScrollProvider, useAutoScrollContext } from './AutoScrollProvider';
export {
CommonValuesContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
createContext,
type PropsWithChildren,
type ReactNode,
useContext,
useMemo
} from 'react';
Expand All @@ -20,7 +21,7 @@ export default function createProvider<
factory: (props: ProviderProps) => {
value?: ContextValue;
enabled?: boolean;
children?: React.ReactNode;
children?: ReactNode;
}
) {
const { guarded = true } = options ?? {};
Expand Down
35 changes: 23 additions & 12 deletions packages/react-native-sortables/src/types/providers/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ import type { AnimatedValues, AnyRecord, Maybe } from '../utils';

export type ControlledContainerDimensions = { width: boolean; height: boolean };

// ACTIVE ITEM VALUES

export type ActiveItemValuesContextType = {
// POSITIONS
touchPosition: SharedValue<Vector | null>;
activeItemPosition: SharedValue<Vector | null>;

// DIMENSIONS
activeItemDimensions: SharedValue<Dimensions | null>;

// DRAG STATE
prevActiveItemKey: SharedValue<null | string>;
activeItemKey: SharedValue<null | string>;
activationState: SharedValue<DragActivationState>;
activeAnimationProgress: SharedValue<number>;
inactiveAnimationProgress: SharedValue<number>;
activeItemDropped: SharedValue<boolean>;
};

// COMMON VALUES

/**
* Context values shared between all providers.
* (they are stored in a single context to make the access to them easier
Expand All @@ -45,27 +66,16 @@ export type CommonValuesContextType = {

// POSITIONS
itemPositions: SharedValue<Record<string, Vector>>;
touchPosition: SharedValue<Vector | null>;
activeItemPosition: SharedValue<Vector | null>;

// DIMENSIONS
controlledContainerDimensions: SharedValue<ControlledContainerDimensions>;
measuredContainerWidth: SharedValue<null | number>;
measuredContainerHeight: SharedValue<null | number>;
containerWidth: SharedValue<null | number>;
containerHeight: SharedValue<null | number>;
activeItemDimensions: SharedValue<Dimensions | null>;
itemDimensions: SharedValue<Record<string, Dimensions>>;
itemsStyleOverride: SharedValue<Maybe<ViewStyle>>;

// DRAG STATE
prevActiveItemKey: SharedValue<null | string>;
activeItemKey: SharedValue<null | string>;
activationState: SharedValue<DragActivationState>;
activeAnimationProgress: SharedValue<number>;
inactiveAnimationProgress: SharedValue<number>;
activeItemDropped: SharedValue<boolean>;

// OTHER
containerRef: AnimatedRef<View>;
sortEnabled: SharedValue<boolean>;
Expand All @@ -75,7 +85,8 @@ export type CommonValuesContextType = {
customHandle: boolean;

itemsOverridesStyle: AnimatedStyle<ViewStyle>;
} & AnimatedValues<ActiveItemDecorationSettings> &
} & ActiveItemValuesContextType &
AnimatedValues<ActiveItemDecorationSettings> &
AnimatedValues<ActiveItemSnapSettings> &
AnimatedValues<Omit<ItemDragSettings, 'overDrag' | 'reorderTriggerOrigin'>>;

Expand Down
Loading