Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor document actions to handle template part titles #26043

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 73 additions & 59 deletions packages/edit-site/src/components/header/document-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { sprintf, __ } from '@wordpress/i18n';
import {
__experimentalGetBlockLabel as getBlockLabel,
getBlockType,
Expand All @@ -16,12 +16,6 @@ import { Dropdown, Button, VisuallyHidden } from '@wordpress/components';
import { chevronDown } from '@wordpress/icons';
import { useRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import TemplateDetails from '../../template-details';
import { getTemplateInfo } from '../../../utils';

function getBlockDisplayText( block ) {
return block
? getBlockLabel( getBlockType( block.name ), block.attributes )
Expand Down Expand Up @@ -50,8 +44,20 @@ function useSecondaryText() {
return {};
}

export default function DocumentActions( { template } ) {
const { title: documentTitle } = getTemplateInfo( template );
/**
* @param {Object} props Props for the DocumentActions component.
* @param {string} props.entityTitle The title to display.
* @param {string} props.entityLabel A label to use for entity-related options.
* E.g. "template" would be used for "edit
* template" and "show template details".
* @param {Object[]} props.children React component to use for the
* information dropdown area.
*/
export default function DocumentActions( {
entityTitle,
entityLabel,
children: dropdownContent,
} ) {
const { label, isActive } = useSecondaryText();

// Title is active when there is no secondary item, or when the secondary
Expand All @@ -63,71 +69,79 @@ export default function DocumentActions( { template } ) {
// part of it.
const titleRef = useRef();

// Return a simple loading indicator until we have information to show.
if ( ! entityTitle ) {
return (
<div className="edit-site-document-actions">
{ __( 'Loading…' ) }
</div>
);
}

return (
<div
className={ classnames( 'edit-site-document-actions', {
'has-secondary-label': !! label,
} ) }
>
{ documentTitle ? (
<>
<div
ref={ titleRef }
className="edit-site-document-actions__title-wrapper"
>
<h1>
<VisuallyHidden>
{ __( 'Edit template:' ) }
</VisuallyHidden>
<div
className={ classnames(
'edit-site-document-actions__title',
{
'is-active': isTitleActive,
'is-secondary-title-active': isActive,
}
) }
>
{ documentTitle }
</div>
</h1>
{ ! isActive && (
<Dropdown
popoverProps={ {
anchorRef: titleRef.current,
} }
position="bottom center"
renderToggle={ ( { isOpen, onToggle } ) => (
<Button
className="edit-site-document-actions__get-info"
icon={ chevronDown }
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
label={ __( 'Show template details' ) }
/>
) }
renderContent={ () => (
<TemplateDetails template={ template } />
) }
/>
<div
ref={ titleRef }
className="edit-site-document-actions__title-wrapper"
>
<h1>
<VisuallyHidden>
{ sprintf(
/* translators: %s: the entity being edited, like "template"*/
__( 'Edit %s:' ),
entityLabel
) }
</div>

</VisuallyHidden>
<div
className={ classnames(
'edit-site-document-actions__secondary-item',
'edit-site-document-actions__title',
{
'is-active': isTitleActive,
'is-secondary-title-active': isActive,
}
) }
>
{ label ?? '' }
{ entityTitle }
</div>
</>
) : (
__( 'Loading…' )
) }
</h1>
{ dropdownContent && ! isActive && (
<Dropdown
popoverProps={ {
anchorRef: titleRef.current,
} }
position="bottom center"
renderToggle={ ( { isOpen, onToggle } ) => (
<Button
className="edit-site-document-actions__get-info"
icon={ chevronDown }
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
label={ sprintf(
/* translators: %s: the entity to see details about, like "template"*/
__( 'Show %s details' ),
entityLabel
) }
/>
) }
renderContent={ () => dropdownContent }
/>
) }
</div>
<div
className={ classnames(
'edit-site-document-actions__secondary-item',
{
'is-secondary-title-active': isActive,
}
) }
>
{ label ?? '' }
</div>
</div>
);
}
76 changes: 52 additions & 24 deletions packages/edit-site/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,44 @@ import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import DocumentActions from './document-actions';
import TemplateDetails from '../template-details';
import { getTemplateInfo } from '../../utils';

export default function Header( { openEntitiesSavedStates } ) {
const { deviceType, hasFixedToolbar, template, isInserterOpen } = useSelect(
( select ) => {
const {
__experimentalGetPreviewDeviceType,
isFeatureActive,
getTemplateId,
isInserterOpened,
} = select( 'core/edit-site' );
const { getEntityRecord } = select( 'core' );
const {
deviceType,
hasFixedToolbar,
template,
templatePart,
templateType,
isInserterOpen,
} = useSelect( ( select ) => {
const {
__experimentalGetPreviewDeviceType,
isFeatureActive,
getTemplateId,
getTemplatePartId,
getTemplateType,
isInserterOpened,
} = select( 'core/edit-site' );
const { getEntityRecord } = select( 'core' );

const _templateId = getTemplateId();
return {
deviceType: __experimentalGetPreviewDeviceType(),
hasFixedToolbar: isFeatureActive( 'fixedToolbar' ),
template: getEntityRecord(
'postType',
'wp_template',
_templateId
),
isInserterOpen: isInserterOpened(),
};
},
[]
);
const templatePartId = getTemplatePartId();
const templateId = getTemplateId();

return {
deviceType: __experimentalGetPreviewDeviceType(),
hasFixedToolbar: isFeatureActive( 'fixedToolbar' ),
template: getEntityRecord( 'postType', 'wp_template', templateId ),
templatePart: getEntityRecord(
'postType',
'wp_template_part',
templatePartId
),
templateType: getTemplateType(),
isInserterOpen: isInserterOpened(),
};
}, [] );

const {
__experimentalSetPreviewDeviceType: setPreviewDeviceType,
Expand All @@ -58,6 +70,11 @@ export default function Header( { openEntitiesSavedStates } ) {
const displayBlockToolbar =
! isLargeViewport || deviceType !== 'Desktop' || hasFixedToolbar;

let { title } = getTemplateInfo( template );
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
if ( 'wp_template_part' === templateType ) {
title = templatePart?.slug;
}

return (
<div className="edit-site-header">
<div className="edit-site-header_start">
Expand Down Expand Up @@ -88,7 +105,18 @@ export default function Header( { openEntitiesSavedStates } ) {
</div>

<div className="edit-site-header_center">
<DocumentActions template={ template } />
<DocumentActions
entityTitle={ title }
entityLabel={
templateType === 'wp_template'
? 'template'
: 'template part'
}
>
{ templateType === 'wp_template' && (
<TemplateDetails template={ template } />
) }
</DocumentActions>
</div>

<div className="edit-site-header_end">
Expand Down