Skip to content

Commit

Permalink
Editor: Unify Header component
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 1, 2024
1 parent 28054e3 commit f62f919
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 819 deletions.
5 changes: 2 additions & 3 deletions packages/e2e-test-utils/src/ensure-sidebar-opened.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/
export async function ensureSidebarOpened() {
const toggleSidebarButton = await page.$(
'.edit-post-header__settings [aria-label="Settings"][aria-expanded="false"],' +
'.editor-header__settings [aria-label="Settings"][aria-expanded="false"],' +
'.edit-site-header__actions [aria-label="Settings"][aria-expanded="false"],' +
'.edit-widgets-header__actions [aria-label="Settings"][aria-expanded="false"],' +
'.edit-site-header-edit-mode__actions [aria-label="Settings"][aria-expanded="false"]'
'.edit-widgets-header__actions [aria-label="Settings"][aria-expanded="false"]'
);

if ( toggleSidebarButton ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-test-utils/src/inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async function isGlobalInserterOpen() {
'.edit-site-header [aria-label="Toggle block inserter"].is-pressed,' +
'.edit-widgets-header [aria-label="Toggle block inserter"].is-pressed,' +
'.edit-widgets-header [aria-label="Add block"].is-pressed,' +
'.edit-site-header-edit-mode__inserter-toggle.is-pressed'
'.edit-site-header-edit-mode__inserter-toggle.is-pressed,' +
'.editor-header [aria-label="Toggle block inserter"].is-pressed'
);
} );
}
Expand Down
4 changes: 1 addition & 3 deletions packages/e2e-test-utils/src/site-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export async function visitSiteEditor( query, skipWelcomeGuide = true ) {
* Toggles the global styles sidebar (opens it if closed and closes it if open).
*/
export async function toggleGlobalStyles() {
await page.click(
'.edit-site-header-edit-mode__actions button[aria-label="Styles"]'
);
await page.click( '.editor-header__settings button[aria-label="Styles"]' );
}

/**
Expand Down
121 changes: 9 additions & 112 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
DocumentBar,
PostSavedState,
PostPreviewButton,
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';
import { useViewportMatch } from '@wordpress/compose';
import { __unstableMotion as motion } from '@wordpress/components';
import { store as preferencesStore } from '@wordpress/preferences';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -28,21 +14,7 @@ import MainDashboardButton from './main-dashboard-button';
import { store as editPostStore } from '../../store';
import { unlock } from '../../lock-unlock';

const {
CollapsableBlockToolbar,
DocumentTools,
PostViewLink,
PreviewDropdown,
PinnedItems,
MoreMenu,
PostPublishButtonOrToggle,
} = unlock( editorPrivateApis );

const slideY = {
hidden: { y: '-50px' },
distractionFreeInactive: { y: 0 },
hover: { y: 0, transition: { type: 'tween', delay: 0.2 } },
};
const { Header: EditorHeader } = unlock( editorPrivateApis );

const slideX = {
hidden: { x: '-100%' },
Expand All @@ -51,39 +23,17 @@ const slideX = {
};

function Header( { setEntitiesSavedStatesCallback, initialPost } ) {
const isWideViewport = useViewportMatch( 'large' );
const isLargeViewport = useViewportMatch( 'medium' );
const {
isTextEditor,
hasActiveMetaboxes,
isPublishSidebarOpened,
showIconLabels,
hasHistory,
hasFixedToolbar,
} = useSelect( ( select ) => {
const { get: getPreference } = select( preferencesStore );
const { getEditorMode } = select( editorStore );

const { hasActiveMetaboxes } = useSelect( ( select ) => {
return {
isTextEditor: getEditorMode() === 'text',
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
hasHistory:
!! select( editorStore ).getEditorSettings()
.onNavigateToPreviousEntityRecord,
isPublishSidebarOpened:
select( editorStore ).isPublishSidebarOpened(),
showIconLabels: getPreference( 'core', 'showIconLabels' ),
hasFixedToolbar: getPreference( 'core', 'fixedToolbar' ),
};
}, [] );

const hasTopToolbar = isLargeViewport && hasFixedToolbar;

const [ isBlockToolsCollapsed, setIsBlockToolsCollapsed ] =
useState( true );

return (
<div className="edit-post-header">
<EditorHeader
forceIsDirty={ hasActiveMetaboxes }
setEntitiesSavedStatesCallback={ setEntitiesSavedStatesCallback }
>
<MainDashboardButton.Slot>
<motion.div
variants={ slideX }
Expand All @@ -95,61 +45,8 @@ function Header( { setEntitiesSavedStatesCallback, initialPost } ) {
/>
</motion.div>
</MainDashboardButton.Slot>
<motion.div
variants={ slideY }
transition={ { type: 'tween', delay: 0.8 } }
className="edit-post-header__toolbar"
>
<DocumentTools disableBlockTools={ isTextEditor } />
{ hasTopToolbar && (
<CollapsableBlockToolbar
isCollapsed={ isBlockToolsCollapsed }
onToggle={ setIsBlockToolsCollapsed }
/>
) }
<div
className={ classnames( 'edit-post-header__center', {
'is-collapsed':
hasHistory &&
! isBlockToolsCollapsed &&
hasTopToolbar,
} ) }
>
{ hasHistory && <DocumentBar /> }
</div>
</motion.div>
<motion.div
variants={ slideY }
transition={ { type: 'tween', delay: 0.8 } }
className="edit-post-header__settings"
>
{ ! isPublishSidebarOpened && (
// This button isn't completely hidden by the publish sidebar.
// We can't hide the whole toolbar when the publish sidebar is open because
// we want to prevent mounting/unmounting the PostPublishButtonOrToggle DOM node.
// We track that DOM node to return focus to the PostPublishButtonOrToggle
// when the publish sidebar has been closed.
<PostSavedState forceIsDirty={ hasActiveMetaboxes } />
) }
<PreviewDropdown forceIsAutosaveable={ hasActiveMetaboxes } />
<PostPreviewButton
className="edit-post-header__post-preview-button"
forceIsAutosaveable={ hasActiveMetaboxes }
/>
<PostViewLink />
<PostPublishButtonOrToggle
forceIsDirty={ hasActiveMetaboxes }
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
/>
{ ( isWideViewport || ! showIconLabels ) && (
<PinnedItems.Slot scope="core" />
) }
<MoreMenu />
<PostEditorMoreMenu />
</motion.div>
</div>
<PostEditorMoreMenu />
</EditorHeader>
);
}

Expand Down

0 comments on commit f62f919

Please sign in to comment.