diff --git a/packages/editor/src/components/preferences-modal/index.js b/packages/editor/src/components/preferences-modal/index.js index a8cfd8245522cd..7ea7ea456ce28e 100644 --- a/packages/editor/src/components/preferences-modal/index.js +++ b/packages/editor/src/components/preferences-modal/index.js @@ -36,25 +36,40 @@ const { } = unlock( preferencesPrivateApis ); export default function EditorPreferencesModal( { extraSections = {} } ) { + const isActive = useSelect( ( select ) => { + return select( interfaceStore ).isModalActive( 'editor/preferences' ); + }, [] ); + const { closeModal } = useDispatch( interfaceStore ); + + if ( ! isActive ) { + return null; + } + + // Please wrap all contents inside PreferencesModalContents to prevent all + // hooks from executing when the modal is not open. + return ( + + + + ); +} + +function PreferencesModalContents( { extraSections = {} } ) { const isLargeViewport = useViewportMatch( 'medium' ); - const { isActive, showBlockBreadcrumbsOption } = useSelect( + const showBlockBreadcrumbsOption = useSelect( ( select ) => { const { getEditorSettings } = select( editorStore ); const { get } = select( preferencesStore ); - const { isModalActive } = select( interfaceStore ); const isRichEditingEnabled = getEditorSettings().richEditingEnabled; const isDistractionFreeEnabled = get( 'core', 'distractionFree' ); - return { - showBlockBreadcrumbsOption: - ! isDistractionFreeEnabled && - isLargeViewport && - isRichEditingEnabled, - isActive: isModalActive( 'editor/preferences' ), - }; + return ( + ! isDistractionFreeEnabled && + isLargeViewport && + isRichEditingEnabled + ); }, [ isLargeViewport ] ); - const { closeModal } = useDispatch( interfaceStore ); const { setIsListViewOpened, setIsInserterOpened } = useDispatch( editorStore ); const { set: setPreference } = useDispatch( preferencesStore ); @@ -330,13 +345,5 @@ export default function EditorPreferencesModal( { extraSections = {} } ) { ] ); - if ( ! isActive ) { - return null; - } - - return ( - - - - ); + return ; } diff --git a/packages/editor/src/components/preferences-modal/test/index.js b/packages/editor/src/components/preferences-modal/test/index.js index 01ac1a88fbe7d8..70102eea40f2ee 100644 --- a/packages/editor/src/components/preferences-modal/test/index.js +++ b/packages/editor/src/components/preferences-modal/test/index.js @@ -19,7 +19,7 @@ jest.mock( '@wordpress/compose/src/hooks/use-viewport-match', () => jest.fn() ); describe( 'EditPostPreferencesModal', () => { it( 'should not render when the modal is not active', () => { - useSelect.mockImplementation( () => [ false, false, false ] ); + useSelect.mockImplementation( () => false ); render( ); expect( screen.queryByRole( 'dialog', { name: 'Preferences' } )