Skip to content

Commit

Permalink
Flash editable block outlines instead of always showing them
Browse files Browse the repository at this point in the history
Updates the editable block outlines that appear within content-locked
containers (added in #57901) to appear and then fade out after 3s when:

- The page loads; or
- The user clicks on the content-locked container.

This is done via a private useFlashEditableBlocks() hook attached to the
container.
  • Loading branch information
noisysocks committed Jan 24, 2024
1 parent a80d1c4 commit 378661a
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 43 deletions.
6 changes: 6 additions & 0 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ function BlockListBlockProvider( props ) {
),
isEditingDisabled:
getBlockEditingMode( clientId ) === 'disabled',
hasEditableOutline:
getBlockEditingMode( clientId ) !== 'disabled' &&
getBlockEditingMode( getBlockRootClientId( clientId ) ) ===
'disabled',
className: hasLightBlockWrapper
? attributes.className
: undefined,
Expand Down Expand Up @@ -660,6 +664,7 @@ function BlockListBlockProvider( props ) {
isBlockMovingMode,
canInsertMovingBlock,
isEditingDisabled,
hasEditableOutline,
className,
defaultClassName,
} = selectedProps;
Expand Down Expand Up @@ -695,6 +700,7 @@ function BlockListBlockProvider( props ) {
isBlockMovingMode,
canInsertMovingBlock,
isEditingDisabled,
hasEditableOutline,
isTemporarilyEditingAsBlocks,
defaultClassName,
mayDisplayControls,
Expand Down
51 changes: 23 additions & 28 deletions packages/block-editor/src/components/block-list/content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -300,35 +300,30 @@ _::-webkit-full-page-media, _:future, :root .has-multi-selection .block-editor-b
}
}

