@@ -22,6 +22,8 @@ const ctx = useItemContext(); // inside a sortable item component
22
22
The ` useItemContext ` hook returns an object with the following properties:
23
23
24
24
- ` 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
25
27
- ` isActive ` - whether the item is currently being dragged
26
28
- ` dragActivationState ` - current drag activation state of the sortable component
27
29
- ` 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:
32
34
``` tsx
33
35
type ItemContextType = {
34
36
itemKey: string ;
37
+ activeItemKey: Readonly <SharedValue <null | string >>;
38
+ prevActiveItemKey: Readonly <SharedValue <null | string >>;
35
39
isActive: Readonly <SharedValue <boolean >>;
36
40
dragActivationState: Readonly <SharedValue <DragActivationState >>;
37
41
activationAnimationProgress: Readonly <SharedValue <number >>;
@@ -45,37 +49,62 @@ type ItemContextType = {
45
49
Here's an example of how to use the ` useItemContext ` hook to create a custom item component that responds to drag states:
46
50
47
51
``` 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 );
54
67
55
- const animatedStyle = useAnimatedStyle (() => {
56
68
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
+ ]
59
85
};
60
86
});
61
87
62
88
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 >
66
92
);
67
93
}
68
-
69
- const styles = StyleSheet .create ({
70
- item: {
71
- padding: 16 ,
72
- backgroundColor: ' #f0f0f0' ,
73
- borderRadius: 8 ,
74
- marginBottom: 8
75
- }
76
- });
77
94
```
78
95
96
+ ### Result
97
+
98
+ import itemContextVideo from ' @site/static/video/item-context.mp4' ;
99
+
100
+ <video autoPlay loop muted width = ' 400px' src = { itemContextVideo } />
101
+
79
102
## Remarks
80
103
81
104
- 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
+ :::
0 commit comments