Skip to content

Commit

Permalink
Add template areas to template inspector (#35239)
Browse files Browse the repository at this point in the history
Co-authored-by: James Koster <james@jameskoster.co.uk>
  • Loading branch information
kevin940726 and jameskoster committed Oct 7, 2021
1 parent dd89478 commit 1264b12
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { store as coreStore } from '@wordpress/core-data';
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
import TemplateAreas from './template-areas';

export default function TemplateCard() {
const { title, description, icon } = useSelect( ( select ) => {
Expand All @@ -36,9 +37,11 @@ export default function TemplateCard() {
<Icon className="edit-site-template-card__icon" icon={ icon } />
<div className="edit-site-template-card__content">
<h2 className="edit-site-template-card__title">{ title }</h2>
<span className="edit-site-template-card__description">
<div className="edit-site-template-card__description">
{ description }
</span>
</div>

<TemplateAreas />
</div>
</div>
);
Expand Down
23 changes: 23 additions & 0 deletions packages/edit-site/src/components/sidebar/template-card/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

.edit-site-template-card__description {
font-size: $default-font-size;
margin: 0 0 $grid-unit-20;
}

.edit-site-template-card__icon {
Expand All @@ -26,3 +27,25 @@
width: $icon-size;
height: $icon-size;
}

h3.edit-site-template-card__template-areas-title {
font-weight: 500;
margin: 0 0 $grid-unit-10;
}

.edit-site-template-card__template-areas-list {
margin: 0;

> li {
margin: 0;
}
}

.edit-site-template-card__template-areas-item {
width: 100%;

// Override the default padding.
&.components-button.has-icon {
padding: 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import {
Button,
__experimentalHeading as Heading,
} from '@wordpress/components';
import { getTemplatePartIcon } from '@wordpress/editor';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
import { TEMPLATE_PART_AREA_TO_NAME } from '../../../store/constants';

function TemplateAreaItem( { area, clientId } ) {
const { selectBlock, toggleBlockHighlight } = useDispatch(
blockEditorStore
);
const highlightBlock = () => toggleBlockHighlight( clientId, true );
const cancelHighlightBlock = () => toggleBlockHighlight( clientId, false );

return (
<Button
className="edit-site-template-card__template-areas-item"
icon={ getTemplatePartIcon( area ) }
onMouseOver={ highlightBlock }
onMouseLeave={ cancelHighlightBlock }
onFocus={ highlightBlock }
onBlur={ cancelHighlightBlock }
onClick={ () => {
selectBlock( clientId );
} }
>
{ TEMPLATE_PART_AREA_TO_NAME[ area ] }
</Button>
);
}

export default function TemplateAreas() {
const templateAreaBlocks = useSelect(
( select ) => select( editSiteStore ).getTemplateAreaBlocks(),
[]
);

return (
<section className="edit-site-template-card__template-areas">
<Heading
level={ 3 }
className="edit-site-template-card__template-areas-title"
>
{ __( 'Areas' ) }
</Heading>

<ul className="edit-site-template-card__template-areas-list">
{ Object.entries( templateAreaBlocks ).map(
( [ area, templateAreaBlock ] ) => (
<li key={ area }>
<TemplateAreaItem
area={ area }
clientId={ templateAreaBlock.clientId }
/>
</li>
)
) }
</ul>
</section>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function TemplateAreas() {

return (
<MenuGroup
label={ __( 'Template areas' ) }
label={ __( 'Areas' ) }
className="edit-site-template-details__group edit-site-template-details__template-areas"
>
{ Object.entries( templateAreaBlocks ).map(
Expand Down

0 comments on commit 1264b12

Please sign in to comment.