-
Couldn't load subscription status.
- 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
+354
−33
Merged
Changes from 24 commits
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
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
| 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
2 changes: 2 additions & 0 deletions
2
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,2 @@ | ||
| export { default as BlockVisibilityMenuItem } from './menu-item'; | ||
| export { default as BlockVisibilityToolbar } from './toolbar'; |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
| /** | ||
| * 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 = () => { | ||
| const attributesByClientId = Object.fromEntries( | ||
| blocks?.map( ( { clientId, attributes } ) => [ | ||
| clientId, | ||
| { | ||
| metadata: cleanEmptyObject( { | ||
| ...attributes?.metadata, | ||
| blockVisibility: hasHiddenBlock ? undefined : false, | ||
| } ), | ||
| }, | ||
| ] ) | ||
| ); | ||
| updateBlockAttributes( clientIds, attributesByClientId, { | ||
| uniqueByBlock: true, | ||
| } ); | ||
| }; | ||
|
|
||
| return ( | ||
| <MenuItem | ||
| icon={ hasHiddenBlock ? seen : unseen } | ||
| onClick={ toggleBlockVisibility } | ||
| > | ||
| { hasHiddenBlock ? __( 'Show' ) : __( 'Hide' ) } | ||
| </MenuItem> | ||
| ); | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
| /** | ||
| * 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 { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { hasBlockSupport } from '@wordpress/blocks'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../store'; | ||
| import { unlock } from '../../lock-unlock'; | ||
|
|
||
| export default function BlockVisibilityToolbar( { clientId } ) { | ||
| const { canToggleBlockVisibility, isBlockHidden } = useSelect( | ||
| ( select ) => { | ||
| const { getBlockName } = select( blockEditorStore ); | ||
| const { isBlockHidden: _isBlockHidden } = unlock( | ||
| select( blockEditorStore ) | ||
| ); | ||
| return { | ||
| canToggleBlockVisibility: hasBlockSupport( | ||
| getBlockName( clientId ), | ||
| 'blockVisibility', | ||
| true | ||
| ), | ||
| isBlockHidden: _isBlockHidden( clientId ), | ||
| }; | ||
| }, | ||
| [ 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 ( isBlockHidden ) { | ||
| hasBlockVisibilityButtonShownRef.current = true; | ||
| } | ||
| }, [ isBlockHidden ] ); | ||
|
|
||
| if ( ! isBlockHidden && ! hasBlockVisibilityButtonShownRef.current ) { | ||
| return null; | ||
| } | ||
|
|
||
| const label = isBlockHidden ? __( 'Show' ) : __( 'Hide' ); | ||
|
|
||
| return ( | ||
| <> | ||
| <ToolbarGroup className="block-editor-block-lock-toolbar"> | ||
| <ToolbarButton | ||
| disabled={ ! canToggleBlockVisibility } | ||
| icon={ isBlockHidden ? unseen : seen } | ||
| label={ label } | ||
| onClick={ () => { | ||
| const newBlockHidden = ! isBlockHidden; | ||
| updateBlockAttributes( [ clientId ], { | ||
| metadata: { | ||
| blockVisibility: newBlockHidden | ||
| ? false | ||
| : undefined, | ||
| }, | ||
t-hamano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } ); | ||
| } } | ||
| /> | ||
| </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.
Uh oh!
There was an error while loading. Please reload this page.