Repository navigation
Icons block: insert an icon by default #79111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af93e19
Icon block: default to info icon, remove placeholder
jasmussen 10c47df
Icon block: pick a random icon on insert
jasmussen be0d4a3
Icon block: add changelog entry
jasmussen 83a6b79
Add placeholder back, remove randomisation.
jasmussen 5d3c42c
Icon block: use info icon in block example
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -29,10 +29,11 @@ import { | |
| __experimentalUseBorderProps as useBorderProps, | ||
| __experimentalGetSpacingClassesAndStyles as useSpacingProps, | ||
| getDimensionsClassesAndStyles as useDimensionsProps, | ||
| store as blockEditorStore, | ||
| } from '@wordpress/block-editor'; | ||
| import { useState } from '@wordpress/element'; | ||
| import { useEffect, useState } from '@wordpress/element'; | ||
| import { SVG, Rect, Path } from '@wordpress/primitives'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { store as coreDataStore } from '@wordpress/core-data'; | ||
|
|
||
| /** | ||
|
|
@@ -62,7 +63,7 @@ const IconPlaceholder = ( { className, style } ) => ( | |
| </SVG> | ||
| ); | ||
|
|
||
| export function Edit( { attributes, setAttributes } ) { | ||
| export function Edit( { attributes, setAttributes, clientId } ) { | ||
| const { icon, ariaLabel, flipHorizontal, flipVertical, rotation } = | ||
| attributes; | ||
|
|
||
|
|
@@ -91,6 +92,30 @@ export function Edit( { attributes, setAttributes } ) { | |
| [ isInserterOpen, icon ] | ||
| ); | ||
|
|
||
| const wasJustInserted = useSelect( | ||
| ( select ) => | ||
| select( blockEditorStore ).wasBlockJustInserted( clientId ), | ||
| [ clientId ] | ||
| ); | ||
| const { __unstableMarkNextChangeAsNotPersistent } = | ||
| useDispatch( blockEditorStore ); | ||
|
|
||
| // Default newly inserted Icon blocks to the info icon. Blocks saved in a | ||
| // placeholder state (no icon) are left untouched, so loading a post never | ||
| // silently alters existing content. | ||
| useEffect( () => { | ||
| if ( ! icon && wasJustInserted ) { | ||
| // This side-effect should not create an undo level. | ||
| __unstableMarkNextChangeAsNotPersistent(); | ||
| setAttributes( { icon: 'core/info' } ); | ||
| } | ||
| }, [ | ||
| icon, | ||
| wasJustInserted, | ||
| setAttributes, | ||
| __unstableMarkNextChangeAsNotPersistent, | ||
| ] ); | ||
|
Comment on lines
+106
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default icon should only be applied to blocks that are being inserted. We should be able to use I haven't tested it, but it should look something like this. const wasJustInserted = useSelect(
( select ) =>
select( blockEditorStore ).wasBlockJustInserted( clientId ),
[ clientId ]
);
const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
useEffect( () => {
if ( wasJustInserted && ! icon && allIcons?.length ) {
const randomIcon =
allIcons[ Math.floor( Math.random() * allIcons.length ) ];
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { icon: randomIcon.name } );
}
}, [
wasJustInserted,
icon,
allIcons,
setAttributes,
__unstableMarkNextChangeAsNotPersistent,
] ); |
||
|
|
||
| const iconToDisplay = selectedIcon?.content || ''; | ||
|
|
||
| const flipClasses = { | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just declare
core/infoas the default icon value in attributes and add a migration. We should try to avoid similar side effects as much as possible; it teaches a bad pattern to consumers and anyone who reads core code.@mcsf, I think you were working recently to remove similar patterns from blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, let me know if you want me to follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have another idea, which should be simpler and honor placeholder status more, IMO. I'll try to push PoC tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ping. Yeah, when I read "randomly picked" I immediately thought of this kind of attribute setting. :)
So... I'm not bullish on this! What matters most is that nor undo nor dirtiness break due to this attribute. If
__unstableMarkNextChangeAsNotPersistentachieves this in practice, I wouldn't block it. But I do agree with @Mamaduka on the principles (avoiding fragile side effects, teaching bad patterns).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you'll want to follow up in #79212 which I'm grateful to George for making!