Skip to content

Commit 5bc7746

Browse files
authored
chore: Move active item related value to a separate provider (#359)
## Description This PR is just a preparation for the next one in which I will add support for item dragging between sortable components (like dragging from one grid to another). To implement this feature, I need to know the current position of the active item in all sortable components to detect when it enters another sortable component.
1 parent f927411 commit 5bc7746

6 files changed

Lines changed: 82 additions & 35 deletions

File tree

packages/react-native-sortables/src/providers/SharedProvider.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
SortableCallbacks
2020
} from '../types';
2121
import {
22+
ActiveItemValuesProvider,
2223
AutoScrollProvider,
2324
CommonValuesProvider,
2425
CustomHandleProvider,
@@ -75,6 +76,8 @@ export default function SharedProvider({
7576
<LayerProvider />,
7677
// Provider used for layout debugging (can be used only in dev mode)
7778
__DEV__ && debug && <DebugProvider />,
79+
// Provider used for active item values
80+
<ActiveItemValuesProvider />,
7881
// Provider used for shared values between all providers below
7982
<CommonValuesProvider
8083
customHandle={customHandle}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { ReactNode } from 'react';
2+
import { useSharedValue } from 'react-native-reanimated';
3+
4+
import type {
5+
ActiveItemValuesContextType,
6+
Dimensions,
7+
Vector
8+
} from '../../types';
9+
import { DragActivationState } from '../../types';
10+
import { createProvider } from '../utils';
11+
12+
type ActiveItemValuesProviderProps = {
13+
children?: ReactNode;
14+
};
15+
16+
const { ActiveItemValuesProvider, useActiveItemValuesContext } = createProvider(
17+
'ActiveItemValues'
18+
)<ActiveItemValuesProviderProps, ActiveItemValuesContextType>(() => {
19+
// POSITIONS
20+
const touchPosition = useSharedValue<Vector | null>(null);
21+
const activeItemPosition = useSharedValue<Vector | null>(null);
22+
23+
// DIMENSIONS
24+
const activeItemDimensions = useSharedValue<Dimensions | null>(null);
25+
26+
// DRAG STATE
27+
const activeItemKey = useSharedValue<null | string>(null);
28+
const prevActiveItemKey = useSharedValue<null | string>(null);
29+
const activationState = useSharedValue(DragActivationState.INACTIVE);
30+
const activeAnimationProgress = useSharedValue(0);
31+
const inactiveAnimationProgress = useSharedValue(0);
32+
const activeItemDropped = useSharedValue(true);
33+
34+
return {
35+
value: {
36+
activationState,
37+
activeAnimationProgress,
38+
activeItemDimensions,
39+
activeItemDropped,
40+
activeItemKey,
41+
activeItemPosition,
42+
inactiveAnimationProgress,
43+
prevActiveItemKey,
44+
touchPosition
45+
}
46+
};
47+
});
48+
49+
export { ActiveItemValuesProvider, useActiveItemValuesContext };

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import type {
2121
Maybe,
2222
Vector
2323
} from '../../types';
24-
import { AbsoluteLayoutState, DragActivationState } from '../../types';
24+
import { AbsoluteLayoutState } from '../../types';
2525
import { areArraysDifferent } from '../../utils';
2626
import { createProvider } from '../utils';
27+
import { useActiveItemValuesContext } from './ActiveItemValuesProvider';
2728

2829
let nextId = 0;
2930

@@ -75,8 +76,6 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
7576

7677
// POSITIONS
7778
const itemPositions = useSharedValue<Record<string, Vector>>({});
78-
const touchPosition = useSharedValue<Vector | null>(null);
79-
const activeItemPosition = useSharedValue<Vector | null>(null);
8079

8180
// DIMENSIONS
8281
// measured dimensions via onLayout used to calculate containerWidth and containerHeight
@@ -88,20 +87,11 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
8887
// (containerWidth and containerHeight should be used in most cases)
8988
const containerWidth = useSharedValue<null | number>(null);
9089
const containerHeight = useSharedValue<null | number>(null);
91-
const activeItemDimensions = useSharedValue<Dimensions | null>(null);
9290
const itemDimensions = useSharedValue<Record<string, Dimensions>>({});
9391
const itemsStyleOverride = useSharedValue<Maybe<ViewStyle>>(
9492
initialItemsStyleOverride
9593
);
9694

97-
// DRAG STATE
98-
const activeItemKey = useSharedValue<null | string>(null);
99-
const prevActiveItemKey = useSharedValue<null | string>(null);
100-
const activationState = useSharedValue(DragActivationState.INACTIVE);
101-
const activeAnimationProgress = useSharedValue(0);
102-
const inactiveAnimationProgress = useSharedValue(0);
103-
const activeItemDropped = useSharedValue(true);
104-
10595
// ITEM ACTIVATION SETTINGS
10696
const dragActivationDelay = useAnimatableValue(_dragActivationDelay);
10797
const activationAnimationDuration = useAnimatableValue(
@@ -149,15 +139,10 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
149139

150140
return {
151141
value: {
142+
...useActiveItemValuesContext(),
152143
absoluteLayoutState,
153144
activationAnimationDuration,
154-
activationState,
155-
activeAnimationProgress,
156-
activeItemDimensions,
157-
activeItemDropped,
158-
activeItemKey,
159145
activeItemOpacity,
160-
activeItemPosition,
161146
activeItemScale,
162147
activeItemShadowOpacity,
163148
animateLayoutOnReorderOnly,
@@ -171,7 +156,6 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
171156
dragActivationFailOffset,
172157
dropAnimationDuration,
173158
enableActiveItemSnap,
174-
inactiveAnimationProgress,
175159
inactiveItemOpacity,
176160
inactiveItemScale,
177161
indexToKey,
@@ -183,12 +167,10 @@ const { CommonValuesContext, CommonValuesProvider, useCommonValuesContext } =
183167
keyToIndex,
184168
measuredContainerHeight,
185169
measuredContainerWidth,
186-
prevActiveItemKey,
187170
shouldAnimateLayout,
188171
snapOffsetX,
189172
snapOffsetY,
190-
sortEnabled,
191-
touchPosition
173+
sortEnabled
192174
}
193175
};
194176
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { ActiveItemValuesProvider } from './ActiveItemValuesProvider';
12
export { AutoScrollProvider, useAutoScrollContext } from './AutoScrollProvider';
23
export {
34
CommonValuesContext,

packages/react-native-sortables/src/providers/utils/createProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {
44
createContext,
55
type PropsWithChildren,
6+
type ReactNode,
67
useContext,
78
useMemo
89
} from 'react';
@@ -20,7 +21,7 @@ export default function createProvider<
2021
factory: (props: ProviderProps) => {
2122
value?: ContextValue;
2223
enabled?: boolean;
23-
children?: React.ReactNode;
24+
children?: ReactNode;
2425
}
2526
) {
2627
const { guarded = true } = options ?? {};

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ import type { AnimatedValues, AnyRecord, Maybe } from '../utils';
3131

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

34+
// ACTIVE ITEM VALUES
35+
36+
export type ActiveItemValuesContextType = {
37+
// POSITIONS
38+
touchPosition: SharedValue<Vector | null>;
39+
activeItemPosition: SharedValue<Vector | null>;
40+
41+
// DIMENSIONS
42+
activeItemDimensions: SharedValue<Dimensions | null>;
43+
44+
// DRAG STATE
45+
prevActiveItemKey: SharedValue<null | string>;
46+
activeItemKey: SharedValue<null | string>;
47+
activationState: SharedValue<DragActivationState>;
48+
activeAnimationProgress: SharedValue<number>;
49+
inactiveAnimationProgress: SharedValue<number>;
50+
activeItemDropped: SharedValue<boolean>;
51+
};
52+
53+
// COMMON VALUES
54+
3455
/**
3556
* Context values shared between all providers.
3657
* (they are stored in a single context to make the access to them easier
@@ -45,27 +66,16 @@ export type CommonValuesContextType = {
4566

4667
// POSITIONS
4768
itemPositions: SharedValue<Record<string, Vector>>;
48-
touchPosition: SharedValue<Vector | null>;
49-
activeItemPosition: SharedValue<Vector | null>;
5069

5170
// DIMENSIONS
5271
controlledContainerDimensions: SharedValue<ControlledContainerDimensions>;
5372
measuredContainerWidth: SharedValue<null | number>;
5473
measuredContainerHeight: SharedValue<null | number>;
5574
containerWidth: SharedValue<null | number>;
5675
containerHeight: SharedValue<null | number>;
57-
activeItemDimensions: SharedValue<Dimensions | null>;
5876
itemDimensions: SharedValue<Record<string, Dimensions>>;
5977
itemsStyleOverride: SharedValue<Maybe<ViewStyle>>;
6078

61-
// DRAG STATE
62-
prevActiveItemKey: SharedValue<null | string>;
63-
activeItemKey: SharedValue<null | string>;
64-
activationState: SharedValue<DragActivationState>;
65-
activeAnimationProgress: SharedValue<number>;
66-
inactiveAnimationProgress: SharedValue<number>;
67-
activeItemDropped: SharedValue<boolean>;
68-
6979
// OTHER
7080
containerRef: AnimatedRef<View>;
7181
sortEnabled: SharedValue<boolean>;
@@ -75,7 +85,8 @@ export type CommonValuesContextType = {
7585
customHandle: boolean;
7686

7787
itemsOverridesStyle: AnimatedStyle<ViewStyle>;
78-
} & AnimatedValues<ActiveItemDecorationSettings> &
88+
} & ActiveItemValuesContextType &
89+
AnimatedValues<ActiveItemDecorationSettings> &
7990
AnimatedValues<ActiveItemSnapSettings> &
8091
AnimatedValues<Omit<ItemDragSettings, 'overDrag' | 'reorderTriggerOrigin'>>;
8192

0 commit comments

Comments
 (0)