Skip to content

Commit

Permalink
Retain block preview context.
Browse files Browse the repository at this point in the history
  • Loading branch information
apeatling committed Jan 27, 2023
1 parent 4445590 commit 19b93a8
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions packages/block-library/src/query/edit/pattern-selection-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useDispatch } from '@wordpress/data';
import { Modal, SearchControl } from '@wordpress/components';
import { useAsyncList } from '@wordpress/compose';
import {
BlockContextProvider,
store as blockEditorStore,
__experimentalBlockPatternsList as BlockPatternsList,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -38,6 +39,14 @@ export default function PatternSelectionModal( {
selectBlock( queryClientIds[ 0 ] );
}
};
// When we preview Query Loop blocks we should prefer the current
// block's postType, which is passed through block context.
const blockPreviewContext = useMemo(
() => ( {
previewPostType: attributes.query.postType,
} ),
[ attributes.query.postType ]
);
const blockNameForPatterns = useBlockNameForPatterns(
clientId,
attributes
Expand All @@ -55,22 +64,24 @@ export default function PatternSelectionModal( {
closeLabel={ __( 'Cancel' ) }
onRequestClose={ () => setIsPatternSelectionModalOpen( false ) }
>
<div className="block-library-query-pattern__selection-content">
<div className="block-library-query-pattern__selection-search">
<SearchControl
__nextHasNoMarginBottom
onChange={ setSearchValue }
value={ searchValue }
label={ __( 'Search for patterns' ) }
placeholder={ __( 'Search' ) }
<BlockContextProvider value={ blockPreviewContext }>
<div className="block-library-query-pattern__selection-content">
<div className="block-library-query-pattern__selection-search">
<SearchControl
__nextHasNoMarginBottom
onChange={ setSearchValue }
value={ searchValue }
label={ __( 'Search for patterns' ) }
placeholder={ __( 'Search' ) }
/>
</div>
<BlockPatternsList
blockPatterns={ filteredBlockPatterns }
shownPatterns={ shownBlockPatterns }
onClickPattern={ onBlockPatternSelect }
/>
</div>
<BlockPatternsList
blockPatterns={ filteredBlockPatterns }
shownPatterns={ shownBlockPatterns }
onClickPattern={ onBlockPatternSelect }
/>
</div>
</BlockContextProvider>
</Modal>
);
}

0 comments on commit 19b93a8

Please sign in to comment.