BorderControl 是一个用于设置边框颜色、样式和宽度的输入控件,适用于 WordPress 组件库。它整合了多个子组件,允许开发者灵活配置边框属性,但不包括边框半径。
import { useState } from 'react';
import { BorderControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
const colors = [
{ name: 'Blue 20', color: '#72aee6' },
// ...
];
const MyBorderControl = () => {
const [ border, setBorder ] = useState();
return (
<BorderControl
__next40pxDefaultSize
colors={ colors }
label={ __( 'Border' ) }
onChange={ setBorder }
value={ border }
/>
);
};An input control for a border’s color, style, and width.
The BorderControl brings together internal sub-components which allow users to
set the various properties of a border. The first sub-component, a
BorderDropdown contains options representing border color and style. The
border width is controlled via a UnitControl and an optional RangeControl.
Border radius is not covered by this control as it may be desired separate to
color, style, and width. For example, the border radius may be absorbed under
a “shape” abstraction.
import { useState } from 'react';
import { BorderControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
const colors = [
{ name: 'Blue 20', color: '#72aee6' },
// ...
];
const MyBorderControl = () => {
const [ border, setBorder ] = useState();
return (
<BorderControl
__next40pxDefaultSize
colors={ colors }
label={ __( 'Border' ) }
onChange={ setBorder }
value={ border }
/>
);
};
If you’re using this component outside the editor, you can
ensure Tooltip positioning
for the BorderControl‘s color and style options, by rendering your
BorderControl with a Popover.Slot further up the element tree and within a
SlotFillProvider overall.
colors: ( PaletteObject | ColorObject )[]An array of color definitions. This may also be a multi-dimensional array where
colors are organized by multiple origins.
Each color may be an object containing a name and color value.
[]disableCustomColors: booleanThis toggles the ability to choose custom colors.
disableUnits: booleanThis controls whether unit selection should be disabled.
enableAlpha: booleanThis controls whether the alpha channel will be offered when selecting
custom colors.
trueenableStyle: booleanThis controls whether to support border style selection.
truehideLabelFromVision: booleanProvides control over whether the label will only be visible to screen readers.
isCompact: booleanThis flags the BorderControl to render with a more compact appearance. It
restricts the width of the control and prevents it from expanding to take up
additional space.
label: stringIf provided, a label will be generated using this as the content.
Whether it is visible only to screen readers is controlled via
hideLabelFromVision.
onChange: ( value?: Object ) => voidA callback function invoked when the border value is changed via an interaction
that selects or clears, border color, style, or width.
Note: the value may be undefined if a user clears all border properties.
shouldSanitizeBorder: booleanIf opted into, sanitizing the border means that if no width or color have been
selected, the border style is also cleared and undefined is returned as the
new border value.
truesize: stringSize of the control.
defaultdefault, __unstable-largevalue: ObjectAn object representing a border or undefined. Used to set the current border
configuration for this component.
Example:
{
color: '#72aee6',
style: 'solid',
width: '2px,
}
width: CSSProperties[ 'width' ]Controls the visual width of the BorderControl. It has no effect if the
isCompact prop is set to true.
withSlider: booleanFlags whether this BorderControl should also render a RangeControl for
additional control over a border’s width.
__next40pxDefaultSize: booleanStart opting into the larger default height that will become the default size in a future version.
false