Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- Icon Block: Insert with a default icon instead of an empty placeholder ([#79111](https://github.com/WordPress/gutenberg/pull/79111)).

## 9.48.0 (2026-06-10)

### Code Quality
Expand Down
31 changes: 28 additions & 3 deletions packages/block-library/src/icon/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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' } );

Copy link
Copy Markdown
Member

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/info as 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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor

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 __unstableMarkNextChangeAsNotPersistent achieves this in practice, I wouldn't block it. But I do agree with @Mamaduka on the principles (avoiding fragile side effects, teaching bad patterns).

Copy link
Copy Markdown
Contributor Author

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!

}
}, [
icon,
wasJustInserted,
setAttributes,
__unstableMarkNextChangeAsNotPersistent,
] );
Comment on lines +106 to +117

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 wasBlockJustInserted.

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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/icon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const settings = {
icon,
example: {
attributes: {
icon: 'core/audio',
icon: 'core/info',
style: {
dimensions: {
width: '48px',
Expand Down
Loading