Skip to content
Closed
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
17 changes: 15 additions & 2 deletions packages/block-editor/src/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ import useBlockInspectorAnimationSettings from './useBlockInspectorAnimationSett
import { useBorderPanelLabel } from '../../hooks/border';
import { BlockStateBadges, BlockStatesControl } from '../../hooks/states';
import ContentTab from '../inspector-controls-tabs/content-tab';
import ViewportVisibilityInfo from '../block-visibility/viewport-visibility-info';
import {
ViewportVisibilityInfo,
ViewportVisibilityInspectorControl,
} from '../block-visibility';
import { unlock } from '../../lock-unlock';
import {
hasPseudoBlockStyleState,
Expand Down Expand Up @@ -466,14 +469,24 @@ const BlockInspectorSingleBlock = ( {
/>
</Spacer>
) }
<ViewportVisibilityInfo clientId={ renderedBlockClientId } />
{ ! isEditingStyleState && (
<ViewportVisibilityInfo clientId={ renderedBlockClientId } />
) }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Decided to hide this info when the inspector control is shown, as otherwise it causes a layout shift whenever toggling to hide a block via the new control:
Image

Plus the info is already shown via the toggle. Possibly we could move it to below the toggle, but it might be too much.

@talldan talldan Jul 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is a small issue though, the toggle has its own early return rendering logic that causes it to be not shown:

	if (
		! clientId ||
		! viewport ||
		! viewportLabel ||
		blockEditingMode !== 'default' ||
		blockVisibility === false ||
		! hasBlockSupport( blockName, 'visibility', true )
	) {
		return null;
	}

So in those cases the visibility info isn't shown but nor is the toggle. I'm looking at a fix for this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm thinking out loud here, should we show the badge still if the block is hidden everywhere?

Maybe it's different to "responsive" editing because it's not rendered 🤔 I'm just wondering if a block is hidden everywhere and you enter responsive editing mode, if it'd be helpful to be able to toggle it off and apply viewport-specific rules.

Maybe a follow up.

<EditContents clientId={ renderedBlockClientId } />
{ ! isEditingStyleState && (
<BlockVariationTransforms
blockClientId={ renderedBlockClientId }
/>
) }
<BlockInspectorPreTabsSlot />
{ isEditingStyleState && (
<ViewportVisibilityInspectorControl
blockEditingMode={ blockEditingMode }
blockName={ blockName }
clientId={ renderedBlockClientId }
selectedBlockStyleState={ selectedBlockStyleState }
/>
) }
{ isEditingStyleState && ! isSectionBlock && (
<StyleStateInspectorSlots
blockName={ blockName }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { default as useBlockVisibility } from './use-block-visibility';
export { default as ViewportVisibilityToolbar } from './viewport-toolbar';
export { default as BlockVisibilityViewportMenuItem } from './viewport-menu-item';
export { default as ViewportVisibilityInfo } from './viewport-visibility-info';
export { default as ViewportVisibilityInspectorControl } from './viewport-inspector-control';
export { getBlockVisibilityLabel } from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getBlockVisibilityViewportEntries,
getViewportCheckboxState,
getHideEverywhereCheckboxState,
isBlockHiddenForViewport,
} from '../utils';

describe( 'block-visibility utils', () => {
Expand Down Expand Up @@ -290,4 +291,37 @@ describe( 'block-visibility utils', () => {
expect( getHideEverywhereCheckboxState( blocks ) ).toBe( false );
} );
} );

describe( 'isBlockHiddenForViewport', () => {
it( 'detects viewport-specific hidden state', () => {
expect(
isBlockHiddenForViewport(
{
attributes: {
metadata: {
blockVisibility: {
viewport: { mobile: false },
},
},
},
},
'mobile'
)
).toBe( true );
expect(
isBlockHiddenForViewport(
{
attributes: {
metadata: {
blockVisibility: {
viewport: { tablet: false },
},
},
},
},
'mobile'
)
).toBe( false );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getBlockVisibilityViewportEntries( viewportSettings ) {
* @param {string} viewport The viewport to check (e.g., 'mobile', 'tablet', 'desktop').
* @return {boolean} Whether the block is hidden for the viewport.
*/
function isBlockHiddenForViewport( block, viewport ) {
export function isBlockHiddenForViewport( block, viewport ) {
if ( ! block ) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* WordPress dependencies
*/
import { hasBlockSupport } from '@wordpress/blocks';
import {
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { cleanEmptyObject } from '../../hooks/utils';
import { useToolsPanelDropdownMenuProps } from '../global-styles/utils';
import { BLOCK_VISIBILITY_VIEWPORTS } from './constants';
import { isBlockHiddenForViewport } from './utils';

const VIEWPORT_BY_STYLE_STATE = {
'@tablet': BLOCK_VISIBILITY_VIEWPORTS.tablet.key,
'@mobile': BLOCK_VISIBILITY_VIEWPORTS.mobile.key,
};

export default function ViewportVisibilityInspectorControl( {
blockEditingMode,
blockName,
clientId,
selectedBlockStyleState,
} ) {
const viewport =
VIEWPORT_BY_STYLE_STATE[ selectedBlockStyleState?.viewport ];
const viewportLabel = BLOCK_VISIBILITY_VIEWPORTS[ viewport ]?.label;
const attributes = useSelect(
( select ) => select( blockEditorStore ).getBlockAttributes( clientId ),
[ clientId ]
);
const blockVisibility = attributes?.metadata?.blockVisibility;
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

if (
! clientId ||
! viewport ||
! viewportLabel ||
blockEditingMode !== 'default' ||
blockVisibility === false ||
! hasBlockSupport( blockName, 'visibility', true )
) {
return null;
}

const isHidden = isBlockHiddenForViewport( { attributes }, viewport );
const setViewportHidden = ( isHiddenOnViewport ) => {
const nextBlockVisibility = cleanEmptyObject( {
viewport: {
...blockVisibility?.viewport,
[ viewport ]: isHiddenOnViewport ? false : undefined,
},
} );
Comment on lines +57 to +62

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
const nextBlockVisibility = cleanEmptyObject( {
viewport: {
...blockVisibility?.viewport,
[ viewport ]: isHiddenOnViewport ? false : undefined,
},
} );
const nextBlockVisibility = {
viewport: {
...blockVisibility?.viewport,
[ viewport ]: isHiddenOnViewport ? false : undefined,
},
};

Nit: This cleanEmptyObject seems redundant.


updateBlockAttributes( clientId, {
metadata: cleanEmptyObject( {
...attributes?.metadata,
blockVisibility: nextBlockVisibility,
} ),
} );
};
const label = sprintf(
/* translators: %s: viewport name, e.g. Tablet or Mobile. */
__( 'Hide on %s' ),
viewportLabel
);

return (
<ToolsPanel
label={ __( 'Visibility' ) }
resetAll={ () => setViewportHidden( false ) }
panelId={ clientId }
dropdownMenuProps={ dropdownMenuProps }
>
<ToolsPanelItem
label={ label }
hasValue={ () => isHidden }
onDeselect={ () => setViewportHidden( false ) }
isShownByDefault
panelId={ clientId }
>
<ToggleControl
label={ label }
checked={ isHidden }
onChange={ setViewportHidden }
/>
</ToolsPanelItem>
</ToolsPanel>
);
}
Loading