Skip to content

Dimensions tool: Show an icon for each aspect ratio option - #85812

Merged
mirka merged 4 commits into
WordPress:trunkfrom
00kaku:fix/81626-aspect-ratio-icons
Oct 29, 2026
Merged

mirka merged 4 commits into
WordPress:trunkfrom
00kaku:fix/81626-aspect-ratio-icons

Conversation

@00kaku

@00kaku 00kaku commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Adds a small icon drawn in the shape of each ratio to the options in the aspect ratio dropdown, and to the selected value in the trigger. Original and custom ratios use the generic aspect ratio icon.

What?

Closes #81626

Adds an icon to each option in the Aspect ratio dropdown, and to the selected value shown in the closed dropdown.

Why?

The options are text only, so it's hard to tell at a glance what shape "4:3" or "9:16" gives you. An icon drawn in the shape of each ratio makes the list easier to scan. The issue asked for this, and the maintainer pointed to the new SelectControl item and trigger customisation as the way to do it.

How?

  • Adds an AspectRatioIcon component in dimensions-tool. It draws a rounded rectangle in the proportions of the ratio, within a 24×24 viewBox. Extreme ratios are clamped so the icon never collapses to a line.
  • "Original" and custom ratios, which can't be parsed into a shape, use the generic aspect ratio icon.
  • AspectRatioTool renders the icon and label in each SelectControl.Item, and in SelectControl.Trigger for the selected value. Hidden options are filtered out before the items are passed in.
  • The icons are aria-hidden, so the accessible name of each option is unchanged.

Because the Styles panel in global styles uses the same tool, it gets the icons too.

Testing Instructions

  1. Insert an Image block and select it.
  2. Open the Aspect ratio dropdown in the block's settings sidebar.
  3. Confirm each option shows an icon in the shape of its ratio, and "Original" shows the generic icon.
  4. Choose "Wide - 16:9". Confirm the closed dropdown shows the 16:9 icon and label, and the image takes that ratio.
  5. Open the dropdown with the keyboard (Enter), move with ArrowDown and select with Enter. Confirm it works as before.
  6. Optionally check Cover, Site Logo or Featured Image, and the Styles sidebar in the Site Editor.

Unit tests: npm run test:unit -- packages/block-editor/src/components/dimensions-tool packages/block-editor/src/components/global-styles/test/dimensions-panel.js

Keyboard

Keyboard behaviour is unchanged: Enter opens the list, arrow keys move and Enter selects.

Screenshots or screencast

Before After
image image

Closed dropdown after choosing 16:9: image

Use of AI Tools

I used Claude Code (Anthropic) to help investigate the issue, locate the cause in the code, and draft the fix. I reviewed the changes, ran the tests and verified the behavior in the browser myself, and I take responsibility for them.

Adds a small icon drawn in the shape of each ratio to the options in the aspect ratio dropdown, and to the selected value in the trigger. Original and custom ratios use the generic aspect ratio icon.
@00kaku
00kaku requested a review from ellatrix as a code owner October 28, 2026 05:28
@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: 00kaku <akashsrma@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: beafialho <beafialho@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@jasmussen
jasmussen requested a review from a team October 28, 2026 09:17
@jasmussen

Copy link
Copy Markdown
Contributor

This is interesting, thanks for contributing! I've pinged a group to see whether this can be landed.

@t-hamano t-hamano added [Type] Enhancement A suggestion for improvement. [Feature] Layout Layout block support, its UI controls, and style output. labels Oct 28, 2026

@t-hamano t-hamano left a comment

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 PR! It's hard to picture the actual shape just from the ratio numbers, so this is a really valuable improvement for me.

If we move this PR forward, we might also want to improve the aspect ratio dropdown in the media editor modal, so the two stay consistent:

Image

However, @wordpress/media-editor doesn't depend on @wordpress/block-editor, so sharing the UI between them would need some extra work, e.g. moving the component to a package both can use. Does anyone have ideas on the best approach?

Comment on lines +104 to +113
trigger={
<SelectControl.Trigger>
{ ( item ) => (
<span className="block-editor-dimensions-tool__aspect-ratio-option">
<AspectRatioIcon ratio={ item.value ?? '' } />
{ item.label }
</span>
) }
</SelectControl.Trigger>
}

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.

Because the trigger's child is now a flex span instead of the plain label, the ellipsis no longer applies, and long labels get cut off without "…".

Image

I think there are two options:

  1. If the icon isn't needed in the trigger: simply remove this custom trigger.
  2. If the icon is needed in the trigger: we'd need some custom CSS to restore the ellipsis.

Personally, I think having the icon in the trigger is helpful too, but I'd like to hear feedback from others.

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.

I also prefer the icon in the trigger approach as it's more intuitive. Should I push a fix for it?

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.

Yes, I agree the icon is useful enough to be worth some custom CSS 👍