// Indicate which blocks are editable within a locked context.
// 1. User must be hovering an editor with renderingMode = 'template-lock'; or...
.is-template-locked:hover,
// ...a container block.
.block-editor-block-list__block:hover {
// 2. Look for locked blocks; or...
.block-editor-block-list__block.is-editing-disabled,
// ...container blocks that have locked children.
&:has(> .block-editor-block-list__block.is-editing-disabled) {
// 3. Highlight any unlocked children of that locked block.
& > .block-editor-block-list__block:not(.is-editing-disabled):not(.is-selected):not(.has-child-selected) {
&::after {
content: "";
border-style: dotted;
position: absolute;
pointer-events: none;
top: $border-width;
left: $border-width;
right: $border-width;
bottom: $border-width;
border: 1px dotted var(--wp-admin-theme-color);
border-radius: $radius-block-ui - $border-width;
}
@keyframes block-editor-has-editable-outline__fade-out-animation {
from {
border-color: rgba(var(--wp-admin-theme-color--rgb), 1);
}
to {
border-color: rgba(var(--wp-admin-theme-color--rgb), 0);
}
}

&.is-hovered::after {
background: rgba(var(--wp-admin-theme-color--rgb), 0.1);
border: none;
}
}
.block-editor-block-list__block.has-editable-outline {
&::after {
content: "";
position: absolute;
pointer-events: none;
top: 0;
left: 0;
right: 0;
bottom: 0;
border: 1px dotted rgba(var(--wp-admin-theme-color--rgb), 1);
border-radius: $radius-block-ui;
animation: block-editor-has-editable-outline__fade-out-animation 0.3s ease-out;
animation-delay: 3s;
animation-fill-mode: forwards;
@include reduce-motion("animation");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
isBlockMovingMode,
canInsertMovingBlock,
isEditingDisabled,
hasEditableOutline,
isTemporarilyEditingAsBlocks,
defaultClassName,
} = useContext( PrivateBlockContext );
Expand Down Expand Up @@ -152,6 +153,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
'is-block-moving-mode': isBlockMovingMode,
'can-insert-moving-block': canInsertMovingBlock,
'is-editing-disabled': isEditingDisabled,
'has-editable-outline': hasEditableOutline,
'is-content-locked-temporarily-editing-as-blocks':
isTemporarilyEditingAsBlocks,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { useRefEffect } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

export default function useFlashEditableBlocks( rootClientId = '' ) {
const { getEnabledClientIdsTree } = unlock( useSelect( blockEditorStore ) );

return useRefEffect( ( element ) => {
const flashEditableBlocks = () => {
getEnabledClientIdsTree( rootClientId ).forEach(
( { clientId } ) => {
const blockElement = element.querySelector(
`[data-block="${ clientId }"]`
);
if ( ! blockElement ) {
return;
}
blockElement.classList.remove( 'has-editable-outline' );
// Force reflow to trigger the animation.
// eslint-disable-next-line no-unused-expressions
blockElement.offsetWidth;
blockElement.classList.add( 'has-editable-outline' );
}
);
};

const handleClick = ( event ) => {
if ( event.defaultPrevented ) {
return;
}
event.preventDefault();
flashEditableBlocks();
};

element.addEventListener( 'click', handleClick );
return () => {
element.removeEventListener( 'click', handleClick );
};
} );
}
2 changes: 2 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { usesContextKey } from './components/rich-text/format-edit';
import { ExperimentalBlockCanvas } from './components/block-canvas';
import { getDuotoneFilter } from './components/duotone/utils';
import useFlashEditableBlocks from './components/use-flash-editable-blocks';

/**
* Private @wordpress/block-editor APIs.
Expand Down Expand Up @@ -52,4 +53,5 @@ lock( privateApis, {
ReusableBlocksRenameHint,
useReusableBlocksRenameHint,
usesContextKey,
useFlashEditableBlocks,
} );
8 changes: 7 additions & 1 deletion packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import { parse, cloneBlock } from '@wordpress/blocks';
*/
import { unlock } from '../lock-unlock';

const { useLayoutClasses } = unlock( blockEditorPrivateApis );
const { useLayoutClasses, useFlashEditableBlocks } = unlock(
blockEditorPrivateApis
);
const { PARTIAL_SYNCING_SUPPORTED_BLOCKS } = unlock( patternsPrivateApis );

function isPartiallySynced( block ) {
Expand Down Expand Up @@ -272,7 +274,10 @@ export default function ReusableBlockEdit( {
);
const layoutClasses = useLayoutClasses( { layout }, name );

const flashEditableBlocksRef = useFlashEditableBlocks( patternClientId );

const blockProps = useBlockProps( {
ref: flashEditableBlocksRef,
className: classnames(
'block-library-block__reusable-block-container',
layout && layoutClasses,
Expand All @@ -281,6 +286,7 @@ export default function ReusableBlockEdit( {
} );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
ref: flashEditableBlocksRef,
templateLock: 'all',
layout,
renderAppender: innerBlocks?.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import { store as editorStore } from '../../store';
* editor iframe canvas.
*/
export default function EditTemplateBlocksNotification( { contentRef } ) {
const renderingMode = useSelect(
( select ) => select( editorStore ).getRenderingMode(),
[]
);
const { getNotices } = useSelect( noticesStore );

const { createInfoNotice, removeNotice } = useDispatch( noticesStore );
Expand All @@ -42,18 +38,17 @@ export default function EditTemplateBlocksNotification( { contentRef } ) {

useEffect( () => {
const handleClick = async ( event ) => {
if ( renderingMode !== 'template-locked' ) {
return;
}
if ( ! event.target.classList.contains( 'is-root-container' ) ) {
return;
}

const isNoticeAlreadyShowing = getNotices().some(
( notice ) => notice.id === lastNoticeId.current
);
if ( isNoticeAlreadyShowing ) {
return;
}

const { notice } = await createInfoNotice(
__( 'Edit your template to edit this block.' ),
{
Expand All @@ -71,9 +66,6 @@ export default function EditTemplateBlocksNotification( { contentRef } ) {
};

const handleDblClick = ( event ) => {
if ( renderingMode !== 'template-locked' ) {
return;
}
if ( ! event.target.classList.contains( 'is-root-container' ) ) {
return;
}
Expand All @@ -90,7 +82,7 @@ export default function EditTemplateBlocksNotification( { contentRef } ) {
canvas?.removeEventListener( 'click', handleClick );
canvas?.removeEventListener( 'dblclick', handleDblClick );
};
}, [ lastNoticeId, renderingMode, contentRef.current ] );
}, [ lastNoticeId, contentRef.current ] );

return (
<ConfirmDialog
Expand Down
10 changes: 7 additions & 3 deletions packages/editor/src/components/editor-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
useLayoutClasses,
useLayoutStyles,
ExperimentalBlockCanvas: BlockCanvas,
useFlashEditableBlocks,
} = unlock( blockEditorPrivateApis );

const noop = () => {};
Expand Down Expand Up @@ -287,9 +288,11 @@ function EditorCanvas( {

const localRef = useRef();
const typewriterRef = useTypewriter();
const flashEditableBlocksRef = useFlashEditableBlocks();
const contentRef = useMergeRefs( [
localRef,
renderingMode === 'post-only' ? typewriterRef : noop,
renderingMode === 'template-locked' ? flashEditableBlocksRef : noop,
] );

return (
Expand Down Expand Up @@ -364,8 +367,7 @@ function EditorCanvas( {
'is-' + deviceType.toLowerCase() + '-preview',
renderingMode !== 'post-only'
? 'wp-site-blocks'
: `${ blockListLayoutClass } wp-block-post-content`, // Ensure root level blocks receive default/flow blockGap styling rules.
renderingMode !== 'all' && 'is-' + renderingMode
: `${ blockListLayoutClass } wp-block-post-content` // Ensure root level blocks receive default/flow blockGap styling rules.
) }
layout={ blockListLayout }
dropZoneElement={
Expand All @@ -377,7 +379,9 @@ function EditorCanvas( {
}
renderAppender={ renderAppender }
/>
<EditTemplateBlocksNotification contentRef={ localRef } />
{ renderingMode === 'template-locked' && (
<EditTemplateBlocksNotification contentRef={ localRef } />
) }
</RecursionProvider>
{ children }
</BlockCanvas>
Expand Down

0 comments on commit 378661a

Please sign in to comment.