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
27 changes: 17 additions & 10 deletions src/components/FAB/Extended.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import Animated, {
type AnimatedStyle,
measure,
ReduceMotion,
useAnimatedRef,
useAnimatedStyle,
useSharedValue,
Expand Down Expand Up @@ -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';
Expand Down
21 changes: 15 additions & 6 deletions src/components/FAB/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ColorValue, GestureResponderEvent } from 'react-native';

import Animated, {
interpolate,
ReduceMotion,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions src/components/FAB/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from 'react-native';

import Animated, {
ReduceMotion,
useAnimatedStyle,
useSharedValue,
withSpring,
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions src/components/__tests__/FAB.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<FAB icon="plus" />)).toJSON();
expect(tree).toMatchSnapshot();
Expand Down Expand Up @@ -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(
<PaperProvider reduceMotion="off">
<FAB icon="plus" />
</PaperProvider>
);
jest.mocked(Reanimated.withSpring).mockClear();

await rerender(
<PaperProvider reduceMotion="off">
<FAB icon="plus" visible={false} />
</PaperProvider>
);
await rerender(
<PaperProvider reduceMotion="off">
<FAB icon="plus" visible />
</PaperProvider>
);

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();
Expand Down
36 changes: 36 additions & 0 deletions src/components/__tests__/FABExtended.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -18,6 +19,7 @@ jest.mock('react-native-reanimated', () => {
...ReanimatedModule,
default: ReanimatedModule.default,
measure: jest.fn(),
withSpring: jest.fn(ReanimatedModule.withSpring),
};
});

Expand Down Expand Up @@ -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(
<PaperProvider reduceMotion="off">
<FAB.Extended icon="plus" label="New message" expanded />
</PaperProvider>
);
await jest.runAllTimersAsync();
jest.mocked(Reanimated.withSpring).mockClear();

await rerender(
<PaperProvider reduceMotion="off">
<FAB.Extended icon="plus" label="New message" expanded={false} />
</PaperProvider>
);
await jest.runAllTimersAsync();
await rerender(
<PaperProvider reduceMotion="off">
<FAB.Extended icon="plus" label="New message" expanded />
</PaperProvider>
);
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(
<FAB.Extended
Expand Down
65 changes: 65 additions & 0 deletions src/components/__tests__/FABMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { 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';
import type { MenuItemProps } from '../FAB/Menu';

jest.mock('react-native-reanimated', () => {
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] => [
Expand Down Expand Up @@ -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(
<PaperProvider reduceMotion="off">
<FAB.Menu
expanded={false}
onDismiss={jest.fn()}
trigger={{ icon: 'plus' }}
items={makeItems()}
/>
</PaperProvider>
);
jest.mocked(Reanimated.withSpring).mockClear();
jest.mocked(Reanimated.withDelay).mockClear();

await rerender(
<PaperProvider reduceMotion="off">
<FAB.Menu
expanded
onDismiss={jest.fn()}
trigger={{ icon: 'plus' }}
items={makeItems()}
/>
</PaperProvider>
);

// 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(
Expand Down
Loading