Skip to content

Commit

Permalink
Colorize template parts and Reusable blocks (#45473)
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz authored Nov 23, 2022
1 parent 7e76592 commit d98e24e
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 31 deletions.
7 changes: 7 additions & 0 deletions packages/base-styles/_colors.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import "./functions";

/**
* Colors
*/
Expand All @@ -24,3 +26,8 @@ $light-gray-placeholder: rgba($white, 0.65);
$alert-yellow: #f0b849;
$alert-red: #cc1818;
$alert-green: #4ab866;

:root {
--wp-block-synced-color: #7a00df;
--wp-block-synced-color--rgb: #{hex-to-rgb(#7a00df)};
}
12 changes: 11 additions & 1 deletion packages/block-editor/src/components/block-card/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand All @@ -17,6 +22,7 @@ function BlockCard( {
blockType,
parentBlockClientId,
handleBackButton,
isSynced,
} ) {
if ( blockType ) {
deprecated( '`blockType` property in `BlockCard component`', {
Expand All @@ -30,7 +36,11 @@ function BlockCard( {
window?.__experimentalEnableOffCanvasNavigationEditor === true;

return (
<div className="block-editor-block-card">
<div
className={ classnames( 'block-editor-block-card', {
'is-synced': isSynced,
} ) }
>
{ isOffCanvasNavigationEditorEnabled && parentBlockClientId && (
<Button
onClick={ handleBackButton }
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/block-card/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
width: $button-size-small;
height: $button-size-small;
}

.block-editor-block-card.is-synced .block-editor-block-icon {
color: var(--wp-block-synced-color);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
}

&:hover:not(.is-dragging-blocks)::before {
background: rgba(var(--wp-admin-theme-color--rgb), 0.3);
background: rgba(var(--wp-admin-theme-color--rgb), 0.04);
box-shadow: 0 0 0 $border-width var(--wp-admin-theme-color) inset;
}

&.is-reusable:hover:not(.is-dragging-blocks)::before,
&.wp-block-template-part:hover:not(.is-dragging-blocks)::before {
background: rgba(var(--wp-block-synced-color--rgb), 0.04);
box-shadow: 0 0 0 $border-width var(--wp-block-synced-color) inset;
}

&.is-selected:not(.is-dragging-blocks)::before {
box-shadow: 0 0 0 $border-width var(--wp-admin-theme-color) inset;
}
Expand Down
17 changes: 12 additions & 5 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import classnames from 'classnames';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRef } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import { getBlockType, hasBlockSupport } from '@wordpress/blocks';
import {
getBlockType,
hasBlockSupport,
isReusableBlock,
isTemplatePart,
} from '@wordpress/blocks';
import { ToolbarGroup } from '@wordpress/components';

/**
Expand Down Expand Up @@ -109,11 +114,13 @@ const BlockToolbar = ( { hideDragHandle } ) => {

const shouldShowVisualToolbar = isValid && isVisual;
const isMultiToolbar = blockClientIds.length > 1;
const isSynced =
isReusableBlock( blockType ) || isTemplatePart( blockType );

const classes = classnames(
'block-editor-block-toolbar',
shouldShowMovers && 'is-showing-movers'
);
const classes = classnames( 'block-editor-block-toolbar', {
'is-showing-movers': shouldShowMovers,
'is-synced': isSynced,
} );

return (
<div className={ classes }>
Expand Down
10 changes: 10 additions & 0 deletions packages/block-editor/src/components/block-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
border-right: $border-width solid $gray-300;
}

&.is-synced .block-editor-block-switcher .components-button .block-editor-block-icon {
color: var(--wp-block-synced-color);
}

&.is-synced .components-toolbar-button.block-editor-block-switcher__no-switcher-icon {
&:disabled .block-editor-block-icon.has-colors {
color: var(--wp-block-synced-color);
}
}

> :last-child,
> :last-child .components-toolbar-group,
> :last-child .components-toolbar {
Expand Down
12 changes: 11 additions & 1 deletion packages/block-editor/src/components/inserter-list-item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { useMemo, useRef, memo } from '@wordpress/element';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
isReusableBlock,
isTemplatePart,
} from '@wordpress/blocks';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { ENTER, isAppleOS } from '@wordpress/keycodes';
Expand Down Expand Up @@ -47,6 +49,8 @@ function InserterListItem( {
];
}, [ item.name, item.initialAttributes, item.initialAttributes ] );

const isSynced = isReusableBlock( item ) || isTemplatePart( item );

return (
<InserterDraggableBlocks
isEnabled={ isDraggable && ! item.disabled }
Expand All @@ -55,7 +59,13 @@ function InserterListItem( {
>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<div
className="block-editor-block-types-list__list-item"
className={ classnames(
'block-editor-block-types-list__list-item',

{
'is-synced': isSynced,
}
) }
draggable={ draggable }
onDragStart={ ( event ) => {
isDragging.current = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
width: 33.33%;
padding: 0;
margin: 0;
&.is-synced {
.block-editor-block-icon.has-colors {
color: var(--wp-block-synced-color);
}
}
}

.components-button.block-editor-block-types-list__item {
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function ListViewBlock( {
isExpanded,
selectedClientIds,
preventAnnouncement,
isSyncedBranch,
} ) {
const cellRef = useRef( null );
const [ isHovered, setIsHovered ] = useState( false );
Expand Down Expand Up @@ -205,8 +206,10 @@ function ListViewBlock( {
'is-first-selected': isFirstSelectedBlock,
'is-last-selected': isLastSelectedBlock,
'is-branch-selected': isBranchSelected,
'is-synced-branch': isSyncedBranch,
'is-dragging': isDragged,
'has-single-cell': ! showBlockActions,
'is-synced': blockInformation.isSynced,
} );

// Only include all selected blocks if the currently clicked on block
Expand Down
7 changes: 7 additions & 0 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ListViewBlock from './block';
import { useListViewContext } from './context';
import { isClientIdSelected } from './utils';
import { store as blockEditorStore } from '../../store';
import useBlockDisplayInformation from '../use-block-display-information';

/**
* Given a block, returns the total number of blocks in that subtree. This is used to help determine
Expand Down Expand Up @@ -92,8 +93,12 @@ function ListViewBranch( props ) {
isExpanded,
parentId,
shouldShowInnerBlocks = true,
isSyncedBranch = false,
} = props;

const parentBlockInformation = useBlockDisplayInformation( parentId );
const syncedBranch = isSyncedBranch || !! parentBlockInformation?.isSynced;

const canParentExpand = useSelect(
( select ) => {
if ( ! parentId ) {
Expand Down Expand Up @@ -179,6 +184,7 @@ function ListViewBranch( props ) {
isExpanded={ shouldExpand }
listPosition={ nextPosition }
selectedClientIds={ selectedClientIds }
isSyncedBranch={ syncedBranch }
/>
) }
{ ! showBlock && (
Expand All @@ -199,6 +205,7 @@ function ListViewBranch( props ) {
isBranchSelected={ isSelectedBranch }
selectedClientIds={ selectedClientIds }
isExpanded={ isExpanded }
isSyncedBranch={ syncedBranch }
/>
) }
</AsyncModeProvider>
Expand Down
24 changes: 19 additions & 5 deletions packages/block-editor/src/components/list-view/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
&.is-selected td {
background: var(--wp-admin-theme-color);
}
&.is-selected.is-synced td {
background: var(--wp-block-synced-color);
}
&.is-synced:not(.is-selected) .block-editor-list-view-block-contents .block-editor-block-icon {
color: var(--wp-block-synced-color);
}
&.is-selected .block-editor-list-view-block-contents,
&.is-selected .components-button.has-icon {
color: $white;
Expand All @@ -37,6 +43,13 @@
0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
}
}
&.is-selected.is-synced .block-editor-list-view-block-contents:focus {
&::after {
box-shadow:
inset 0 0 0 1px $white,
0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
}
}
&.is-selected .block-editor-list-view-block__menu:focus {
box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) $white;
}
Expand All @@ -58,11 +71,11 @@
&.is-last-selected td:last-child {
border-bottom-right-radius: $radius-block-ui;
}
&.is-branch-selected:not(.is-selected) {
// Lighten a CSS variable without introducing a new SASS variable
background:
linear-gradient(transparentize($white, 0.1), transparentize($white, 0.1)),
linear-gradient(var(--wp-admin-theme-color), var(--wp-admin-theme-color));
&.is-branch-selected:not(.is-selected):not(.is-synced-branch) {
background: rgba(var(--wp-admin-theme-color--rgb), 0.04);
}
&.is-synced-branch:not(.is-selected) {
background: rgba(var(--wp-block-synced-color--rgb), 0.04);
}
&.is-branch-selected.is-first-selected td:first-child {
border-top-left-radius: $radius-block-ui;
Expand Down Expand Up @@ -142,6 +155,7 @@
}
}
}

// Fix focus styling width when one row has fewer cells.
&.has-single-cell .block-editor-list-view-block-contents:focus::after {
right: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';
import {
store as blocksStore,
isReusableBlock,
isTemplatePart,
} from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -16,10 +20,11 @@ import { store as blockEditorStore } from '../../store';
*
* @typedef {Object} WPBlockDisplayInformation
*
* @property {string} title Human-readable block type label.
* @property {WPIcon} icon Block type icon.
* @property {string} description A detailed block type description.
* @property {string} anchor HTML anchor.
* @property {boolean} isSynced True if is a reusable block or template part
* @property {string} title Human-readable block type label.
* @property {WPIcon} icon Block type icon.
* @property {string} description A detailed block type description.
* @property {string} anchor HTML anchor.
*/

/**
Expand Down Expand Up @@ -50,7 +55,10 @@ export default function useBlockDisplayInformation( clientId ) {
if ( ! blockType ) return null;
const attributes = getBlockAttributes( clientId );
const match = getActiveBlockVariation( blockName, attributes );
const isSynced =
isReusableBlock( blockType ) || isTemplatePart( blockType );
const blockTypeInfo = {
isSynced,
title: blockType.title,
icon: blockType.icon,
description: blockType.description,
Expand All @@ -59,6 +67,7 @@ export default function useBlockDisplayInformation( clientId ) {
if ( ! match ) return blockTypeInfo;

return {
isSynced,
title: match.title || blockType.title,
icon: match.icon || blockType.icon,
description: match.description || blockType.description,
Expand Down
17 changes: 17 additions & 0 deletions packages/block-library/src/block/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@
display: none;
}
}

.edit-post-visual-editor .block-editor-block-list__block:not(.remove-outline).is-reusable {
&.is-highlighted,
&.is-selected {
box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
}

&.block-editor-block-list__block:not([contenteditable]):focus {
&::after {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
// Show a light color for dark themes.
.is-dark-theme & {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) $dark-theme-focus;
}
}
}
}
22 changes: 20 additions & 2 deletions packages/block-library/src/template-part/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// and height are used instead of max-(width/height).
.components-modal__frame {
@include break-small() {
width: calc(100% - #{ $grid-unit-20 * 2 });
height: calc(100% - #{ $header-height * 2 });
width: calc(100% - #{$grid-unit-20 * 2});
height: calc(100% - #{$header-height * 2});
}
@include break-medium() {
width: $break-medium - $grid-unit-20 * 2;
Expand All @@ -24,3 +24,21 @@
padding: $grid-unit-20 0;
z-index: z-index(".block-library-template-part__selection-search");
}

.is-outline-mode .block-editor-block-list__block:not(.remove-outline).wp-block-template-part,
.is-outline-mode .block-editor-block-list__block:not(.remove-outline).is-reusable {
&.is-highlighted,
&.is-selected {
box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
}

&.block-editor-block-list__block:not([contenteditable]):focus {
&::after {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
// Show a light color for dark themes.
.is-dark-theme & {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) $dark-theme-focus;
}
}
}
}
2 changes: 1 addition & 1 deletion packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ export function isReusableBlock( blockOrType ) {
* @return {boolean} Whether the given block is a template part.
*/
export function isTemplatePart( blockOrType ) {
return blockOrType.name === 'core/template-part';
return blockOrType?.name === 'core/template-part';
}

/**
Expand Down
Loading

0 comments on commit d98e24e

Please sign in to comment.