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 @@ -64,6 +64,7 @@
- Convert the color utilities, `withColors`, the gradient utilities, `ColorPalette`, `PanelColorSettings`, `BlockColorsStyleSelector` and `ContrastChecker` to TypeScript ([#84875](https://github.com/WordPress/gutenberg/pull/84875)).
- Convert the block visibility, block lock and block bindings components to TypeScript ([#84876](https://github.com/WordPress/gutenberg/pull/84876)).
- Convert `DimensionsTool`, `ResolutionTool` and `ImageSizeControl` to TypeScript ([#84877](https://github.com/WordPress/gutenberg/pull/84877)).
- Convert `PresetInputControl` to TypeScript ([#84878](https://github.com/WordPress/gutenberg/pull/84878)).

## 18.1.0 (2026-10-07)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { CustomValueSettings } from './types';

export const ICON_SIZE = 24;
export const RANGE_CONTROL_MAX_SIZE = 8;
export const CUSTOM_VALUE_SETTINGS = {
export const CUSTOM_VALUE_SETTINGS: CustomValueSettings = {
px: { max: 300, steps: 1 },
'%': { max: 100, steps: 1 },
vw: { max: 100, steps: 1 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@ import {
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { Tooltip } from '@wordpress/ui';
import type { CustomValueControlsProps } from './types';

/**
* CustomValueControls component for handling custom value input.
*
* Renders a UnitControl and RangeControl for custom value input mode.
* Handles conditional tooltip wrapping and drag event coordination.
*
* @param {Object} props
* @param {boolean} props.allowNegativeOnDrag Whether to allow negative values during drag operations.
* @param {string} props.ariaLabel Accessible label for the controls.
* @param {string} props.allPlaceholder Placeholder text (e.g., "Mixed").
* @param {number} props.minValue Minimum allowed value.
* @param {number} props.parsedQuantity The numeric part of the current value.
* @param {string} props.computedUnit The unit part of the current value.
* @param {Array} props.units Array of available unit objects.
* @param {boolean} props.isMixed Whether the current value is mixed.
* @param {number} props.step Step value for the range control.
* @param {number} props.max Maximum value for the range control.
* @param {boolean} props.showTooltip Whether to wrap UnitControl in a tooltip.
* @param {string} props.value Current value for drag event checks.
* @param {number} props.minimumCustomValue Minimum custom value for drag end reset.
* @param {Function} props.onCustomValueChange Callback when UnitControl value changes.
* @param {Function} props.onCustomValueSliderChange Callback when RangeControl value changes.
* @param {Function} props.onUnitChange Callback when unit changes.
* @param {Function} props.onMouseOut Mouse out event handler.
* @param {Function} props.onMouseOver Mouse over event handler.
* @param {Function} props.setMinValue Function to set minimum value state.
* @param props
* @param props.allowNegativeOnDrag Whether to allow negative values during drag operations.
* @param props.ariaLabel Accessible label for the controls.
* @param props.allPlaceholder Placeholder text (e.g., "Mixed").
* @param props.minValue Minimum allowed value.
* @param props.parsedQuantity The numeric part of the current value.
* @param props.computedUnit The unit part of the current value.
* @param props.units Array of available unit objects.
* @param props.isMixed Whether the current value is mixed.
* @param props.step Step value for the range control.
* @param props.max Maximum value for the range control.
* @param props.showTooltip Whether to wrap UnitControl in a tooltip.
* @param props.value Current value for drag event checks.
* @param props.minimumCustomValue Minimum custom value for drag end reset.
* @param props.onCustomValueChange Callback when UnitControl value changes.
* @param props.onCustomValueSliderChange Callback when RangeControl value changes.
* @param props.onUnitChange Callback when unit changes.
* @param props.onMouseOut Mouse out event handler.
* @param props.onMouseOver Mouse over event handler.
* @param props.setMinValue Function to set minimum value state.
*
* @return {Element} The CustomValueControls component.
* @return The CustomValueControls component.
*/
export default function CustomValueControls( {
allowNegativeOnDrag,
Expand All @@ -53,7 +54,7 @@ export default function CustomValueControls( {
onMouseOut,
onMouseOver,
setMinValue,
} ) {
}: CustomValueControlsProps ) {
const unitControl = (
<UnitControl
className="preset-input-control__unit-control"
Expand All @@ -69,7 +70,8 @@ export default function CustomValueControls( {
onMouseOver={ onMouseOver }
units={ units }
value={ [ parsedQuantity, computedUnit ].join( '' ) }
placeholder={ allPlaceholder }
// A `null` placeholder renders none.
placeholder={ allPlaceholder as string | undefined }
onDragStart={ () => {
if ( allowNegativeOnDrag && value?.charAt( 0 ) === '-' ) {
setMinValue( 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isValuePreset,
} from './utils';
import CustomValueControls from './custom-value-controls';
import type { Preset, PresetInputControlProps } from './types';

/**
* PresetInputControl component for selecting preset values or entering custom values.
Expand All @@ -30,29 +31,29 @@ import CustomValueControls from './custom-value-controls';
* Allows toggling to custom value mode with a UnitControl and RangeControl.
* Handles unit tracking and conversion between preset and custom values.
*
* @param {Object} props Component props.
* @param {boolean} props.allowNegativeOnDrag Whether to allow negative values during drag operations.
* @param {string} props.ariaLabel Accessible label for the control.
* @param {string} props.className Optional CSS class name.
* @param {Object} props.customValueSettings Optional custom value settings for max/steps per unit.
* @param {boolean} props.disableCustomValues Whether to disable custom value input.
* @param {Object} props.icon Icon to display alongside the control.
* @param {boolean} props.isMixed Whether the current value is mixed (multiple values).
* @param {number} props.minimumCustomValue Minimum allowed custom value.
* @param {Function} props.onChange Callback when value changes.
* @param {Function} props.onMouseOut Callback for mouse out events.
* @param {Function} props.onMouseOver Callback for mouse over events.
* @param {Function} props.onUnitChange Callback when unit changes.
* @param {string} props.placeholder Placeholder text forwarded to the inner UnitControl when
* rendered in custom-value mode.
* @param {Array} props.presets Array of preset objects with name, slug, and size.
* @param {string} props.presetType Type of preset (e.g., 'spacing', 'border-radius').
* @param {string} props.selectedUnit Currently selected unit (e.g., 'px', 'em').
* @param {boolean} props.showTooltip Whether to show tooltip on custom UnitControl.
* @param {Array} props.units Array of available unit objects (can include max and step).
* @param {string} props.value Current value (preset or custom).
* @param props Component props.
* @param props.allowNegativeOnDrag Whether to allow negative values during drag operations.
* @param props.ariaLabel Accessible label for the control.
* @param props.className Optional CSS class name.
* @param props.customValueSettings Optional custom value settings for max/steps per unit.
* @param props.disableCustomValues Whether to disable custom value input.
* @param props.icon Icon to display alongside the control.
* @param props.isMixed Whether the current value is mixed (multiple values).
* @param props.minimumCustomValue Minimum allowed custom value.
* @param props.onChange Callback when value changes.
* @param props.onMouseOut Callback for mouse out events.
* @param props.onMouseOver Callback for mouse over events.
* @param props.onUnitChange Callback when unit changes.
* @param props.placeholder Placeholder text forwarded to the inner UnitControl when
* rendered in custom-value mode.
* @param props.presets Array of preset objects with name, slug, and size.
* @param props.presetType Type of preset (e.g., 'spacing', 'border-radius').
* @param props.selectedUnit Currently selected unit (e.g., 'px', 'em').
* @param props.showTooltip Whether to show tooltip on custom UnitControl.
* @param props.units Array of available unit objects (can include max and step).
* @param props.value Current value (preset or custom).
*
* @return {Element} The PresetInputControl component.
* @return The PresetInputControl component.
*/
export default function PresetInputControl( {
allowNegativeOnDrag = false,
Expand All @@ -74,7 +75,7 @@ export default function PresetInputControl( {
selectedUnit,
showTooltip,
units,
} ) {
}: PresetInputControlProps ) {
const value = useMemo(
() => getPresetValueFromCustomValue( valueProp, presets, presetType ),
[ valueProp, presets, presetType ]
Expand All @@ -100,7 +101,7 @@ export default function PresetInputControl( {
! isValuePreset( value, presetType )
);

let currentValue = null;
let currentValue: number | string | null | undefined = null;

const previousValue = usePrevious( value );

Expand All @@ -122,7 +123,7 @@ export default function PresetInputControl( {
( ! isValuePreset( value, presetType ) ||
( isValuePreset( value, presetType ) && isMixed ) );

let selectListOptions = presets;
let selectListOptions: Preset[] = presets;
if ( showCustomValueInSelectList ) {
selectListOptions = [
...presets,
Expand Down Expand Up @@ -163,7 +164,7 @@ export default function PresetInputControl( {
const max =
unitConfig?.max ?? customValueSettings[ computedUnit ]?.max ?? 10;

const handleCustomValueChange = ( newValue ) => {
const handleCustomValueChange = ( newValue?: string ) => {
// Treat empty or undefined as an explicit clear and propagate undefined.
if ( newValue === undefined || newValue === '' ) {
onChange( undefined );
Expand All @@ -177,14 +178,19 @@ export default function PresetInputControl( {

onChange( newValue );
};
const handleCustomValueSliderChange = ( next ) => {
const handleCustomValueSliderChange = ( next?: number ) => {
onChange( [ next, computedUnit ].join( '' ) );
};
const customTooltipContent = ( newValue ) =>
value === undefined ? undefined : presets[ newValue ]?.name;
const customTooltipContent = ( newValue?: number | '' | null ) =>
// The tooltip gets the slider position.
value === undefined ? undefined : presets[ newValue as number ]?.name;

const getNewPresetValue = ( next, controlType ) => {
const newValue = parseInt( next, 10 );
const getNewPresetValue = (
next: number | string | undefined,
controlType?: string
) => {
// `parseInt` reads a number as its string form.
const newValue = parseInt( next as string, 10 );

if ( controlType === 'selectList' ) {
if ( newValue === 0 && presets[ 0 ]?.slug === '0' ) {
Expand All @@ -196,7 +202,8 @@ export default function PresetInputControl( {
} else if ( newValue === 0 ) {
return '0';
}
return `var:preset|${ presetType }|${ presets[ next ]?.slug }`;
// The next value is a preset index.
return `var:preset|${ presetType }|${ presets[ next as number ]?.slug }`;
};

return (
Expand Down Expand Up @@ -235,8 +242,10 @@ export default function PresetInputControl( {
) }
{ hasPresets && showRangeControl && ! showCustomValueControl && (
<RangeControl
aria-valuenow={ currentValue }
aria-valuetext={ presets[ currentValue ]?.name }
// In preset mode the current value is a slider position.
aria-valuenow={ currentValue as number | undefined }
// In preset mode the current value is a slider position.
aria-valuetext={ presets[ currentValue as number ]?.name }
className="preset-input-control__preset-range"
hideLabelFromVision
initialPosition={ 0 }
Expand All @@ -261,7 +270,7 @@ export default function PresetInputControl( {
onMouseOver={ onMouseOver }
renderTooltipContent={ customTooltipContent }
step={ 1 }
value={ currentValue }
value={ currentValue as number | undefined }
withInputField={ false }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ describe( 'isValuePreset', () => {

it( 'should return false for undefined or null values', () => {
expect( isValuePreset( undefined, 'spacing' ) ).toBe( false );
// @ts-expect-error Covers a `null` value.
expect( isValuePreset( null, 'spacing' ) ).toBe( false );
} );

it( 'should return false for non-string values', () => {
// @ts-expect-error Covers a value that is not a string.
expect( isValuePreset( 10, 'spacing' ) ).toBe( false );
// @ts-expect-error Covers a value that is not a string.
expect( isValuePreset( {}, 'spacing' ) ).toBe( false );
// @ts-expect-error Covers a value that is not a string.
expect( isValuePreset( [], 'spacing' ) ).toBe( false );
} );

Expand All @@ -53,6 +57,7 @@ describe( 'getPresetSlug', () => {
it( 'should return undefined for empty values', () => {
expect( getPresetSlug( '', 'spacing' ) ).toBe( undefined );
expect( getPresetSlug( undefined, 'spacing' ) ).toBe( undefined );
// @ts-expect-error Covers a `null` value.
expect( getPresetSlug( null, 'spacing' ) ).toBe( undefined );
} );

Expand Down Expand Up @@ -112,6 +117,7 @@ describe( 'getSliderValueFromPreset', () => {
expect( getSliderValueFromPreset( '0', mockPresets, 'spacing' ) ).toBe(
0
);
// @ts-expect-error Covers a numeric value.
expect( getSliderValueFromPreset( 0, mockPresets, 'spacing' ) ).toBe(
0
);
Expand Down Expand Up @@ -317,6 +323,7 @@ describe( 'getPresetValueFromCustomValue', () => {
getPresetValueFromCustomValue( '', mockPresets, 'spacing' )
).toBe( '' );
expect(
// @ts-expect-error Covers a `null` value.
getPresetValueFromCustomValue( null, mockPresets, 'spacing' )
).toBe( null );
} );
Expand Down Expand Up @@ -415,6 +422,7 @@ describe( 'getPresetValueFromCustomValue', () => {
getPresetValueFromCustomValue( '10', mixedPresets, 'spacing' )
).toBe( 'var:preset|spacing|small' );
expect(
// @ts-expect-error Covers a numeric value.
getPresetValueFromCustomValue( 20, mixedPresets, 'spacing' )
).toBe( 'var:preset|spacing|medium' );
} );
Expand Down
Loading
Loading