Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Animated, {
Extrapolation,
runOnUI,
cancelAnimation,
useWorkletCallback,
type WithSpringConfig,
type WithTimingConfig,
type SharedValue,
Expand Down Expand Up @@ -569,13 +568,15 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
//#endregion

//#region animation
const stopAnimation = useWorkletCallback(() => {
const stopAnimation = useCallback(() => {
'worklet';
cancelAnimation(animatedPosition);
animatedAnimationSource.value = ANIMATION_SOURCE.NONE;
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
}, [animatedPosition, animatedAnimationState, animatedAnimationSource]);
const animateToPositionCompleted = useWorkletCallback(
const animateToPositionCompleted = useCallback(
function animateToPositionCompleted(isFinished?: boolean) {
'worklet';
if (!isFinished) {
return;
}
Expand Down Expand Up @@ -603,15 +604,17 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
animatedNextPosition.value = INITIAL_VALUE;
animatedNextPositionIndex.value = INITIAL_VALUE;
animatedContainerHeightDidChange.value = false;
}
},
[]
);
const animateToPosition: AnimateToPositionType = useWorkletCallback(
const animateToPosition: AnimateToPositionType = useCallback(
function animateToPosition(
position: number,
source: ANIMATION_SOURCE,
velocity = 0,
configs?: WithTimingConfig | WithSpringConfig
) {
'worklet';
if (__DEV__) {
runOnJS(print)({
component: 'BottomSheet',
Expand Down Expand Up @@ -694,9 +697,10 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
*
* @param targetPosition position to be set.
*/
const setToPosition = useWorkletCallback(function setToPosition(
const setToPosition = useCallback(function setToPosition(
targetPosition: number
) {
'worklet';
if (
targetPosition === animatedPosition.value ||
targetPosition === undefined ||
Expand Down Expand Up @@ -737,7 +741,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
* Calculate and evaluate the current position based on multiple
* local states.
*/
const getEvaluatedPosition = useWorkletCallback(
const getEvaluatedPosition = useCallback(
function getEvaluatedPosition(source: ANIMATION_SOURCE) {
'worklet';
const currentIndex = animatedCurrentIndex.value;
Expand Down Expand Up @@ -847,11 +851,12 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
/**
* Evaluate the bottom sheet position based based on a event source and other local states.
*/
const evaluatePosition = useWorkletCallback(
const evaluatePosition = useCallback(
function evaluatePosition(
source: ANIMATION_SOURCE,
animationConfigs?: WithSpringConfig | WithTimingConfig
) {
'worklet';
/**
* if a force closing is running and source not from user, then we early exit
*/
Expand Down Expand Up @@ -1026,11 +1031,12 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
animationConfigs
);
});
const handleSnapToPosition = useWorkletCallback(
const handleSnapToPosition = useCallback(
function handleSnapToPosition(
position: number | string,
animationConfigs?: WithSpringConfig | WithTimingConfig
) {
'worklet';
if (__DEV__) {
print({
component: BottomSheet.name,
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/useGestureEventsHandlersDefault.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from 'react';
import { Keyboard, Platform } from 'react-native';
import {
runOnJS,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';
import {
ANIMATION_SOURCE,
Expand Down Expand Up @@ -70,8 +70,9 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
//#endregion

//#region gesture methods
const handleOnStart: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnStart: GestureEventHandlerCallbackType = useCallback(
function handleOnStart(__, _) {
'worklet';
// cancel current animation
stopAnimation();

Expand Down Expand Up @@ -111,8 +112,9 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
animatedScrollableContentOffsetY,
]
);
const handleOnChange: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnChange: GestureEventHandlerCallbackType = useCallback(
function handleOnChange(source, { translationY }) {
'worklet';
let highestSnapPoint = animatedHighestSnapPoint.value;

/**
Expand Down Expand Up @@ -267,8 +269,9 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
animatedScrollableContentOffsetY,
]
);
const handleOnEnd: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnEnd: GestureEventHandlerCallbackType = useCallback(
function handleOnEnd(source, { translationY, absoluteY, velocityY }) {
'worklet';
const highestSnapPoint = animatedHighestSnapPoint.value;
const isSheetAtHighestSnapPoint =
animatedPosition.value === highestSnapPoint;
Expand Down Expand Up @@ -402,8 +405,9 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
);

const handleOnFinalize: GestureEventHandlerCallbackType =
useWorkletCallback(
useCallback(
function handleOnFinalize() {
'worklet';
resetContext(context);
},
[context]
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/useGestureEventsHandlersDefault.web.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from 'react';
import { Keyboard, Platform } from 'react-native';
import {
runOnJS,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';
import {
ANIMATION_SOURCE,
Expand Down Expand Up @@ -67,8 +67,9 @@ export const useGestureEventsHandlersDefault = () => {
//#endregion

//#region gesture methods
const handleOnStart: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnStart: GestureEventHandlerCallbackType = useCallback(
function handleOnStart(__, { translationY }) {
'worklet';
// cancel current animation
stopAnimation();

Expand All @@ -95,8 +96,9 @@ export const useGestureEventsHandlersDefault = () => {
animatedScrollableContentOffsetY,
]
);
const handleOnChange: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnChange: GestureEventHandlerCallbackType = useCallback(
function handleOnChange(source, { translationY }) {
'worklet';
let highestSnapPoint = animatedHighestSnapPoint.value;

translationY = translationY - context.value.initialTranslationY;
Expand Down Expand Up @@ -249,8 +251,9 @@ export const useGestureEventsHandlersDefault = () => {
animatedScrollableContentOffsetY,
]
);
const handleOnEnd: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnEnd: GestureEventHandlerCallbackType = useCallback(
function handleOnEnd(source, { translationY, absoluteY, velocityY }) {
'worklet';
const highestSnapPoint = animatedHighestSnapPoint.value;
const isSheetAtHighestSnapPoint =
animatedPosition.value === highestSnapPoint;
Expand Down Expand Up @@ -382,8 +385,9 @@ export const useGestureEventsHandlersDefault = () => {
animateToPosition,
]
);
const handleOnFinalize: GestureEventHandlerCallbackType = useWorkletCallback(
const handleOnFinalize: GestureEventHandlerCallbackType = useCallback(
function handleOnFinalize() {
'worklet';
resetContext(context);
},
[context]
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/useGestureHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback } from 'react';
import {
type GestureStateChangeEvent,
type GestureUpdateEvent,
Expand All @@ -6,7 +7,6 @@ import {
State,
} from 'react-native-gesture-handler';
import type { SharedValue } from 'react-native-reanimated';
import { useWorkletCallback } from 'react-native-reanimated';
import { GESTURE_SOURCE } from '../constants';
import type {
GestureEventHandlerCallbackType,
Expand All @@ -22,8 +22,9 @@ export const useGestureHandler: GestureHandlersHookType = (
onEnd: GestureEventHandlerCallbackType,
onFinalize: GestureEventHandlerCallbackType
) => {
const handleOnStart = useWorkletCallback(
const handleOnStart = useCallback(
(event: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
'worklet';
state.value = State.BEGAN;
gestureSource.value = source;

Expand All @@ -33,12 +34,13 @@ export const useGestureHandler: GestureHandlersHookType = (
[state, gestureSource, source, onStart]
);

const handleOnChange = useWorkletCallback(
const handleOnChange = useCallback(
(
event: GestureUpdateEvent<
PanGestureHandlerEventPayload & PanGestureChangeEventPayload
>
) => {
'worklet';
if (gestureSource.value !== source) {
return;
}
Expand All @@ -49,8 +51,9 @@ export const useGestureHandler: GestureHandlersHookType = (
[state, gestureSource, source, onChange]
);

const handleOnEnd = useWorkletCallback(
const handleOnEnd = useCallback(
(event: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
'worklet';
if (gestureSource.value !== source) {
return;
}
Expand All @@ -63,8 +66,9 @@ export const useGestureHandler: GestureHandlersHookType = (
[state, gestureSource, source, onEnd]
);

const handleOnFinalize = useWorkletCallback(
const handleOnFinalize = useCallback(
(event: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
'worklet';
if (gestureSource.value !== source) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useKeyboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import {
Keyboard,
type KeyboardEvent,
Expand All @@ -10,7 +10,6 @@ import {
runOnUI,
useAnimatedReaction,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';
import { KEYBOARD_STATE, SCREEN_HEIGHT } from '../constants';

Expand Down Expand Up @@ -42,14 +41,15 @@ export const useKeyboard = () => {
//#endregion

//#region worklets
const handleKeyboardEvent = useWorkletCallback(
const handleKeyboardEvent = useCallback(
(
state: KEYBOARD_STATE,
height: number,
duration: number,
easing: KeyboardEventEasing,
bottomOffset?: number
) => {
'worklet';
if (state === KEYBOARD_STATE.SHOWN && !shouldHandleKeyboardEvents.value) {
/**
* if the keyboard event was fired before the `onFocus` on TextInput,
Expand Down
15 changes: 10 additions & 5 deletions src/hooks/useScrollEventsHandlersDefault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from 'react';
import { State } from 'react-native-gesture-handler';
import { scrollTo, useWorkletCallback } from 'react-native-reanimated';
import { scrollTo } from 'react-native-reanimated';
import { ANIMATION_STATE, SCROLLABLE_STATE, SHEET_STATE } from '../constants';
import type {
ScrollEventHandlerCallbackType,
Expand Down Expand Up @@ -27,8 +28,9 @@ export const useScrollEventsHandlersDefault: ScrollEventsHandlersHookType = (

//#region callbacks
const handleOnScroll: ScrollEventHandlerCallbackType<ScrollEventContextType> =
useWorkletCallback(
useCallback(
({ contentOffset: { y } }, context) => {
'worklet';
/**
* if sheet position is extended or fill parent, then we reset
* `shouldLockInitialPosition` value to false.
Expand Down Expand Up @@ -67,8 +69,9 @@ export const useScrollEventsHandlersDefault: ScrollEventsHandlersHookType = (
]
);
const handleOnBeginDrag: ScrollEventHandlerCallbackType<ScrollEventContextType> =
useWorkletCallback(
useCallback(
({ contentOffset: { y } }, context) => {
'worklet';
scrollableContentOffsetY.value = y;
rootScrollableContentOffsetY.value = y;
context.initialContentOffsetY = y;
Expand All @@ -94,8 +97,9 @@ export const useScrollEventsHandlersDefault: ScrollEventsHandlersHookType = (
]
);
const handleOnEndDrag: ScrollEventHandlerCallbackType<ScrollEventContextType> =
useWorkletCallback(
useCallback(
({ contentOffset: { y } }, context) => {
'worklet';
if (animatedScrollableState.value === SCROLLABLE_STATE.LOCKED) {
const lockPosition = context.shouldLockInitialPosition
? (context.initialContentOffsetY ?? 0)
Expand All @@ -120,8 +124,9 @@ export const useScrollEventsHandlersDefault: ScrollEventsHandlersHookType = (
]
);
const handleOnMomentumEnd: ScrollEventHandlerCallbackType<ScrollEventContextType> =
useWorkletCallback(
useCallback(
({ contentOffset: { y } }, context) => {
'worklet';
if (animatedScrollableState.value === SCROLLABLE_STATE.LOCKED) {
const lockPosition = context.shouldLockInitialPosition
? (context.initialContentOffsetY ?? 0)
Expand Down