Skip to content

Commit

Permalink
Block editor: placeholders: try admin shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jul 16, 2021
1 parent 4fdffac commit a2fd8f3
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions packages/components/src/placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,38 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useResizeObserver } from '@wordpress/compose';
import { useResizeObserver, useRefEffect } from '@wordpress/compose';
import { useState, createPortal } from '@wordpress/element';

/**
* Internal dependencies
*/
import Icon from '../icon';
import StyleProvider from '../style-provider';

function AdminShadow( { children } ) {
const [ shadow, setShadow ] = useState();
const ref = useRefEffect( ( element ) => {
const root = element.attachShadow( { mode: 'open' } );
Array.from( document.styleSheets ).forEach( ( { ownerNode } ) => {
if ( ! ownerNode.getAttribute( 'data-emotion' ) ) {
root.appendChild( ownerNode.cloneNode( true ) );
}
} );
setShadow( root );
}, [] );
return (
<div ref={ ref }>
{ shadow &&
createPortal(
<StyleProvider document={ { head: shadow } }>
{ children }
</StyleProvider>,
shadow
) }
</div>
);
}

/**
* Renders a placeholder. Normally used by blocks to render their empty state.
Expand Down Expand Up @@ -61,25 +87,27 @@ function Placeholder( {
'is-column-layout': isColumnLayout,
} );
return (
<div { ...additionalProps } className={ classes }>
{ resizeListener }
{ notices }
{ preview && (
<div className="components-placeholder__preview">
{ preview }
<AdminShadow>
<div { ...additionalProps } className={ classes }>
{ resizeListener }
{ notices }
{ preview && (
<div className="components-placeholder__preview">
{ preview }
</div>
) }
<div className="components-placeholder__label">
<Icon icon={ icon } />
{ label }
</div>
) }
<div className="components-placeholder__label">
<Icon icon={ icon } />
{ label }
{ !! instructions && (
<div className="components-placeholder__instructions">
{ instructions }
</div>
) }
<div className={ fieldsetClasses }>{ children }</div>
</div>
{ !! instructions && (
<div className="components-placeholder__instructions">
{ instructions }
</div>
) }
<div className={ fieldsetClasses }>{ children }</div>
</div>
</AdminShadow>
);
}

Expand Down

0 comments on commit a2fd8f3

Please sign in to comment.