-
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.
Refactor sidebar controls provider to application level so that its s…
…tate is not lost when re-initializing
- Loading branch information
Showing
4 changed files
with
81 additions
and
38 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/customize-widgets/src/components/active-sidebar-control-provider/index.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,41 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from '@wordpress/element'; | ||
|
||
const ActiveSidebarControlContext = createContext( [] ); | ||
|
||
export default function ActiveSidebarControlProvider( { | ||
children, | ||
sidebarControls, | ||
} ) { | ||
const [ activeSidebarControl, setActiveSidebarControl ] = useState( null ); | ||
|
||
useEffect( () => { | ||
const unsubscribers = sidebarControls.map( ( sidebarControl ) => | ||
sidebarControl.subscribe( ( expanded ) => { | ||
if ( expanded ) { | ||
setActiveSidebarControl( sidebarControl ); | ||
} | ||
} ) | ||
); | ||
|
||
return () => { | ||
unsubscribers.forEach( ( unsubscriber ) => unsubscriber() ); | ||
}; | ||
}, [ sidebarControls ] ); | ||
|
||
return ( | ||
<ActiveSidebarControlContext.Provider value={ activeSidebarControl }> | ||
{ children } | ||
</ActiveSidebarControlContext.Provider> | ||
); | ||
} | ||
|
||
export const useActiveSidebarControl = () => | ||
useContext( ActiveSidebarControlContext ); |
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
30 changes: 24 additions & 6 deletions
30
packages/customize-widgets/src/components/sidebar-controls/index.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
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