-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronize the sidebar state in the url
- Loading branch information
1 parent
13f8ae4
commit 87166aa
Showing
13 changed files
with
183 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/edit-site/src/components/sync-state-with-url/use-sync-sidebar-path-with-url.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __experimentalUseNavigator as useNavigator } from '@wordpress/components'; | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useLocation, useHistory } from '../routes'; | ||
|
||
export default function useSyncSidebarPathWithURL() { | ||
const history = useHistory(); | ||
const { params } = useLocation(); | ||
const { sidebar = '/' } = params; | ||
const { location, goTo } = useNavigator(); | ||
const currentSidebar = useRef( sidebar ); | ||
const currentNavigatorLocation = useRef( location.path ); | ||
useEffect( () => { | ||
currentSidebar.current = sidebar; | ||
if ( sidebar !== currentNavigatorLocation.current ) { | ||
goTo( sidebar ); | ||
} | ||
}, [ sidebar ] ); | ||
useEffect( () => { | ||
currentNavigatorLocation.current = location.path; | ||
if ( location.path !== currentSidebar.current ) { | ||
history.push( { | ||
...params, | ||
sidebar: location.path, | ||
} ); | ||
} | ||
}, [ location.path, history ] ); | ||
|
||
return sidebar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.