diff --git a/src/components/FloatingActionButton.tsx b/src/components/FloatingActionButton.tsx index 13cb871d6485..20036f30144a 100644 --- a/src/components/FloatingActionButton.tsx +++ b/src/components/FloatingActionButton.tsx @@ -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(() => { @@ -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`}], @@ -115,22 +116,26 @@ function FloatingActionButton({onPress, onLongPress, isActive, accessibilityLabe shouldUseHapticsOnLongPress testID="floating-action-button" > - {({hovered}) => ( - - { + isHovered.set(hovered); + + return ( + - - - - )} + + + + + ); + }} ); } diff --git a/tests/ui/FloatingActionButtonTest.tsx b/tests/ui/FloatingActionButtonTest.tsx index ed32b967bbb5..80c2ce8a8d31 100644 --- a/tests/ui/FloatingActionButtonTest.tsx +++ b/tests/ui/FloatingActionButtonTest.tsx @@ -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; // 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) => { + mockUseAnimatedStyleUpdater = updater; + return updater(); + }, + }; }); describe('FloatingActionButton hover', () => { @@ -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})); }); });