Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Replace react-native Animated API with react-native-reanimated #52956

Closed
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
12 changes: 6 additions & 6 deletions src/components/AttachmentModal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {Str} from 'expensify-common';
import React, {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {Animated, Keyboard, View} from 'react-native';
import {Keyboard, View} from 'react-native';
import {GestureHandlerRootView} from 'react-native-gesture-handler';
import {useOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import {useSharedValue} from 'react-native-reanimated';
import Animated, {FadeIn, useSharedValue} from 'react-native-reanimated';
import type {ValueOf} from 'type-fest';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
Expand Down Expand Up @@ -166,7 +165,6 @@ function AttachmentModal({
attachmentLink = '',
}: AttachmentModalProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [isModalOpen, setIsModalOpen] = useState(defaultOpen);
const [shouldLoadAttachment, setShouldLoadAttachment] = useState(false);
const [isAttachmentInvalid, setIsAttachmentInvalid] = useState(false);
Expand All @@ -177,7 +175,6 @@ function AttachmentModal({
const [sourceState, setSourceState] = useState<AvatarSource>(() => source);
const [modalType, setModalType] = useState<ModalType>(CONST.MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE);
const [isConfirmButtonDisabled, setIsConfirmButtonDisabled] = useState(false);
const [confirmButtonFadeAnimation] = useState(() => new Animated.Value(1));
const [isDownloadButtonReadyToBeShown, setIsDownloadButtonReadyToBeShown] = React.useState(true);
const isPDFLoadError = useRef(false);
const {windowWidth} = useWindowDimensions();
Expand Down Expand Up @@ -590,7 +587,10 @@ function AttachmentModal({
{!!onConfirm && !isConfirmButtonDisabled && (
<SafeAreaConsumer>
{({safeAreaPaddingBottomStyle}) => (
<Animated.View style={[StyleUtils.fade(confirmButtonFadeAnimation), safeAreaPaddingBottomStyle]}>
<Animated.View
style={[safeAreaPaddingBottomStyle]}
entering={FadeIn}
>
<Button
ref={viewRef(submitRef)}
success
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {Animated} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import useSafeAreaInsets from '@hooks/useSafeAreaInsets';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -9,8 +9,9 @@ function GrowlNotificationContainer({children, translateY}: GrowlNotificationCon
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const insets = useSafeAreaInsets();
const animatedStyles = useAnimatedStyle(() => styles.growlNotificationTranslateY(translateY));

return <Animated.View style={[StyleUtils.getSafeAreaPadding(insets), styles.growlNotificationContainer, styles.growlNotificationTranslateY(translateY)]}>{children}</Animated.View>;
return <Animated.View style={[StyleUtils.getSafeAreaPadding(insets), styles.growlNotificationContainer, animatedStyles]}>{children}</Animated.View>;
}

GrowlNotificationContainer.displayName = 'GrowlNotificationContainer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import React from 'react';
import {Animated} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import type GrowlNotificationContainerProps from './types';

function GrowlNotificationContainer({children, translateY}: GrowlNotificationContainerProps) {
const styles = useThemeStyles();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const animatedStyles = useAnimatedStyle(() => styles.growlNotificationTranslateY(translateY));

return (
<Animated.View
style={[styles.growlNotificationContainer, styles.growlNotificationDesktopContainer, styles.growlNotificationTranslateY(translateY), shouldUseNarrowLayout && styles.mwn]}
>
{children}
</Animated.View>
<Animated.View style={[styles.growlNotificationContainer, styles.growlNotificationDesktopContainer, animatedStyles, shouldUseNarrowLayout && styles.mwn]}>{children}</Animated.View>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {Animated} from 'react-native';
import type {SharedValue} from 'react-native-reanimated';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

type GrowlNotificationContainerProps = ChildrenProps & {
translateY: Animated.Value;
translateY: SharedValue<number>;
};

export default GrowlNotificationContainerProps;
17 changes: 9 additions & 8 deletions src/components/GrowlNotification/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import {Animated, View} from 'react-native';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useState} from 'react';
import {View} from 'react-native';
import {Directions, Gesture, GestureDetector} from 'react-native-gesture-handler';
import {useSharedValue, withSpring} from 'react-native-reanimated';
import type {SvgProps} from 'react-native-svg';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
Expand All @@ -11,7 +12,6 @@
import useThemeStyles from '@hooks/useThemeStyles';
import * as Growl from '@libs/Growl';
import type {GrowlRef} from '@libs/Growl';
import useNativeDriver from '@libs/useNativeDriver';
import CONST from '@src/CONST';
import GrowlNotificationContainer from './GrowlNotificationContainer';

Expand All @@ -20,7 +20,7 @@
const PressableWithoutFeedback = Pressables.PressableWithoutFeedback;

function GrowlNotification(_: unknown, ref: ForwardedRef<GrowlRef>) {
const translateY = useRef(new Animated.Value(INACTIVE_POSITION_Y)).current;
const translateY = useSharedValue(INACTIVE_POSITION_Y);
const [bodyText, setBodyText] = useState('');
const [type, setType] = useState('success');
const [duration, setDuration] = useState<number>();
Expand Down Expand Up @@ -76,10 +76,11 @@
*/
const fling = useCallback(
(val = INACTIVE_POSITION_Y) => {
Animated.spring(translateY, {
toValue: val,
useNativeDriver,
}).start();
'worklet';

translateY.value = withSpring(val, {

Check failure on line 81 in src/components/GrowlNotification/index.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
overshootClamping: false,
});
},
[translateY],
);
Expand Down
45 changes: 23 additions & 22 deletions src/components/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, {useEffect, useRef} from 'react';
import {Animated, InteractionManager} from 'react-native';
import React from 'react';
import {InteractionManager} from 'react-native';
import Animated, {interpolateColor, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useNativeDriver from '@libs/useNativeDriver';
import CONST from '@src/CONST';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
Expand Down Expand Up @@ -35,7 +35,7 @@

function Switch({isOn, onToggle, accessibilityLabel, disabled, showLockIcon, disabledAction}: SwitchProps) {
const styles = useThemeStyles();
const offsetX = useRef(new Animated.Value(isOn ? OFFSET_X.ON : OFFSET_X.OFF));
const offsetX = useSharedValue(isOn ? OFFSET_X.ON : OFFSET_X.OFF);
const theme = useTheme();

const handleSwitchPress = () => {
Expand All @@ -44,22 +44,22 @@
disabledAction?.();
return;
}
offsetX.value = withTiming(isOn ? OFFSET_X.OFF : OFFSET_X.ON, {duration: 300});

Check failure on line 47 in src/components/Switch.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
onToggle(!isOn);
});
};

useEffect(() => {
Animated.timing(offsetX.current, {
toValue: isOn ? OFFSET_X.ON : OFFSET_X.OFF,
duration: 300,
useNativeDriver,
}).start();
}, [isOn]);
const animatedThumbStyle = useAnimatedStyle(() => ({
transform: [{translateX: offsetX.value}],

Check failure on line 53 in src/components/Switch.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
}));

const animatedSwitchTrackStyle = useAnimatedStyle(() => ({
backgroundColor: interpolateColor(offsetX.value, [OFFSET_X.OFF, OFFSET_X.ON], [theme.icon, theme.success]),

Check failure on line 57 in src/components/Switch.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
}));

return (
<PressableWithFeedback
disabled={!disabledAction && disabled}
style={[styles.switchTrack, !isOn && styles.switchInactive]}
onPress={handleSwitchPress}
onLongPress={handleSwitchPress}
role={CONST.ROLE.SWITCH}
Expand All @@ -69,16 +69,17 @@
hoverDimmingValue={1}
pressDimmingValue={0.8}
>
{/* eslint-disable-next-line react-compiler/react-compiler */}
<Animated.View style={[styles.switchThumb, styles.switchThumbTransformation(offsetX.current)]}>
{(!!disabled || !!showLockIcon) && (
<Icon
src={Expensicons.Lock}
fill={isOn ? theme.text : theme.icon}
width={styles.toggleSwitchLockIcon.width}
height={styles.toggleSwitchLockIcon.height}
/>
)}
<Animated.View style={[styles.switchTrack, animatedSwitchTrackStyle]}>
<Animated.View style={[styles.switchThumb, animatedThumbStyle]}>
{(!!disabled || !!showLockIcon) && (
<Icon
src={Expensicons.Lock}
fill={isOn ? theme.text : theme.icon}
width={styles.toggleSwitchLockIcon.width}
height={styles.toggleSwitchLockIcon.height}
/>
)}
</Animated.View>
</Animated.View>
</PressableWithFeedback>
);
Expand Down
20 changes: 6 additions & 14 deletions src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInput, TextInputFocusEventData, ViewStyle} from 'react-native';
import {ActivityIndicator, Animated, StyleSheet, View} from 'react-native';
import {ActivityIndicator, StyleSheet, View} from 'react-native';
import {useSharedValue, withSpring} from 'react-native-reanimated';
import Checkbox from '@components/Checkbox';
import FormHelpMessage from '@components/FormHelpMessage';
import Icon from '@components/Icon';
Expand All @@ -23,7 +24,6 @@
import useThemeStyles from '@hooks/useThemeStyles';
import getSecureEntryKeyboardType from '@libs/getSecureEntryKeyboardType';
import isInputAutoFilled from '@libs/isInputAutoFilled';
import useNativeDriver from '@libs/useNativeDriver';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {BaseTextInputProps, BaseTextInputRef} from './types';
Expand Down Expand Up @@ -94,8 +94,8 @@
const [textInputHeight, setTextInputHeight] = useState(0);
const [height, setHeight] = useState<number>(variables.componentSizeLarge);
const [width, setWidth] = useState<number | null>(null);
const labelScale = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE)).current;
const labelTranslateY = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y)).current;
const labelScale = useSharedValue<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE);
const labelTranslateY = useSharedValue<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y);
const input = useRef<TextInput | null>(null);
const isLabelActive = useRef(initialActiveLabel);

Expand All @@ -117,16 +117,8 @@

const animateLabel = useCallback(
(translateY: number, scale: number) => {
Animated.parallel([
Animated.spring(labelTranslateY, {
toValue: translateY,
useNativeDriver,
}),
Animated.spring(labelScale, {
toValue: scale,
useNativeDriver,
}),
]).start();
labelScale.value = withSpring(scale, {overshootClamping: false});

Check failure on line 120 in src/components/TextInput/BaseTextInput/index.native.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
labelTranslateY.value = withSpring(translateY, {overshootClamping: false});

Check failure on line 121 in src/components/TextInput/BaseTextInput/index.native.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
},
[labelScale, labelTranslateY],
);
Expand Down
21 changes: 7 additions & 14 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInputFocusEventData, ViewStyle} from 'react-native';
import {ActivityIndicator, Animated, StyleSheet, View} from 'react-native';
import {ActivityIndicator, StyleSheet, View} from 'react-native';
import {useSharedValue, withSpring} from 'react-native-reanimated';
import Checkbox from '@components/Checkbox';
import FormHelpMessage from '@components/FormHelpMessage';
import Icon from '@components/Icon';
Expand All @@ -24,7 +25,6 @@
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import isInputAutoFilled from '@libs/isInputAutoFilled';
import useNativeDriver from '@libs/useNativeDriver';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {BaseTextInputProps, BaseTextInputRef} from './types';
Expand Down Expand Up @@ -99,8 +99,9 @@
const [height, setHeight] = useState<number>(variables.componentSizeLarge);
const [width, setWidth] = useState<number | null>(null);

const labelScale = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE)).current;
const labelTranslateY = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y)).current;
const labelScale = useSharedValue<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE);
const labelTranslateY = useSharedValue<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y);

