块编辑器开发文档

FocusableIframe

💡 云策文档标注

概述

FocusableIframe 是一个已弃用的 React 组件,用于渲染增强的 iframe 元素,以支持焦点事件检测。它通过模拟 FocusEvent 并允许事件冒泡,使父组件能够响应 iframe 内的焦点变化。

关键要点

  • 组件已弃用,不建议在新项目中使用
  • 通过监听 window blur 事件来推断 iframe 的焦点状态,并分发模拟的 FocusEvent
  • 事件会冒泡,允许在父组件上绑定 onFocus 回调
  • 用法类似于标准 iframe,支持传递 onFocus 和 iframeRef 等 props

代码示例

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

const MyFocusableIframe = () => (
    <FocusableIframe
        src="/my-iframe-url"
        onFocus={ () => console.log( 'iframe is focused' ) }
    />
);

注意事项

  • 除 onFocus 和 iframeRef 外,其他 props 会传递给底层的 iframe 元素
  • onFocus 回调接收一个模拟的 FocusEvent 对象作为参数
  • iframeRef 可用于获取底层 DOM 元素的引用

📄 原文内容

Deprecated

<FocusableIframe /> is a component rendering an iframe element enhanced to support focus events. By default, it is not possible to detect when an iframe is focused or clicked within. This enhanced component uses a technique which checks whether the target of a window blur event is the iframe, inferring that this has resulted in the focus of the iframe. It dispatches an emulated FocusEvent on the iframe element with event bubbling, so a parent component binding its own onFocus event will account for focus transitioning within the iframe.

Usage

Use as you would a standard iframe. You may pass onFocus directly as the callback to be invoked when the iframe receives focus, or on an ancestor component since the event will bubble.

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

const MyFocusableIframe = () => (
    <FocusableIframe
        src="/my-iframe-url"
        onFocus={ () => console.log( 'iframe is focused' ) }
    />
);

Props

Any props aside from those listed below will be passed to the FocusableIframe will be passed through to the underlying iframe element.

onFocus

  • Type: Function
  • Required: No

Callback to invoke when iframe receives focus. Passes an emulated FocusEvent object as the first argument.

iframeRef

  • Type: React.Ref
  • Required: No

If a reference to the underlying DOM element is needed, pass iframeRef as the result of a React.createRef called from your component.