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
1 change: 1 addition & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- Convert `BorderRadiusControl` and `DimensionControl` to TypeScript ([#84880](https://github.com/WordPress/gutenberg/pull/84880)).
- Convert the keyboard shortcut components, `ObserveTyping`, `Typewriter`, `BlockSelectionClearer`, `SkipToSelectedBlock` and `MultiSelectScrollIntoView` to TypeScript ([#84902](https://github.com/WordPress/gutenberg/pull/84902)).
- Convert `useOnBlockDrop`, `usePasteStyles`, `useMovingAnimation`, `useFlashEditableBlocks`, `useBlockCommands`, the convert to group buttons, the note icon slots, `useListViewPanelState` and `useResizeCanvas` to TypeScript ([#84906](https://github.com/WordPress/gutenberg/pull/84906)).
- Convert `SpacingSizesControl`, `FitTextSizeWarning`, the line height hook and the spacing visualizer to TypeScript ([#84907](https://github.com/WordPress/gutenberg/pull/84907)).

## 18.1.0 (2026-10-07)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { speak } from '@wordpress/a11y';
* Component that checks if the fit text computed font size is below a minimum
* threshold and displays a warning notice.
*
* @return {Element|null} Warning notice element or null.
* @return Warning notice element or null.
*/
export default function FitTextSizeWarning() {
const message = __(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useSettings } from '../../use-settings';
import { RANGE_CONTROL_MAX_SIZE } from '../utils';
import type { SpacingSize } from '../types';

const EMPTY_ARRAY = [];
const EMPTY_ARRAY: SpacingSize[] = [];

const compare = new Intl.Collator( 'und', { numeric: true } ).compare;

Expand All @@ -20,26 +21,32 @@ export default function useSpacingSizes() {
'spacing.defaultSpacingSizes'
);

const customSizes = customSpacingSizes ?? EMPTY_ARRAY;
// The spacing size settings hold presets.
const customSizes =
( customSpacingSizes as SpacingSize[] | undefined ) ?? EMPTY_ARRAY;

const themeSizes = themeSpacingSizes ?? EMPTY_ARRAY;
const themeSizes =
( themeSpacingSizes as SpacingSize[] | undefined ) ?? EMPTY_ARRAY;

const defaultSizes =
defaultSpacingSizes && defaultSpacingSizesEnabled !== false
? defaultSpacingSizes
? ( defaultSpacingSizes as SpacingSize[] )
: EMPTY_ARRAY;

return useMemo( () => {
const sizes = [
const sizes: SpacingSize[] = [
{ name: __( 'None' ), slug: '0', size: 0 },
...customSizes,
...themeSizes,
...defaultSizes,
];

// Using numeric slugs opts-in to sorting by slug.
if ( sizes.every( ( { slug } ) => /^[0-9]/.test( slug ) ) ) {
sizes.sort( ( a, b ) => compare( a.slug, b.slug ) );
// `test` and `compare` read numeric slugs as strings.
if ( sizes.every( ( { slug } ) => /^[0-9]/.test( slug as string ) ) ) {
sizes.sort( ( a, b ) =>
compare( a.slug as string, b.slug as string )
);
}

return sizes.length > RANGE_CONTROL_MAX_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
VIEWS,
getInitialView,
} from './utils';
import type { Side, SideValues, SpacingSizesControlProps } from './types';

/**
* A flexible control for managing spacing values in the block editor. Supports single, axial,
Expand Down Expand Up @@ -72,7 +73,7 @@ export default function SpacingSizesControl( {
sides = ALL_SIDES,
useSelect,
values,
} ) {
}: SpacingSizesControlProps ) {
const spacingSizes = useSpacingSizes();
const inputValues = values || DEFAULT_VALUES;
const hasOneSide = sides?.length === 1;
Expand All @@ -87,7 +88,7 @@ export default function SpacingSizesControl( {
setView( view === VIEWS.axial ? VIEWS.custom : VIEWS.axial );
};

const handleOnChange = ( nextValue ) => {
const handleOnChange = ( nextValue: SideValues ) => {
const newValues = { ...values, ...nextValue };
onChange( newValues );
};
Expand Down Expand Up @@ -122,7 +123,10 @@ export default function SpacingSizesControl( {
};

const sideLabel =
ALL_SIDES.includes( view ) && showSideInLabel ? LABELS[ view ] : '';
// A single side view is named after its side.
ALL_SIDES.includes( view as Side ) && showSideInLabel
? LABELS[ view as Side ]
: '';

const label = sprintf(
// translators: 1: Type of spacing being modified (Padding, Margin, etc.). 2: The side of the block being modified (Top, Bottom, Left, etc.).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getPresetValueFromCustomValue,
hasAxisSupport,
} from '../utils';
import type { SideValues, SpacingInputControlsProps } from '../types';

const groupedSides = [ 'vertical', 'horizontal' ];

Expand All @@ -17,17 +18,20 @@ export default function AxialInputControls( {
spacingSizes,
type,
values,
} ) {
const createHandleOnChange = ( side ) => ( next ) => {
}: SpacingInputControlsProps ) {
const createHandleOnChange = ( side: string ) => ( next?: string ) => {
if ( ! onChange ) {
return;
}

// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
...Object.keys( values ).reduce<
Record< string, string | undefined >
>( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
// The keys come from the side values.
values[ key as keyof SideValues ],
spacingSizes
);
return acc;
Expand Down Expand Up @@ -60,8 +64,9 @@ export default function AxialInputControls( {
return (
<SpacingInputControl
key={ `spacing-sizes-control-${ side }` }
icon={ ICONS[ side ] }
label={ LABELS[ side ] }
// The grouped sides are axes.
icon={ ICONS[ side as keyof typeof ICONS ] }
label={ LABELS[ side as keyof typeof LABELS ] }
minimumCustomValue={ minimumCustomValue }
onChange={ createHandleOnChange( side ) }
onMouseOut={ onMouseOut }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ICONS,
getPresetValueFromCustomValue,
} from '../utils';
import type { Side, SideValues, SpacingInputControlsProps } from '../types';

export default function SeparatedInputControls( {
minimumCustomValue,
Expand All @@ -15,18 +16,21 @@ export default function SeparatedInputControls( {
spacingSizes,
type,
values,
} ) {
}: SpacingInputControlsProps ) {
// Filter sides if custom configuration provided, maintaining default order.
const filteredSides = sides?.length
? ALL_SIDES.filter( ( side ) => sides.includes( side ) )
: ALL_SIDES;

const createHandleOnChange = ( side ) => ( next ) => {
const createHandleOnChange = ( side: Side ) => ( next?: string ) => {
// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
...Object.keys( values ).reduce<
Record< string, string | undefined >
>( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
// The keys come from the side values.
values[ key as keyof SideValues ],
spacingSizes
);
return acc;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import SpacingInputControl from './spacing-input-control';
import { LABELS, getPresetValueFromCustomValue } from '../utils';
import type { Side, SideValues, SpacingInputControlsProps } from '../types';

export default function SingleInputControl( {
minimumCustomValue,
onChange,
onMouseOut,
onMouseOver,
showSideInLabel,
side,
spacingSizes,
type,
values,
}: SpacingInputControlsProps ) {
const createHandleOnChange =
( currentSide: string ) => ( next?: string ) => {
// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce<
Record< string, string | undefined >
>( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
// The keys come from the side values.
values[ key as keyof SideValues ],
spacingSizes
);
return acc;
}, {} ),
};

nextValues[ currentSide ] = next;

onChange( nextValues );
};

return (
<SpacingInputControl
// The single view is a side.
label={ LABELS[ side as Side ] }
minimumCustomValue={ minimumCustomValue }
onChange={ createHandleOnChange( side as Side ) }
onMouseOut={ onMouseOut }
onMouseOver={ onMouseOver }
showSideInLabel={ showSideInLabel }
side={ side as Side }
spacingSizes={ spacingSizes }
type={ type }
value={ values[ side as Side ] }
withInputField={ false }
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PresetInputControl from '../../preset-input-control';
import { useSettings } from '../../use-settings';
import { store as blockEditorStore } from '../../../store';
import { ALL_SIDES, LABELS } from '../utils';
import type { Side, SpacingInputControlProps } from '../types';

const CUSTOM_VALUE_SETTINGS = {
px: { max: 300, steps: 1 },
Expand Down Expand Up @@ -51,15 +52,21 @@ export default function SpacingInputControl( {
type,
value,
...restProps
} ) {
}: SpacingInputControlProps ) {
const disableCustomSpacingSizes = useSelect( ( select ) => {
const editorSettings = select( blockEditorStore ).getSettings();
return editorSettings?.disableCustomSpacingSizes;
// The setting is a boolean flag.
return editorSettings?.disableCustomSpacingSizes as boolean | undefined;
} );

const [ availableUnits ] = useSettings( 'spacing.units' );
const units = useCustomUnits( {
availableUnits: availableUnits || [ 'px', 'em', 'rem' ],
// The setting lists unit names.
availableUnits: ( availableUnits as string[] | undefined ) || [
'px',
'em',
'rem',
],
} );

// Convert spacing preset format to generic preset format for PresetInputControl
Expand All @@ -75,10 +82,11 @@ export default function SpacingInputControl( {

// Generate aria label
const sideLabel =
( ALL_SIDES.includes( side ) ||
( ALL_SIDES.includes( side as Side ) ||
[ 'vertical', 'horizontal' ].includes( side ) ) &&
showSideInLabel
? LABELS[ side ]
? // Only sides and axes have labels.
LABELS[ side as keyof typeof LABELS ]
: '';
const typeLabel = showSideInLabel ? type?.toLowerCase() : type;

Expand All @@ -94,7 +102,8 @@ export default function SpacingInputControl( {

return (
<PresetInputControl
allowNegativeOnDrag={ minimumCustomValue < 0 }
// An unset minimum compares as `NaN`, which is not negative.
allowNegativeOnDrag={ ( minimumCustomValue as number ) < 0 }
ariaLabel={ ariaLabel }
className="spacing-sizes-control"
customValueSettings={ CUSTOM_VALUE_SETTINGS }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Button } from '@wordpress/components';
import { link, linkOff } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import type { LinkedButtonProps } from './types';

export default function LinkedButton( { isLinked, ...props } ) {
export default function LinkedButton( {
isLinked,
...props
}: LinkedButtonProps ) {
const label = isLinked ? __( 'Unlink sides' ) : __( 'Link sides' );

return (
Expand Down
Loading
Loading