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 @@ -66,6 +66,7 @@
- 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)).
- Convert `URLPopover` and `LinkPicker` to TypeScript ([#84879](https://github.com/WordPress/gutenberg/pull/84879)).
- Convert `BorderRadiusControl` and `DimensionControl` to TypeScript ([#84880](https://github.com/WordPress/gutenberg/pull/84880)).

## 18.1.0 (2026-10-07)

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ _Parameters_
- _props_ `Object`:
- _props.label_ `?string`: A label for the control.
- _props.onChange_ `( value: string ) => void`: Called when the dimension value changes.
- _props.value_ `string`: The current dimension value.
- _props.value_ `string|number`: The current dimension value.
- _props.placeholder_ `?string`: Placeholder text for the custom-value input.
- _props.className_ `?string`: Additional CSS class on the wrapping fieldset.
- _props.dimensionSizes_ `?Object`: Optional dimension size presets. Falls back to settings from the store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
cornerTopLeft,
cornerTopRight,
} from '@wordpress/icons';
import type { Preset } from '../preset-input-control/types';

export const DEFAULT_VALUES = {
topLeft: undefined,
Expand All @@ -16,7 +17,7 @@ export const DEFAULT_VALUES = {

export const RANGE_CONTROL_MAX_SIZE = 8;

export const EMPTY_ARRAY = [];
export const EMPTY_ARRAY: Preset[] = [];

export const CORNERS = {
all: __( 'Border radius' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ import {
ICONS,
MIN_BORDER_RADIUS_VALUE,
} from './constants';
import type { Preset } from '../preset-input-control/types';
import type {
BorderRadiusControlProps,
BorderRadiusCorner,
BorderRadiusCornerValues,
BorderRadiusPresets,
BorderRadiusUnits,
BorderRadiusValues,
} from './types';

function useBorderRadiusSizes( presets ) {
function useBorderRadiusSizes( presets?: BorderRadiusPresets ) {
const defaultSizes = presets?.default ?? EMPTY_ARRAY;
const customSizes = presets?.custom ?? EMPTY_ARRAY;
const themeSizes = presets?.theme ?? EMPTY_ARRAY;

return useMemo( () => {
const sizes = [
const sizes: Preset[] = [
{ name: __( 'None' ), slug: '0', size: 0 },
...customSizes,
...themeSizes,
Expand All @@ -48,12 +57,15 @@ function useBorderRadiusSizes( presets ) {
/**
* Gets the value for a specific corner from the values object.
*
* @param {Object|string} values Border radius values.
* @param {string} corner Corner name ('all', 'topLeft', etc.).
* @param values Border radius values.
* @param corner Corner name ('all', 'topLeft', etc.).
*
* @return {string|undefined} The corner value.
* @return The corner value.
*/
function getCornerValue( values, corner ) {
function getCornerValue(
values: BorderRadiusValues | undefined,
corner: BorderRadiusCorner | 'all'
): string | undefined {
if ( corner === 'all' ) {
return getAllValue( values );
}
Expand All @@ -64,18 +76,22 @@ function getCornerValue( values, corner ) {
}

// Handle object values (longhand)
return values?.[ corner ];
// PresetInputControl reads a stored number such as `0` at runtime.
return values?.[ corner ] as string | undefined;
}

/**
* Gets the selected unit for a specific corner.
*
* @param {Object} selectedUnits Units object.
* @param {string} corner Corner name.
* @param selectedUnits Units object.
* @param corner Corner name.
*
* @return {string} The selected unit.
* @return The selected unit.
*/
function getCornerUnit( selectedUnits, corner ) {
function getCornerUnit(
selectedUnits: BorderRadiusUnits,
corner: BorderRadiusCorner | 'all'
): string | undefined {
if ( corner === 'all' ) {
return selectedUnits.flat;
}
Expand All @@ -85,14 +101,18 @@ function getCornerUnit( selectedUnits, corner ) {
/**
* Creates an onChange handler for a specific corner.
*
* @param {string} corner Corner name.
* @param {Object} values Current values.
* @param {Function} onChange Original onChange callback.
* @param corner Corner name.
* @param values Current values.
* @param onChange Original onChange callback.
*
* @return {Function} Corner-specific onChange handler.
* @return Corner-specific onChange handler.
*/
function createCornerChangeHandler( corner, values, onChange ) {
return ( newValue ) => {
function createCornerChangeHandler(
corner: BorderRadiusCorner | 'all',
values: BorderRadiusValues | undefined,
onChange: BorderRadiusControlProps[ 'onChange' ]
) {
return ( newValue?: string ) => {
if ( corner === 'all' ) {
onChange( {
topLeft: newValue,
Expand Down Expand Up @@ -123,18 +143,18 @@ function createCornerChangeHandler( corner, values, onChange ) {
/**
* Creates a unit change handler for a specific corner.
*
* @param {string} corner Corner name.
* @param {Object} selectedUnits Current selected units.
* @param {Function} setSelectedUnits Unit setter function.
* @param corner Corner name.
* @param selectedUnits Current selected units.
* @param setSelectedUnits Unit setter function.
*
* @return {Function} Corner-specific unit change handler.
* @return Corner-specific unit change handler.
*/
function createCornerUnitChangeHandler(
corner,
selectedUnits,
setSelectedUnits
corner: BorderRadiusCorner | 'all',
selectedUnits: BorderRadiusUnits,
setSelectedUnits: ( units: BorderRadiusUnits ) => void
) {
return ( newUnit ) => {
return ( newUnit?: string ) => {
const newUnits = { ...selectedUnits };
if ( corner === 'all' ) {
newUnits.flat = newUnit;
Expand All @@ -152,37 +172,52 @@ function createCornerUnitChangeHandler(
/**
* Control to display border radius options.
*
* @param {Object} props Component props.
* @param {Function} props.onChange Callback to handle onChange.
* @param {Object} props.values Border radius values.
* @param {Object} props.presets Border radius presets.
* @param props Component props.
* @param props.onChange Callback to handle onChange.
* @param props.values Border radius values.
* @param props.presets Border radius presets.
*
* @return {Element} Custom border radius control.
* @return Custom border radius control.
*/
export default function BorderRadiusControl( { onChange, values, presets } ) {
export default function BorderRadiusControl( {
onChange,
values,
presets,
}: BorderRadiusControlProps ) {
const [ isLinked, setIsLinked ] = useState(
! hasDefinedValues( values ) || ! hasMixedValues( values )
);
const options = useBorderRadiusSizes( presets );
// Tracking selected units via internal state allows filtering of CSS unit
// only values from being saved while maintaining preexisting unit selection
// behaviour. Filtering CSS unit only values prevents invalid style values.
const [ selectedUnits, setSelectedUnits ] = useState( {
// A shorthand value has no corners, which read as undefined.
const cornerValues = values as BorderRadiusCornerValues | undefined;
const [ selectedUnits, setSelectedUnits ] = useState< BorderRadiusUnits >( {
flat:
typeof values === 'string'
? parseQuantityAndUnitFromRawValue( values )[ 1 ]
: undefined,
topLeft: parseQuantityAndUnitFromRawValue( values?.topLeft )[ 1 ],
topRight: parseQuantityAndUnitFromRawValue( values?.topRight )[ 1 ],
bottomLeft: parseQuantityAndUnitFromRawValue( values?.bottomLeft )[ 1 ],
topLeft: parseQuantityAndUnitFromRawValue( cornerValues?.topLeft )[ 1 ],
topRight: parseQuantityAndUnitFromRawValue(
cornerValues?.topRight
)[ 1 ],
bottomLeft: parseQuantityAndUnitFromRawValue(
cornerValues?.bottomLeft
)[ 1 ],
bottomRight: parseQuantityAndUnitFromRawValue(
values?.bottomRight
cornerValues?.bottomRight
)[ 1 ],
} );

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

const toggleLinked = () => setIsLinked( ! isLinked );
Expand Down Expand Up @@ -220,12 +255,14 @@ export default function BorderRadiusControl( { onChange, values, presets } ) {
/>
) : (
<VStack>
{ [
'topLeft',
'topRight',
'bottomLeft',
'bottomRight',
].map( ( corner ) => (
{ (
[
'topLeft',
'topRight',
'bottomLeft',
'bottomRight',
] as const
).map( ( corner ) => (
<PresetInputControl
key={ corner }
ariaLabel={ CORNERS[ corner ] }
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 { BorderRadiusLinkedButtonProps } from './types';

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

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { useState } from '@wordpress/element';
import BorderRadiusControl from '../';

const meta = {
const meta: Meta< typeof BorderRadiusControl > = {
id: 'blockeditor-borderradiuscontrol',
title: 'Editor/Block Editor/BorderRadiusControl',
component: BorderRadiusControl,
Expand All @@ -23,6 +24,7 @@ const meta = {
},
onChange: {
action: 'onChange',
// @ts-expect-error Storybook types omit `null`, which hides the control.
control: { type: null },
table: {
type: { summary: 'function' },
Expand All @@ -34,7 +36,9 @@ const meta = {

export default meta;

export const Default = {
type Story = StoryObj< typeof meta >;

export const Default: Story = {
render: function Template( { onChange, ...args } ) {
const [ values, setValues ] = useState( args.values );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ describe( 'getAllValue', () => {

describe( 'when provided invalid values', () => {
it( 'should return undefined when passed an array', () => {
// @ts-expect-error An array is not a valid value.
expect( getAllValue( [] ) ).toBe( undefined );
} );
it( 'should return undefined when passed a boolean', () => {
// @ts-expect-error A boolean is not a valid value.
expect( getAllValue( false ) ).toBe( undefined );
} );
it( 'should return undefined when passed undefined', () => {
Expand Down Expand Up @@ -229,6 +231,7 @@ describe( 'hasMixedValues', () => {
describe( 'hasDefinedValues', () => {
it( 'should return false when passed a falsy value', () => {
expect( hasDefinedValues( undefined ) ).toBe( false );
// @ts-expect-error `null` is not a valid value.
expect( hasDefinedValues( null ) ).toBe( false );
expect( hasDefinedValues( '' ) ).toBe( false );
} );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { ComponentProps } from 'react';
import type { Button } from '@wordpress/components';
import type { Preset } from '../preset-input-control/types';

/**
* A corner of the border radius.
*/
export type BorderRadiusCorner =
'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';

/**
* The radius of each corner.
*/
export type BorderRadiusCornerValues = Partial<
Record< BorderRadiusCorner, string >
>;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/**
* Corner values whose presets are converted to their sizes.
*/
export type BorderRadiusConvertedValues = Partial<
Record< BorderRadiusCorner, string | number >
>;

/**
* Border radius values, either a shorthand value or one per corner, where a
* stored corner may be the number `0`. Global styles also pass the shared
* radius of an inherited value as `all`.
*/
export type BorderRadiusValues =
string | ( BorderRadiusConvertedValues & { all?: string } );

/**
* The selected units of the flat radius and each corner.
*/
export type BorderRadiusUnits = Partial<
Record< BorderRadiusCorner | 'flat', string >
>;

/**
* Border radius presets, by origin.
*/
export interface BorderRadiusPresets {
default?: Preset[];
theme?: Preset[];
custom?: Preset[];
}

/**
* Props of `BorderRadiusControl`.
*/
export interface BorderRadiusControlProps {
/**
* Callback to handle onChange. Corners the change leaves alone keep their
* stored values, which may be the number `0`.
*/
onChange: ( values: BorderRadiusConvertedValues ) => void;
/**
* Border radius values.
*/
values?: BorderRadiusValues;
/**
* Border radius presets.
*/
presets?: BorderRadiusPresets;
}

/**
* Props of the `LinkedButton` of `BorderRadiusControl`.
*/
export type BorderRadiusLinkedButtonProps = ComponentProps< typeof Button > & {
isLinked: boolean;
};
Loading
Loading