- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.6k
Block API: Add block visibility control support and UI #71203
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            42 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6436944
              
                Block API: Add block visibility control support and UI
              
              
                t-hamano c12c3cf
              
                Merge branch 'trunk' into block-visibility
              
              
                t-hamano 7f8d055
              
                Render hidden block block
              
              
                t-hamano 52913e1
              
                Remove unused code
              
              
                t-hamano a5d0146
              
                Always hide block when it's not selected
              
              
                t-hamano c3f7135
              
                Extend existing isBlockVisible selector and use it
              
              
                t-hamano 5f459c7
              
                Revert "Extend existing isBlockVisible selector and use it"
              
              
                t-hamano 1a46d6b
              
                Remove SCSS import statement
              
              
                t-hamano 6577955
              
                Use "isBlockHidden"
              
              
                t-hamano 19b7b9c
              
                Remove useBlockVisibility hook
              
              
                t-hamano b1dad49
              
                useBlockDropZone: Filter out blocks that are hidden
              
              
                t-hamano 6e42b65
              
                Remove useBlockVisibility hook
              
              
                t-hamano 7849d2f
              
                Update doc
              
              
                t-hamano 2bfa54e
              
                Show hidden block placeholder when dragging
              
              
                t-hamano 913ad06
              
                Revert "Show hidden block placeholder when dragging"
              
              
                t-hamano 8c5c625
              
                Show block toolbar button only when editing mode is default
              
              
                t-hamano d8dd684
              
                Merge branch 'trunk' into block-visibility
              
              
                t-hamano fb2d668
              
                Process metadata updates for multiple blocks at once
              
              
                t-hamano 03c1826
              
                Preserve block visibility metadata in block transforms
              
              
                t-hamano 9c12705
              
                Merge branch 'trunk' into block-visibility
              
              
                t-hamano a78bdd5
              
                Remove support from schema and docs
              
              
                t-hamano 599ed80
              
                Add experiments setting
              
              
                t-hamano 6c04b64
              
                Make Block Visibility as experiment feature
              
              
                t-hamano e54603e
              
                Rename __experimentalBlockVisibility to blockVisibility
              
              
                t-hamano 854b5d5
              
                Merge branch 'trunk' into block-visibility
              
              
                t-hamano 3b152ca
              
                Fix: other metadata attributes get stripped when toggling via the too…
              
              
                t-hamano fffcf35
              
                Show block toolbar button when multiple blocks are selected
              
              
                t-hamano 1c9b407
              
                Fix incorrect block.json definition
              
              
                t-hamano 5596f83
              
                Apply opacity to hidden and selected blocks
              
              
                t-hamano c39d72a
              
                Revert "Apply opacity to hidden and selected blocks"
              
              
                t-hamano 7de5b65
              
                Provide blockVilibility metadata to preview context
              
              
                t-hamano f773c15
              
                Accordion Header, Accordion Panel: disable blockVisibility support
              
              
                t-hamano f36f529
              
                Visually hide block
              
              
                t-hamano f5c1572
              
                Merge branch 'trunk' into block-visibility
              
              
                t-hamano bb9ec37
              
                Stabilize feature
              
              
                t-hamano 4c7338c
              
                Add backport changelog
              
              
                t-hamano 861689c
              
                Update server side function
              
              
                t-hamano 0929559
              
                Use "visibility:hidden" to visually hide blocks
              
              
                t-hamano dd0d1cb
              
                Combine multiple useSelect call into one
              
              
                t-hamano 96428fd
              
                Reset styles from hidden blocks
              
              
                t-hamano 465777d
              
                Update lib/block-supports/block-visibility.php
              
              
                t-hamano ff7a8d5
              
                Better behavior for hidden blocks
              
              
                t-hamano 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
        
    Next
      
          
      
  
Next commit 
    
       Block API: Add block visibility control support and UI
    
  - Loading branch information
    
    
      commit 6436944dfa438f7f4eb1c2796bbec18d9cbbee6f
    
  
          Some comments aren't visible on the classic Files Changed page.
        
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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
| /** | ||
| * Block visibility block support flag. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|  | ||
| /** | ||
| * Render nothing if the block is hidden. | ||
| * | ||
| * @param string $block_content The block content. | ||
| * @param array $block The block. | ||
| * | ||
| * @return string The block content. | ||
| */ | ||
| function gutenberg_render_block_visibility_support( $block_content, $block ) { | ||
| if ( isset( $block['attrs']['metadata']['blockVisibility'] ) && false === $block['attrs']['metadata']['blockVisibility'] ) { | ||
| return ''; | ||
| } | ||
|  | ||
| return $block_content; | ||
| } | ||
|  | ||
| if ( function_exists( 'wp_render_block_visibility_support' ) ) { | ||
| remove_filter( 'render_block', 'wp_render_block_visibility_support' ); | ||
| } | ||
| add_filter( 'render_block', 'gutenberg_render_block_visibility_support', 10, 2 ); | 
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            3 changes: 3 additions & 0 deletions
          
          3 
        
  packages/block-editor/src/components/block-visibility/index.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,3 @@ | ||
