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

Animate StatusBar background color #20663

Merged
merged 20 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions src/components/withCurrentReportId.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const CurrentReportIdContext = createContext(null);
const withCurrentReportIdPropTypes = {
/** Actual content wrapped by this component */
children: PropTypes.node.isRequired,

/** Function used to update the current reportId. Should probably only ever be used in one place in the whole app. */
// eslint-disable-next-line react/no-unused-prop-types
updateCurrentReportId: PropTypes.func.isRequired,
roryabraham marked this conversation as resolved.
Show resolved Hide resolved
};

class CurrentReportIdContextProvider extends React.Component {
Expand Down Expand Up @@ -61,6 +65,7 @@ export default function withCurrentReportId(WrappedComponent) {
</CurrentReportIdContext.Consumer>
));

WithCurrentReportId.propTypes = withCurrentReportIdPropTypes;
WithCurrentReportId.displayName = `withCurrentReportId(${getComponentDisplayName(WrappedComponent)})`;

return WithCurrentReportId;
Expand Down
49 changes: 42 additions & 7 deletions src/libs/Navigation/NavigationRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import React, {useRef} from 'react';
import PropTypes from 'prop-types';
import {NavigationContainer, DefaultTheme, getPathFromState} from '@react-navigation/native';
import {useFlipper} from '@react-navigation/devtools';
import {useSharedValue, useAnimatedReaction, interpolateColor, withTiming, withDelay, Easing, runOnJS} from 'react-native-reanimated';
import Navigation, {navigationRef} from './Navigation';
import linkingConfig from './linkingConfig';
import AppNavigator from './AppNavigator';
import themeColors from '../../styles/themes/default';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../components/withWindowDimensions';
import Log from '../Log';
import withCurrentReportId from '../../components/withCurrentReportId';
import compose from '../compose';
import withCurrentReportId, {withCurrentReportIdPropTypes} from '../../components/withCurrentReportId';
import StatusBar from '../StatusBar';
import useWindowDimensions from '../../hooks/useWindowDimensions';

// https://reactnavigation.org/docs/themes
const navigationTheme = {
Expand All @@ -21,13 +22,13 @@ const navigationTheme = {
};

const propTypes = {
...windowDimensionsPropTypes,

/** Whether the current user is logged in with an authToken */
authenticated: PropTypes.bool.isRequired,

/** Fired when react-navigation is ready */
onReady: PropTypes.func.isRequired,

...withCurrentReportIdPropTypes,
};

/**
Expand All @@ -52,18 +53,52 @@ function parseAndLogRoute(state) {
}

const NavigationRoot = (props) => {
const {isSmallScreenWidth} = useWindowDimensions();
useFlipper(navigationRef);
const navigationStateRef = useRef(undefined);

const prevStatusBarBackgroundColor = useRef(themeColors.appBG);
const statusBarBackgroundColor = useRef(themeColors.appBG);
const statusBarAnimation = useSharedValue(0);

useAnimatedReaction(
() => statusBarAnimation.value,
() => {
const color = interpolateColor(statusBarAnimation.value, [0, 1], [prevStatusBarBackgroundColor.current, statusBarBackgroundColor.current]);
runOnJS(StatusBar.setBackgroundColor)(color);
Copy link
Contributor

@sobitneupane sobitneupane Jun 23, 2023

Choose a reason for hiding this comment

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

Looks like above console error is related to runOnJS.

},
);

const animateStatusBarBackgroundColor = () => {
const currentRoute = navigationRef.getCurrentRoute();
const currentScreenBackgroundColor = themeColors.PAGE_BACKGROUND_COLORS[currentRoute.name] || themeColors.appBG;

prevStatusBarBackgroundColor.current = statusBarBackgroundColor.current;
statusBarBackgroundColor.current = currentScreenBackgroundColor;
if (prevStatusBarBackgroundColor.current === statusBarBackgroundColor.current) {
return;
}

statusBarAnimation.value = 0;
statusBarAnimation.value = withDelay(
300,
withTiming(1, {
duration: 300,
easing: Easing.in,
}),
);
};

const updateSavedNavigationStateAndLogRoute = (state) => {
navigationStateRef.current = state;
props.updateCurrentReportId(state);
parseAndLogRoute(state);
animateStatusBarBackgroundColor();
};

return (
<NavigationContainer
key={props.isSmallScreenWidth ? 'small' : 'big'}
key={isSmallScreenWidth ? 'small' : 'big'}
onStateChange={updateSavedNavigationStateAndLogRoute}
initialState={navigationStateRef.current}
onReady={props.onReady}
Expand All @@ -81,4 +116,4 @@ const NavigationRoot = (props) => {

NavigationRoot.displayName = 'NavigationRoot';
NavigationRoot.propTypes = propTypes;
export default compose(withWindowDimensions, withCurrentReportId)(NavigationRoot);
export default withCurrentReportId(NavigationRoot);
5 changes: 5 additions & 0 deletions src/styles/themes/default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-unused-vars */
import colors from '../colors';
import SCREENS from '../../SCREENS';

const darkTheme = {
// Figma keys
Expand Down Expand Up @@ -77,6 +78,10 @@ const darkTheme = {
ourMentionBG: colors.green600,
};

darkTheme.PAGE_BACKGROUND_COLORS = {
[SCREENS.HOME]: darkTheme.sidebar,
};

const oldTheme = {
shadow: colors.black,
link: colors.blue,
Expand Down