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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { __experimentalToolsPanelItem as ToolsPanelItem } from '@wordpress/components';
import { Icon, getAspectRatioIcon } from '@wordpress/icons';
import { SelectControl } from '@wordpress/ui';
import { __, _x } from '@wordpress/i18n';
import { useSettings } from '../use-settings';
import AspectRatioIcon from './aspect-ratio-icon';
import { findAspectRatioOption } from './utils';
import { findAspectRatioOption, parseAspectRatio } from './utils';
import type {
AspectRatioPreset,
AspectRatioToolProps,
Expand Down Expand Up @@ -101,12 +101,14 @@ export default function AspectRatioTool( {
<SelectControl.Trigger>
{ ( item ) => (
<span className="block-editor-dimensions-tool__aspect-ratio-option">
<AspectRatioIcon
ratio={
item.value === 'custom'
? ( value ?? '' )
: ( item.value ?? '' )
}
<Icon
icon={ getAspectRatioIcon(
parseAspectRatio(
item.value === 'custom'
? value
: item.value
)
) }
/>
<span className="block-editor-dimensions-tool__aspect-ratio-label">
{ item.label }
Expand All @@ -125,7 +127,11 @@ export default function AspectRatioTool( {
>
<SelectControl.ItemLabel>
<span className="block-editor-dimensions-tool__aspect-ratio-option">
<AspectRatioIcon ratio={ item.value } />
<Icon
icon={ getAspectRatioIcon(
parseAspectRatio( item.value )
) }
/>
{ item.label }
</span>
</SelectControl.ItemLabel>
Expand Down
4 changes: 4 additions & 0 deletions packages/icons/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Rename the `file` icon to `folder`. The drawing it carried was a folder, so that is what it is now called ([#86526](https://github.com/WordPress/gutenberg/pull/86526)).
- Redraw `file` as a single document, the icon the library was missing. Import `folder` to keep the previous drawing ([#86526](https://github.com/WordPress/gutenberg/pull/86526)).

### New Features

- Add the `getAspectRatioIcon` function, which returns an icon that draws the shape of an aspect ratio, to pass to `Icon` ([#86348](https://github.com/WordPress/gutenberg/pull/86348)).

### Enhancements

- `formatListNumbered`, `formatListNumberedRTL`, `postDate`, `queryPaginationNumbers`, `subscript`, `superscript` and `termCount`: enlarge the numerals and draw them from one construction, so they stay legible at small sizes ([#85450](https://github.com/WordPress/gutenberg/pull/85450)).
Expand Down
42 changes: 42 additions & 0 deletions packages/icons/src/aspect-ratio-icon/index.tsx
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>
);
}
79 changes: 79 additions & 0 deletions packages/icons/src/aspect-ratio-icon/test/index.test.tsx
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' } );
} );
} );
46 changes: 46 additions & 0 deletions packages/icons/src/aspect-ratio-icon/utils.ts
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,
};
}
1 change: 1 addition & 0 deletions packages/icons/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as Icon } from './icon';
export { default as getAspectRatioIcon } from './aspect-ratio-icon';

// The ./library module is generated upon building this package. Make sure you
// run `build`, otherwise tools won't be able to resolve this import.
Expand Down
1 change: 1 addition & 0 deletions packages/media-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- `MediaEditor.MoreMenu` takes `actions` and `item` props, and lists the actions eligible for the item, such as "Permanently delete". It now also shows for media other than images when it has actions ([#86349](https://github.com/WordPress/gutenberg/pull/86349)).
- Show the shape of each aspect ratio in the aspect ratio dropdown of the crop panel and in the aspect ratio menu used on narrow screens ([#86348](https://github.com/WordPress/gutenberg/pull/86348)).

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SelectControl, Stack, VisuallyHidden } from '@wordpress/ui';
import { __ } from '@wordpress/i18n';
import { Icon, getAspectRatioIcon } from '@wordpress/icons';
import type { AspectRatioPreset } from '../../state/aspect-ratio-presets';
import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
import MediaEditorImageControls from '../media-editor-image-controls';
Expand Down Expand Up @@ -66,7 +67,42 @@ export default function MediaEditorCropPanel( {
} }
disabled={ disabled }
items={ items }
/>
trigger={
<SelectControl.Trigger>
{ ( item ) => (
<span className="media-editor-crop-panel__aspect-ratio-option">
<Icon
icon={ getAspectRatioIcon(
Number( item.value )
) }
/>
<span className="media-editor-crop-panel__aspect-ratio-label">
{ item.label }
</span>
</span>
) }
</SelectControl.Trigger>
}
>
{ items.map( ( item ) => (
<SelectControl.Item
key={ item.value }
value={ item }
label={ item.label }
>
<SelectControl.ItemLabel>
<span className="media-editor-crop-panel__aspect-ratio-option">
<Icon
icon={ getAspectRatioIcon(
Number( item.value )
) }
/>
{ item.label }
</span>
</SelectControl.ItemLabel>
</SelectControl.Item>
) ) }
</SelectControl>
</Stack>
);
}
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;
}
}
Comment on lines +3 to +13

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.

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.

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.

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.


// 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { __ } from '@wordpress/i18n';
import {
aspectRatio as aspectRatioIcon,
getAspectRatioIcon,
rotateLeft,
rotateRight,
flipHorizontal,
Expand Down Expand Up @@ -198,6 +199,11 @@ export default function MediaEditorImageControls( {
value={ preset.value.toString() }
closeOnClick
disabled={ disabled }
prefix={
<Menu.PrefixIcon
icon={ getAspectRatioIcon( preset.value ) }
/>
}
>
<Menu.ItemLabel>{ preset.label }</Menu.ItemLabel>
</Menu.RadioItem>
Expand Down
Loading
Loading