diff --git a/src/components/FAB/Extended.tsx b/src/components/FAB/Extended.tsx index 5a35159f4f..857ab4bd52 100644 --- a/src/components/FAB/Extended.tsx +++ b/src/components/FAB/Extended.tsx @@ -11,6 +11,7 @@ import type { import Animated, { type AnimatedStyle, measure, + ReduceMotion, useAnimatedRef, useAnimatedStyle, useSharedValue, @@ -211,16 +212,22 @@ const Extended = ({ return; } - const widthSpring = toRawSpring( - expanded - ? theme.motion.spring.fast.spatial - : theme.motion.spring.default.spatial - ); - const opacitySpring = toRawSpring( - expanded - ? theme.motion.spring.default.effects - : theme.motion.spring.fast.effects - ); + const widthSpring = { + ...toRawSpring( + expanded + ? theme.motion.spring.fast.spatial + : theme.motion.spring.default.spatial + ), + reduceMotion: ReduceMotion.Never, + }; + const opacitySpring = { + ...toRawSpring( + expanded + ? theme.motion.spring.default.effects + : theme.motion.spring.fast.effects + ), + reduceMotion: ReduceMotion.Never, + }; scheduleOnUI(() => { 'worklet'; diff --git a/src/components/FAB/Menu.tsx b/src/components/FAB/Menu.tsx index 597799a6be..77f6e0b0bc 100644 --- a/src/components/FAB/Menu.tsx +++ b/src/components/FAB/Menu.tsx @@ -4,6 +4,7 @@ import type { ColorValue, GestureResponderEvent } from 'react-native'; import Animated, { interpolate, + ReduceMotion, useAnimatedStyle, useDerivedValue, useSharedValue, @@ -179,11 +180,19 @@ const AnimatedItem = ({ } scaleX.value = withDelay( delay, - withSpring(target, toRawSpring(theme.motion.spring.fast.spatial)) + withSpring(target, { + ...toRawSpring(theme.motion.spring.fast.spatial), + reduceMotion: ReduceMotion.Never, + }), + ReduceMotion.Never ); alpha.value = withDelay( delay, - withSpring(target, toRawSpring(theme.motion.spring.fast.effects)) + withSpring(target, { + ...toRawSpring(theme.motion.spring.fast.effects), + reduceMotion: ReduceMotion.Never, + }), + ReduceMotion.Never ); }, [expanded, index, itemCount, theme, reduceMotion, scaleX, alpha]); @@ -369,10 +378,10 @@ const MorphingTrigger = ({ // Compose's ToggleFloatingActionButton uses a single FastSpatial spring // for the full open/close progress (size, corner, color, icon all share // one timeline). - progress.value = withSpring( - expanded ? 1 : 0, - toRawSpring(theme.motion.spring.fast.spatial) - ); + progress.value = withSpring(expanded ? 1 : 0, { + ...toRawSpring(theme.motion.spring.fast.spatial), + reduceMotion: ReduceMotion.Never, + }); }, [expanded, theme, reduceMotion, progress]); // Derived shared values for the morph shape. Passing them to Shell as diff --git a/src/components/FAB/Shell.tsx b/src/components/FAB/Shell.tsx index e7c45cd50c..937223b12d 100644 --- a/src/components/FAB/Shell.tsx +++ b/src/components/FAB/Shell.tsx @@ -9,6 +9,7 @@ import type { } from 'react-native'; import Animated, { + ReduceMotion, useAnimatedStyle, useSharedValue, withSpring, @@ -243,15 +244,15 @@ const Shell = ({ return; } - scale.value = withSpring( - target, - toRawSpring(theme.motion.spring.fast.spatial) - ); + scale.value = withSpring(target, { + ...toRawSpring(theme.motion.spring.fast.spatial), + reduceMotion: ReduceMotion.Never, + }); - alpha.value = withSpring( - target, - toRawSpring(theme.motion.spring.fast.effects) - ); + alpha.value = withSpring(target, { + ...toRawSpring(theme.motion.spring.fast.effects), + reduceMotion: ReduceMotion.Never, + }); }, [visible, theme, reduceMotion, scale, alpha]); // Fallback shared values track the static size-driven dimensions. Consumers diff --git a/src/components/__tests__/FAB.test.tsx b/src/components/__tests__/FAB.test.tsx index 1c577f0b1a..7b140b4eca 100644 --- a/src/components/__tests__/FAB.test.tsx +++ b/src/components/__tests__/FAB.test.tsx @@ -9,10 +9,25 @@ import { jest, } from '@jest/globals'; import { fireEvent, userEvent } from '@testing-library/react-native'; +import * as Reanimated from 'react-native-reanimated'; +import PaperProvider from '../../core/PaperProvider'; import { render, screen } from '../../test-utils'; import FAB from '../FAB'; +jest.mock('react-native-reanimated', () => { + const ReanimatedModule = jest.requireActual< + typeof import('react-native-reanimated') + >('react-native-reanimated'); + + return { + __esModule: true, + ...ReanimatedModule, + default: ReanimatedModule.default, + withSpring: jest.fn(ReanimatedModule.withSpring), + }; +}); + it('renders FAB with default props', async () => { const tree = (await render()).toJSON(); expect(tree).toMatchSnapshot(); @@ -94,6 +109,37 @@ it('renders FAB transitioning to visible', async () => { expect(toJSON()).toMatchSnapshot(); }); +it('keeps the visibility springs on with reduceMotion="off" whatever the device setting', async () => { + const { rerender } = await render( + + + + ); + jest.mocked(Reanimated.withSpring).mockClear(); + + await rerender( + + + + ); + await rerender( + + + + ); + + expect( + jest + .mocked(Reanimated.withSpring) + .mock.calls.map(([, config]) => config?.reduceMotion) + ).toEqual([ + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + ]); +}); + it('calls onPress when FAB is pressed', async () => { const user = userEvent.setup(); const onPress = jest.fn(); diff --git a/src/components/__tests__/FABExtended.test.tsx b/src/components/__tests__/FABExtended.test.tsx index fdf52d2510..93d69291b5 100644 --- a/src/components/__tests__/FABExtended.test.tsx +++ b/src/components/__tests__/FABExtended.test.tsx @@ -5,6 +5,7 @@ import { afterEach, expect, it, jest } from '@jest/globals'; import { fireEvent, userEvent } from '@testing-library/react-native'; import * as Reanimated from 'react-native-reanimated'; +import PaperProvider from '../../core/PaperProvider'; import { render, screen } from '../../test-utils'; import FAB from '../FAB'; @@ -18,6 +19,7 @@ jest.mock('react-native-reanimated', () => { ...ReanimatedModule, default: ReanimatedModule.default, measure: jest.fn(), + withSpring: jest.fn(ReanimatedModule.withSpring), }; }); @@ -115,6 +117,40 @@ it('renders extended FAB transitioning to collapsed', async () => { expect(toJSON()).toMatchSnapshot(); }); +it('keeps the expand and collapse springs on with reduceMotion="off" whatever the device setting', async () => { + const { rerender } = await render( + + + + ); + await jest.runAllTimersAsync(); + jest.mocked(Reanimated.withSpring).mockClear(); + + await rerender( + + + + ); + await jest.runAllTimersAsync(); + await rerender( + + + + ); + await jest.runAllTimersAsync(); + + expect( + jest + .mocked(Reanimated.withSpring) + .mock.calls.map(([, config]) => config?.reduceMotion) + ).toEqual([ + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + ]); +}); + it('uses label as default aria-label', async () => { await render( { + const ReanimatedModule = jest.requireActual< + typeof import('react-native-reanimated') + >('react-native-reanimated'); + + return { + __esModule: true, + ...ReanimatedModule, + default: ReanimatedModule.default, + withDelay: jest.fn(ReanimatedModule.withDelay), + withSpring: jest.fn(ReanimatedModule.withSpring), + }; +}); + const makeItems = ( onItemPress = jest.fn<(event?: unknown) => void>() ): [MenuItemProps, MenuItemProps] => [ @@ -71,6 +87,55 @@ it('keeps the trigger mounted across opening and closing without trigger.onPress expect(closed).not.toBeDisabled(); }); +it('keeps the opening springs and item stagger on with reduceMotion="off" whatever the device setting', async () => { + const { rerender } = await render( + + + + ); + jest.mocked(Reanimated.withSpring).mockClear(); + jest.mocked(Reanimated.withDelay).mockClear(); + + await rerender( + + + + ); + + // Scale and alpha for each of the two items, then the trigger's morph + expect( + jest + .mocked(Reanimated.withSpring) + .mock.calls.map(([, config]) => config?.reduceMotion) + ).toEqual([ + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + ]); + expect( + jest + .mocked(Reanimated.withDelay) + .mock.calls.map(([, , reduceMotion]) => reduceMotion) + ).toEqual([ + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + Reanimated.ReduceMotion.Never, + ]); +}); + it('renders FAB.Menu with 6 items', async () => { const tree = ( await render(