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

Add animation for switch children #55478

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
81 changes: 81 additions & 0 deletions src/components/Accordion/index.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type {ReactNode} from 'react';
import React from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {SharedValue} from 'react-native-reanimated';
import Animated, {Easing, useAnimatedStyle, useDerivedValue, useSharedValue, withTiming} from 'react-native-reanimated';
import useThemeStyles from '@hooks/useThemeStyles';

type AccordionProps = {
/** Giving information whether the component is open */
isExpanded: SharedValue<boolean>;

/** Element that is inside Accordion */
children: ReactNode;

/** Duration of expansion animation */
duration?: number;

/** Additional external style */
style?: StyleProp<ViewStyle>;

/** Was toggle triggered */
isToggleTriggered: SharedValue<boolean>;
};

function Accordion({isExpanded, children, duration = 300, isToggleTriggered, style}: AccordionProps) {
const height = useSharedValue(0);
const styles = useThemeStyles();

const derivedHeight = useDerivedValue(() => {
if (!isToggleTriggered.get()) {
return isExpanded.get() ? height.get() : 0;
}

return withTiming(height.get() * Number(isExpanded.get()), {
duration,
easing: Easing.inOut(Easing.quad),
});
});

const derivedOpacity = useDerivedValue(() => {
if (!isToggleTriggered.get()) {
return isExpanded.get() ? 1 : 0;
}

return withTiming(isExpanded.get() ? 1 : 0, {
duration,
easing: Easing.inOut(Easing.quad),
});
});

const animatedStyle = useAnimatedStyle(() => {
if (!isToggleTriggered.get() && !isExpanded.get()) {
return {
height: 0,
opacity: 0,
};
}
return {
height: !isToggleTriggered.get() ? height.get() : derivedHeight.get(),
opacity: derivedOpacity.get(),
};
});

return (
<Animated.View style={[animatedStyle, style]}>
<View
onLayout={(e) => {
height.set(e.nativeEvent.layout.height);
}}
style={[styles.pAbsolute, styles.l0, styles.r0, styles.t0]}
>
{children}
</View>
</Animated.View>
);
}

Accordion.displayName = 'Accordion';

export default Accordion;
78 changes: 78 additions & 0 deletions src/components/Accordion/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type {ReactNode} from 'react';
import React from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {SharedValue} from 'react-native-reanimated';
import Animated, {Easing, useAnimatedStyle, useDerivedValue, useSharedValue, withTiming} from 'react-native-reanimated';

type AccordionProps = {
/** Giving information whether the component is open */
isExpanded: SharedValue<boolean>;

/** Element that is inside Accordion */
children: ReactNode;

/** Duration of expansion animation */
duration?: number;

/** Additional external style */
style?: StyleProp<ViewStyle>;

/** Was toggle triggered */
isToggleTriggered: SharedValue<boolean>;
};

function Accordion({isExpanded, children, duration = 300, isToggleTriggered, style}: AccordionProps) {
const height = useSharedValue(0);

const derivedHeight = useDerivedValue(() => {
if (!isToggleTriggered.get()) {
return isExpanded.get() ? height.get() : 0;
}

return withTiming(height.get() * Number(isExpanded.get()), {
duration,
easing: Easing.inOut(Easing.quad),
});
});

const derivedOpacity = useDerivedValue(() => {
if (!isToggleTriggered.get()) {
return isExpanded.get() ? 1 : 0;
}

return withTiming(isExpanded.get() ? 1 : 0, {
duration,
easing: Easing.inOut(Easing.quad),
});
});

const animatedStyle = useAnimatedStyle(() => {
if (!isToggleTriggered.get() && !isExpanded.get()) {
return {
height: 0,
opacity: 0,
};
}

return {
height: !isToggleTriggered.get() ? undefined : derivedHeight.get(),
opacity: derivedOpacity.get(),
};
});

return (
<Animated.View style={[animatedStyle, style]}>
<View
onLayout={(e) => {
height.set(e.nativeEvent.layout.height);
}}
>
{children}
</View>
</Animated.View>
);
}
Accordion.displayName = 'Accordion';

export default Accordion;
10 changes: 8 additions & 2 deletions src/libs/Navigation/linkTo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ export default function linkTo(navigation: NavigationContainerRef<RootStackParam
}
}

