Skip to content

Commit

Permalink
Move handlePageLoad function to react hook
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Nov 27, 2023
1 parent 0d19e19 commit 4325287
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ErrorBoundary } from 'react-error-boundary';
// eslint-disable-next-line import/no-unresolved
import { store as noticesStore } from '@wordpress/notices';
import { kebabCase, filter } from 'lodash';
import { useHandlePageLoad } from './util/hooks';
import { Root } from '@newfold/ui-component-library';
import { AppNav } from './components/app-nav';
import { SiteInfoBar } from './components/site-info';
Expand Down Expand Up @@ -50,6 +51,8 @@ const AppBody = ( props ) => {
const hashedPath = '#' + location.pathname;
const { booted, hasError } = useContext( AppStore );

useHandlePageLoad();

return (
<main
id="wppbh-app-rendered"
Expand Down
1 change: 1 addition & 0 deletions src/app/util/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useHandlePageLoad } from './useHandlePageLoad.js';
18 changes: 18 additions & 0 deletions src/app/util/hooks/useHandlePageLoad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useLocation } from 'react-router-dom';

const useHandlePageLoad = () => {
const location = useLocation();
const routeContents = document.querySelector( '.wppbh-app-body-inner' );

useEffect( () => {
window.scrollTo( 0, 0 );
if ( routeContents ) {
routeContents.focus( { preventScroll: true } );
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ location.pathname ] );

return true;
};

export default useHandlePageLoad;

0 comments on commit 4325287

Please sign in to comment.