|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +description: 'Hook that provides context with useful item-related values' |
| 4 | +--- |
| 5 | + |
| 6 | +# useItemContext |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +The `useItemContext` hook provides context with useful item-related values. |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +```tsx |
| 15 | +import { useItemContext } from 'react-native-sortables'; |
| 16 | + |
| 17 | +const ctx = useItemContext(); // inside a sortable item component |
| 18 | +``` |
| 19 | + |
| 20 | +## Return Values |
| 21 | + |
| 22 | +The `useItemContext` hook returns an object with the following properties: |
| 23 | + |
| 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 |
| 27 | +- `isActive` - whether the item is currently being dragged |
| 28 | +- `dragActivationState` - current drag activation state of the sortable component |
| 29 | +- `activationAnimationProgress` - progress of the activation animation (0 to 1) of the currently active item |
| 30 | + |
| 31 | +<details> |
| 32 | + <summary>Type definitions</summary> |
| 33 | + |
| 34 | +```tsx |
| 35 | +type ItemContextType = { |
| 36 | + itemKey: string; |
| 37 | + activeItemKey: Readonly<SharedValue<null | string>>; |
| 38 | + prevActiveItemKey: Readonly<SharedValue<null | string>>; |
| 39 | + isActive: Readonly<SharedValue<boolean>>; |
| 40 | + dragActivationState: Readonly<SharedValue<DragActivationState>>; |
| 41 | + activationAnimationProgress: Readonly<SharedValue<number>>; |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +</details> |
| 46 | + |
| 47 | +## Example |
| 48 | + |
| 49 | +Here's an example of how to use the `useItemContext` hook to create a custom item component that responds to drag states: |
| 50 | + |
| 51 | +```tsx |
| 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); |
| 67 | + |
| 68 | + return { |
| 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 | + ] |
| 85 | + }; |
| 86 | + }); |
| 87 | + |
| 88 | + return ( |
| 89 | + <Animated.View style={[styles.card, colorStyle, shakeStyle]}> |
| 90 | + <Text style={styles.text}>{item}</Text> |
| 91 | + </Animated.View> |
| 92 | + ); |
| 93 | +} |
| 94 | +``` |
| 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 | + |
| 102 | +## Remarks |
| 103 | + |
| 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