Skip to content
Open
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
19 changes: 17 additions & 2 deletions src/components/BottomNavigation/BottomNavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -335,6 +338,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
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;
Expand Down Expand Up @@ -392,6 +396,11 @@ const BottomNavigationBar = <Route extends BaseRoute>({

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], {
Expand All @@ -406,7 +415,13 @@ const BottomNavigationBar = <Route extends BaseRoute>({
tabsAnims.map((tab, i) => tab.setValue(i === index ? 1 : 0));
});
},
[getDuration, navigationState.routes, tabsAnims, animationEasing]
[
getDuration,
navigationState.routes,
reduceMotion,
tabsAnims,
animationEasing,
]
);

React.useEffect(() => {
Expand Down
15 changes: 13 additions & 2 deletions src/components/CrossFadeIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;

Expand Down
15 changes: 14 additions & 1 deletion src/components/Drawer/DrawerCollapsedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down
44 changes: 25 additions & 19 deletions src/components/__tests__/CrossFadeIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<PaperProvider reduceMotion={reduceMotion}>
<CrossFadeIcon source={source} color="black" size={24} />
</PaperProvider>
);
it('cross-fades to a new icon in 200 ms when reduceMotion is "off"', async () => {
const makeIcon = (source: string) => (
<PaperProvider reduceMotion="off">
<CrossFadeIcon source={source} color="black" size={24} />
</PaperProvider>
);

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) => (
<PaperProvider reduceMotion="on">
<CrossFadeIcon source={source} color="black" size={24} />
</PaperProvider>
);

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);
Expand Down
Loading