-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flash editable block outlines instead of always showing them (#58159)
* Flash editable block outlines instead of always showing them 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. * Don't show outlines when site editor is in view mode * Prevent outlines from flashing when selecting an editable inner block * Fix React warning * Avoid multiple calls to getBlockEditingMode() * Flash editable outlines on template patterns with templateLock = 'contentOnly' * Reduce fade out delay
- Loading branch information
1 parent
f2d2a81
commit 24a2cb7
Showing
7 changed files
with
129 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/block-editor/src/components/use-flash-editable-blocks/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 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 function useFlashEditableBlocks( { | ||
clientId = '', | ||
isEnabled = true, | ||
} = {} ) { | ||
const { getEnabledClientIdsTree } = unlock( useSelect( blockEditorStore ) ); | ||
|
||
return useRefEffect( | ||
( element ) => { | ||
if ( ! isEnabled ) { | ||
return; | ||
} | ||
|
||
const flashEditableBlocks = () => { | ||
getEnabledClientIdsTree( clientId ).forEach( | ||
( { clientId: id } ) => { | ||
const block = element.querySelector( | ||
`[data-block="${ id }"]` | ||
); | ||
if ( ! block ) { | ||
return; | ||
} | ||
block.classList.remove( 'has-editable-outline' ); | ||
// Force reflow to trigger the animation. | ||
// eslint-disable-next-line no-unused-expressions | ||
block.offsetWidth; | ||
block.classList.add( 'has-editable-outline' ); | ||
} | ||
); | ||
}; | ||
|
||
const handleClick = ( event ) => { | ||
const shouldFlash = | ||
event.target === element || | ||
event.target.classList.contains( 'is-root-container' ); | ||
if ( ! shouldFlash ) { | ||
return; | ||
} | ||
if ( event.defaultPrevented ) { | ||
return; | ||
} | ||
event.preventDefault(); | ||
flashEditableBlocks(); | ||
}; | ||
|
||
element.addEventListener( 'click', handleClick ); | ||
return () => element.removeEventListener( 'click', handleClick ); | ||
}, | ||
[ isEnabled ] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters