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
24 changes: 15 additions & 9 deletions src/components/Drawer/DrawerCollapsedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import useLatestCallback from 'use-latest-callback';

import { useInternalTheme } from '../../core/theming';
import { tokens } from '../../theme/tokens';
Expand Down Expand Up @@ -141,19 +142,24 @@

const outlineScale = useSharedValue(active ? 1 : 0.5);

React.useEffect(() => {
if (!active) {
outlineScale.value = 0.5;
}
}, [active, outlineScale]);

const handlePressOut = () => {
const expandOutline = useLatestCallback(() => {
outlineScale.value = withTiming(1, {
duration: 150 * scale,
easing: Easing.inOut(Easing.ease),
reduceMotion: ReduceMotion.Never,
});
};
});

React.useEffect(() => {
if (active) {
// The item can become active without a press, for example when the
// current route changes. After a press the indicator is already at full
// size or on its way there, and this carries on from where it is.
expandOutline();
} else {
outlineScale.value = 0.5;
}
}, [active, expandOutline, outlineScale]);

Check warning on line 162 in src/components/Drawer/DrawerCollapsedItem.tsx

View check run for this annotation

apex-sentinel-staging / Apex Sentinel

minor: New no-press expansion animates even when the app sets reduceMotion

With `reduceMotion="on"` on `PaperProvider`, a route change that sets `active` true animates the indicator over 150ms\*scale instead of snapping, because the new effect path hardcodes `ReduceMotion.Never` rather than consulting `useReduceMotion()`.
Comment thread
Doberjohn marked this conversation as resolved.
Resolved

const iconPadding = ((!label ? itemSize : outlineHeight) - iconSize) / 2;

Expand Down Expand Up @@ -196,7 +202,7 @@
<View {...rest}>
<Pressable
onPress={onPress}
onPressOut={onPress ? handlePressOut : undefined}
onPressOut={onPress ? expandOutline : undefined}
disabled={disabled}
role="button"
aria-selected={active}
Expand Down
55 changes: 53 additions & 2 deletions src/components/__tests__/Drawer/DrawerCollapsedItem.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, expect, it } from '@jest/globals';
import * as React from 'react';

import { render } from '../../../test-utils';
import { describe, expect, it, jest } from '@jest/globals';
import { getAnimatedStyle } from 'react-native-reanimated';

import { act, render, screen, userEvent } from '../../../test-utils';
import DrawerCollapsedItem from '../../Drawer/DrawerCollapsedItem';

describe('DrawerCollapsedItem', () => {
Expand Down Expand Up @@ -59,4 +62,52 @@ describe('DrawerCollapsedItem', () => {

expect(tree).toMatchSnapshot();
});

describe('active indicator', () => {
const indicatorStyle = () => {
const [indicator] = screen
.getByRole('button')
.queryAll((node) => 'transform' in getAnimatedStyle(node));
return getAnimatedStyle(indicator);
};

it('expands to full size when the item becomes active without a press', async () => {
await render(
<DrawerCollapsedItem label="Inbox" focusedIcon="inbox" active={false} />
);

await screen.rerender(
<DrawerCollapsedItem label="Inbox" focusedIcon="inbox" active />
);
await act(() => jest.advanceTimersByTimeAsync(300));

expect(indicatorStyle().transform).toEqual([{ scaleX: 1 }]);
});

it('animates the expansion when a press makes the item active', async () => {
const Item = () => {
const [active, setActive] = React.useState(false);
return (
<DrawerCollapsedItem
label="Inbox"
focusedIcon="inbox"
active={active}
onPress={() => setActive(true)}
/>
);
};
await render(<Item />);
const collapsed = indicatorStyle().transform;

await userEvent.press(screen.getByRole('button'));
await act(() => jest.advanceTimersByTimeAsync(75));
const midway = indicatorStyle().transform;
await act(() => jest.advanceTimersByTimeAsync(300));
const expanded = indicatorStyle().transform;

expect(expanded).toEqual([{ scaleX: 1 }]);
expect(midway).not.toEqual(collapsed);
expect(midway).not.toEqual(expanded);
});
});
});
Loading