Repository navigation
Block Visibility: Show toggle option in the block inspector when responsive editing #79995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/block-editor/src/components/block-visibility/viewport-inspector-control.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit: This |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:

Plus the info is already shown via the toggle. Possibly we could move it to below the toggle, but it might be too much.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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:
So in those cases the visibility info isn't shown but nor is the toggle. I'm looking at a fix for this.
There was a problem hiding this comment.
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.