-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Popover: Fix iframe story and add test (#53182)
* Popover: Fix iframe story and add test * Simplify state ref * Move test utils file * Fix Firefox case * Fix iframe in Chrome
- Loading branch information
Showing
3 changed files
with
103 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createPortal, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Popover from '../..'; | ||
import { Provider as SlotFillProvider } from '../../../slot-fill'; | ||
import type { WordPressComponentProps } from '../../../ui/context'; | ||
|
||
const GenericIframe = ( { | ||
children, | ||
...props | ||
}: WordPressComponentProps< { children: React.ReactNode }, 'iframe' > ) => { | ||
const [ containerNode, setContainerNode ] = useState< HTMLElement >(); | ||
|
||
return ( | ||
<iframe | ||
{ ...props } | ||
title="My Iframe" | ||
srcDoc="<!doctype html><html><body></body></html>" | ||
// Waiting for the load event ensures that this works in Firefox. | ||
// See https://github.com/facebook/react/issues/22847#issuecomment-991394558 | ||
onLoad={ ( event ) => { | ||
if ( event.currentTarget.contentDocument ) { | ||
setContainerNode( | ||
event.currentTarget.contentDocument.body | ||
); | ||
} | ||
} } | ||
> | ||
{ containerNode && createPortal( children, containerNode ) } | ||
</iframe> | ||
); | ||
}; | ||
|
||
export const PopoverInsideIframeRenderedInExternalSlot = ( | ||
props: React.ComponentProps< typeof Popover > | ||
) => { | ||
const SLOT_NAME = 'my-slot'; | ||
const [ anchorRef, setAnchorRef ] = useState< HTMLParagraphElement | null >( | ||
null | ||
); | ||
|
||
return ( | ||
<SlotFillProvider> | ||
{ /* @ts-expect-error Slot is not currently typed on Popover */ } | ||
<Popover.Slot name={ SLOT_NAME } /> | ||
<GenericIframe | ||
style={ { | ||
width: '100%', | ||
height: '400px', | ||
border: '0', | ||
outline: '1px solid purple', | ||
} } | ||
> | ||
<div | ||
style={ { | ||
height: '200vh', | ||
paddingTop: '10vh', | ||
} } | ||
> | ||
<p | ||
style={ { | ||
padding: '8px', | ||
background: 'salmon', | ||
maxWidth: '200px', | ||
marginTop: '100px', | ||
marginLeft: 'auto', | ||
marginRight: 'auto', | ||
} } | ||
ref={ setAnchorRef } | ||
> | ||
Popover's anchor | ||
</p> | ||
<Popover | ||
{ ...props } | ||
__unstableSlotName={ SLOT_NAME } | ||
anchor={ anchorRef } | ||
/> | ||
</div> | ||
</GenericIframe> | ||
</SlotFillProvider> | ||
); | ||
}; |