Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Popover: Fix iframe story and add test #53182

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 2 additions & 46 deletions packages/components/src/popover/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { __unstableIframe as Iframe } from '@wordpress/block-editor';
* Internal dependencies
*/
import Button from '../../button';
import { Provider as SlotFillProvider } from '../../slot-fill';
import { Popover } from '..';
import { PopoverInsideIframeRenderedInExternalSlot } from '../test/utils';
import type { PopoverProps } from '../types';

const AVAILABLE_PLACEMENTS: PopoverProps[ 'placement' ][] = [
Expand Down Expand Up @@ -249,51 +249,7 @@ DynamicHeight.args = {
export const WithSlotOutsideIframe: ComponentStory< typeof Popover > = (
args
) => {
const anchorRef = useRef( null );
const slotName = 'popover-with-slot-outside-iframe';

return (
<SlotFillProvider>
<div>
{ /* @ts-expect-error Slot is not currently typed on Popover */ }
<Popover.Slot name={ slotName } />
<Iframe
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={ anchorRef }
>
Popover&apos;s anchor
</p>
<Popover
{ ...args }
__unstableSlotName={ slotName }
anchorRef={ anchorRef }
/>
</div>
</Iframe>
</div>
</SlotFillProvider>
);
return <PopoverInsideIframeRenderedInExternalSlot { ...args } />;
};
WithSlotOutsideIframe.args = {
...Default.args,
Expand Down
14 changes: 14 additions & 0 deletions packages/components/src/popover/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../utils';
import Popover from '..';
import type { PopoverProps } from '../types';
import { PopoverInsideIframeRenderedInExternalSlot } from './utils';

type PositionToPlacementTuple = [
NonNullable< PopoverProps[ 'position' ] >,
Expand Down Expand Up @@ -175,6 +176,19 @@ describe( 'Popover', () => {
} );
} );

describe( 'Slot outside iframe', () => {
it( 'should support cross-document rendering', async () => {
render(
<PopoverInsideIframeRenderedInExternalSlot>
<span>content</span>
</PopoverInsideIframeRenderedInExternalSlot>
);
await waitFor( async () =>
expect( screen.getByText( 'content' ) ).toBeVisible()
);
} );
} );

describe( 'positionToPlacement', () => {
it.each( ALL_POSITIONS_TO_EXPECTED_PLACEMENTS )(
'converts `%s` to `%s`',
Expand Down
80 changes: 80 additions & 0 deletions packages/components/src/popover/test/utils.tsx
mirka marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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 [ iframeRef, setIframeRef ] = useState< HTMLIFrameElement | null >(
null
);

return (
<iframe { ...props } title="My Iframe" ref={ setIframeRef }>
{ iframeRef?.contentWindow &&
createPortal(
children,
iframeRef?.contentWindow.document.body
) }
</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&apos;s anchor
</p>
<Popover
{ ...props }
__unstableSlotName={ SLOT_NAME }
anchor={ anchorRef }
/>
</div>
</GenericIframe>
</SlotFillProvider>
);
};
Loading