.block-editor-dimensions-tool__aspect-ratio-option {
display: flex;
align-items: center;
gap: 8px;

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.

Suggested change
gap: 8px;
gap: $grid-unit-10;

@mirka

mirka commented Oct 28, 2026

Copy link
Copy Markdown
Member

Does anyone have ideas on the best approach?

We could maybe start with a private component in @wordpress/components, extracting the most minimal part we can? Maybe even just AspectRatioIcon, if that's reasonable.

@mirka mirka left a comment

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.

Since there's already a story for the AspectRatioTool, could we add a story to show case this few feature? Right now I can only see "Original".

@00kaku

00kaku commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

I've tested all of this locally (the ellipsis, custom ratio icons, gap token and the new stories) and will push the changes once we decide on the icon in the trigger. @t-hamano I'd keep it as it's more intuitive, but I'd also like to hear from others.

For the media editor dropdown, should we keep that in this PR or do it separately? My suggestion is a follow-up PR that extracts a minimal private AspectRatioIcon into @wordpress/components, as @mirka suggested, so this PR stays focused on the block editor.

@mirka

mirka commented Oct 29, 2026

Copy link
Copy Markdown
Member

For the media editor dropdown, should we keep that in this PR or do it separately?

No strong opinion, feel free to punt the refactor to a follow up.

Truncate the label in the trigger so that long names end with an ellipsis, draw custom ratios in the trigger with their own shape, use the spacing token for the gap, and add stories for options, a custom ratio and a long label.
@00kaku

00kaku commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @mirka! I've pushed the review changes: the trigger label truncates with an ellipsis again (icon kept), a custom ratio draws its real shape in the trigger, the gap uses $grid-unit-10, and there are new WithOptions, CustomRatio and LongLabel stories. Tested locally in the editor and in Storybook.

Closed control, top to bottom: a long theme preset name, a custom 21/9, a custom 123/456, and the "Wide - 16:9" preset:

image

I'll do the media editor dropdown in a follow-up PR, starting with a minimal private AspectRatioIcon in @wordpress/components.

…atio-icons

# Conflicts:
#	packages/block-editor/src/components/dimensions-tool/aspect-ratio-tool.tsx

@mirka mirka left a comment

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.

Can we add an Enhancements entry to packages/block-editor/CHANGELOG.md? The icons show up wherever the package renders the aspect ratio control, and earlier DimensionsTool changes like #80747 have entries. Something like:

- DimensionsTool: Show an icon in the shape of each ratio next to the aspect ratio options and the selected value ([#85812](https://github.com/WordPress/gutenberg/pull/85812)).

Other than that, I think we're good to go 👍

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.

Looking at the Storybook (thanks for that!), it makes me want to add pixel snapping so the custom shapes aren't blurry and match the "original" icon better:

Before After
Aspect ratio dropdown before the change, with soft, blurred outlines on the ratio icons Aspect ratio dropdown after the change, with crisp ratio icon outlines that match the Original icon

Let's punt this to follow up though, so this PR doesn't drag on unnecessarily.

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.

Thanks @mirka! Added the changelog entry. Pixel snapping for the custom shapes sounds good as a follow-up, I'll include it with the shared AspectRatioIcon work.

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.

Created a followup ticket #86297 and PR here - #86344, Only for snapping thing. Other improvement from Aki is still needed it's followup and work.

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.

@hbhalodia thanks for picking up #86297 and opening #86344! Both PRs fix the same blur: #86348 includes the same pixel snapping as part of moving the icon into @wordpress/components, which @mirka suggested in the #85812 review. I compared the two approaches, and they draw identical shapes for every preset ratio and other common ones like 21:9, with every edge on the .25/.75 grid.

Since #86348 deletes the file #86344 edits, I'd suggest landing #86348 first. Once it's in, #86344 would only need to be closed, and I'm happy to link #86297 from #86348 so the issue closes with it.

@mirka does that order work for you? If you'd rather land the smaller #86344 first, I'll rebase #86348 on top of it.

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'm reviewing @hbhalodia's PR right now and it looks like the pixel snapping itself warrants its own review. Let's land that first and do the refactor separately 👍

@mirka
mirka enabled auto-merge (squash) October 29, 2026 10:41
@mirka
mirka merged commit ea7d975 into WordPress:trunk Oct 29, 2026
87 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.4 milestone Oct 29, 2026
@00kaku

00kaku commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@t-hamano @mirka, #86348 is ready for review. It's now only the move of the icon into @wordpress/components, rebased on #86344 as agreed, and the shapes are identical to what's on trunk.

I'd also like your thoughts on the open questions in the description:

  1. Media editor. @wordpress/media-editor is a bundled package and can't use private APIs, so it can't use this component as it is. Should the icon become a public export, move to another package the media editor already uses, or stay a small separate copy there?
  2. API. The component takes the ratio as a number. Is that the right input, or should it accept both numbers and ratio strings?
  3. Story placement. Does Components/@wordpress-components/Icons make sense for a private component?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Layout Layout block support, its UI controls, and style output. [Package] Block editor /packages/block-editor [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aspect Ratio: Consider adding icon

5 participants