Skip to content

Commit

Permalink
Fix: Loop item bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusborne committed Dec 17, 2024
1 parent 2698b44 commit 0fcb5b9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/blocks/loop-item/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"generateblocks/loopIndex",
"generateblocks/loopItem",
"generateblocks/loopPreviewId",
"generateblocks/hasLoopItems",
"postId"
]
}
6 changes: 5 additions & 1 deletion src/blocks/loop-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { BlockStylesBuilder, BlockAppender } from '@components/index';
import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js';
import { getBlockClasses } from '@utils/getBlockClasses';

import './editor.scss';

function EditBlock( props ) {
const {
attributes,
Expand Down Expand Up @@ -49,6 +51,7 @@ function EditBlock( props ) {
if ( isBlockPreview ) {
classNames.push( 'gb-block-preview' );
}

const blockProps = useBlockProps( {
className: classNames.join( ' ' ).trim(),
...editorHtmlAttributes,
Expand Down Expand Up @@ -105,6 +108,7 @@ function EditBlock( props ) {

const contextPostId = context?.postId ?? context?.[ 'generateblocks/loopIndex' ] ?? 0;
const previewId = context?.[ 'generateblocks/loopPreviewId' ] ?? 0;
const hasLoopItems = context?.[ 'generateblocks/hasLoopItems' ] ?? false;

return (
<>
Expand All @@ -128,7 +132,7 @@ function EditBlock( props ) {
</InspectorControls>
<TagName { ...otherInnerBlocksProps }>
{ innerBlocksChildren }
{ previewId !== contextPostId && (
{ !! hasLoopItems && previewId !== contextPostId && (
<button
className="gb-block-preview__toggle"
data-block-id={ clientId }
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/loop-item/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wp-block-generateblocks-loop-item {
min-height: 35px;
}
2 changes: 2 additions & 0 deletions src/blocks/looper/components/LoopInnerBlocksRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export function LoopInnerBlocksRenderer( props ) {
'generateblocks/loopItem': item,
'generateblocks/loopIndex': index + 1, // Preview doesn't support pagination so this index is correct.
'generateblocks/loopPreviewId': previewId,
'generateblocks/hasLoopItems': true,
};
} );

Expand Down Expand Up @@ -247,6 +248,7 @@ export function LoopInnerBlocksRenderer( props ) {
},
'generateblocks/loopIndex': 1,
'generateblocks/loopPreviewId': previewId,
'generateblocks/hasLoopItems': false,
} ];
}, [ data, hasResolvedData, query?.posts_per_page, query?.offset, previewId ] );

Expand Down

0 comments on commit 0fcb5b9

Please sign in to comment.