Skip to content

Commit

Permalink
useEvent to avoid memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 26, 2024
1 parent a5d9759 commit ca9164f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"types": "build-types",
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/compose": "*",
"@wordpress/element": "*",
"@wordpress/private-apis": "*",
"@wordpress/url": "*",
Expand Down
94 changes: 50 additions & 44 deletions packages/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getPath,
buildQueryString,
} from '@wordpress/url';
import { useEvent } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -91,53 +92,58 @@ export function useLocation() {

export function useHistory() {
const { pathArg, beforeNavigate } = useContext( ConfigContext );
return useMemo(
() => ( {
navigate( rawPath: string, options: NavigationOptions = {} ) {
const query = getQueryArgs( rawPath );
const path = getPath( 'http://domain.com/' + rawPath ) ?? '';
const performPush = () => {
const result = beforeNavigate
? beforeNavigate( { path, query } )
: { path, query };
return history.push(
{
search: buildQueryString( {
[ pathArg ]: result.path,
...result.query,
} ),
},
options.state
);
};

/*
* Skip transition in mobile, otherwise it crashes the browser.
* See: https://github.com/WordPress/gutenberg/pull/63002.
*/
const isMediumOrBigger =
window.matchMedia( '(min-width: 782px)' ).matches;
if (
! isMediumOrBigger ||
// @ts-expect-error
! document.startViewTransition ||
! options.transition
) {
return performPush();
}
document.documentElement.classList.add( options.transition );

const navigate = useEvent(
( rawPath: string, options: NavigationOptions = {} ) => {
const query = getQueryArgs( rawPath );
const path = getPath( 'http://domain.com/' + rawPath ) ?? '';
const performPush = () => {
const result = beforeNavigate
? beforeNavigate( { path, query } )
: { path, query };
return history.push(
{
search: buildQueryString( {
[ pathArg ]: result.path,
...result.query,
} ),
},
options.state
);
};

/*
* Skip transition in mobile, otherwise it crashes the browser.
* See: https://github.com/WordPress/gutenberg/pull/63002.
*/
const isMediumOrBigger =
window.matchMedia( '(min-width: 782px)' ).matches;
if (
! isMediumOrBigger ||
// @ts-expect-error
const transition = document.startViewTransition( () =>
performPush()
! document.startViewTransition ||
! options.transition
) {
return performPush();
}
document.documentElement.classList.add( options.transition );
// @ts-expect-error
const transition = document.startViewTransition( () =>
performPush()
);
transition.finished.finally( () => {
document.documentElement.classList.remove(
options.transition ?? ''
);
transition.finished.finally( () => {
document.documentElement.classList.remove(
options.transition ?? ''
);
} );
},
} );
}
);

return useMemo(
() => ( {
navigate,
} ),
[ pathArg, beforeNavigate ]
[ navigate ]
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"types": [ "gutenberg-env" ]
},
"references": [
{ "path": "../compose" },
{ "path": "../element" },
{ "path": "../private-apis" },
{ "path": "../url" }
Expand Down

0 comments on commit ca9164f

Please sign in to comment.