Repository navigation
Navigation: Use areInnerBlocksControlled to detect controlled inner blocks
#87096
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
mikachan
merged 5 commits into
trunk
from
update/navigation-are-inner-blocks-controlled
Nov 19, 2026
+38
−48
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
519a1c7
Use areInnerBlocksControlled
mikachan cf3625b
Remove .js suppression
mikachan 8dcd51d
Navigation: Add useInnerBlocks tests for empty menus, stale tree entr…
getdave 0bccfb2
Derive values from single hook
mikachan e8b0130
Remove jsdom tests
mikachan 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
113 changes: 113 additions & 0 deletions
113
packages/block-library/src/navigation/edit/test/use-inner-blocks.jsdom.test.tsx
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,113 @@ | ||
| import type { ReactNode } from 'react'; | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { | ||
| createBlock, | ||
| registerBlockType, | ||
| unregisterBlockType, | ||
| } from '@wordpress/blocks'; | ||
| import type { Block } from '@wordpress/blocks'; | ||
| import { createRegistry, RegistryProvider } from '@wordpress/data'; | ||
| import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
| import { useInnerBlocks } from '../use-inner-blocks'; | ||
|
|
||
| type Registry = ReturnType< typeof createRegistry >; | ||
|
|
||
| const BLOCK_NAMES = [ | ||
| 'core/navigation', | ||
| 'core/navigation-link', | ||
| 'core/page-list', | ||
| ]; | ||
|
|
||
| function setup( innerBlocks: Block[] = [] ) { | ||
| const registry = createRegistry(); | ||
| registry.register( blockEditorStore ); | ||
|
|
||
| const navigationBlock = createBlock( 'core/navigation', {}, innerBlocks ); | ||
| registry.dispatch( blockEditorStore ).resetBlocks( [ navigationBlock ] ); | ||
|
|
||
| const { result } = renderHook( | ||
| () => useInnerBlocks( navigationBlock.clientId ), | ||
| { | ||
| wrapper: ( { children }: { children: ReactNode } ) => ( | ||
| <RegistryProvider value={ registry }> | ||
| { children } | ||
| </RegistryProvider> | ||
| ), | ||
| } | ||
| ); | ||
|
|
||
| return { registry, clientId: navigationBlock.clientId, result }; | ||
| } | ||
|
|
||
| function controlInnerBlocks( | ||
| registry: Registry, | ||
| clientId: string, | ||
| blocks: Block[] | ||
| ) { | ||
| const { setHasControlledInnerBlocks, replaceInnerBlocks } = | ||
| registry.dispatch( blockEditorStore ); | ||
| act( () => { | ||
| registry.batch( () => { | ||
| setHasControlledInnerBlocks( clientId, true ); | ||
| replaceInnerBlocks( clientId, blocks ); | ||
| } ); | ||
| } ); | ||
| } | ||
|
|
||
| describe( 'useInnerBlocks', () => { | ||
| beforeAll( () => { | ||
| BLOCK_NAMES.forEach( ( name ) => | ||
| registerBlockType( name, { | ||
| apiVersion: 3, | ||
| title: name, | ||
| category: 'design', | ||
| attributes: {}, | ||
| save: () => null, | ||
| } ) | ||
| ); | ||
| } ); | ||
|
|
||
| afterAll( () => { | ||
| BLOCK_NAMES.forEach( ( name ) => unregisterBlockType( name ) ); | ||
| } ); | ||
|
|
||
| it( 'returns no inner blocks for an empty Navigation block', () => { | ||
| const { result } = setup(); | ||
|
|
||
| expect( result.current ).toMatchObject( { | ||
| innerBlocks: [], | ||
| hasUncontrolledInnerBlocks: false, | ||
| uncontrolledInnerBlocks: [], | ||
| controlledInnerBlocks: [], | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'returns uncontrolled inner blocks', () => { | ||
| const { result } = setup( [ createBlock( 'core/page-list' ) ] ); | ||
|
|
||
| expect( result.current ).toMatchObject( { | ||
| innerBlocks: [ { name: 'core/page-list' } ], | ||
| hasUncontrolledInnerBlocks: true, | ||
| uncontrolledInnerBlocks: [ { name: 'core/page-list' } ], | ||
| controlledInnerBlocks: [], | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'returns controlled inner blocks once uncontrolled ones are replaced', () => { | ||
| const { registry, clientId, result } = setup( [ | ||
| createBlock( 'core/page-list' ), | ||
| ] ); | ||
|
|
||
| controlInnerBlocks( registry, clientId, [ | ||
| createBlock( 'core/navigation-link' ), | ||
| ] ); | ||
|
|
||
| expect( result.current ).toMatchObject( { | ||
| innerBlocks: [ { name: 'core/navigation-link' } ], | ||
| hasUncontrolledInnerBlocks: false, | ||
| uncontrolledInnerBlocks: [], | ||
| controlledInnerBlocks: [ { name: 'core/navigation-link' } ], | ||
| } ); | ||
| } ); | ||
| } ); |
36 changes: 0 additions & 36 deletions
36
packages/block-library/src/navigation/edit/use-inner-blocks.js
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
packages/block-library/src/navigation/edit/use-inner-blocks.ts
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,48 @@ | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
| import type { Block } from '@wordpress/blocks'; | ||
|
|
||
| const EMPTY_ARRAY: Block[] = []; | ||
|
|
||
| interface InnerBlocks { | ||
| innerBlocks: Block[]; | ||
| hasUncontrolledInnerBlocks: boolean; | ||
| uncontrolledInnerBlocks: Block[]; | ||
| controlledInnerBlocks: Block[]; | ||
| isInnerBlockSelected: boolean; | ||
| } | ||
|
|
||
| export function useInnerBlocks( clientId: string ): InnerBlocks { | ||
| return useSelect( | ||
| ( select ) => { | ||
| const { | ||
| areInnerBlocksControlled, | ||
| getBlocks, | ||
| hasSelectedInnerBlock, | ||
| } = select( blockEditorStore ); | ||
|
|
||
| // Inner blocks become controlled once they are synced with a | ||
| // `wp_navigation` entity. Until then, any inner blocks belong to | ||
| // the Navigation block itself. | ||
| const _innerBlocks = getBlocks( clientId ); | ||
| const _areInnerBlocksControlled = | ||
| areInnerBlocksControlled( clientId ); | ||
|
|
||
| const _hasUncontrolledInnerBlocks = | ||
| ! _areInnerBlocksControlled && !! _innerBlocks.length; | ||
|
|
||
| return { | ||
| innerBlocks: _innerBlocks, | ||
| hasUncontrolledInnerBlocks: _hasUncontrolledInnerBlocks, | ||
| uncontrolledInnerBlocks: _areInnerBlocksControlled | ||
| ? EMPTY_ARRAY | ||
| : _innerBlocks, | ||
| controlledInnerBlocks: _areInnerBlocksControlled | ||
| ? _innerBlocks | ||
| : EMPTY_ARRAY, | ||
| isInnerBlockSelected: hasSelectedInnerBlock( clientId, true ), | ||
|
mikachan marked this conversation as resolved.
Resolved
|
||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
| } | ||
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.