Skip to content

Commit 7b2ecaa

Browse files
authored
[Types] Remove onUpdate from discrete gestures (#3811)
## Description Discrete gestures shouldn't be sending `update` events, so the `onUpdate` method shouldn't be accepted by the type definitions. It also moves the type definitions for events and gestures above the hook definition. I'm pretty sure it was also correct before, but I think it's cleaner when things are declared before they are used. LMK what you think. ## Test plan ``` const tap = useTap({ onUpdate: () => { 'worklet'; }, }); const fling = useFling({ onUpdate: () => { 'worklet'; }, }); const longpress = useLongPress({ onUpdate: () => { 'worklet'; }, }); const native = useNative({ onUpdate: () => { 'worklet'; }, }); ```
1 parent 8e64fef commit 7b2ecaa

File tree

10 files changed

+115
-92
lines changed

10 files changed

+115
-92
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
BaseGestureConfig,
2+
BaseDiscreteGestureConfig,
33
ExcludeInternalConfigProps,
44
SingleGesture,
55
SingleGestureName,
@@ -20,27 +20,28 @@ type FlingHandlerData = {
2020

2121
type FlingGestureProperties = WithSharedValue<FlingGestureNativeProperties>;
2222

23-
type FlingGestureInternalConfig = BaseGestureConfig<
23+
type FlingGestureInternalConfig = BaseDiscreteGestureConfig<
2424
FlingHandlerData,
2525
FlingGestureProperties
2626
>;
2727

2828
export type FlingGestureConfig =
2929
ExcludeInternalConfigProps<FlingGestureInternalConfig>;
3030

31-
export function useFling(config: FlingGestureConfig) {
32-
const flingConfig = cloneConfig<FlingHandlerData, FlingGestureProperties>(
33-
config
34-
);
35-
36-
return useGesture(SingleGestureName.Fling, flingConfig);
37-
}
38-
3931
export type FlingGestureStateChangeEvent =
4032
GestureStateChangeEvent<FlingHandlerData>;
33+
4134
export type FlingGestureUpdateEvent = GestureUpdateEvent<FlingHandlerData>;
4235

4336
export type FlingGesture = SingleGesture<
4437
FlingHandlerData,
4538
FlingGestureProperties
4639
>;
40+
41+
export function useFling(config: FlingGestureConfig): FlingGesture {
42+
const flingConfig = cloneConfig<FlingHandlerData, FlingGestureProperties>(
43+
config
44+
);
45+
46+
return useGesture(SingleGestureName.Fling, flingConfig);
47+
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ type HoverGestureInternalConfig = BaseGestureConfig<
3737
export type HoverGestureConfig =
3838
ExcludeInternalConfigProps<HoverGestureInternalConfig>;
3939

40+
export type HoverGestureStateChangeEvent =
41+
GestureStateChangeEvent<HoverHandlerData>;
42+
43+
export type HoverGestureUpdateEvent = GestureUpdateEvent<HoverHandlerData>;
44+
45+
export type HoverGesture = SingleGesture<
46+
HoverHandlerData,
47+
HoverGestureProperties
48+
>;
49+
4050
function diffCalculator(
4151
current: HandlerData<HoverHandlerData>,
4252
previous: HandlerData<HoverHandlerData> | null
@@ -48,7 +58,7 @@ function diffCalculator(
4858
};
4959
}
5060

51-
export function useHover(config: HoverGestureConfig) {
61+
export function useHover(config: HoverGestureConfig): HoverGesture {
5262
const hoverConfig = cloneConfig<HoverHandlerData, HoverGestureProperties>(
5363
config
5464
);
@@ -57,12 +67,3 @@ export function useHover(config: HoverGestureConfig) {
5767

5868
return useGesture(SingleGestureName.Hover, hoverConfig);
5969
}
60-
61-
export type HoverGestureStateChangeEvent =
62-
GestureStateChangeEvent<HoverHandlerData>;
63-
export type HoverGestureUpdateEvent = GestureUpdateEvent<HoverHandlerData>;
64-
65-
export type HoverGesture = SingleGesture<
66-
HoverHandlerData,
67-
HoverGestureProperties
68-
>;

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
BaseGestureConfig,
2+
BaseDiscreteGestureConfig,
33
ExcludeInternalConfigProps,
44
SingleGesture,
55
SingleGestureName,
@@ -29,14 +29,25 @@ type LongPressGestureInternalProperties =
2929
WithSharedValue<LongPressGestureNativeProperties>;
3030

3131
export type LongPressGestureConfig = ExcludeInternalConfigProps<
32-
BaseGestureConfig<LongPressHandlerData, LongPressGestureProperties>
32+
BaseDiscreteGestureConfig<LongPressHandlerData, LongPressGestureProperties>
3333
>;
3434

35-
type LongPressGestureInternalConfig = BaseGestureConfig<
35+
type LongPressGestureInternalConfig = BaseDiscreteGestureConfig<
3636
LongPressHandlerData,
3737
LongPressGestureInternalProperties
3838
>;
3939

40+
export type LongPressGestureStateChangeEvent =
41+
GestureStateChangeEvent<LongPressHandlerData>;
42+
43+
export type LongPressGestureUpdateEvent =
44+
GestureUpdateEvent<LongPressHandlerData>;
45+
46+
export type LongPressGesture = SingleGesture<
47+
LongPressHandlerData,
48+
LongPressGestureProperties
49+
>;
50+
4051
const LongPressPropsMapping = new Map<
4152
keyof LongPressGestureProperties,
4253
keyof LongPressGestureInternalProperties
@@ -45,7 +56,7 @@ const LongPressPropsMapping = new Map<
4556
['maxDistance', 'maxDist'],
4657
]);
4758

48-
export function useLongPress(config: LongPressGestureConfig) {
59+
export function useLongPress(config: LongPressGestureConfig): LongPressGesture {
4960
const longPressConfig = cloneConfig<
5061
LongPressHandlerData,
5162
LongPressGestureInternalProperties
@@ -65,13 +76,3 @@ export function useLongPress(config: LongPressGestureConfig) {
6576
longPressConfig
6677
);
6778
}
68-
69-
export type LongPressGestureStateChangeEvent =
70-
GestureStateChangeEvent<LongPressHandlerData>;
71-
export type LongPressGestureUpdateEvent =
72-
GestureUpdateEvent<LongPressHandlerData>;
73-
74-
export type LongPressGesture = SingleGesture<
75-
LongPressHandlerData,
76-
LongPressGestureProperties
77-
>;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@ type ManualGestureInternalConfig = BaseGestureConfig<
1818
ManualHandlerData,
1919
ManualGestureProperties
2020
>;
21+
2122
export type ManualGestureConfig =
2223
ExcludeInternalConfigProps<ManualGestureInternalConfig>;
2324

24-
export function useManual(config: ManualGestureConfig) {
25-
const manualConfig = cloneConfig<ManualHandlerData, ManualGestureProperties>(
26-
config
27-
);
28-
29-
return useGesture(SingleGestureName.Manual, manualConfig);
30-
}
31-
3225
export type ManualGestureStateChangeEvent =
3326
GestureStateChangeEvent<ManualHandlerData>;
3427
export type ManualGestureUpdateEvent = GestureUpdateEvent<ManualHandlerData>;
@@ -37,3 +30,11 @@ export type ManualGesture = SingleGesture<
3730
ManualHandlerData,
3831
ManualGestureProperties
3932
>;
33+
34+
export function useManual(config: ManualGestureConfig): ManualGesture {
35+
const manualConfig = cloneConfig<ManualHandlerData, ManualGestureProperties>(
36+
config
37+
);
38+
39+
return useGesture(SingleGestureName.Manual, manualConfig);
40+
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
BaseGestureConfig,
2+
BaseDiscreteGestureConfig,
33
ExcludeInternalConfigProps,
44
SingleGesture,
55
SingleGestureName,
@@ -18,29 +18,30 @@ type NativeViewHandlerData = {
1818
type NativeViewGestureProperties =
1919
WithSharedValue<NativeGestureNativeProperties>;
2020

21-
type NativeViewGestureInternalConfig = BaseGestureConfig<
21+
type NativeViewGestureInternalConfig = BaseDiscreteGestureConfig<
2222
NativeViewHandlerData,
2323
NativeViewGestureProperties
2424
>;
2525

2626
export type NativeViewGestureConfig =
2727
ExcludeInternalConfigProps<NativeViewGestureInternalConfig>;
2828

29-
export function useNative(config: NativeViewGestureConfig) {
30-
const nativeConfig = cloneConfig<
31-
NativeViewHandlerData,
32-
NativeViewGestureProperties
33-
>(config);
34-
35-
return useGesture(SingleGestureName.Native, nativeConfig);
36-
}
37-
3829
export type NativeGestureStateChangeEvent =
3930
GestureStateChangeEvent<NativeViewHandlerData>;
31+
4032
export type NativeGestureUpdateEvent =
4133
GestureUpdateEvent<NativeViewHandlerData>;
4234

4335
export type NativeGesture = SingleGesture<
4436
NativeViewHandlerData,
4537
NativeViewGestureProperties
4638
>;
39+
40+
export function useNative(config: NativeViewGestureConfig): NativeGesture {
41+
const nativeConfig = cloneConfig<
42+
NativeViewHandlerData,
43+
NativeViewGestureProperties
44+
>(config);
45+
46+
return useGesture(SingleGestureName.Native, nativeConfig);
47+
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ type PanGestureInternalConfig = BaseGestureConfig<
5151
PanGestureInternalProperties
5252
>;
5353

54+
export type PanGestureStateChangeEvent =
55+
GestureStateChangeEvent<PanHandlerData>;
56+
57+
export type PanGestureUpdateEvent = GestureUpdateEvent<PanHandlerData>;
58+
59+
export type PanGesture = SingleGesture<PanHandlerData, PanGestureProperties>;
60+
5461
const PanPropsMapping = new Map<
5562
keyof PanGestureProperties,
5663
keyof PanGestureInternalProperties
@@ -154,7 +161,7 @@ function diffCalculator(
154161
};
155162
}
156163

157-
export function usePan(config: PanGestureConfig) {
164+
export function usePan(config: PanGestureConfig): PanGesture {
158165
if (__DEV__) {
159166
validatePanConfig(config);
160167
}
@@ -177,9 +184,3 @@ export function usePan(config: PanGestureConfig) {
177184
panConfig
178185
);
179186
}
180-
181-
export type PanGestureStateChangeEvent =
182-
GestureStateChangeEvent<PanHandlerData>;
183-
export type PanGestureUpdateEvent = GestureUpdateEvent<PanHandlerData>;
184-
185-
export type PanGesture = SingleGesture<PanHandlerData, PanGestureProperties>;

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ type PinchGestureInternalConfig = BaseGestureConfig<
2929
export type PinchGestureConfig =
3030
ExcludeInternalConfigProps<PinchGestureInternalConfig>;
3131

32+
export type PinchGestureStateChangeEvent =
33+
GestureStateChangeEvent<PinchHandlerData>;
34+
35+
export type PinchGestureUpdateEvent = GestureUpdateEvent<PinchHandlerData>;
36+
37+
export type PinchGesture = SingleGesture<
38+
PinchHandlerData,
39+
PinchGestureProperties
40+
>;
41+
3242
function diffCalculator(
3343
current: HandlerData<PinchHandlerData>,
3444
previous: HandlerData<PinchHandlerData> | null
@@ -39,7 +49,7 @@ function diffCalculator(
3949
};
4050
}
4151

42-
export function usePinch(config: PinchGestureConfig) {
52+
export function usePinch(config: PinchGestureConfig): PinchGesture {
4353
const pinchConfig = cloneConfig<PinchHandlerData, PinchGestureProperties>(
4454
config
4555
);
@@ -48,12 +58,3 @@ export function usePinch(config: PinchGestureConfig) {
4858

4959
return useGesture(SingleGestureName.Pinch, pinchConfig);
5060
}
51-
52-
export type PinchGestureStateChangeEvent =
53-
GestureStateChangeEvent<PinchHandlerData>;
54-
export type PinchGestureUpdateEvent = GestureUpdateEvent<PinchHandlerData>;
55-
56-
export type PinchGesture = SingleGesture<
57-
PinchHandlerData,
58-
PinchGestureProperties
59-
>;

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ type RotationGestureInternalConfig = BaseGestureConfig<
2929
export type RotationGestureConfig =
3030
ExcludeInternalConfigProps<RotationGestureInternalConfig>;
3131

32+
export type RotationGestureStateChangeEvent =
33+
GestureStateChangeEvent<RotationHandlerData>;
34+
35+
export type RotationGestureUpdateEvent =
36+
GestureUpdateEvent<RotationHandlerData>;
37+
38+
export type RotationGesture = SingleGesture<
39+
RotationHandlerData,
40+
RotationGestureProperties
41+
>;
42+
3243
function diffCalculator(
3344
current: HandlerData<RotationHandlerData>,
3445
previous: HandlerData<RotationHandlerData> | null
@@ -41,7 +52,7 @@ function diffCalculator(
4152
};
4253
}
4354

44-
export function useRotation(config: RotationGestureConfig) {
55+
export function useRotation(config: RotationGestureConfig): RotationGesture {
4556
const rotationConfig = cloneConfig<
4657
RotationHandlerData,
4758
RotationGestureProperties
@@ -52,13 +63,3 @@ export function useRotation(config: RotationGestureConfig) {
5263

5364
return useGesture(SingleGestureName.Rotation, rotationConfig);
5465
}
55-
56-
export type RotationGestureStateChangeEvent =
57-
GestureStateChangeEvent<RotationHandlerData>;
58-
export type RotationGestureUpdateEvent =
59-
GestureUpdateEvent<RotationHandlerData>;
60-
61-
export type RotationGesture = SingleGesture<
62-
RotationHandlerData,
63-
RotationGestureProperties
64-
>;

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
BaseGestureConfig,
2+
BaseDiscreteGestureConfig,
33
ExcludeInternalConfigProps,
44
GestureStateChangeEvent,
55
GestureUpdateEvent,
6-
SingleGesture,
6+
DiscreteSingleGesture,
77
SingleGestureName,
88
WithSharedValue,
99
} from '../../../types';
@@ -26,14 +26,24 @@ type TapGestureProperties = WithSharedValue<TapGestureExternalConfig>;
2626
type TapGestureInternalProperties = WithSharedValue<TapGestureNativeConfig>;
2727

2828
export type TapGestureConfig = ExcludeInternalConfigProps<
29-
BaseGestureConfig<TapHandlerData, TapGestureProperties>
29+
BaseDiscreteGestureConfig<TapHandlerData, TapGestureProperties>
3030
>;
3131

32-
type TapGestureInternalConfig = BaseGestureConfig<
32+
type TapGestureInternalConfig = BaseDiscreteGestureConfig<
3333
TapHandlerData,
3434
TapGestureInternalProperties
3535
>;
3636

37+
export type TapGestureStateChangeEvent =
38+
GestureStateChangeEvent<TapHandlerData>;
39+
40+
export type TapGestureUpdateEvent = GestureUpdateEvent<TapHandlerData>;
41+
42+
export type TapGesture = DiscreteSingleGesture<
43+
TapHandlerData,
44+
TapGestureProperties
45+
>;
46+
3747
const TapPropsMapping = new Map<
3848
keyof TapGestureProperties,
3949
keyof TapGestureInternalProperties
@@ -43,7 +53,7 @@ const TapPropsMapping = new Map<
4353
['maxDelay', 'maxDelayMs'],
4454
]);
4555

46-
export function useTap(config: TapGestureConfig) {
56+
export function useTap(config: TapGestureConfig): TapGesture {
4757
const tapConfig = cloneConfig<TapHandlerData, TapGestureInternalProperties>(
4858
config
4959
);
@@ -58,9 +68,3 @@ export function useTap(config: TapGestureConfig) {
5868
tapConfig
5969
);
6070
}
61-
62-
export type TapGestureStateChangeEvent =
63-
GestureStateChangeEvent<TapHandlerData>;
64-
export type TapGestureUpdateEvent = GestureUpdateEvent<TapHandlerData>;
65-
66-
export type TapGesture = SingleGesture<TapHandlerData, TapGestureProperties>;

0 commit comments

Comments
 (0)