块编辑器开发文档

💡 云策文档标注

概述

BorderControl 是一个用于设置边框颜色、样式和宽度的输入控件,适用于 WordPress 组件库。它整合了多个子组件,允许开发者灵活配置边框属性,但不包括边框半径。

关键要点

  • BorderControl 由 BorderDropdown(颜色和样式选择)、UnitControl(宽度控制)和可选的 RangeControl 组成。
  • 支持通过 props 自定义颜色、样式、宽度、标签等属性,并提供回调函数 onChange 处理值变化。
  • 可在编辑器内外使用,外部使用时需结合 Popover.Slot 和 SlotFillProvider 确保 Tooltip 定位。

代码示例

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 }
        />
    );
};

注意事项

  • 边框半径不包含在此控件中,可能需单独处理或归入“形状”抽象。
  • onChange 回调可能返回 undefined,当用户清除所有边框属性时。
  • shouldSanitizeBorder 默认为 true,用于在无宽度或颜色时清除样式并返回 undefined。

📄 原文内容

An input control for a border’s color, style, and width.

Development guidelines

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.

Usage

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.

Props

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.

  • Required: No
  • Default: []

disableCustomColors: boolean

This toggles the ability to choose custom colors.

  • Required: No

disableUnits: boolean

This controls whether unit selection should be disabled.

  • Required: No

enableAlpha: boolean

This controls whether the alpha channel will be offered when selecting
custom colors.

  • Required: No
  • Default: true

enableStyle: boolean

This controls whether to support border style selection.

  • Required: No
  • Default: true

hideLabelFromVision: boolean

Provides control over whether the label will only be visible to screen readers.

  • Required: No

isCompact: boolean

This 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.

  • Required: No

label: string

If provided, a label will be generated using this as the content.

Whether it is visible only to screen readers is controlled via
hideLabelFromVision.

  • Required: No

onChange: ( value?: Object ) => void

A 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.

  • Required: Yes

shouldSanitizeBorder: boolean

If 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.

  • Required: No
  • Default: true

size: string

Size of the control.

  • Required: No
  • Default: default
  • Allowed values: default, __unstable-large

value: Object

An object representing a border or undefined. Used to set the current border
configuration for this component.

Example:

 {
    color: '#72aee6',
    style: 'solid',
    width: '2px,
}
  • Required: No

width: CSSProperties[ 'width' ]

Controls the visual width of the BorderControl. It has no effect if the
isCompact prop is set to true.

  • Required: No

withSlider: boolean

Flags whether this BorderControl should also render a RangeControl for
additional control over a border’s width.

  • Required: No

__next40pxDefaultSize: boolean

Start opting into the larger default height that will become the default size in a future version.

  • Required: No
  • Default: false