-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Tabs: Remove editor-only block context #79848
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
+81
−130
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3013dc7
Tabs: Remove editor-only block context in favor of local derivation
t-hamano e42bae4
Tabs: Simplify tab-panel visibility to a derived boolean
t-hamano ec6fe8f
Tabs: Drop redundant comment in tab-list derivation
t-hamano 0c72520
Fix useSelect warning
Mamaduka 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ import { | |
| store as blockEditorStore, | ||
| } from '@wordpress/block-editor'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { useMemo, useEffect } from '@wordpress/element'; | ||
| import { useEffect } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -24,25 +24,30 @@ const TEMPLATE = [ | |
| ], | ||
| ]; | ||
|
|
||
| export default function Edit( { clientId, context, isSelected } ) { | ||
| // Consume tab indices from context | ||
| const activeTabIndex = context[ 'core/tabs-activeTabIndex' ]; | ||
| const editorActiveTabIndex = context[ 'core/tabs-editorActiveTabIndex' ]; | ||
| const effectiveActiveIndex = editorActiveTabIndex ?? activeTabIndex; | ||
|
|
||
| const { blockIndex, hasInnerBlocksSelected, tabsClientId } = useSelect( | ||
| export default function Edit( { clientId, isSelected } ) { | ||
| const { | ||
| activeTabIndex, | ||
| editorActiveTabIndex, | ||
| blockIndex, | ||
| hasInnerBlocksSelected, | ||
| tabsClientId, | ||
| } = useSelect( | ||
| ( select ) => { | ||
| const { | ||
| getBlockRootClientId, | ||
| getBlockIndex, | ||
| hasSelectedInnerBlock, | ||
| getBlockAttributes, | ||
| } = select( blockEditorStore ); | ||
|
|
||
| // Get the tab-panel parent first | ||
| const tabPanelsClientId = getBlockRootClientId( clientId ); | ||
| // Then get the tabs parent | ||
| const _tabsClientId = getBlockRootClientId( tabPanelsClientId ); | ||
|
|
||
| // Read the active tab indices directly from the tabs block. | ||
| const tabsAttributes = getBlockAttributes( _tabsClientId ) ?? {}; | ||
|
|
||
| // Get data about this instance of core/tab. | ||
| const _blockIndex = getBlockIndex( clientId ); | ||
| const _hasInnerBlocksSelected = hasSelectedInnerBlock( | ||
|
|
@@ -51,6 +56,8 @@ export default function Edit( { clientId, context, isSelected } ) { | |
| ); | ||
|
|
||
| return { | ||
| activeTabIndex: tabsAttributes.activeTabIndex, | ||
| editorActiveTabIndex: tabsAttributes.editorActiveTabIndex, | ||
| blockIndex: _blockIndex, | ||
| hasInnerBlocksSelected: _hasInnerBlocksSelected, | ||
| tabsClientId: _tabsClientId, | ||
|
|
@@ -59,6 +66,8 @@ export default function Edit( { clientId, context, isSelected } ) { | |
| [ clientId ] | ||
| ); | ||
|
|
||
| const effectiveActiveIndex = editorActiveTabIndex ?? activeTabIndex; | ||
|
|
||
| const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } = | ||
| useDispatch( blockEditorStore ); | ||
|
|
||
|
|
@@ -93,22 +102,8 @@ export default function Edit( { clientId, context, isSelected } ) { | |
| // Determine if this is the default tab (for the "Default Tab" toggle in controls) | ||
| const isDefaultTab = activeTabIndex === blockIndex; | ||
|
|
||
| /** | ||
| * This hook determines if the current tab panel should be visible. | ||
| * This is true if it is the editor active tab, or if it is selected directly. | ||
| */ | ||
| const isSelectedTab = useMemo( () => { | ||
| // Show if this tab is directly selected or has selected inner blocks | ||
| if ( isSelected || hasInnerBlocksSelected ) { | ||
| return true; | ||
| } | ||
| // Always show the active tab (at effectiveActiveIndex) regardless of other selection state. | ||
| // This ensures the tab panel remains visible when editing labels in tab-list. | ||
| if ( isActiveTab ) { | ||
| return true; | ||
| } | ||
| return false; | ||
| }, [ isSelected, hasInnerBlocksSelected, isActiveTab ] ); | ||
| // Visible when selected, containing the selection, or the active tab. | ||
| const isSelectedTab = isSelected || hasInnerBlocksSelected || isActiveTab; | ||
|
Contributor
Author
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. Small cleanup: |
||
|
|
||
| const blockProps = useBlockProps( { | ||
| hidden: ! isSelectedTab, | ||
|
|
||
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
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.
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.
Deriving this value within
useSelectproduced a warning. Fixed via 0c72520.Screenshot