Repository navigation
Icons: Add getAspectRatioIcon and use it in the media editor #86348
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
7 commits
Select commit
Hold shift + click to select a range
63c3b62
Dimensions tool: Share the aspect ratio icon and snap it to the pixel…
00kaku 1a55128
Docs: Add the AspectRatioIcon README to the docs manifest
00kaku 6e28b9f
Add changelog entries for the shared aspect ratio icon
00kaku 2dcd4d1
Merge remote-tracking branch 'upstream/trunk' into feat/aspect-ratio-…
00kaku 40e71dc
Merge remote-tracking branch 'upstream/trunk' into feat/aspect-ratio-…
00kaku ff5e82c
Icons: Add getAspectRatioIcon and use it in the media editor
00kaku 52c4b70
Media editor: Truncate long aspect ratio labels and add icons to the …
00kaku 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
63 changes: 0 additions & 63 deletions
63
packages/block-editor/src/components/dimensions-tool/aspect-ratio-icon.tsx
This file was deleted.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { Rect, SVG } from '@wordpress/primitives'; | ||
| import aspectRatio from '../library/aspect-ratio'; | ||
| import { BOX_SIZE, getAspectRatioShape } from './utils'; | ||
|
|
||
| const CORNER_RADIUS = 1.25; | ||
|
|
||
| /** | ||
| * Returns an icon that draws the shape of an aspect ratio, so that a ratio can | ||
| * be recognized without reading its label. Pass the result to `Icon`, like any | ||
| * other icon. | ||
| * | ||
| * A value that is not a positive number, such as `0`, gets the generic | ||
| * `aspectRatio` icon. Ratios are clamped between 1:3 and 3:1, so that a very | ||
| * wide or very tall ratio is still visible. | ||
| * | ||
| * ```jsx | ||
| * <Icon icon={ getAspectRatioIcon( 16 / 9 ) } /> | ||
| * ``` | ||
| * | ||
| * @param ratio The aspect ratio, as width divided by height, e.g. `16 / 9`. | ||
| */ | ||
| export default function getAspectRatioIcon( ratio?: number | null ) { | ||
| const shape = getAspectRatioShape( ratio ); | ||
|
|
||
| if ( ! shape ) { | ||
| return aspectRatio; | ||
| } | ||
|
|
||
| return ( | ||
| <SVG | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| viewBox={ `0 0 ${ BOX_SIZE } ${ BOX_SIZE }` } | ||
| // Set inline, as for the other stroke icons: a `fill` rule from a | ||
| // parent, such as `.components-button svg`, overrides the attribute. | ||
| style={ { fill: 'none' } } | ||
| stroke="currentColor" | ||
| strokeWidth="1.5" | ||
| > | ||
| <Rect { ...shape } rx={ CORNER_RADIUS } /> | ||
| </SVG> | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import getAspectRatioIcon from '..'; | ||
| import aspectRatio from '../../library/aspect-ratio'; | ||
| import { getAspectRatioShape } from '../utils'; | ||
|
|
||
| describe( 'getAspectRatioShape', () => { | ||
| it.each( [ undefined, null, 0, -1, NaN, Infinity ] )( | ||
| 'returns null for %s, which gets the generic icon', | ||
| ( ratio ) => { | ||
| expect( getAspectRatioShape( ratio ) ).toBeNull(); | ||
| } | ||
| ); | ||
|
|
||
| it( 'returns a square for a square ratio', () => { | ||
| expect( getAspectRatioShape( 1 ) ).toEqual( { | ||
| x: 4.25, | ||
| y: 4.25, | ||
| width: 15.5, | ||
| height: 15.5, | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'returns a wide shape for a landscape ratio', () => { | ||
| expect( getAspectRatioShape( 16 / 9 ) ).toEqual( { | ||
| x: 4.25, | ||
| y: 7.75, | ||
| width: 15.5, | ||
| height: 8.5, | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'returns a tall shape for a portrait ratio', () => { | ||
| expect( getAspectRatioShape( 9 / 16 ) ).toEqual( { | ||
| x: 7.75, | ||
| y: 4.25, | ||
| width: 8.5, | ||
| height: 15.5, | ||
| } ); | ||
| } ); | ||
|
|
||
| it.each( [ 1 / 10, 10 ] )( 'clamps the extreme ratio %s', ( ratio ) => { | ||
| const { width, height } = getAspectRatioShape( ratio )!; | ||
|
|
||
| expect( | ||
| Math.max( width, height ) / Math.min( width, height ) | ||
| ).toBeCloseTo( 15.5 / 5.5 ); | ||
| } ); | ||
|
|
||
| it.each( [ 1, 4 / 3, 3 / 2, 16 / 9, 21 / 9, 3 / 4, 2 / 3, 9 / 16, 1 / 3 ] )( | ||
| 'keeps the outline of %s on the same grid as the generic icon', | ||
| ( ratio ) => { | ||
| const { x, y } = getAspectRatioShape( ratio )!; | ||
|
|
||
| // The generic icon is drawn at 4.25, 6.25 and so on: a quarter | ||
| // past a half pixel. | ||
| expect( ( x - 0.25 ) % 0.5 ).toBe( 0 ); | ||
| expect( ( y - 0.25 ) % 0.5 ).toBe( 0 ); | ||
| } | ||
| ); | ||
| } ); | ||
|
|
||
| describe( 'getAspectRatioIcon', () => { | ||
| it.each( [ undefined, null, 0, -1, NaN ] )( | ||
| 'returns the generic aspect ratio icon for %s', | ||
| ( ratio ) => { | ||
| expect( getAspectRatioIcon( ratio ) ).toBe( aspectRatio ); | ||
| } | ||
| ); | ||
|
|
||
| it( 'returns a different icon when there is a ratio to draw', () => { | ||
| expect( getAspectRatioIcon( 16 / 9 ) ).not.toBe( aspectRatio ); | ||
| } ); | ||
|
|
||
| it( 'keeps the icon unfilled even when a parent sets a fill', () => { | ||
| const icon = getAspectRatioIcon( 16 / 9 ); | ||
|
|
||
| expect( icon.props.style ).toEqual( { fill: 'none' } ); | ||
| } ); | ||
| } ); |
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| export type AspectRatioShape = { | ||
| x: number; | ||
| y: number; | ||
| width: number; | ||
| height: number; | ||
| }; | ||
|
|
||
| // The icon is drawn inside a 24px box, leaving a 4px margin on every side. | ||
| export const BOX_SIZE = 24; | ||
| const MAX_SIDE = 16; | ||
| // Keeps very wide or very tall ratios from collapsing into a line. | ||
| const MIN_RATIO = 1 / 3; | ||
| const MAX_RATIO = 3; | ||
|
|
||
| // The 1.5px line is centred on the rectangle's edges. A size ending in .5 puts | ||
| // those edges on .25 or .75, as in the generic icon, so the line covers whole | ||
| // pixels on a 2x display instead of being blurred across them. | ||
| function snapToPixels( size: number ) { | ||
| return Math.ceil( size ) - 0.5; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the position and size, in the coordinates of the icon's 24px box, of | ||
| * the rounded rectangle that represents an aspect ratio, or `null` when the | ||
| * value is not a ratio that can be drawn. | ||
| * | ||
| * @param ratio The aspect ratio, as width divided by height. | ||
| */ | ||
| export function getAspectRatioShape( | ||
| ratio?: number | null | ||
| ): AspectRatioShape | null { | ||
| if ( ! ratio || ! Number.isFinite( ratio ) || ratio <= 0 ) { | ||
| return null; | ||
| } | ||
|
|
||
| const clampedRatio = Math.min( MAX_RATIO, Math.max( MIN_RATIO, ratio ) ); | ||
| const width = snapToPixels( MAX_SIDE * Math.min( 1, clampedRatio ) ); | ||
| const height = snapToPixels( MAX_SIDE / Math.max( 1, clampedRatio ) ); | ||
|
|
||
| return { | ||
| x: ( BOX_SIZE - width ) / 2, | ||
| y: ( BOX_SIZE - height ) / 2, | ||
| width, | ||
| height, | ||
| }; | ||
| } |
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
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
21 changes: 21 additions & 0 deletions
21
packages/media-editor/src/components/media-editor-crop-panel/style.scss
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| @use "@wordpress/base-styles/variables" as *; | ||
|
|
||
| .media-editor-crop-panel__aspect-ratio-option { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: $grid-unit-10; | ||
| // Allow the label to shrink so that it can be truncated. | ||
| min-width: 0; | ||
|
|
||
| svg { | ||
| flex-shrink: 0; | ||
| } | ||
| } | ||
|
|
||
| // The trigger truncates its value with an ellipsis, which doesn't apply | ||
| // through the flex wrapper, so truncate the label itself. | ||
| .media-editor-crop-panel__aspect-ratio-label { | ||
| overflow: hidden; | ||
| text-overflow: ellipsis; | ||
| white-space: nowrap; | ||
| } | ||
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.
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.
When the media editor is opened as a modal from the editor, it receives the aspect ratios defined in
theme.json, so long labels can be passed in. So it might be better to add styles that account for truncation, as in the block editor's DimensionsTool implementation.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.
Added the same truncation styles as the DimensionsTool. I checked in the media editor that a long label ends with an ellipsis in the trigger, and the icon keeps its size.