forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Open the last inspector tab used when selecting a block
- Loading branch information
Showing
2 changed files
with
50 additions
and
3 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
40 changes: 40 additions & 0 deletions
40
...-editor/src/components/inspector-controls-tabs/use-last-selected-inspector-control-tab.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,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import { TAB_LIST_VIEW } from './utils'; | ||
|
||
const SETTINGS_KEY = 'lastSelectedInspectorControlTab'; | ||
|
||
export default function useLastSelectedInspectorControlTab( tabs ) { | ||
const lastTab = useSelect( ( select ) => { | ||
const settings = select( blockEditorStore ).getSettings(); | ||
return settings[ SETTINGS_KEY ]; | ||
} ); | ||
|
||
const { updateSettings } = useDispatch( blockEditorStore ); | ||
|
||
const setLastSelectedTab = useCallback( | ||
( tabName ) => { | ||
if ( TAB_LIST_VIEW.name === tabName ) { | ||
return; | ||
} | ||
updateSettings( { | ||
[ SETTINGS_KEY ]: tabName, | ||
} ); | ||
}, | ||
[ updateSettings ] | ||
); | ||
|
||
const selectedBlockHasTab = !! tabs?.find( | ||
( { name } ) => lastTab === name | ||
); | ||
|
||
return [ selectedBlockHasTab ? lastTab : undefined, setLastSelectedTab ]; | ||
} |