diff --git a/packages/editor/src/components/document-bar/index.js b/packages/editor/src/components/document-bar/index.js index 2ac03a63e73fb..84005331d18bb 100644 --- a/packages/editor/src/components/document-bar/index.js +++ b/packages/editor/src/components/document-bar/index.js @@ -12,6 +12,8 @@ import { Button, __experimentalText as Text, __experimentalHStack as HStack, + __unstableMotion as motion, + __unstableAnimatePresence as AnimatePresence, } from '@wordpress/components'; import { BlockIcon } from '@wordpress/block-editor'; import { @@ -22,16 +24,17 @@ import { symbol, } from '@wordpress/icons'; import { displayShortcut } from '@wordpress/keycodes'; -import { useEntityRecord } from '@wordpress/core-data'; +import { store as coreStore } from '@wordpress/core-data'; import { store as commandsStore } from '@wordpress/commands'; -import { useState, useEffect, useRef } from '@wordpress/element'; +import { useRef, useEffect } from '@wordpress/element'; +import { useReducedMotion } from '@wordpress/compose'; /** * Internal dependencies */ import { store as editorStore } from '../../store'; -const typeLabels = { +const TYPE_LABELS = { // translators: 1: Pattern title. wp_pattern: __( 'Editing pattern: %s' ), // translators: 1: Navigation menu title. @@ -42,127 +45,149 @@ const typeLabels = { wp_template_part: __( 'Editing template part: %s' ), }; -const icons = { +const ICONS = { wp_block: symbol, wp_navigation: navigationIcon, }; -export default function DocumentBar() { - const { postType, postId, onNavigateToPreviousEntityRecord } = useSelect( - ( select ) => { - const { - getCurrentPostId, - getCurrentPostType, - getEditorSettings: getSettings, - } = select( editorStore ); - return { - postType: getCurrentPostType(), - postId: getCurrentPostId(), - onNavigateToPreviousEntityRecord: - getSettings().onNavigateToPreviousEntityRecord, - getEditorSettings: getSettings, - }; - }, - [] - ); +const TEMPLATE_POST_TYPES = [ 'wp_template', 'wp_template_part' ]; - const handleOnBack = () => { - if ( onNavigateToPreviousEntityRecord ) { - onNavigateToPreviousEntityRecord(); - } - }; +const GLOBAL_POST_TYPES = [ + ...TEMPLATE_POST_TYPES, + 'wp_block', + 'wp_navigation', +]; - return ( - - ); -} +const MotionButton = motion( Button ); -function BaseDocumentActions( { postType, postId, onBack } ) { - const { open: openCommandCenter } = useDispatch( commandsStore ); - const { editedRecord: doc, isResolving } = useEntityRecord( - 'postType', +export default function DocumentBar() { + const { postType, - postId - ); - const { templateIcon, templateTitle } = useSelect( ( select ) => { - const { __experimentalGetTemplateInfo: getTemplateInfo } = - select( editorStore ); - const templateInfo = getTemplateInfo( doc ); + document, + isResolving, + templateIcon, + templateTitle, + onNavigateToPreviousEntityRecord, + } = useSelect( ( select ) => { + const { + getCurrentPostType, + getCurrentPostId, + getEditorSettings, + __experimentalGetTemplateInfo: getTemplateInfo, + } = select( editorStore ); + const { getEditedEntityRecord, getIsResolving } = select( coreStore ); + const _postType = getCurrentPostType(); + const _postId = getCurrentPostId(); + const _document = getEditedEntityRecord( + 'postType', + _postType, + _postId + ); + const _templateInfo = getTemplateInfo( _document ); return { - templateIcon: templateInfo.icon, - templateTitle: templateInfo.title, + postType: _postType, + document: _document, + isResolving: getIsResolving( + 'getEditedEntityRecord', + 'postType', + _postType, + _postId + ), + templateIcon: _templateInfo.icon, + templateTitle: _templateInfo.title, + onNavigateToPreviousEntityRecord: + getEditorSettings().onNavigateToPreviousEntityRecord, }; - } ); - const isNotFound = ! doc && ! isResolving; - const icon = icons[ postType ] ?? pageIcon; - const [ isAnimated, setIsAnimated ] = useState( false ); - const isMounting = useRef( true ); - const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( - postType - ); - const isGlobalEntity = [ - 'wp_template', - 'wp_navigation', - 'wp_template_part', - 'wp_block', - ].includes( postType ); + }, [] ); - useEffect( () => { - if ( ! isMounting.current ) { - setIsAnimated( true ); - } - isMounting.current = false; - }, [ postType, postId ] ); + const { open: openCommandCenter } = useDispatch( commandsStore ); + const isReducedMotion = useReducedMotion(); + + const isNotFound = ! document && ! isResolving; + const icon = ICONS[ postType ] ?? pageIcon; + const isTemplate = TEMPLATE_POST_TYPES.includes( postType ); + const isGlobalEntity = GLOBAL_POST_TYPES.includes( postType ); + const hasBackButton = !! onNavigateToPreviousEntityRecord; + const title = isTemplate ? templateTitle : document.title; - const title = isTemplate ? templateTitle : doc.title; + const mounted = useRef( false ); + useEffect( () => { + mounted.current = true; + }, [] ); return (
- { onBack && ( - - ) } - { isNotFound && { __( 'Document not found' ) } } - { ! isNotFound && ( + + { hasBackButton && ( + { + event.stopPropagation(); + onNavigateToPreviousEntityRecord(); + } } + size="compact" + initial={ + mounted.current + ? { opacity: 0, transform: 'translateX(15%)' } + : false // Don't show entry animation when DocumentBar mounts. + } + animate={ { opacity: 1, transform: 'translateX(0%)' } } + exit={ { opacity: 0, transform: 'translateX(15%)' } } + transition={ + isReducedMotion ? { duration: 0 } : undefined + } + > + { __( 'Back' ) } + + ) } + + { isNotFound ? ( + { __( 'Document not found' ) } + ) : (