Skip to content

Commit 19b93a8

Browse files
committed
Retain block preview context.
1 parent 4445590 commit 19b93a8

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

packages/block-library/src/query/edit/pattern-selection-modal.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useDispatch } from '@wordpress/data';
66
import { Modal, SearchControl } from '@wordpress/components';
77
import { useAsyncList } from '@wordpress/compose';
88
import {
9+
BlockContextProvider,
910
store as blockEditorStore,
1011
__experimentalBlockPatternsList as BlockPatternsList,
1112
} from '@wordpress/block-editor';
@@ -38,6 +39,14 @@ export default function PatternSelectionModal( {
3839
selectBlock( queryClientIds[ 0 ] );
3940
}
4041
};
42+
// When we preview Query Loop blocks we should prefer the current
43+
// block's postType, which is passed through block context.
44+
const blockPreviewContext = useMemo(
45+
() => ( {
46+
previewPostType: attributes.query.postType,
47+
} ),
48+
[ attributes.query.postType ]
49+
);
4150
const blockNameForPatterns = useBlockNameForPatterns(
4251
clientId,
4352
attributes
@@ -55,22 +64,24 @@ export default function PatternSelectionModal( {
5564
closeLabel={ __( 'Cancel' ) }
5665
onRequestClose={ () => setIsPatternSelectionModalOpen( false ) }
5766
>
58-
<div className="block-library-query-pattern__selection-content">
59-
<div className="block-library-query-pattern__selection-search">
60-
<SearchControl
61-
__nextHasNoMarginBottom
62-
onChange={ setSearchValue }
63-
value={ searchValue }
64-
label={ __( 'Search for patterns' ) }
65-
placeholder={ __( 'Search' ) }
67+
<BlockContextProvider value={ blockPreviewContext }>
68+
<div className="block-library-query-pattern__selection-content">
69+
<div className="block-library-query-pattern__selection-search">
70+
<SearchControl
71+
__nextHasNoMarginBottom
72+
onChange={ setSearchValue }
73+
value={ searchValue }
74+
label={ __( 'Search for patterns' ) }
75+
placeholder={ __( 'Search' ) }
76+
/>
77+
</div>
78+
<BlockPatternsList
79+
blockPatterns={ filteredBlockPatterns }
80+
shownPatterns={ shownBlockPatterns }
81+
onClickPattern={ onBlockPatternSelect }
6682
/>
6783
</div>
68-
<BlockPatternsList
69-
blockPatterns={ filteredBlockPatterns }
70-
shownPatterns={ shownBlockPatterns }
71-
onClickPattern={ onBlockPatternSelect }
72-
/>
73-
</div>
84+
</BlockContextProvider>
7485
</Modal>
7586
);
7687
}

0 commit comments

Comments
 (0)