Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
allroundexperts committed Mar 27, 2024
1 parent e4dec8f commit 23309a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
10 changes: 6 additions & 4 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {GestureResponderEvent, StyleProp, TextStyle, ViewStyle} from 'react
import {View} from 'react-native';
import type {AnimatedStyle} from 'react-native-reanimated';
import type {ValueOf} from 'type-fest';
import useAnimatedHighlightStyle from '@hooks/useAnimatedHighlightStyle';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -149,8 +150,8 @@ type MenuItemBaseProps = {
/** Whether item is focused or active */
focused?: boolean;

/** Style for the highlighted state */
highlightStyle?: StyleProp<AnimatedStyle<ViewStyle>>;
/** Whether item is highlighted */
highlighted?: boolean;

/** Should we disable this menu item? */
disabled?: boolean;
Expand Down Expand Up @@ -289,7 +290,7 @@ function MenuItem(
success = false,
focused = false,
disabled = false,
highlightStyle = {},
highlighted = false,
title,
subtitle,
shouldShowBasicTitle,
Expand Down Expand Up @@ -329,6 +330,7 @@ function MenuItem(
const StyleUtils = useStyleUtils();
const combinedStyle = [style, styles.popoverMenuItem];
const {isSmallScreenWidth} = useWindowDimensions();
const animatedHighlightStyle = useAnimatedHighlightStyle({shouldHighlight: highlighted, height: 56});
const [html, setHtml] = useState('');
const titleRef = useRef('');
const {isExecuting, singleExecution, waitForNavigate} = useContext(MenuItemGroupContext) ?? {};
Expand Down Expand Up @@ -432,7 +434,7 @@ function MenuItem(
onPressIn={() => shouldBlockSelection && isSmallScreenWidth && DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={ControlSelection.unblock}
onSecondaryInteraction={onSecondaryInteraction}
wrapperStyle={highlightStyle}
wrapperStyle={animatedHighlightStyle}
style={({pressed}) =>
[
containerStyle,
Expand Down
42 changes: 23 additions & 19 deletions src/hooks/useAnimatedHighlightStyle.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import {useFocusEffect} from '@react-navigation/native';
import React from 'react';
import {InteractionManager} from 'react-native';
import {interpolate, interpolateColor, useAnimatedStyle, useSharedValue, withDelay, withSequence, withTiming} from 'react-native-reanimated';
import CONST from '@src/CONST';
import useTheme from './useTheme';

type Props = {
height: number;
shouldHighlight: boolean;
highlightDuration?: number;
delay?: number;
};

/**
* Returns a highlight style that interpolates the colour, height and opacity giving a fading effect.
*/
export default function useAnimatedHighlightStyle(shouldHighlight: boolean, highlightDuration: number = CONST.ANIMATED_TRANSITION, delay = 100) {
export default function useAnimatedHighlightStyle({shouldHighlight, highlightDuration = CONST.ANIMATED_TRANSITION, delay = CONST.ANIMATION_IN_TIMING, height}: Props) {
const repeatableProgress = useSharedValue(0);
const nonRepeatableProgress = useSharedValue(0);
const theme = useTheme();

const highlightBackgroundStyle = useAnimatedStyle(() => ({
backgroundColor: interpolateColor(repeatableProgress.value, [0, 1], ['rgba(0, 0, 0, 0)', theme.border]),
flex: interpolate(nonRepeatableProgress.value, [0, 1], [0, 1]),
height: interpolate(nonRepeatableProgress.value, [0, 1], [0, height]),
opacity: interpolate(nonRepeatableProgress.value, [0, 1], [0, 1]),
}));

useFocusEffect(
React.useCallback(() => {
if (!shouldHighlight) {
return;
}
React.useEffect(() => {
if (!shouldHighlight) {
return;
}
InteractionManager.runAfterInteractions(() => {
nonRepeatableProgress.value = withTiming(1, {duration: highlightDuration});
InteractionManager.runAfterInteractions(() => {
repeatableProgress.value = withSequence(
withDelay(delay, withTiming(0)),
withTiming(1, {duration: highlightDuration}),
withDelay(delay, withTiming(1)),
withTiming(0, {duration: highlightDuration}),
);
});
}, [shouldHighlight, highlightDuration, delay, repeatableProgress, nonRepeatableProgress]),
);
repeatableProgress.value = withSequence(
withDelay(delay, withTiming(0)),
withTiming(1, {duration: highlightDuration}),
withDelay(delay, withTiming(1)),
withTiming(0, {duration: highlightDuration}),
);
});
}, [shouldHighlight, highlightDuration, delay, repeatableProgress, nonRepeatableProgress]);

return highlightBackgroundStyle;
return shouldHighlight ? highlightBackgroundStyle : null;
}
4 changes: 1 addition & 3 deletions src/pages/workspace/WorkspaceInitialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import MenuItem from '@components/MenuItem';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import useAnimatedHighlightStyle from '@hooks/useAnimatedHighlightStyle';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useSingleExecution from '@hooks/useSingleExecution';
Expand Down Expand Up @@ -231,7 +230,6 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, policyMembers, r
const enabledFeatureRouteName = route.params?.enabledFeatureRouteName ?? '';

const enabledItem = menuItems.find((item) => item.name === enabledFeatureRouteName);
const animatedHighlightStyle = useAnimatedHighlightStyle(!!enabledItem, 500, 150);

// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage =
Expand Down Expand Up @@ -292,7 +290,7 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, policyMembers, r
onPress={item.action}
brickRoadIndicator={item.brickRoadIndicator}
wrapperStyle={styles.sectionMenuItem}
highlightStyle={enabledItem?.translationKey === item.translationKey ? [animatedHighlightStyle, {borderRadius: styles.border.borderRadius}] : undefined}
highlighted={enabledItem?.name === item.name}
focused={!!(item.translationKey && activeRoute?.startsWith(item.name))}

Check failure on line 294 in src/pages/workspace/WorkspaceInitialPage.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
hoverAndPressStyle={styles.hoveredComponentBG}
isPaneMenu
Expand Down

0 comments on commit 23309a5

Please sign in to comment.