Skip to content

Commit f9c1c75

Browse files
Style states: fix exception when there's no selected block (#84140)
* Block editor: Prevent getSelectedBlockStyleState selector from throwing an exception when no clientId is undefined * Avoid passing `undefined` to selector * Add unit test * changelog ---- Co-authored-by: talldan <talldanwp@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
1 parent 6a30898 commit f9c1c75

5 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎packages/block-editor/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- `BlockCompare`: Show whitespace differences in the Resolve Block dialog. The markup's spaces and blank lines are kept instead of collapsed by the browser, and each added or removed line break gets a visible marker, so a diff made only of whitespace no longer looks identical on both sides. The dialog also shows the markup of a converted Custom HTML block, which is kept in its `innerContent` rather than produced by `save`, instead of an empty diff ([#82397](https://github.com/WordPress/gutenberg/pull/82397)).
2222
- Inserter: Stop re-rendering the block list when the block selection changes while the inserter is open ([#83284](https://github.com/WordPress/gutenberg/pull/83284)).
2323
- Fix style edits applied to a multi-block selection overwriting each block's other styles. Only the changed values are now applied to every selected block, so distinct styles are preserved and resets propagate to the whole selection ([#80070](https://github.com/WordPress/gutenberg/pull/80070)).
24+
- Fix exception thrown by `getSelectedBlockStyleState` when an `undefined` `clientId` is passed as an argument. ([#84140](https://github.com/WordPress/gutenberg/pull/84140))
2425

2526
### Internal
2627

‎packages/block-editor/src/components/block-inspector/index.jsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ function BlockInspector() {
219219
hasBlockStyles: _hasBlockStyles,
220220
editedContentOnlySection: getEditedContentOnlySection(),
221221
blockEditingMode: getBlockEditingMode( _renderedBlockClientId ),
222-
selectedBlockStyleState: getSelectedBlockStyleState(
223-
_renderedBlockClientId
224-
),
222+
selectedBlockStyleState: _renderedBlockClientId
223+
? getSelectedBlockStyleState( _renderedBlockClientId )
224+
: undefined,
225225
showStateOnCanvas: isSelectedBlockStyleStateShownOnCanvas(
226226
_renderedBlockClientId
227227
),

‎packages/block-editor/src/components/block-toolbar/index.jsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export function PrivateBlockToolbar( {
133133
isBlockHiddenAtViewport( id, _currentDeviceType )
134134
);
135135
const _isEditingResponsiveStyleState =
136+
!! selectedBlockClientId &&
136137
isResponsiveEditing() &&
137138
hasViewportBlockStyleState(
138139
getSelectedBlockStyleState( selectedBlockClientId )

‎packages/block-editor/src/store/selectors.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,8 +3629,8 @@ export function __unstableGetTemporarilyEditingAsBlocks( state ) {
36293629
export const getSelectedBlockStyleState = createSelector(
36303630
( state, clientId ) => {
36313631
const perBlockState =
3632-
state.selectedBlockStyleState?.clientId === clientId
3633-
? ( state.selectedBlockStyleState.value ??
3632+
clientId && state.selectedBlockStyleState?.clientId === clientId
3633+
? ( state.selectedBlockStyleState?.value ??
36343634
DEFAULT_BLOCK_STYLE_STATE )
36353635
: DEFAULT_BLOCK_STYLE_STATE;
36363636

‎packages/block-editor/src/store/test/selectors.jsdom.test.jsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5581,6 +5581,15 @@ describe( 'getBlockEditingMode', () => {
55815581
} );
55825582

55835583
describe( 'getSelectedBlockStyleState', () => {
5584+
it( 'returns default when no clientId is passed', () => {
5585+
const state = {};
5586+
5587+
expect( getSelectedBlockStyleState( state, undefined ) ).toEqual( {
5588+
viewport: 'default',
5589+
pseudo: 'default',
5590+
} );
5591+
} );
5592+
55845593
it( 'returns default when the block has no selected state', () => {
55855594
const state = {};
55865595

0 commit comments

Comments
 (0)