Skip to content

Commit 1b591cf

Browse files
committed
Block Editor: Add a new hook for getting a stable block context object
1 parent 92a40bd commit 1b591cf

2 files changed

Lines changed: 47 additions & 23 deletions

File tree

packages/block-editor/src/components/inner-blocks/index.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { forwardRef, useMemo } from '@wordpress/element';
1111
import { useSelect } from '@wordpress/data';
1212
import {
1313
getBlockSupport,
14-
getBlockType,
1514
store as blocksStore,
1615
__unstableGetInnerBlocksProps as getInnerBlocksProps,
1716
} from '@wordpress/blocks';
@@ -23,7 +22,7 @@ import ButtonBlockAppender from './button-block-appender';
2322
import DefaultBlockAppender from './default-block-appender';
2423
import useNestedSettingsUpdate from './use-nested-settings-update';
2524
import useInnerBlockTemplateSync from './use-inner-block-template-sync';
26-
import getBlockContext from './get-block-context';
25+
import useBlockContext from './use-block-context';
2726
import { BlockListItems } from '../block-list';
2827
import { BlockContextProvider } from '../block-context';
2928
import { useBlockEditContext } from '../block-edit/context';
@@ -75,28 +74,10 @@ function UncontrolledInnerBlocks( props ) {
7574
templateInsertUpdatesSelection
7675
);
7776

78-
const { context, name } = useSelect(
77+
const context = useBlockContext( clientId );
78+
const name = useSelect(
7979
( select ) => {
80-
const block = select( blockEditorStore ).getBlock( clientId );
81-
82-
// This check is here to avoid the Redux zombie bug where a child subscription
83-
// is called before a parent, causing potential JS errors when the child has been removed.
84-
if ( ! block ) {
85-
return {};
86-
}
87-
88-
const blockType = getBlockType( block.name );
89-
90-
if (
91-
Object.keys( blockType?.providesContext ?? {} ).length === 0
92-
) {
93-
return { name: block.name };
94-
}
95-
96-
return {
97-
context: getBlockContext( block.attributes, blockType ),
98-
name: block.name,
99-
};
80+
return select( blockEditorStore ).getBlock( clientId )?.name;
10081
},
10182
[ clientId ]
10283
);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { mapValues } from 'lodash';
5+
6+
/**
7+
* WordPress dependencies
8+
*/
9+
import { getBlockType } from '@wordpress/blocks';
10+
import { useSelect } from '@wordpress/data';
11+
12+
/**
13+
* Internal dependencies
14+
*/
15+
import { store as blockEditorStore } from '../../store';
16+
17+
/**
18+
* Returns a context object for a given block.
19+
*
20+
* @param {string} clientId The block client ID.
21+
*
22+
* @return {Record<string,*>} Context value.
23+
*/
24+
export default function useBlockContext( clientId ) {
25+
return useSelect(
26+
( select ) => {
27+
const block = select( blockEditorStore ).getBlock( clientId );
28+
const blockType = getBlockType( block.name );
29+
30+
if (
31+
Object.keys( blockType?.providesContext ?? {} ).length === 0
32+
) {
33+
return undefined;
34+
}
35+
36+
return mapValues(
37+
blockType.providesContext,
38+
( attributeName ) => block.attributes[ attributeName ]
39+
);
40+
},
41+
[ clientId ]
42+
);
43+
}

0 commit comments

Comments
 (0)