块编辑器开发文档

IsolatedEventContainer

💡 云策文档标注

概述

IsolatedEventContainer 是一个已弃用的 WordPress 组件,用于包装 UI 元素(如模态框和弹出框),以防止特定事件(如 mousedown)传播到容器外部,从而避免外部 UI 的意外交互。事件在组件内部仍正常工作。

关键要点

  • 已弃用:文档明确标记为 Deprecated,开发者应考虑替代方案。
  • 事件隔离:阻止 mousedown 事件传播到容器外,防止外部 UI 交互问题。
  • 应用场景:适用于模态框、弹出框等需要隔离事件的 UI 元素。
  • 组件使用:通过 @wordpress/components 导入 IsolatedEventContainer,并传递 props 以自定义组件。

代码示例

import { IsolatedEventContainer } from '@wordpress/components';

const MyModal = () => {
    return (
        <IsolatedEventContainer
            className="component-some_component"
            onClick={ clickHandler }
        >
            <p>This is an isolated component</p>
        </IsolatedEventContainer>
    );
};

注意事项

  • 所有 props 直接传递给 IsolatedEventContainer 组件,需确保兼容性。
  • 当前仅支持 mousedown 事件隔离,其他事件可能不受影响。

📄 原文内容

Deprecated

This is a container that prevents certain events from propagating outside of the container. This is used to wrap
UI elements such as modals and popovers where the propagated event can cause problems. The event continues to work
inside the component.

For example, a mousedown event in a modal container can propagate to the surrounding DOM, causing UI outside of the
modal to be interacted with.

The current isolated events are:

  • mousedown – This prevents UI interaction with other mousedown event handlers, such as selection

Usage

Creates a custom component that won’t propagate mousedown events outside of the component.

import { IsolatedEventContainer } from '@wordpress/components';

const MyModal = () => {
    return (
        <IsolatedEventContainer
            className="component-some_component"
            onClick={ clickHandler }
        >
            <p>This is an isolated component</p>
        </IsolatedEventContainer>
    );
};

Props

All props are passed as-is to the <IsolatedEventContainer />