Skip to content

Commit 1ae4d44

Browse files
authored
Iframe: adjust keydown event bubbling (#54565)
1 parent a0b1a6b commit 1ae4d44

File tree

1 file changed

+20
-12
lines changed
  • packages/block-editor/src/components/iframe

1 file changed

+20
-12
lines changed

packages/block-editor/src/components/iframe/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ function Iframe( {
237237
return (
238238
<>
239239
{ tabIndex >= 0 && before }
240+
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
240241
<iframe
241242
{ ...props }
242243
style={ {
@@ -264,6 +265,25 @@ function Iframe( {
264265
// content.
265266
src={ src }
266267
title={ __( 'Editor canvas' ) }
268+
onKeyDown={ ( event ) => {
269+
// If the event originates from inside the iframe, it means
270+
// it bubbled through the portal, but only with React
271+
// events. We need to to bubble native events as well,
272+
// though by doing so we also trigger another React event,
273+
// so we need to stop the propagation of this event to avoid
274+
// duplication.
275+
if (
276+
event.currentTarget.ownerDocument !==
277+
event.target.ownerDocument
278+
) {
279+
event.stopPropagation();
280+
bubbleEvent(
281+
event,
282+
window.KeyboardEvent,
283+
event.currentTarget
284+
);
285+
}
286+
} }
267287
>
268288
{ iframeDocument &&
269289
createPortal(
@@ -277,18 +297,6 @@ function Iframe( {
277297
'editor-styles-wrapper',
278298
...bodyClasses
279299
) }
280-
onKeyDown={ ( event ) => {
281-
// This stopPropagation call ensures React doesn't create a syncthetic event to bubble this event
282-
// which would result in two React events being bubbled throught the iframe.
283-
event.stopPropagation();
284-
const { defaultView } = iframeDocument;
285-
const { frameElement } = defaultView;
286-
bubbleEvent(
287-
event,
288-
window.KeyboardEvent,
289-
frameElement
290-
);
291-
} }
292300
>
293301
{ contentResizeListener }
294302
<StyleProvider document={ iframeDocument }>

0 commit comments

Comments
 (0)