Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator: disable initial animation #49062

Merged
merged 6 commits into from
Mar 15, 2023
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- `FontSizePicker`: Allow custom units for custom font size control ([#48468](https://github.com/WordPress/gutenberg/pull/48468)).
- `Navigator`: Disable initial screen animation ([#49062](https://github.com/WordPress/gutenberg/pull/49062)).
- `FormTokenField`: Hide suggestions list on blur event if the input value is invalid ([#48785](https://github.com/WordPress/gutenberg/pull/48785)).

### Bug Fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,19 @@ function UnconnectedNavigatorScreen(
},
x: 0,
};
const initial = {
opacity: 0,
x:
( isRTL() && location.isBack ) || ( ! isRTL() && ! location.isBack )
? 50
: -50,
};
// Disable the initial animation if the screen is the very first screen to be
// rendered within the current `NavigatorProvider`.
const initial =
location.isInitial && ! location.isBack
? false
: {
opacity: 0,
x:
( isRTL() && location.isBack ) ||
( ! isRTL() && ! location.isBack )
? 50
: -50,
};
const exit = {
delay: animationExitDelay,
opacity: 0,
Expand Down
87 changes: 65 additions & 22 deletions packages/components/src/navigator/test/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import type { ReactNode, ForwardedRef, ComponentPropsWithoutRef } from 'react';
import type { ComponentPropsWithoutRef } from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

Expand All @@ -23,27 +23,6 @@ import {
useNavigator,
} from '..';

jest.mock( 'framer-motion', () => {
const actual = jest.requireActual( 'framer-motion' );
return {
__esModule: true,
...actual,
AnimatePresence:
( { children }: { children?: ReactNode } ) =>
() =>
<div>{ children }</div>,
motion: {
...actual.motion,
div: require( 'react' ).forwardRef(
(
{ children }: { children?: ReactNode },
ref: ForwardedRef< HTMLDivElement >
) => <div ref={ ref }>{ children }</div>
),
},
};
} );

const INVALID_HTML_ATTRIBUTE = {
raw: ' "\'><=invalid_path',
escaped: " &quot;'&gt;<=invalid_path",
Expand Down Expand Up @@ -738,4 +717,68 @@ describe( 'Navigator', () => {
expect( getNavigationButton( 'toChildScreen' ) ).toHaveFocus();
} );
} );

describe( 'animation', () => {
it( 'should not animate the initial screen', async () => {
const onHomeAnimationStartSpy = jest.fn();

render(
<NavigatorProvider initialPath="/">
<NavigatorScreen
path="/"
onAnimationStart={ onHomeAnimationStartSpy }
>
<CustomNavigatorButton path="/child">
To child
</CustomNavigatorButton>
</NavigatorScreen>
</NavigatorProvider>
);

expect( onHomeAnimationStartSpy ).not.toHaveBeenCalled();
} );

it( 'should animate all other screens (including the initial screen when navigating back)', async () => {
const user = userEvent.setup();

const onHomeAnimationStartSpy = jest.fn();
const onChildAnimationStartSpy = jest.fn();

render(
<NavigatorProvider initialPath="/">
<NavigatorScreen
path="/"
onAnimationStart={ onHomeAnimationStartSpy }
>
<CustomNavigatorButton path="/child">
To child
</CustomNavigatorButton>
</NavigatorScreen>
<NavigatorScreen
path="/child"
onAnimationStart={ onChildAnimationStartSpy }
>
<CustomNavigatorBackButton>
Back to home
</CustomNavigatorBackButton>
</NavigatorScreen>
</NavigatorProvider>
);

expect( onHomeAnimationStartSpy ).not.toHaveBeenCalled();
expect( onChildAnimationStartSpy ).not.toHaveBeenCalled();

await user.click(
screen.getByRole( 'button', { name: 'To child' } )
);
expect( onChildAnimationStartSpy ).toHaveBeenCalledTimes( 1 );
expect( onHomeAnimationStartSpy ).not.toHaveBeenCalled();

await user.click(
screen.getByRole( 'button', { name: 'Back to home' } )
);
expect( onChildAnimationStartSpy ).toHaveBeenCalledTimes( 1 );
expect( onHomeAnimationStartSpy ).toHaveBeenCalledTimes( 1 );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ exports[`EditPostPreferencesModal should match snapshot when the modal is active
class="emotion-2 components-navigator-screen"
data-wp-c16t="true"
data-wp-component="NavigatorScreen"
style="opacity: 0; transform: translateX(50px) translateZ(0);"
style="opacity: 1; transform: none;"
>
<div
class="components-surface components-card emotion-3 emotion-1"
Expand Down