块编辑器开发文档

💡 云策文档标注

概述

ResizeTooltip 是一个用于在元素尺寸变化时显示其宽度和高度的组件。它适用于需要实时监控元素尺寸的 WordPress 开发场景,如编辑器或布局工具。

关键要点

  • ResizeTooltip 通过监听父元素的宽度或高度变化来动态显示尺寸标签。
  • 使用时必须确保父元素定义了 position 样式属性,以便组件能基于位置技术检测尺寸变化。
  • 组件支持两种位置模式:bottom(默认,在父元素底部居中显示)和 corner(在父元素右上角显示)。
  • 提供多个 Props 来自定义行为,如 axis 限制轴、fadeTimeout 控制标签消失延迟、position 设置位置等。

代码示例

const Example = () => {
    return (
        <div style={ { position: 'relative' } }>
            <ResizeTooltip />
            ...
        </div>
    );
};

注意事项

  • 父元素必须设置 position 样式(如 relative、absolute 或 fixed),否则 ResizeTooltip 可能无法正常工作。
  • 默认情况下,标签会在 180 毫秒后淡出,可通过 fadeTimeout 调整。
  • 使用 axis Prop 可以限制标签仅响应特定轴(x 或 y)的尺寸变化。

📄 原文内容

ResizeTooltip displays the dimensions of an element whenever the width or height of the element changes.

Usage

const Example = () => {
    return (
        <div style={ { position: 'relative' } }>
            <ResizeTooltip />
            ...
        </div>
    );
};

Be sure that the parent element containing <ResizeTooltip /> has the position style property defined. This is important as <ResizeTooltip /> uses position based techniques to determine size changes.

Positions

<ResizeTooltip /> has three positions;

  • bottom (Default)
  • corner

bottom

The bottom position (default) renders the dimensions label at the bottom-center of the (parent) element.

corner

The corner position renders the dimensions label in the top-right corner of the (parent) element.

Props

axis

Limits the label to render corresponding to the axis. By default, the label will automatically render based on both x and y changes.

  • Type: String
  • Required: No
  • Values: x | y

fadeTimeout

Duration (in ms) before the label disappears after resize event.

  • Type: Number
  • Required: No
  • Default: 180

isVisible

Determines if the label can render.

  • Type: Boolean
  • Required: No
  • Default: true

labelRef

Callback Ref for the label element.

  • Type: Function
  • Required: No

onMove

Callback function when the (observed) element resizes, specifically with a mousemove based event.

  • Type: Function
  • Required: No

onResize

Callback function when the (observed) element resizes.

  • Type: Function
  • Required: No

position

The positions for the label.

  • Type: String
  • Required: No
  • Default: corner
  • Values: bottom | corner

showPx

Renders a PX unit suffix after the width or height value in the label.

  • Type: Boolean
  • Required: No
  • Default: true

zIndex

The z-index style property for the label.

  • Type: Number
  • Required: No
  • Default: 1000