const input = useRef<HTMLInputElement | null>(null);
const isLabelActive = useRef(initialActiveLabel);

Expand All @@ -122,16 +123,8 @@

const animateLabel = useCallback(
(translateY: number, scale: number) => {
Animated.parallel([
Animated.spring(labelTranslateY, {
toValue: translateY,
useNativeDriver,
}),
Animated.spring(labelScale, {
toValue: scale,
useNativeDriver,
}),
]).start();
labelScale.value = withSpring(scale, {overshootClamping: false});

Check failure on line 126 in src/components/TextInput/BaseTextInput/index.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
labelTranslateY.value = withSpring(translateY, {overshootClamping: false});

Check failure on line 127 in src/components/TextInput/BaseTextInput/index.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
},
[labelScale, labelTranslateY],
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/TextInput/TextInputLabel/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react';
import {Animated} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import useThemeStyles from '@hooks/useThemeStyles';
import type TextInputLabelProps from './types';

function TextInputLabel({label, labelScale, labelTranslateY}: TextInputLabelProps) {
const styles = useThemeStyles();

const animatedStyle = useAnimatedStyle(() => styles.textInputLabelTransformation(labelTranslateY, labelScale));

return (
<Animated.Text
allowFontScaling={false}
style={[styles.textInputLabel, styles.textInputLabelTransformation(labelTranslateY, labelScale)]}
style={[styles.textInputLabel, animatedStyle]}
>
{label}
</Animated.Text>
Expand Down
6 changes: 4 additions & 2 deletions src/components/TextInput/TextInputLabel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useEffect, useRef} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {Text} from 'react-native';
import {Animated} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import textRef from '@src/types/utils/textRef';
Expand All @@ -19,12 +19,14 @@ function TextInputLabel({for: inputId = '', label, labelTranslateY, labelScale}:
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);

const animatedStyle = useAnimatedStyle(() => styles.textInputLabelTransformation(labelTranslateY, labelScale));

return (
<Animated.Text
// eslint-disable-next-line react-compiler/react-compiler
ref={textRef(labelRef)}
role={CONST.ROLE.PRESENTATION}
style={[styles.textInputLabel, styles.textInputLabelTransformation(labelTranslateY, labelScale), styles.pointerEventsNone]}
style={[styles.textInputLabel, animatedStyle, styles.pointerEventsNone]}
>
{label}
</Animated.Text>
Expand Down
6 changes: 3 additions & 3 deletions src/components/TextInput/TextInputLabel/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {Animated} from 'react-native';
import type {SharedValue} from 'react-native-reanimated';

type TextInputLabelProps = {
/** Label */
label: string;

/** Label vertical translate */
labelTranslateY: Animated.Value;
labelTranslateY: SharedValue<number>;

/** Label scale */
labelScale: Animated.Value;
labelScale: SharedValue<number>;

/** For attribute for label */
for?: string;
Expand Down
Loading
Loading