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

Add a StyleProvider to support CSS-in-JS components inside iframes. #31010

Merged
merged 3 commits into from
Apr 21, 2021
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useMergeRefs } from '@wordpress/compose';
import { __experimentalFrameProvider as FrameProvider } from '@wordpress/components';

const BODY_CLASS_NAME = 'editor-styles-wrapper';
const BLOCK_PREFIX = 'wp-block';
Expand Down Expand Up @@ -182,7 +183,13 @@ function Iframe( { contentRef, children, head, headHTML, ...props }, ref ) {
title={ __( 'Editor canvas' ) }
name="editor-canvas"
>
{ iframeDocument && createPortal( children, iframeDocument.body ) }
{ iframeDocument &&
createPortal(
<FrameProvider iframeDocument={ iframeDocument }>
{ children }
</FrameProvider>,
iframeDocument.body
) }
{ iframeDocument && createPortal( head, iframeDocument.head ) }
</iframe>
);
Expand Down
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"dependencies": {
"@babel/runtime": "^7.13.10",
"@emotion/cache": "^10.0.27",
"@emotion/core": "^10.1.1",
"@emotion/css": "^10.0.22",
"@emotion/hash": "^0.8.0",
Expand Down
20 changes: 20 additions & 0 deletions packages/components/src/frame-provider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import { CacheProvider } from '@emotion/core';
import createCache from '@emotion/cache';
import memoize from 'memize';

const memoizedCreateCacheWithContainer = memoize( ( container ) => {
return createCache( { container } );
} );

export default function FrameProvider( { children, iframeDocument } ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be renamed to emotion cache provider, perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe more StyleFrameProvider or something, I don't think we should be tied to a specific library.

Copy link
Member

@ellatrix ellatrix Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It provides the cache right? StyleCacheProvider? Or simply StyleProvider.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is what makes the styles injection work, I prefer StyleProvider

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StyleProvider might imply that it is necessary for basic style injection, but it's not. I think including the fact that this is specifically for iframes is wise, like StyleFrameProvider or IFrameStyleProvider if we want to be the most descriptive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think @ellatrix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just wanted to point out that it's not providing an iframe but rather the style cache. Maybe StylesForIframeProvider?

Not a blocker, I don't care that much :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can always rename it later since it's experimental. I don't mind "StyleProvider" personally because it probably won't break anything if used outside an iframe, it's just useless.

if ( ! iframeDocument ) {
return null;
}

const cache = memoizedCreateCacheWithContainer( iframeDocument.head );

return <CacheProvider value={ cache }>{ children }</CacheProvider>;
}
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export {
Provider as SlotFillProvider,
useSlot as __experimentalUseSlot,
} from './slot-fill';
export { default as __experimentalFrameProvider } from './frame-provider';

// Higher-Order Components
export {
Expand Down