if (action && 'payload' in action && action.payload && 'name' in action.payload && isSideModalNavigator(action.payload.name)) {
if (
action &&
'payload' in action &&
action.payload &&
'name' in action.payload &&
(isSideModalNavigator(action.payload.name) || action.payload.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

) {
// Information about the state may be in the params.
const currentFocusedRoute = findFocusedRoute(extrapolateStateFromParams(rootState));
const targetFocusedRoute = findFocusedRoute(stateFromPath);
Expand All @@ -201,7 +207,7 @@ export default function linkTo(navigation: NavigationContainerRef<RootStackParam
// There are situations where a route already exists on the current navigation stack
// But we want to push the same route instead of going back in the stack
// Which would break the user navigation history
if (!isActiveRoute && type === CONST.NAVIGATION.ACTION_TYPE.PUSH) {
if ((!isActiveRoute && type === CONST.NAVIGATION.ACTION_TYPE.PUSH) || action.payload.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR) {
minimalAction.type = CONST.NAVIGATION.ACTION_TYPE.PUSH;
}
root.dispatch(minimalAction);
Expand Down
6 changes: 3 additions & 3 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,9 +1063,9 @@ function removePendingFieldsFromCustomUnit(customUnit: CustomUnit): CustomUnit {
return cleanedCustomUnit;
}

function navigateWhenEnableFeature(policyID: string) {
function goBackWhenEnableFeature(policyID: string) {
setTimeout(() => {
Copy link
Member

@thesahindia thesahindia Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned about using setTimeout, we try to avoid it unless absolutely necessary.

cc: @mountiny

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is exactly the same solution as it was before in the navigate function:

function navigateWhenEnableFeature(policyID: string) {
    setTimeout(() => {
        Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
    }, CONST.WORKSPACE_ENABLE_FEATURE_REDIRECT_DELAY);
}

which we replacing here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad. Let's not make any changes here then.

Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
Navigation.goBack(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
}, CONST.WORKSPACE_ENABLE_FEATURE_REDIRECT_DELAY);
}

Expand Down Expand Up @@ -1355,7 +1355,7 @@ export {
getDistanceRateCustomUnitRate,
sortWorkspacesBySelected,
removePendingFieldsFromCustomUnit,
navigateWhenEnableFeature,
goBackWhenEnableFeature,
getIntegrationLastSuccessfulDate,
getCurrentConnectionName,
getCustomersOrJobsLabelNetSuite,
Expand Down
8 changes: 4 additions & 4 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {translateLocal} from '@libs/Localize';
import Log from '@libs/Log';
import enhanceParameters from '@libs/Network/enhanceParameters';
import {hasEnabledOptions} from '@libs/OptionsListUtils';
import {getPolicy, navigateWhenEnableFeature} from '@libs/PolicyUtils';
import {getPolicy, goBackWhenEnableFeature} from '@libs/PolicyUtils';
import {getAllPolicyReports} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -958,7 +958,7 @@ function deleteWorkspaceCategories(policyID: string, categoryNamesToDelete: stri
API.write(WRITE_COMMANDS.DELETE_WORKSPACE_CATEGORIES, parameters, onyxData);
}

function enablePolicyCategories(policyID: string, enabled: boolean, shouldNavigate = true) {
function enablePolicyCategories(policyID: string, enabled: boolean, shouldGoBack = true) {
const onyxUpdatesToDisableCategories: OnyxUpdate[] = [];
if (!enabled) {
onyxUpdatesToDisableCategories.push(
Expand Down Expand Up @@ -1029,8 +1029,8 @@ function enablePolicyCategories(policyID: string, enabled: boolean, shouldNaviga

API.write(WRITE_COMMANDS.ENABLE_POLICY_CATEGORIES, parameters, onyxData);

if (enabled && getIsNarrowLayout() && shouldNavigate) {
navigateWhenEnableFeature(policyID);
if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Policy/DistanceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {getDistanceRateCustomUnit, navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import {getDistanceRateCustomUnit, goBackWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -166,7 +166,7 @@ function enablePolicyDistanceRates(policyID: string, enabled: boolean) {
API.write(WRITE_COMMANDS.ENABLE_POLICY_DISTANCE_RATES, parameters, onyxData);

if (enabled && getIsNarrowLayout()) {
navigateWhenEnableFeature(policyID);
goBackWhenEnableFeature(policyID);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/libs/actions/Policy/PerDiem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {translateLocal} from '@libs/Localize';
import enhanceParameters from '@libs/Network/enhanceParameters';
import * as NumberUtils from '@libs/NumberUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import {goBackWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -70,7 +70,7 @@ function generateCustomUnitID(): string {
return NumberUtils.generateHexadecimalValue(13);
}

function enablePerDiem(policyID: string, enabled: boolean, customUnitID?: string, disableRedirect?: boolean) {
function enablePerDiem(policyID: string, enabled: boolean, customUnitID?: string, shouldGoBack?: boolean) {
const doesCustomUnitExists = !!customUnitID;
const finalCustomUnitID = doesCustomUnitExists ? customUnitID : generateCustomUnitID();
const optimisticCustomUnit = {
Expand Down Expand Up @@ -123,8 +123,8 @@ function enablePerDiem(policyID: string, enabled: boolean, customUnitID?: string

API.write(WRITE_COMMANDS.TOGGLE_POLICY_PER_DIEM, parameters, onyxData);

if (enabled && getIsNarrowLayout() && !disableRedirect) {
navigateWhenEnableFeature(policyID);
if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import * as NumberUtils from '@libs/NumberUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as PhoneNumber from '@libs/PhoneNumber';
import * as PolicyUtils from '@libs/PolicyUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import {goBackWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import type {PolicySelector} from '@pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover';
import * as PaymentMethods from '@userActions/PaymentMethods';
Expand Down Expand Up @@ -2796,7 +2796,7 @@ function enablePolicyConnections(policyID: string, enabled: boolean) {
API.write(WRITE_COMMANDS.ENABLE_POLICY_CONNECTIONS, parameters, onyxData);

if (enabled && getIsNarrowLayout()) {
navigateWhenEnableFeature(policyID);
goBackWhenEnableFeature(policyID);
}
}

Expand Down Expand Up @@ -2853,11 +2853,11 @@ function enableExpensifyCard(policyID: string, enabled: boolean) {
API.write(WRITE_COMMANDS.ENABLE_POLICY_EXPENSIFY_CARDS, parameters, onyxData);

if (enabled && getIsNarrowLayout()) {
navigateWhenEnableFeature(policyID);
goBackWhenEnableFeature(policyID);
}
}

function enableCompanyCards(policyID: string, enabled: boolean, disableRedirect = false) {
function enableCompanyCards(policyID: string, enabled: boolean, shouldGoBack = true) {
const authToken = NetworkStore.getAuthToken();

const onyxData: OnyxData = {
Expand Down Expand Up @@ -2902,12 +2902,12 @@ function enableCompanyCards(policyID: string, enabled: boolean, disableRedirect

API.write(WRITE_COMMANDS.ENABLE_POLICY_COMPANY_CARDS, parameters, onyxData);

if (enabled && getIsNarrowLayout() && !disableRedirect) {
navigateWhenEnableFeature(policyID);
if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
}
}

function enablePolicyReportFields(policyID: string, enabled: boolean, disableRedirect = false) {
function enablePolicyReportFields(policyID: string, enabled: boolean, shouldGoBack = true) {
const onyxData: OnyxData = {
optimisticData: [
{
Expand Down Expand Up @@ -2950,8 +2950,8 @@ function enablePolicyReportFields(policyID: string, enabled: boolean, disableRed

API.write(WRITE_COMMANDS.ENABLE_POLICY_REPORT_FIELDS, parameters, onyxData);

if (enabled && getIsNarrowLayout() && !disableRedirect) {
navigateWhenEnableFeature(policyID);
if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
}
}

Expand Down Expand Up @@ -3065,7 +3065,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData);

if (enabled && getIsNarrowLayout()) {
navigateWhenEnableFeature(policyID);
goBackWhenEnableFeature(policyID);
}
}

Expand Down Expand Up @@ -3156,7 +3156,7 @@ function enablePolicyWorkflows(policyID: string, enabled: boolean) {
API.write(WRITE_COMMANDS.ENABLE_POLICY_WORKFLOWS, parameters, onyxData);

if (enabled && getIsNarrowLayout()) {
navigateWhenEnableFeature(policyID);
goBackWhenEnableFeature(policyID);
}
}

Expand All @@ -3166,7 +3166,7 @@ const DISABLED_MAX_EXPENSE_VALUES: Pick<Policy, 'maxExpenseAmountNoReceipt' | 'm
maxExpenseAge: CONST.DISABLED_MAX_EXPENSE_VALUE,
};

function enablePolicyRules(policyID: string, enabled: boolean, disableRedirect = false) {
function enablePolicyRules(policyID: string, enabled: boolean, shouldGoBack = true) {
const policy = getPolicy(policyID);
const onyxData: OnyxData = {
optimisticData: [
Expand Down Expand Up @@ -3219,8 +3219,8 @@ function enablePolicyRules(policyID: string, enabled: boolean, disableRedirect =
const parameters: SetPolicyRulesEnabledParams = {policyID, enabled};
API.write(WRITE_COMMANDS.SET_POLICY_RULES_ENABLED, parameters, onyxData);

if (enabled && getIsNarrowLayout() && !disableRedirect) {
navigateWhenEnableFeature(policyID);
if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
}
}

Expand Down Expand Up @@ -3331,7 +3331,7 @@ function enablePolicyInvoicing(policyID: string, enabled: boolean) {
API.write(WRITE_COMMANDS.ENABLE_POLICY_INVOICING, parameters, onyxData);

if (enabled && getIsNarrowLayout()) {
navigateWhenEnableFeature(policyID);
goBackWhenEnableFeature(policyID);
}
}

Expand Down
Loading
Loading