Skip to content

Commit

Permalink
Add template part label
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyip committed Aug 13, 2020
1 parent eaf4b57 commit af2ef8c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BlockControls } from '@wordpress/block-editor';
*/
import useTemplatePartPost from './use-template-part-post';
import TemplatePartNamePanel from './name-panel';
import TemplatePartLabel from './label';
import TemplatePartInnerBlocks from './inner-blocks';
import TemplatePartPlaceholder from './placeholder';

Expand All @@ -29,11 +30,18 @@ export default function TemplatePartEdit( {
// but wait until the third inner blocks change,
// because the first 2 are just the template part
// content loading.
const { innerBlocks } = useSelect(
const { innerBlocks, hasSelectedInnerBlock } = useSelect(
( select ) => {
const { getBlocks } = select( 'core/block-editor' );
const {
getBlocks,
hasSelectedInnerBlock: getHasSelectedInnerBlock,
} = select( 'core/block-editor' );
return {
innerBlocks: getBlocks( clientId ),
hasSelectedInnerBlock: getHasSelectedInnerBlock(
clientId,
true
),
};
},
[ clientId ]
Expand Down Expand Up @@ -67,10 +75,15 @@ export default function TemplatePartEdit( {
setAttributes={ setAttributes }
/>
</BlockControls>
<TemplatePartInnerBlocks
postId={ postId }
hasInnerBlocks={ innerBlocks.length > 0 }
/>
<div style={ { position: 'relative' } }>
{ hasSelectedInnerBlock && (
<TemplatePartLabel postId={ postId } slug={ slug } />
) }
<TemplatePartInnerBlocks
postId={ postId }
hasInnerBlocks={ innerBlocks.length > 0 }
/>
</div>
</>
);
}
Expand Down
27 changes: 27 additions & 0 deletions packages/block-library/src/template-part/edit/label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { Icon } from '@wordpress/components';

export default function TemplatePartLabel( { postId, slug } ) {
const [ title ] = useEntityProp(
'postType',
'wp_template_part',
'title',
postId
);

return (
<div className="wp-block-template-part__label-container">
<div className="wp-block-template-part__label-layout">
<div className="wp-block-template-part__label-content">
<Icon icon="controls-repeat" size={ 12 } />
<span className="wp-block-template-part__label-text">
{ title || slug }
</span>
</div>
</div>
</div>
);
}
29 changes: 29 additions & 0 deletions packages/block-library/src/template-part/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@
}
}

.wp-block-template-part__label-container {
position: absolute;
bottom: 100%;
width: 100%;

.wp-block-template-part__label-layout {
display: flex;
justify-content: flex-start;
}

.wp-block-template-part__label-content {
display: flex;
align-items: center;
border: 1px solid $gray-600;
border-bottom: none;
border-top-right-radius: 2px;
border-top-left-radius: 2px;
padding: 2px 6px;
}

.wp-block-template-part__label-text {
color: $black;
font-size: 10px;
text-transform: uppercase;
padding-left: 4px;
}
}

.is-navigate-mode .is-selected .wp-block-template-part__name-panel {
box-shadow: 0 0 0 $border-width var(--wp-admin-theme-color);

Expand Down Expand Up @@ -111,6 +139,7 @@
&.has-child-selected {
&::after {
box-shadow: 0 0 0 $border-width $gray-600;
border-top-left-radius: 0;
}
}
}

0 comments on commit af2ef8c

Please sign in to comment.