Skip to content

Commit 63e1df4

Browse files
committed
Revert playground example
1 parent 0e55271 commit 63e1df4

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

packages/docs/docs/hooks/useItemContext.mdx

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const ctx = useItemContext(); // inside a sortable item component
2222
The `useItemContext` hook returns an object with the following properties:
2323

2424
- `itemKey` - key of the item where the hook is called
25+
- `activeItemKey` - key of the currently active item
26+
- `prevActiveItemKey` - key of the previously active item
2527
- `isActive` - whether the item is currently being dragged
2628
- `dragActivationState` - current drag activation state of the sortable component
2729
- `activationAnimationProgress` - progress of the activation animation (0 to 1) of the currently active item
@@ -32,6 +34,8 @@ The `useItemContext` hook returns an object with the following properties:
3234
```tsx
3335
type ItemContextType = {
3436
itemKey: string;
37+
activeItemKey: Readonly<SharedValue<null | string>>;
38+
prevActiveItemKey: Readonly<SharedValue<null | string>>;
3539
isActive: Readonly<SharedValue<boolean>>;
3640
dragActivationState: Readonly<SharedValue<DragActivationState>>;
3741
activationAnimationProgress: Readonly<SharedValue<number>>;
@@ -45,37 +49,62 @@ type ItemContextType = {
4549
Here's an example of how to use the `useItemContext` hook to create a custom item component that responds to drag states:
4650

4751
```tsx
48-
import { useItemContext } from 'react-native-sortables';
49-
import { StyleSheet, Text, View } from 'react-native';
50-
import { useAnimatedStyle } from 'react-native-reanimated';
51-
52-
function CustomItem({ item }) {
53-
const { isActive, activationAnimationProgress } = useItemContext();
52+
function GridItem({ item }: { item: string }) {
53+
// highlight-start
54+
const { activationAnimationProgress, dragActivationState } = useItemContext();
55+
// highlight-end
56+
57+
const colorStyle = useAnimatedStyle(() => ({
58+
backgroundColor: interpolateColor(
59+
activationAnimationProgress.value,
60+
[0, 1],
61+
['#36877F', '#063934']
62+
)
63+
}));
64+
65+
const shakeStyle = useAnimatedStyle(() => {
66+
const easeOut = Easing.out(Easing.quad);
5467

55-
const animatedStyle = useAnimatedStyle(() => {
5668
return {
57-
opacity: isActive.value ? 0.8 : 1,
58-
transform: [{ scale: 1 + activationAnimationProgress.value * 0.1 }]
69+
transform: [
70+
dragActivationState.value === DragActivationState.ACTIVE
71+
? {
72+
rotate: withSequence(
73+
withTiming('0.08rad', { duration: 80, easing: Easing.linear }),
74+
withTiming('-0.08rad', { duration: 80, easing: Easing.linear }),
75+
withTiming('0.08rad', { duration: 80, easing: Easing.linear }),
76+
withTiming('-0.06rad', { duration: 80, easing: Easing.linear }),
77+
withTiming('0.06rad', { duration: 80, easing: Easing.linear }),
78+
withTiming('-0.04rad', { duration: 80, easing: Easing.linear }),
79+
withTiming('0.04rad', { duration: 80, easing: Easing.linear }),
80+
withTiming('0rad', { duration: 100, easing: easeOut })
81+
)
82+
}
83+
: { rotate: withTiming('0rad', { duration: 100, easing: easeOut }) }
84+
]
5985
};
6086
});
6187

6288
return (
63-
<View style={[styles.item, animatedStyle]}>
64-
<Text>{item}</Text>
65-
</View>
89+
<Animated.View style={[styles.card, colorStyle, shakeStyle]}>
90+
<Text style={styles.text}>{item}</Text>
91+
</Animated.View>
6692
);
6793
}
68-
69-
const styles = StyleSheet.create({
70-
item: {
71-
padding: 16,
72-
backgroundColor: '#f0f0f0',
73-
borderRadius: 8,
74-
marginBottom: 8
75-
}
76-
});
7794
```
7895

96+
### Result
97+
98+
import itemContextVideo from '@site/static/video/item-context.mp4';
99+
100+
<video autoPlay loop muted width='400px' src={itemContextVideo} />
101+
79102
## Remarks
80103

81104
- The `useItemContext` hook must be used within a component that is rendered as part of a sortable item.
105+
106+
:::info
107+
108+
If you need to access other values, please request them in the [GitHub Discussions](https://github.com/matipl01/react-native-sortables/discussions). There are other properties that can be exposed in the `ItemContextType` type.
109+
110+
:::
634 KB
Binary file not shown.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from './components';
1111
export { useItemContext } from './providers';
1212
import { PortalProvider } from './providers';
13-
1413
export type { SortableHandleProps, SortableLayerProps } from './components';
1514
export * from './constants/layoutAnimations';
1615
export type {
@@ -33,6 +32,7 @@ export type {
3332
SortableGridRenderItem,
3433
SortableGridStrategyFactory
3534
} from './types';
35+
export { DragActivationState } from './types';
3636

3737
/** Collection of sortable components and utilities for React Native */
3838
const Sortable = {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@ import { useCommonValuesContext } from './CommonValuesProvider';
77
type ItemContextProviderProps = PropsWithChildren<
88
{
99
itemKey: string;
10-
} & Omit<ItemContextType, 'dragActivationState'>
10+
} & Pick<ItemContextType, 'activationAnimationProgress' | 'isActive'>
1111
>;
1212

1313
const { ItemContextProvider, useItemContextContext: useItemContext } =
1414
createProvider('ItemContext', { guarded: true })<
1515
ItemContextProviderProps,
1616
ItemContextType
1717
>(props => {
18-
const { activationState } = useCommonValuesContext();
18+
const { activationState, activeItemKey, prevActiveItemKey } =
19+
useCommonValuesContext();
1920

20-
return { value: { ...props, dragActivationState: activationState } };
21+
return {
22+
value: {
23+
...props,
24+
activeItemKey,
25+
dragActivationState: activationState,
26+
prevActiveItemKey
27+
}
28+
};
2129
});
2230

2331
export { ItemContextProvider, useItemContext };

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export type DragContextType = {
136136

137137
export type ItemContextType = {
138138
itemKey: string;
139+
activeItemKey: Readonly<SharedValue<null | string>>;
140+
prevActiveItemKey: Readonly<SharedValue<null | string>>;
139141
isActive: Readonly<SharedValue<boolean>>;
140142
dragActivationState: Readonly<SharedValue<DragActivationState>>;
141143
activationAnimationProgress: Readonly<SharedValue<number>>;

0 commit comments

Comments
 (0)