| export { default as BlockVisibilityMenuItem } from './menu-item'; | ||
| export { default as BlockVisibilityToolbar } from './toolbar'; | ||
| export { default as useBlockVisibility } from './use-block-visibility'; | 
        
          
  
    
      
          
            47 changes: 47 additions & 0 deletions
          
          47 
        
  packages/block-editor/src/components/block-visibility/menu-item.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,47 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { MenuItem } from '@wordpress/components'; | ||
| import { seen, unseen } from '@wordpress/icons'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
|  | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { cleanEmptyObject } from '../../hooks/utils'; | ||
| import { store as blockEditorStore } from '../../store'; | ||
|  | ||
| export default function BlockVisibilityMenuItem( { clientIds } ) { | ||
| const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
| const blocks = useSelect( | ||
| ( select ) => { | ||
| return select( blockEditorStore ).getBlocksByClientId( clientIds ); | ||
| }, | ||
| [ clientIds ] | ||
| ); | ||
|  | ||
| const hasHiddenBlock = blocks.some( | ||
| ( block ) => block.attributes.metadata?.blockVisibility === false | ||
| ); | ||
|  | ||
| const toggleBlockVisibility = () => { | ||
| blocks.forEach( ( block ) => { | ||
| updateBlockAttributes( block.clientId, { | ||
| metadata: cleanEmptyObject( { | ||
| ...block.attributes?.metadata, | ||
| blockVisibility: hasHiddenBlock ? undefined : false, | ||
| } ), | ||
| } ); | ||
| } ); | ||
| }; | ||
|         
                  t-hamano marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| return ( | ||
| <MenuItem | ||
| icon={ hasHiddenBlock ? seen : unseen } | ||
| onClick={ toggleBlockVisibility } | ||
| > | ||
| { hasHiddenBlock ? __( 'Show' ) : __( 'Hide' ) } | ||
| </MenuItem> | ||
| ); | ||
| } | ||
        
          
  
    
      
          
            61 changes: 61 additions & 0 deletions
          
          61 
        
  packages/block-editor/src/components/block-visibility/toolbar.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,61 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; | ||
| import { useRef, useEffect } from '@wordpress/element'; | ||
| import { seen, unseen } from '@wordpress/icons'; | ||
| import { useDispatch } from '@wordpress/data'; | ||
|  | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import useBlockVisibility from './use-block-visibility'; | ||
| import { store as blockEditorStore } from '../../store'; | ||
|  | ||
| export default function BlockVisibilityToolbar( { clientId } ) { | ||
| const { canToggleBlockVisibility, isBlockVisible } = | ||
| useBlockVisibility( clientId ); | ||
|  | ||
| const hasBlockVisibilityButtonShownRef = useRef( false ); | ||
| const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
|  | ||
| // If the block visibility button has been shown, we don't want to | ||
| // remove it from the toolbar until the toolbar is rendered again | ||
| // without it. Removing it beforehand can cause focus loss issues. | ||
| // It needs to return focus from whence it came, and to do that, | ||
| // we need to leave the button in the toolbar. | ||
| useEffect( () => { | ||
| if ( ! isBlockVisible ) { | ||
| hasBlockVisibilityButtonShownRef.current = true; | ||
| } | ||
| }, [ isBlockVisible ] ); | ||
|  | ||
| if ( isBlockVisible && ! hasBlockVisibilityButtonShownRef.current ) { | ||
| return null; | ||
| } | ||
|  | ||
| const label = isBlockVisible ? __( 'Hide' ) : __( 'Show' ); | ||
|  | ||
| return ( | ||
| <> | ||
| <ToolbarGroup className="block-editor-block-lock-toolbar"> | ||
| <ToolbarButton | ||
| disabled={ ! canToggleBlockVisibility } | ||
| icon={ isBlockVisible ? seen : unseen } | ||
| label={ label } | ||
| onClick={ () => { | ||
| const newBlockVisibility = ! isBlockVisible; | ||
| updateBlockAttributes( [ clientId ], { | ||
| metadata: { | ||
| blockVisibility: newBlockVisibility | ||
| ? undefined | ||
| : false, | ||
| }, | ||
| } ); | ||
| } } | ||
| /> | ||
| </ToolbarGroup> | ||
| </> | ||
| ); | ||
| } | 
      
      Oops, something went wrong.
        
    
  
      
      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.
Maybe we should change this to
visibilityand the block part, which seems redundant.P.S. The block editor already uses similar terminology,
isBlockVisibleandsetBlockVisibility, to track if the block is visible in the canvas viewport. It would be nice to avoid confusion, but I can't think of better terminology.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 forgot about
isBlockVisible- thanks for pointing it out. I originally suggested something other than visibility as it's too close the name of a CSS property, and could clash semantically if it block supports ever gets it.We could remove the ambiguity and select a different word. The current
isBlockVisibletells the system whether the block is visible, whereas we're talking about a user-driven setting. What does the user want to do? temporarily hide blocks (otherwise they'd be deleted) and to stop them rendering on the frontend. It's a bit nuanced, swinging between _ prevent from being seen_ and switch off.Some alternatives
There might be a PhD thesis here somewhere 😆
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 don't have a strong opinion on which terminology (
visibilityorblockVisibility) is better, but the existingisBlockVisibleis indeed confusing. There is a proposal to rename the API: #71203 (comment)