Skip to content

Commit

Permalink
Remove ifBlockEditSelected internal higher-order component (#22905)
Browse files Browse the repository at this point in the history
* Block Editor: Remove ifBlockEditSelected internal higher-order component

* Block Editor: Pass children to inspector fills
  • Loading branch information
aduth authored Jun 5, 2020
1 parent 68f62ad commit 7390563
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
9 changes: 7 additions & 2 deletions packages/block-editor/src/components/block-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
/**
* Internal dependencies
*/
import { ifBlockEditSelected } from '../block-edit/context';
import { useBlockEditContext } from '../block-edit/context';

const { Fill, Slot } = createSlotFill( 'BlockControls' );

Expand All @@ -26,6 +26,11 @@ function BlockControlsSlot( props ) {
}

function BlockControlsFill( { controls, children } ) {
const { isSelected } = useBlockEditContext();
if ( ! isSelected ) {
return null;
}

return (
<Fill>
{ ( fillProps ) => {
Expand All @@ -44,7 +49,7 @@ function BlockControlsFill( { controls, children } ) {
);
}

const BlockControls = ifBlockEditSelected( BlockControlsFill );
const BlockControls = BlockControlsFill;

BlockControls.Slot = BlockControlsSlot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports[`BlockControls should render a dynamic toolbar of controls 1`] = `
<WithFilters(Edit)
isSelected={true}
>
<IfBlockEditSelected(BlockControlsFill)
<BlockControlsFill
controls={
Array [
Object {
Expand Down Expand Up @@ -60,7 +60,7 @@ exports[`BlockControls should render a dynamic toolbar of controls 1`] = `
<p>
Child
</p>
</IfBlockEditSelected(BlockControlsFill)>
</BlockControlsFill>
</WithFilters(Edit)>
</ContextProvider>
`;
21 changes: 0 additions & 21 deletions packages/block-editor/src/components/block-edit/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,3 @@ export const withBlockEditContext = ( mapContextToProps ) =>
</Consumer>
);
}, 'withBlockEditContext' );

/**
* A Higher Order Component used to render conditionally the wrapped
* component only when the BlockEdit has selected state set.
*
* @param {WPComponent} OriginalComponent Component to wrap.
*
* @return {WPComponent} Component which renders only when the BlockEdit is selected.
*/
export const ifBlockEditSelected = createHigherOrderComponent(
( OriginalComponent ) => {
return ( props ) => (
<Consumer>
{ ( { isSelected } ) =>
isSelected && <OriginalComponent { ...props } />
}
</Consumer>
);
},
'ifBlockEditSelected'
);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
/**
* Internal dependencies
*/
import { ifBlockEditSelected } from '../block-edit/context';
import { useBlockEditContext } from '../block-edit/context';

const { Fill, Slot } = createSlotFill( 'BlockFormatControls' );

Expand All @@ -25,6 +25,11 @@ function BlockFormatControlsSlot( props ) {
}

function BlockFormatControlsFill( props ) {
const { isSelected } = useBlockEditContext();
if ( ! isSelected ) {
return null;
}

return (
<Fill>
{ ( fillProps ) => {
Expand All @@ -39,7 +44,7 @@ function BlockFormatControlsFill( props ) {
);
}

const BlockFormatControls = ifBlockEditSelected( BlockFormatControlsFill );
const BlockFormatControls = BlockFormatControlsFill;

BlockFormatControls.Slot = BlockFormatControlsSlot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { createSlotFill } from '@wordpress/components';
/**
* Internal dependencies
*/
import { ifBlockEditSelected } from '../block-edit/context';
import { useBlockEditContext } from '../block-edit/context';

const name = 'InspectorAdvancedControls';
const { Fill, Slot } = createSlotFill( name );

const InspectorAdvancedControls = ifBlockEditSelected( Fill );
function InspectorAdvancedControls( { children } ) {
const { isSelected } = useBlockEditContext();
return isSelected ? <Fill>{ children }</Fill> : null;
}

InspectorAdvancedControls.slotName = name;
InspectorAdvancedControls.Slot = Slot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { createSlotFill } from '@wordpress/components';
/**
* Internal dependencies
*/
import { ifBlockEditSelected } from '../block-edit/context';
import { useBlockEditContext } from '../block-edit/context';

const { Fill, Slot } = createSlotFill( 'InspectorControls' );

const InspectorControls = ifBlockEditSelected( Fill );
function InspectorControls( { children } ) {
const { isSelected } = useBlockEditContext();
return isSelected ? <Fill>{ children }</Fill> : null;
}

InspectorControls.Slot = Slot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import { createSlotFill, BottomSheetConsumer } from '@wordpress/components';
/**
* Internal dependencies
*/
import { ifBlockEditSelected } from '../block-edit/context';
import { useBlockEditContext } from '../block-edit/context';
import { BlockSettingsButton } from '../block-settings';

const { Fill, Slot } = createSlotFill( 'InspectorControls' );

const FillWithSettingsButton = ( { children, ...props } ) => {
const { isSelected } = useBlockEditContext();
if ( ! isSelected ) {
return null;
}

return (
<>
<Fill { ...props }>
Expand All @@ -30,7 +35,7 @@ const FillWithSettingsButton = ( { children, ...props } ) => {
);
};

const InspectorControls = ifBlockEditSelected( FillWithSettingsButton );
const InspectorControls = FillWithSettingsButton;

InspectorControls.Slot = Slot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`core/more/edit should match snapshot when noTeaser is false 1`] = `
<React.Fragment>
<IfBlockEditSelected(InspectorControlsFill)>
<InspectorControls>
<ForwardRef(PanelBody)>
<WithInstanceId(ToggleControl)
checked={false}
Expand All @@ -11,7 +11,7 @@ exports[`core/more/edit should match snapshot when noTeaser is false 1`] = `
onChange={[Function]}
/>
</ForwardRef(PanelBody)>
</IfBlockEditSelected(InspectorControlsFill)>
</InspectorControls>
<div
className="wp-block-more"
>
Expand All @@ -32,7 +32,7 @@ exports[`core/more/edit should match snapshot when noTeaser is false 1`] = `

exports[`core/more/edit should match snapshot when noTeaser is true 1`] = `
<React.Fragment>
<IfBlockEditSelected(InspectorControlsFill)>
<InspectorControls>
<ForwardRef(PanelBody)>
<WithInstanceId(ToggleControl)
checked={true}
Expand All @@ -41,7 +41,7 @@ exports[`core/more/edit should match snapshot when noTeaser is true 1`] = `
onChange={[Function]}
/>
</ForwardRef(PanelBody)>
</IfBlockEditSelected(InspectorControlsFill)>
</InspectorControls>
<div
className="wp-block-more"
>
Expand Down

0 comments on commit 7390563

Please sign in to comment.