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
37 changes: 21 additions & 16 deletions src/components/FloatingActionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function FloatingActionButton({onPress, onLongPress, isActive, accessibilityLabe
const fabSize = isLHBVisible ? variables.iconSizeSmall : variables.iconSizeNormal;

const sharedValue = useSharedValue(isActive ? 1 : 0);
const isHovered = useSharedValue(false);
const buttonRef = ref;

useEffect(() => {
Expand All @@ -68,7 +69,7 @@ function FloatingActionButton({onPress, onLongPress, isActive, accessibilityLabe
}, [isActive, sharedValue]);

const animatedStyle = useAnimatedStyle(() => {
const backgroundColor = interpolateColor(sharedValue.get(), [0, 1], [buttonDefaultBG, buttonHoveredBG]);
const backgroundColor = isHovered.get() && !sharedValue.get() ? buttonHoveredBG : interpolateColor(sharedValue.get(), [0, 1], [buttonDefaultBG, buttonHoveredBG]);

return {
transform: [{rotate: `${sharedValue.get() * 135}deg`}],
Expand Down Expand Up @@ -115,22 +116,26 @@ function FloatingActionButton({onPress, onLongPress, isActive, accessibilityLabe
shouldUseHapticsOnLongPress
testID="floating-action-button"
>
{({hovered}) => (
<Animated.View
style={[styles.floatingActionButton, {borderRadius}, styles.floatingActionButtonSmall, animatedStyle, hovered && {backgroundColor: buttonHoveredBG}]}
testID="fab-animated-container"
>
<Svg
width={fabSize}
height={fabSize}
{({hovered}) => {
isHovered.set(hovered);

return (
<Animated.View
style={[styles.floatingActionButton, {borderRadius}, styles.floatingActionButtonSmall, animatedStyle]}
testID="fab-animated-container"
>
<AnimatedPath
d={isLHBVisible ? SMALL_FAB_PATH : FAB_PATH}
fill={icon}
/>
</Svg>
</Animated.View>
)}
<Svg
width={fabSize}
height={fabSize}
>
<AnimatedPath
d={isLHBVisible ? SMALL_FAB_PATH : FAB_PATH}
fill={icon}
/>
</Svg>
</Animated.View>
);
}}
</PressableWithoutFeedback>
);
}
Expand Down
24 changes: 21 additions & 3 deletions tests/ui/FloatingActionButtonTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,26 @@ jest.mock('@hooks/useResponsiveLayout', () => (): {shouldUseNarrowLayout: boolea
// Mock useIsHomeRouteActive to avoid navigation state issues
jest.mock('@navigation/helpers/useIsHomeRouteActive', () => (): boolean => false);

let mockUseAnimatedStyleUpdater: () => Record<string, unknown>;
// Silence react-native-reanimated warnings in Jest
jest.mock('react-native-reanimated', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return require('react-native-reanimated/mock');
return {
...require('react-native-reanimated/mock'),
interpolateColor: (value: number, input: number[], output: string[]) => {
const [, inputMax] = input;
const [colorMin, colorMax] = output;

if (value >= inputMax) {
return colorMax;
}
return colorMin;
},
useAnimatedStyle: (updater: () => Record<string, unknown>) => {
mockUseAnimatedStyleUpdater = updater;
return updater();
},
};
});

describe('FloatingActionButton hover', () => {
Expand Down Expand Up @@ -61,12 +77,14 @@ describe('FloatingActionButton hover', () => {
// Before hover, should not have successHover background
expect(animatedContainer).not.toHaveStyle({backgroundColor: colors.productDark500});

expect(mockUseAnimatedStyleUpdater()).not.toEqual(expect.objectContaining({backgroundColor: colors.productDark500}));

// Test hover in
fireEvent(fab, 'hoverIn');
expect(animatedContainer).toHaveStyle({backgroundColor: colors.productDark500});
expect(mockUseAnimatedStyleUpdater()).toEqual(expect.objectContaining({backgroundColor: colors.productDark500}));

// Test hover out
fireEvent(fab, 'hoverOut');
expect(animatedContainer).not.toHaveStyle({backgroundColor: colors.productDark500});
expect(mockUseAnimatedStyleUpdater()).not.toEqual(expect.objectContaining({backgroundColor: colors.productDark500}));
});
});