Skip to content

Commit

Permalink
Merge pull request #1664 from tomusborne/tweak/select-loop-item-on-co…
Browse files Browse the repository at this point in the history
…ntext-change

Tweak: Select Loop Item on context change
  • Loading branch information
tomusborne authored Jan 15, 2025
2 parents f46c86b + 0d335ee commit 83a8900
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/blocks/looper/components/LoopInnerBlocksRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
__experimentalUseBlockPreview as useBlockPreview, // eslint-disable-line @wordpress/no-unsafe-wp-apis
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { Spinner } from '@wordpress/components';
import { memo, useEffect, useMemo, useState } from '@wordpress/element';
import { applyFilters, addAction } from '@wordpress/hooks';
Expand Down Expand Up @@ -131,6 +131,7 @@ export function LoopInnerBlocksRenderer( props ) {
queryParams: [],
};
const { getSelectedBlock } = useSelect( blockEditorStore );
const { selectBlock } = useDispatch( blockEditorStore );
const selectedBlock = getSelectedBlock();
const wpQuery = useWpQuery( 'WP_Query' === queryType, { query, context, queryType, attributes, selectedBlock } );

Expand Down Expand Up @@ -169,7 +170,13 @@ export function LoopInnerBlocksRenderer( props ) {
addAction(
'generateblocks.editor.loopItem.togglePreview',
'generateblocks/looper/togglePreview',
setPreviewId
( id, blockProps ) => {
setPreviewId( id );

if ( blockProps.clientId ) {
selectBlock( blockProps.clientId );
}
}
);

const innerBlocksProps = useInnerBlocksProps(
Expand Down

0 comments on commit 83a8900

Please sign in to comment.