BoxControl 是一个 WordPress 组件,允许用户设置上、右、下、左四个方向的值,常用于处理如内边距或外边距等 CSS 属性。它提供灵活的配置选项,包括预设、单位控制和轴拆分功能。
import { useState } from 'react';
import { BoxControl } from '@wordpress/components';
function Example() {
const [ values, setValues ] = useState( {
top: '50px',
left: '10%',
right: '10%',
bottom: '50px',
} );
return (
<BoxControl
__next40pxDefaultSize
values={ values }
onChange={ setValues }
/>
);
};See the WordPress Storybook for more detailed, interactive documentation.
A control that lets users set values for top, right, bottom, and left. Can be
used as an input control for values like padding or margin.
import { useState } from 'react';
import { BoxControl } from '@wordpress/components';
function Example() {
const [ values, setValues ] = useState( {
top: '50px',
left: '10%',
right: '10%',
bottom: '50px',
} );
return (
<BoxControl
__next40pxDefaultSize
values={ values }
onChange={ setValues }
/>
);
};
__next40pxDefaultSizebooleanfalseStart opting into the larger default height that will become the default size in a future version.
allowResetbooleantrueIf this property is true, a button to reset the box control is rendered.
idstringThe id to use as a base for the unique HTML id attribute of the control.
inputPropsUnitControlPassthroughProps{<br />
min: 0,<br />
}Props for the internal UnitControl components.
labelstring__( 'Box Control' )Heading label for the control.
onChange(next: BoxControlValue) => void() => {}A callback function when an input value changes.
presetsPreset[]Available presets to pick from.
presetKeystringThe key of the preset to apply.
If you provide a list of presets, you must provide a preset key to use.
The format of preset selected values is going to be var:preset|${ presetKey }|${ presetSlug }
resetValuesBoxControlValue{<br />
top: undefined,<br />
right: undefined,<br />
bottom: undefined,<br />
left: undefined,<br />
}The top, right, bottom, and left box dimension values to use when the control is reset.
sidesreadonly (keyof BoxControlValue | "horizontal" | "vertical")[]Collection of sides to allow control of. If omitted or empty, all sides will be available.
Allowed values are “top”, “right”, “bottom”, “left”, “vertical”, and “horizontal”.
splitOnAxisbooleanfalseIf this property is true, when the box control is unlinked, vertical and horizontal controls
can be used instead of updating individual sides.
unitsWPUnitControlUnit[]CSS_UNITSAvailable units to select from.
valuesBoxControlValueThe current values of the control, expressed as an object of top, right, bottom, and left values.