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 all 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 { __experimentalStyleProvider as StyleProvider } 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(
<StyleProvider iframeDocument={ iframeDocument }>
{ children }
</StyleProvider>,
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
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 __experimentalStyleProvider } from './style-provider';

// Higher-Order Components
export {
Expand Down
20 changes: 20 additions & 0 deletions packages/components/src/style-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 StyleProvider( { children, iframeDocument } ) {
if ( ! iframeDocument ) {
return null;
}

const cache = memoizedCreateCacheWithContainer( iframeDocument.head );

return <CacheProvider value={ cache }>{ children }</CacheProvider>;
Copy link
Member

Choose a reason for hiding this comment

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

I didn't know this existed. It's great that emotion provides this CacheProvider and consumes it.

}