-
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.
Fix insertion point in Widgets editors (#33802)
- Loading branch information
1 parent
b4c5c94
commit 466c9e3
Showing
10 changed files
with
153 additions
and
25 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
33 changes: 21 additions & 12 deletions
33
packages/customize-widgets/src/components/inserter/use-inserter.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 |
---|---|---|
@@ -1,33 +1,42 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect, useCallback } from '@wordpress/element'; | ||
import { useEffect, useCallback } from '@wordpress/element'; | ||
import { useSelect, useDispatch, select as selectStore } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as customizeWidgetsStore } from '../../store'; | ||
|
||
export default function useInserter( inserter ) { | ||
const [ isInserterOpened, setIsInserterOpened ] = useState( | ||
() => inserter.isOpen | ||
const isInserterOpened = useSelect( ( select ) => | ||
select( customizeWidgetsStore ).isInserterOpened() | ||
); | ||
const { setIsInserterOpened } = useDispatch( customizeWidgetsStore ); | ||
|
||
useEffect( () => { | ||
return inserter.subscribe( setIsInserterOpened ); | ||
}, [ inserter ] ); | ||
if ( isInserterOpened ) { | ||
inserter.open(); | ||
} else { | ||
inserter.close(); | ||
} | ||
}, [ inserter, isInserterOpened ] ); | ||
|
||
return [ | ||
isInserterOpened, | ||
useCallback( | ||
( updater ) => { | ||
let isOpen = updater; | ||
if ( typeof updater === 'function' ) { | ||
isOpen = updater( inserter.isOpen ); | ||
isOpen = updater( | ||
selectStore( customizeWidgetsStore ).isInserterOpened() | ||
); | ||
} | ||
|
||
if ( isOpen ) { | ||
inserter.open(); | ||
} else { | ||
inserter.close(); | ||
} | ||
setIsInserterOpened( isOpen ); | ||
}, | ||
[ inserter ] | ||
[ setIsInserterOpened ] | ||
), | ||
]; | ||
} |
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
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