Skip to content
Merged
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ Insert an SVG icon.
- **Name:** [core/icon](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-icon/)
- **Category:** [media](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/)
- **Supports:** align (center, left, right), anchor, ariaLabel, color (background, text), dimensions (width), interactivity (clientNavigation), spacing (margin, padding), ~~html~~
- **Attributes:** icon
- **Attributes:** flipHorizontal, flipVertical, icon, rotation

## Image

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/icon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ _Defined via the [`attributes`](https://developer.wordpress.org/block-editor/ref
| Attribute | [Type](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#type-validation) | [Default](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#default-value) | Description |
|-----------|------|---------|-------------|
| `icon` | `string` | — | [Role](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#role): `content` |
| `flipHorizontal` | `boolean` | `false` | — |
| `flipVertical` | `boolean` | `false` | — |
| `rotation` | `number` | `0` | — |

## Supports

Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/icon/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"icon": {
"type": "string",
"role": "content"
},
"flipHorizontal": {
"type": "boolean",
"default": false
},
"flipVertical": {
"type": "boolean",
"default": false
},
"rotation": {
"type": "number",
"default": 0
}
},
"supports": {
Expand Down
76 changes: 65 additions & 11 deletions packages/block-library/src/icon/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import {
flipHorizontal as flipHorizontalIcon,
flipVertical as flipVerticalIcon,
rotateRight,
} from '@wordpress/icons';
import {
BlockControls,
InspectorControls,
Expand Down Expand Up @@ -58,7 +63,8 @@ const IconPlaceholder = ( { className, style } ) => (
);

export function Edit( { attributes, setAttributes } ) {
const { icon, ariaLabel } = attributes;
const { icon, ariaLabel, flipHorizontal, flipVertical, rotation } =
attributes;

const [ isInserterOpen, setInserterOpen ] = useState( false );

Expand Down Expand Up @@ -87,23 +93,67 @@ export function Edit( { attributes, setAttributes } ) {

const iconToDisplay = selectedIcon?.content || '';

const flipClasses = {
'is-flip-horizontal': flipHorizontal,
'is-flip-vertical': flipVertical,
};

const rotationStyle = rotation ? { rotate: `${ rotation }deg` } : {};

const blockControls = (
<>
<BlockControls group={ isContentOnlyMode ? 'inline' : 'other' }>
<ToolbarButton
onClick={ () => {
setInserterOpen( true );
} }
>
{ icon ? __( 'Replace' ) : __( 'Choose icon' ) }
</ToolbarButton>
<ToolbarGroup>
<ToolbarButton
onClick={ () => {
setInserterOpen( true );
} }
>
{ icon ? __( 'Replace' ) : __( 'Choose icon' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
{ icon && (
<BlockControls group="other">
<ToolbarGroup>
<ToolbarButton
icon={ flipHorizontalIcon }
label={ __( 'Flip horizontal' ) }
isPressed={ flipHorizontal }
onClick={ () =>
setAttributes( {
flipHorizontal: ! flipHorizontal,
} )
}
/>
<ToolbarButton
icon={ flipVerticalIcon }
label={ __( 'Flip vertical' ) }
isPressed={ flipVertical }
onClick={ () =>
setAttributes( {
flipVertical: ! flipVertical,
} )
}
/>
<ToolbarButton
icon={ rotateRight }
label={ __( 'Rotate' ) }
onClick={ () =>
setAttributes( {
rotation: ( ( rotation || 0 ) + 90 ) % 360,
} )
}
/>
Comment on lines +139 to +147

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.

Ideally, it should be possible to set any angle, not just the four angles of 0, 90, 180, and 270. For example, here is a UI that would allow this:

Image

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.

I'm actually not sure; I think that might be best handled by a separate tool we can explore for animation in a more generic sense; i.e. I think the argument is valid for any block, not specifically the icon block.

And specifically for the icon block, by default, I would constrain it to the cardinal directions. The thing is, even if they are SVGs, they are designed on a precise pixel grid, which is also why we are being careful about the default size increments suggested. It can scale and rotate, but especially on older 1x resolution monitors, the hinting matters. This hinting is intact in the straight degree rotations, but not on the arbitrary ones.

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 feedback!

Should we remove the Rotate setting itself, or is rotation in 90-degree increments allowed?

I think that might be best handled by a separate tool we can explore for animation in a more generic sense; i.e. I think the argument is valid for any block, not specifically the icon block.

I understand your intention, but I think it's fine to use an ad-hoc approach for now.

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.

or is rotation in 90-degree increments allowed?

I think the 90 degree increments are okay, since the hinting will be intact there. I can't think of a ton of use cases; mostly the arrows, and they already exist in the cardinal directions. But one use case would be vertical text, with a vertical icon.

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.

I think it is necessary allow custom values when adding controls that privilege presets. In this case, and seconding that the approach is applicable to most blocks, I would add it in the Styles tab in the block inspector. But agree with @t-hamano that ad-hoc is the best approach to start.

</ToolbarGroup>
</BlockControls>
) }
{ isContentOnlyMode && icon && (
// Add some extra controls for content attributes when content only mode is active.
// With content only mode active, the inspector is hidden, so users need another way
// to edit these attributes.
<BlockControls group="other">
<ToolbarGroup className="components-toolbar-group">
<ToolbarGroup>
<DropdownMenu
icon=""
popoverProps={ {
Expand Down Expand Up @@ -182,13 +232,15 @@ export function Edit( { attributes, setAttributes } ) {
colorProps.className,
borderProps.className,
spacingProps.className,
dimensionsProps.className
dimensionsProps.className,
flipClasses
),
style: {
...colorProps.style,
...borderProps.style,
...spacingProps.style,
...dimensionsProps.style,
...rotationStyle,
},
} }
/>
Expand All @@ -197,12 +249,14 @@ export function Edit( { attributes, setAttributes } ) {
className={ clsx(
borderProps.className,
spacingProps.className,
dimensionsProps.className
dimensionsProps.className,
flipClasses
) }
style={ {
...borderProps.style,
...spacingProps.style,
...dimensionsProps.style,
...rotationStyle,
height: 'auto',
} }
/>
Expand Down
23 changes: 23 additions & 0 deletions packages/block-library/src/icon/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ function render_block_core_icon( $attributes ) {
$processor->add_class( $styles['classnames'] );
}

// Apply flip classes to the SVG.
$flip_horizontal = $attributes['flipHorizontal'] ?? false;
$flip_vertical = $attributes['flipVertical'] ?? false;

if ( $flip_horizontal ) {
$processor->add_class( 'is-flip-horizontal' );
}
if ( $flip_vertical ) {
$processor->add_class( 'is-flip-vertical' );
}

$rotation = isset( $attributes['rotation'] ) ? (int) $attributes['rotation'] : 0;

if ( $rotation ) {
$current_style = $processor->get_attribute( 'style' ) ?? '';
$rotation_css = 'rotate: ' . $rotation . 'deg;';
if ( $current_style ) {
$processor->set_attribute( 'style', $current_style . ' ' . $rotation_css );
} else {
$processor->set_attribute( 'style', $rotation_css );
}
}

$aria_label = ! empty( $attributes['ariaLabel'] ) ? $attributes['ariaLabel'] : '';

if ( ! $aria_label ) {
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/icon/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
svg {
box-sizing: border-box;
fill: currentColor;

&.is-flip-horizontal {
transform: scaleX(-1);
}

&.is-flip-vertical {
transform: scaleY(-1);
}

&.is-flip-horizontal.is-flip-vertical {
transform: scaleX(-1) scaleY(-1);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion test/integration/fixtures/blocks/core__icon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{
"name": "core/icon",
"isValid": true,
"attributes": {},
"attributes": {
"flipHorizontal": false,
"flipVertical": false,
"rotation": 0
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": "",
"sizeSlug": "large",
"width": "164px",
"height": "164px"
"height": "164px",
"sizeSlug": "large"
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": "",
"sizeSlug": "large",
"width": "200px"
"width": "200px",
"sizeSlug": "large"
},
"innerBlocks": []
}
Expand Down
Loading