Skip to content

Commit 50805b0

Browse files
authored
Add SharedValue type to config. (#3717)
## Description This PR updates types to accept `SharedValues` into config objects. ## Test plan <details> <summary>Tested on the following example:</summary> ```tsx import * as React from 'react'; import { Animated, Button } from 'react-native'; import { GestureHandlerRootView, MouseButton, NativeDetector, usePan, } from 'react-native-gesture-handler'; import { useSharedValue } from 'react-native-reanimated'; export default function App() { const [visible, setVisible] = React.useState(true); const sv1 = useSharedValue(0); const sv2 = useSharedValue(MouseButton.LEFT); const gesture = usePan({ onEnd: (e) => { 'worklet'; console.log('tap', e.handlerData); }, activeOffsetX: [sv1, 120], mouseButton: sv2, }); return ( <GestureHandlerRootView style={{ flex: 1, backgroundColor: 'white', paddingTop: 8 }}> <Button title="Toggle visibility" onPress={() => { setVisible(!visible); }} /> {visible && ( <NativeDetector gesture={gesture}> <Animated.View style={[ { width: 150, height: 150, backgroundColor: 'blue', opacity: 0.5, borderWidth: 10, borderColor: 'green', marginTop: 20, marginLeft: 40, }, ]} /> </NativeDetector> )} </GestureHandlerRootView> ); } ``` </details>
1 parent 4a12b70 commit 50805b0

File tree

11 files changed

+186
-143
lines changed

11 files changed

+186
-143
lines changed

packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { SharedValue } from '../../../v3/types';
12
import { GestureType, HandlerCallbacks } from '../gesture';
2-
import { SharedValue } from '../reanimatedWrapper';
33

44
export interface AttachedGestureState {
55
// Array of gestures that should be attached to the view under that gesture detector

packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import { ComponentClass } from 'react';
22
import { tagMessage } from '../../utils';
3-
import { GestureCallbacks, GestureUpdateEvent } from '../../v3/types';
4-
5-
export interface SharedValue<Value = unknown> {
6-
value: Value;
7-
get(): Value;
8-
set(value: Value | ((value: Value) => Value)): void;
9-
addListener: (listenerID: number, listener: (value: Value) => void) => void;
10-
removeListener: (listenerID: number) => void;
11-
modify: (
12-
modifier?: <T extends Value>(value: T) => T,
13-
forceUpdate?: boolean
14-
) => void;
15-
}
3+
import {
4+
GestureCallbacks,
5+
GestureUpdateEvent,
6+
SharedValue,
7+
} from '../../v3/types';
168

179
export type ReanimatedContext<THandlerData> = {
1810
lastUpdateEvent: GestureUpdateEvent<THandlerData> | undefined;
@@ -52,7 +44,7 @@ let Reanimated:
5244
) => (event: unknown) => void;
5345
useSharedValue: <T>(value: T) => SharedValue<T>;
5446
setGestureState: (handlerTag: number, newState: number) => void;
55-
isSharedValue: (value: unknown) => value is SharedValue<unknown>;
47+
isSharedValue: <T = unknown>(value: unknown) => value is SharedValue<T>;
5648
isWorkletFunction<
5749
Args extends unknown[] = unknown[],
5850
ReturnValue = unknown,

packages/react-native-gesture-handler/src/v3/hooks/gestures/useFling.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
BaseGestureConfig,
33
ExcludeInternalConfigProps,
44
SingleGestureName,
5+
WithSharedValue,
56
} from '../../types';
67
import { useGesture } from '../useGesture';
78
import { cloneConfig } from '../utils';
89

9-
type FlingGestureProperties = {
10+
type FlingGestureProperties = WithSharedValue<{
1011
/**
1112
* Expressed allowed direction of movement. It's possible to pass one or many
1213
* directions in one parameter:
@@ -27,7 +28,7 @@ type FlingGestureProperties = {
2728
* Determine exact number of points required to handle the fling gesture.
2829
*/
2930
numberOfPointers?: number;
30-
};
31+
}>;
3132

3233
type FlingHandlerData = {
3334
x: number;

packages/react-native-gesture-handler/src/v3/hooks/gestures/useHover.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ import {
55
ExcludeInternalConfigProps,
66
HandlerData,
77
SingleGestureName,
8+
WithSharedValue,
89
} from '../../types';
910
import { useGesture } from '../useGesture';
1011
import { cloneConfig, getChangeEventCalculator } from '../utils';
1112

12-
type HoverGestureProperties = {
13-
/**
14-
* Visual effect applied to the view while the view is hovered. The possible values are:
15-
*
16-
* - `HoverEffect.None`
17-
* - `HoverEffect.Lift`
18-
* - `HoverEffect.Highlight`
19-
*
20-
* Defaults to `HoverEffect.None`
21-
*/
22-
hoverEffect?: HoverEffect;
23-
};
13+
type HoverGestureProperties = WithSharedValue<
14+
{
15+
/**
16+
* Visual effect applied to the view while the view is hovered. The possible values are:
17+
*
18+
* - `HoverEffect.None`
19+
* - `HoverEffect.Lift`
20+
* - `HoverEffect.Highlight`
21+
*
22+
* Defaults to `HoverEffect.None`
23+
*/
24+
hoverEffect?: HoverEffect;
25+
},
26+
HoverEffect
27+
>;
2428

2529
type HoverHandlerData = {
2630
x: number;

packages/react-native-gesture-handler/src/v3/hooks/gestures/useLongPress.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
BaseGestureConfig,
33
ExcludeInternalConfigProps,
44
SingleGestureName,
5+
WithSharedValue,
56
} from '../../types';
67
import { useGesture } from '../useGesture';
78
import { cloneConfig, remapProps } from '../utils';
89

9-
type LongPressGestureProperties = {
10+
type LongPressGestureProperties = WithSharedValue<{
1011
/**
1112
* Minimum time, expressed in milliseconds, that a finger must remain pressed on
1213
* the corresponding view. The default value is 500.
@@ -25,13 +26,13 @@ type LongPressGestureProperties = {
2526
* Determine exact number of points required to handle the long press gesture.
2627
*/
2728
numberOfPointers?: number;
28-
};
29+
}>;
2930

30-
type LongPressGestureInternalProperties = {
31+
type LongPressGestureInternalProperties = WithSharedValue<{
3132
minDurationMs?: number;
3233
maxDist?: number;
3334
numberOfPointers?: number;
34-
};
35+
}>;
3536

3637
type LongPressHandlerData = {
3738
x: number;

packages/react-native-gesture-handler/src/v3/hooks/gestures/useNative.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
BaseGestureConfig,
33
ExcludeInternalConfigProps,
44
SingleGestureName,
5+
WithSharedValue,
56
} from '../../types';
67
import { useGesture } from '../useGesture';
78
import { cloneConfig } from '../utils';
89

9-
type NativeViewGestureProperties = {
10+
type NativeViewGestureProperties = WithSharedValue<{
1011
/**
1112
* Android only.
1213
*
@@ -20,7 +21,7 @@ type NativeViewGestureProperties = {
2021
* `NativeViewGestureHandler` receives an `ACTIVE` state event.
2122
*/
2223
disallowInterruption?: boolean;
23-
};
24+
}>;
2425

2526
type NativeViewHandlerData = {
2627
pointerInside: boolean;

0 commit comments

Comments
 (0)