diff --git a/packages/editor/src/components/post-template/block-theme.js b/packages/editor/src/components/post-template/block-theme.js index 62de10a2c715ff..6d7f7f787b32f8 100644 --- a/packages/editor/src/components/post-template/block-theme.js +++ b/packages/editor/src/components/post-template/block-theme.js @@ -4,12 +4,18 @@ import { useSelect, useDispatch } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components'; +import { useState, useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { useEntityRecord, store as coreStore } from '@wordpress/core-data'; import { check } from '@wordpress/icons'; import { store as noticesStore } from '@wordpress/notices'; import { store as preferencesStore } from '@wordpress/preferences'; +/** + * Internal dependencies + */ +import PostPanelRow from '../post-panel-row'; + /** * Internal dependencies */ @@ -19,11 +25,6 @@ import ResetDefaultTemplate from './reset-default-template'; import { unlock } from '../../lock-unlock'; import CreateNewTemplate from './create-new-template'; -const POPOVER_PROPS = { - className: 'editor-post-template__dropdown', - placement: 'bottom-start', -}; - export default function BlockThemeControl( { id } ) { const { isTemplateHidden, @@ -63,6 +64,21 @@ export default function BlockThemeControl( { id } ) { [] ); + const [ popoverAnchor, setPopoverAnchor ] = useState( null ); + // Memoize popoverProps to avoid returning a new object every time. + const popoverProps = useMemo( + () => ( { + // Anchor the popover to the middle of the entire row so that it doesn't + // move around when the label changes. + anchor: popoverAnchor, + className: 'editor-post-template__dropdown', + placement: 'left-start', + offset: 36, + shift: true, + } ), + [ popoverAnchor ] + ); + if ( ! hasResolved ) { return null; } @@ -90,60 +106,62 @@ export default function BlockThemeControl( { id } ) { } }; return ( - - { ( { onClose } ) => ( - <> - - { canCreateTemplate && ( + + + { ( { onClose } ) => ( + <> + + { canCreateTemplate && ( + { + onNavigateToEntityRecord( { + postId: template.id, + postType: 'wp_template', + } ); + onClose(); + mayShowTemplateEditNotice(); + } } + > + { __( 'Edit template' ) } + + ) } + + + + { canCreateTemplate && ( + + ) } + + { - onNavigateToEntityRecord( { - postId: template.id, - postType: 'wp_template', - } ); - onClose(); - mayShowTemplateEditNotice(); + setRenderingMode( + isTemplateHidden + ? 'template-locked' + : 'post-only' + ); } } > - { __( 'Edit template' ) } + { __( 'Show template' ) } - ) } - - - - { canCreateTemplate && ( - - ) } - - - { - setRenderingMode( - isTemplateHidden - ? 'template-locked' - : 'post-only' - ); - } } - > - { __( 'Show template' ) } - - - - ) } - + + + ) } + + ); } diff --git a/packages/editor/src/components/post-template/classic-theme.js b/packages/editor/src/components/post-template/classic-theme.js index 4345e06211c661..6cef7d1582abd2 100644 --- a/packages/editor/src/components/post-template/classic-theme.js +++ b/packages/editor/src/components/post-template/classic-theme.js @@ -16,11 +16,7 @@ import { store as noticesStore } from '@wordpress/notices'; import { store as editorStore } from '../../store'; import CreateNewTemplateModal from './create-new-template-modal'; import { useAllowSwitchingTemplates } from './hooks'; - -const POPOVER_PROPS = { - className: 'editor-post-template__dropdown', - placement: 'bottom-start', -}; +import PostPanelRow from '../post-panel-row'; function PostTemplateToggle( { isOpen, onClick } ) { const templateTitle = useSelect( ( select ) => { @@ -216,17 +212,37 @@ function PostTemplateDropdownContent( { onClose } ) { } function ClassicThemeControl() { + const [ popoverAnchor, setPopoverAnchor ] = useState( null ); + // Memoize popoverProps to avoid returning a new object every time. + const popoverProps = useMemo( + () => ( { + // Anchor the popover to the middle of the entire row so that it doesn't + // move around when the label changes. + anchor: popoverAnchor, + className: 'editor-post-template__dropdown', + placement: 'left-start', + offset: 36, + shift: true, + } ), + [ popoverAnchor ] + ); + return ( - ( - - ) } - renderContent={ ( { onClose } ) => ( - - ) } - /> + + ( + + ) } + renderContent={ ( { onClose } ) => ( + + ) } + /> + ); } diff --git a/packages/editor/src/components/post-template/panel.js b/packages/editor/src/components/post-template/panel.js index 903612ef11ed15..0bc86695fb8020 100644 --- a/packages/editor/src/components/post-template/panel.js +++ b/packages/editor/src/components/post-template/panel.js @@ -2,7 +2,6 @@ * WordPress dependencies */ import { useSelect } from '@wordpress/data'; -import { __ } from '@wordpress/i18n'; import { store as coreStore } from '@wordpress/core-data'; /** @@ -11,7 +10,6 @@ import { store as coreStore } from '@wordpress/core-data'; import { store as editorStore } from '../../store'; import ClassicThemeControl from './classic-theme'; import BlockThemeControl from './block-theme'; -import PostPanelRow from '../post-panel-row'; /** * Displays the template controls based on the current editor settings and user permissions. @@ -65,19 +63,11 @@ export default function PostTemplatePanel() { }, [] ); if ( ( ! isBlockTheme || ! canViewTemplates ) && isVisible ) { - return ( - - - - ); + return ; } if ( isBlockTheme && !! templateId ) { - return ( - - - - ); + return ; } return null; }