Skip to content

Commit

Permalink
HTML: Use 'Disabled.Context' (#39669)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Mar 23, 2022
1 parent 8abde21 commit 44dbfa6
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions packages/block-library/src/html/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useContext, useState } from '@wordpress/element';
import {
BlockControls,
PlainText,
Expand All @@ -20,6 +20,7 @@ import { useSelect } from '@wordpress/data';

export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
const [ isPreview, setIsPreview ] = useState();
const isDisabled = useContext( Disabled.Context );

const styles = useSelect( ( select ) => {
// Default styles used to unset some of the styles
Expand Down Expand Up @@ -69,35 +70,26 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
<Disabled.Consumer>
{ ( isDisabled ) =>
isPreview || isDisabled ? (
<>
<SandBox
html={ attributes.content }
styles={ styles }
/>
{ /*
An overlay is added when the block is not selected in order to register click events.
Some browsers do not bubble up the clicks from the sandboxed iframe, which makes it
difficult to reselect the block.
*/ }
{ ! isSelected && (
<div className="block-library-html__preview-overlay"></div>
) }
</>
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) =>
setAttributes( { content } )
}
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
)
}
</Disabled.Consumer>
{ isPreview || isDisabled ? (
<>
<SandBox html={ attributes.content } styles={ styles } />
{ /*
An overlay is added when the block is not selected in order to register click events.
Some browsers do not bubble up the clicks from the sandboxed iframe, which makes it
difficult to reselect the block.
*/ }
{ ! isSelected && (
<div className="block-library-html__preview-overlay"></div>
) }
</>
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
) }
</div>
);
}

0 comments on commit 44dbfa6

Please sign in to comment.