diff --git a/src/components/BottomNavigation/BottomNavigationBar.tsx b/src/components/BottomNavigation/BottomNavigationBar.tsx index e2ba52677c..6bdb586118 100644 --- a/src/components/BottomNavigation/BottomNavigationBar.tsx +++ b/src/components/BottomNavigation/BottomNavigationBar.tsx @@ -16,7 +16,10 @@ import { getLabelColor, } from './utils'; import { useInternalTheme } from '../../core/theming'; -import { useMotionDuration } from '../../theme/accessibility/ReduceMotionContext'; +import { + useMotionDuration, + useReduceMotion, +} from '../../theme/accessibility/ReduceMotionContext'; import type { ThemeProp } from '../../theme/types'; import useAnimatedValue from '../../utils/useAnimatedValue'; import useAnimatedValueArray from '../../utils/useAnimatedValueArray'; @@ -335,6 +338,7 @@ const BottomNavigationBar = ({ const theme = useInternalTheme(themeOverrides); const { colors } = theme; const { bottom, left, right } = useSafeAreaInsets(); + const reduceMotion = useReduceMotion(); const getDuration = useMotionDuration(); const compact = compactProp ?? false; let shifting = shiftingProp ?? false; @@ -392,6 +396,11 @@ const BottomNavigationBar = ({ const animateToIndex = React.useCallback( (index: number) => { + if (reduceMotion) { + tabsAnims.forEach((tab, i) => tab.setValue(i === index ? 1 : 0)); + return; + } + Animated.parallel( navigationState.routes.map((_, i) => Animated.timing(tabsAnims[i], { @@ -406,7 +415,13 @@ const BottomNavigationBar = ({ tabsAnims.map((tab, i) => tab.setValue(i === index ? 1 : 0)); }); }, - [getDuration, navigationState.routes, tabsAnims, animationEasing] + [ + getDuration, + navigationState.routes, + reduceMotion, + tabsAnims, + animationEasing, + ] ); React.useEffect(() => { diff --git a/src/components/CrossFadeIcon.tsx b/src/components/CrossFadeIcon.tsx index 1f67aa7843..926066a6f4 100644 --- a/src/components/CrossFadeIcon.tsx +++ b/src/components/CrossFadeIcon.tsx @@ -14,7 +14,10 @@ import Animated, { import Icon, { isEqualIcon, isValidIcon } from './Icon'; import type { IconSource } from './Icon'; import { useInternalTheme } from '../core/theming'; -import { useMotionDuration } from '../theme/accessibility/ReduceMotionContext'; +import { + useMotionDuration, + useReduceMotion, +} from '../theme/accessibility/ReduceMotionContext'; import type { ThemeProp } from '../theme/types'; type Props = { @@ -58,14 +61,22 @@ const CrossFadeIcon = ({ const fade = useSharedValue(1); + const reduceMotion = useReduceMotion(); + const reduceMotionRef = React.useRef(reduceMotion); + reduceMotionRef.current = reduceMotion; const getDuration = useMotionDuration(); if (currentIcon !== source) { - setPreviousIcon(() => currentIcon); + setPreviousIcon(() => (reduceMotion ? null : currentIcon)); setCurrentIcon(() => source); } React.useEffect(() => { + if (reduceMotionRef.current) { + fade.value = 0; + return; + } + if (isValidIcon(previousIcon) && !isEqualIcon(previousIcon, currentIcon)) { fade.value = 1; diff --git a/src/components/Drawer/DrawerCollapsedItem.tsx b/src/components/Drawer/DrawerCollapsedItem.tsx index 257a6a3e54..083b55dc39 100644 --- a/src/components/Drawer/DrawerCollapsedItem.tsx +++ b/src/components/Drawer/DrawerCollapsedItem.tsx @@ -18,7 +18,10 @@ import Animated, { import useLatestCallback from 'use-latest-callback'; import { useInternalTheme } from '../../core/theming'; -import { useMotionDuration } from '../../theme/accessibility/ReduceMotionContext'; +import { + useMotionDuration, + useReduceMotion, +} from '../../theme/accessibility/ReduceMotionContext'; import { tokens } from '../../theme/tokens'; import type { ThemeProp } from '../../theme/types'; import { useFocusRing } from '../../utils/useFocusRing'; @@ -119,6 +122,7 @@ const DrawerCollapsedItem = ({ ...rest }: Props) => { const theme = useInternalTheme(themeOverrides); + const reduceMotion = useReduceMotion(); const getDuration = useMotionDuration(); // MD3 rings a navigation rail item as a whole (icon and label) with a @@ -143,7 +147,16 @@ const DrawerCollapsedItem = ({ const outlineScale = useSharedValue(active ? 1 : 0.5); + if (reduceMotion) { + outlineScale.value = active ? 1 : 0.5; + } + const expandOutline = useLatestCallback(() => { + if (reduceMotion) { + outlineScale.value = 1; + return; + } + outlineScale.value = withTiming(1, { duration: getDuration(150), easing: Easing.inOut(Easing.ease), diff --git a/src/components/__tests__/CrossFadeIcon.test.tsx b/src/components/__tests__/CrossFadeIcon.test.tsx index 3e148541d3..05edbf0123 100644 --- a/src/components/__tests__/CrossFadeIcon.test.tsx +++ b/src/components/__tests__/CrossFadeIcon.test.tsx @@ -26,27 +26,33 @@ beforeEach(() => { withTiming.mockClear(); }); -it.each([ - { reduceMotion: 'off', duration: 200 }, - { reduceMotion: 'on', duration: 0 }, -] as const)( - 'cross-fades to a new icon in $duration ms when reduceMotion is "$reduceMotion"', - async ({ reduceMotion, duration }) => { - const makeIcon = (source: string) => ( - - - - ); +it('cross-fades to a new icon in 200 ms when reduceMotion is "off"', async () => { + const makeIcon = (source: string) => ( + + + + ); - await render(makeIcon('plus')); - expect(withTiming).not.toHaveBeenCalled(); + await render(makeIcon('plus')); + expect(withTiming).not.toHaveBeenCalled(); - await screen.rerender(makeIcon('minus')); - expect(withTiming.mock.calls.map(([, config]) => config?.duration)).toEqual( - [duration] - ); - } -); + await screen.rerender(makeIcon('minus')); + expect(withTiming.mock.calls.map(([, config]) => config?.duration)).toEqual([ + 200, + ]); +}); + +it('switches immediately without animating when reduceMotion is "on"', async () => { + const makeIcon = (source: string) => ( + + + + ); + + await render(makeIcon('plus')); + await screen.rerender(makeIcon('minus')); + expect(withTiming).not.toHaveBeenCalled(); +}); it('does not replay the last icon change when the OS setting changes', async () => { const addEventListener = jest.mocked(AccessibilityInfo.addEventListener);