Skip to content

Commit ae212ba

Browse files
authored
feat: Item context and docs (#363)
## Description This PR adds missing `useItemContext` hook export, more values to the `ItemContext` and related docs with the usage example.
1 parent 45b2eec commit ae212ba

File tree

6 files changed

+132
-4
lines changed

6 files changed

+132
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Hooks",
3+
"position": 6,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
:::
634 KB
Binary file not shown.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
SortableHandle,
99
SortableLayer
1010
} from './components';
11+
export { useItemContext } from './providers';
1112
import { PortalProvider } from './providers';
12-
1313
export type { SortableHandleProps, SortableLayerProps } from './components';
1414
export * from './constants/layoutAnimations';
1515
export type {
@@ -32,6 +32,7 @@ export type {
3232
SortableGridRenderItem,
3333
SortableGridStrategyFactory
3434
} from './types';
35+
export { DragActivationState } from './types';
3536

3637
/** Collection of sortable components and utilities for React Native */
3738
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)