Skip to content

Commit f25cd9e

Browse files
committed
Merge branch 'next' into @akwasniewski/logic-detector
2 parents 590a5cd + d76522f commit f25cd9e

File tree

22 files changed

+862
-57
lines changed

22 files changed

+862
-57
lines changed

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :
9898
val factory = RNGestureHandlerFactoryUtil.findFactoryForHandler<GestureHandler>(handler) ?: return
9999

100100
factory.setConfig(handler, config)
101+
102+
if (handler.actionType != GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
103+
interactionManager.dropRelationsForHandlerWithTag(handlerTag)
104+
interactionManager.configureInteractions(handler, config)
105+
}
101106
}
102107

103108
@ReactMethod

packages/react-native-gesture-handler/apple/RNGestureHandler.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ - (void)updateConfig:(NSDictionary *)config
182182
RCTLogError(@"Cannot have all of top, bottom and height defined");
183183
}
184184
}
185+
186+
if (_actionType != RNGestureHandlerActionTypeNativeDetector) {
187+
[self updateRelations:config];
188+
}
185189
}
186190

187191
- (void)updateRelations:(NSDictionary *)relations

packages/react-native-gesture-handler/src/RNGestureHandlerModule.web.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Config, PropsRef } from './web/interfaces';
66
import InteractionManager from './web/tools/InteractionManager';
77
import NodeManager from './web/tools/NodeManager';
88
import { GestureHandlerWebDelegate } from './web/tools/GestureHandlerWebDelegate';
9+
import { GestureRelations } from './v3/types';
910

1011
// init method is called inside attachGestureHandler function. However, this function may
1112
// fail when received view is not valid HTML element. On the other hand, dropGestureHandler
@@ -37,10 +38,6 @@ export default {
3738
handlerTag,
3839
new GestureClass(new GestureHandlerWebDelegate())
3940
);
40-
InteractionManager.instance.configureInteractions(
41-
NodeManager.getHandler(handlerTag),
42-
config as unknown as Config
43-
);
4441
this.setGestureHandlerConfig(handlerTag, config as unknown as Config);
4542
},
4643
attachGestureHandler(
@@ -75,11 +72,6 @@ export default {
7572
},
7673
setGestureHandlerConfig(handlerTag: number, newConfig: Config) {
7774
NodeManager.getHandler(handlerTag).setGestureConfig(newConfig);
78-
79-
InteractionManager.instance.configureInteractions(
80-
NodeManager.getHandler(handlerTag),
81-
newConfig
82-
);
8375
},
8476
updateGestureHandlerConfig(handlerTag: number, newConfig: Config) {
8577
NodeManager.getHandler(handlerTag).updateGestureConfig(newConfig);
@@ -95,6 +87,12 @@ export default {
9587

9688
NodeManager.dropGestureHandler(handlerTag);
9789
},
90+
configureRelations(handlerTag: number, relations: GestureRelations) {
91+
InteractionManager.instance.configureInteractions(
92+
NodeManager.getHandler(handlerTag),
93+
relations
94+
);
95+
},
9896
// eslint-disable-next-line @typescript-eslint/no-empty-function
9997
flushOperations() {},
10098
};

packages/react-native-gesture-handler/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,6 @@ export * from './v3/hooks/relations';
171171

172172
export { SingleGestureName } from './v3/types';
173173

174+
export * from './v3/hooks/gestures';
175+
174176
initialize();

packages/react-native-gesture-handler/src/v3/LogicDetector.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { useCallback, useEffect, useRef, useState } from 'react';
1+
import { RefObject, useCallback, useEffect, useRef, useState } from 'react';
22
import { Wrap } from '../handlers/gestures/GestureDetector/Wrap';
33
import { findNodeHandle, Platform } from 'react-native';
44
import { useDetectorContext } from './NativeDetector/useDetectorContext';
55
import { NativeDetectorProps } from './NativeDetector/NativeDetector';
66
import { isComposedGesture } from './hooks/utils/relationUtils';
7+
import { GestureEvents } from './types';
78

8-
export const LogicDetector = (props: NativeDetectorProps) => {
9+
export function LogicDetector<THandlerData, TConfig>(
10+
props: NativeDetectorProps<THandlerData, TConfig>
11+
) {
912
const { register, unregister } = useDetectorContext();
1013
const viewRef = useRef(null);
1114
const [viewTag, setViewTag] = useState<number>(-1);
@@ -60,12 +63,12 @@ export const LogicDetector = (props: NativeDetectorProps) => {
6063
: [props.gesture.tag],
6164
};
6265

63-
register(logicProps, logicMethods);
66+
register(logicProps, logicMethods as RefObject<GestureEvents<unknown>>);
6467

6568
return () => {
6669
unregister(viewTag);
6770
};
6871
}, [viewTag, props.gesture, register, unregister]);
6972

7073
return <Wrap ref={handleRef}>{props.children}</Wrap>;
71-
};
74+
}

packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import { Reanimated } from '../../handlers/gestures/reanimatedWrapper';
1313
import { configureRelations } from './utils';
1414
import { isComposedGesture } from '../hooks/utils/relationUtils';
1515

16-
export interface NativeDetectorProps {
16+
export interface NativeDetectorProps<THandlerData, TConfig> {
1717
children?: React.ReactNode;
18-
gesture: Gesture;
18+
gesture: Gesture<THandlerData, TConfig>;
1919
}
2020

2121
const AnimatedNativeDetector =
@@ -24,7 +24,10 @@ const AnimatedNativeDetector =
2424
const ReanimatedNativeDetector =
2525
Reanimated?.default.createAnimatedComponent(HostGestureDetector);
2626

27-
export function NativeDetector({ gesture, children }: NativeDetectorProps) {
27+
export function NativeDetector<THandlerData, TConfig>({
28+
gesture,
29+
children,
30+
}: NativeDetectorProps<THandlerData, TConfig>) {
2831
const [logicChildren, setLogicChildren] = useState<LogicChildren[]>([]);
2932
const logicMethods = useRef<Map<number, RefObject<GestureEvents<unknown>>>>(
3033
new Map()
@@ -71,8 +74,8 @@ export function NativeDetector({ gesture, children }: NativeDetectorProps) {
7174

7275
configureRelations(gesture);
7376

74-
const handleGestureEvent = (key: keyof GestureEvents<unknown>) => {
75-
return (e: GestureHandlerEvent<unknown>) => {
77+
const handleGestureEvent = (key: keyof GestureEvents<THandlerData>) => {
78+
return (e: GestureHandlerEvent<THandlerData>) => {
7679
if (gesture.gestureEvents[key]) {
7780
gesture.gestureEvents[key](e);
7881
}
@@ -88,7 +91,7 @@ export function NativeDetector({ gesture, children }: NativeDetectorProps) {
8891

8992
const getHandlers = useCallback(
9093
(key: keyof GestureEvents<unknown>) => {
91-
const handlers: ((e: GestureHandlerEvent<unknown>) => void)[] = [];
94+
const handlers: ((e: GestureHandlerEvent<THandlerData>) => void)[] = [];
9295

9396
if (gesture.gestureEvents[key]) {
9497
handlers.push(
@@ -101,7 +104,9 @@ export function NativeDetector({ gesture, children }: NativeDetectorProps) {
101104
logicMethods.current.forEach((ref) => {
102105
const handler = ref.current?.[key];
103106
if (handler) {
104-
handlers.push(handler as (e: GestureHandlerEvent<unknown>) => void);
107+
handlers.push(
108+
handler as (e: GestureHandlerEvent<THandlerData>) => void
109+
);
105110
}
106111
});
107112

packages/react-native-gesture-handler/src/v3/NativeDetector/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ export const traverseAndConfigureRelations = (
126126
});
127127
};
128128

129-
export function configureRelations(gesture: Gesture) {
129+
export function configureRelations<THandlerData, TConfig>(
130+
gesture: Gesture<THandlerData, TConfig>
131+
) {
130132
if (isComposedGesture(gesture)) {
131133
const simultaneousHandlers = new Set<number>(
132134
gesture.externalSimultaneousHandlers
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export type { TapGestureConfig } from './useTap';
2+
export { useTap } from './useTap';
3+
4+
export type { FlingGestureConfig } from './useFling';
5+
export { useFling } from './useFling';
6+
7+
export type { LongPressGestureConfig } from './useLongPress';
8+
export { useLongPress } from './useLongPress';
9+
10+
export type { PinchGestureConfig } from './usePinch';
11+
export { usePinch } from './usePinch';
12+
13+
export type { RotationGestureConfig } from './useRotation';
14+
export { useRotation } from './useRotation';
15+
16+
export type { HoverGestureConfig } from './useHover';
17+
export { useHover } from './useHover';
18+
19+
export type { ManualGestureConfig } from './useManual';
20+
export { useManual } from './useManual';
21+
22+
export type { NativeViewGestureConfig } from './useNative';
23+
export { useNative } from './useNative';
24+
25+
export type { PanGestureConfig } from './usePan';
26+
export { usePan } from './usePan';
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
BaseGestureConfig,
3+
ExcludeInternalConfigProps,
4+
SingleGestureName,
5+
} from '../../types';
6+
import { useGesture } from '../useGesture';
7+
import { cloneConfig } from '../utils';
8+
9+
type FlingGestureProperties = {
10+
/**
11+
* Expressed allowed direction of movement. It's possible to pass one or many
12+
* directions in one parameter:
13+
*
14+
* ```js
15+
* direction={Directions.RIGHT | Directions.LEFT}
16+
* ```
17+
*
18+
* or
19+
*
20+
* ```js
21+
* direction={Directions.DOWN}
22+
* ```
23+
*/
24+
direction?: number;
25+
26+
/**
27+
* Determine exact number of points required to handle the fling gesture.
28+
*/
29+
numberOfPointers?: number;
30+
};
31+
32+
type FlingHandlerData = {
33+
x: number;
34+
y: number;
35+
absoluteX: number;
36+
absoluteY: number;
37+
};
38+
39+
type FlingGestureInternalConfig = BaseGestureConfig<
40+
FlingHandlerData,
41+
FlingGestureProperties
42+
>;
43+
44+
export type FlingGestureConfig =
45+
ExcludeInternalConfigProps<FlingGestureInternalConfig>;
46+
47+
export function useFling(config: FlingGestureConfig) {
48+
const flingConfig = cloneConfig<FlingHandlerData, FlingGestureProperties>(
49+
config
50+
);
51+
52+
return useGesture(SingleGestureName.Fling, flingConfig);
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { StylusData } from '../../../handlers/gestureHandlerCommon';
2+
import { HoverEffect } from '../../../handlers/gestures/hoverGesture';
3+
import {
4+
BaseGestureConfig,
5+
ExcludeInternalConfigProps,
6+
SingleGestureName,
7+
} from '../../types';
8+
import { useGesture } from '../useGesture';
9+
import { cloneConfig } from '../utils';
10+
11+
type HoverGestureProperties = {
12+
/**
13+
* Visual effect applied to the view while the view is hovered. The possible values are:
14+
*
15+
* - `HoverEffect.None`
16+
* - `HoverEffect.Lift`
17+
* - `HoverEffect.Highlight`
18+
*
19+
* Defaults to `HoverEffect.None`
20+
*/
21+
hoverEffect?: HoverEffect;
22+
};
23+
24+
type HoverHandlerData = {
25+
x: number;
26+
y: number;
27+
absoluteX: number;
28+
absoluteY: number;
29+
stylusData: StylusData;
30+
};
31+
32+
type HoverGestureInternalConfig = BaseGestureConfig<
33+
HoverHandlerData,
34+
HoverGestureProperties
35+
>;
36+
37+
export type HoverGestureConfig =
38+
ExcludeInternalConfigProps<HoverGestureInternalConfig>;
39+
40+
export function useHover(config: HoverGestureConfig) {
41+
const hoverConfig = cloneConfig<HoverHandlerData, HoverGestureProperties>(
42+
config
43+
);
44+
45+
return useGesture(SingleGestureName.Hover, hoverConfig);
46+
}

0 commit comments

Comments
 (0)