Skip to content

Commit

Permalink
Add a FrameProvider to be used to support CSS-in-JS styles inside ifr…
Browse files Browse the repository at this point in the history
…ames
  • Loading branch information
sarayourfriend authored and youknowriad committed Apr 21, 2021
1 parent fe37913 commit 7633796
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"dependencies": {
"@babel/runtime": "^7.13.10",
"@emotion/weak-memoize": "0.2.2",
"@wordpress/a11y": "file:../a11y",
"@wordpress/blob": "file:../blob",
"@wordpress/blocks": "file:../blocks",
Expand Down
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 } ) {
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

0 comments on commit 7633796

Please sign in to comment.