@wordpress/block-library 7.3.0 → 7.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,9 +2,9 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { useSelect, useDispatch } from '@wordpress/data';
5
- import { cloneBlock } from '@wordpress/blocks';
5
+ import { store as blocksStore, cloneBlock } from '@wordpress/blocks';
6
6
  import { useInstanceId } from '@wordpress/compose';
7
- import { useEffect } from '@wordpress/element';
7
+ import { useState, useEffect } from '@wordpress/element';
8
8
  import {
9
9
  BlockControls,
10
10
  InspectorControls,
@@ -12,9 +12,15 @@ import {
12
12
  useSetting,
13
13
  store as blockEditorStore,
14
14
  useInnerBlocksProps,
15
+ __experimentalGetMatchingVariation as getMatchingVariation,
15
16
  __experimentalBlockPatternSetup as BlockPatternSetup,
16
17
  } from '@wordpress/block-editor';
17
- import { SelectControl } from '@wordpress/components';
18
+ import {
19
+ Button,
20
+ SelectControl,
21
+ Placeholder,
22
+ Modal,
23
+ } from '@wordpress/components';
18
24
  import { __ } from '@wordpress/i18n';
19
25
 
20
26
  /**
@@ -27,7 +33,11 @@ import { DEFAULTS_POSTS_PER_PAGE } from '../constants';
27
33
  import { getFirstQueryClientIdFromBlocks } from '../utils';
28
34
 
29
35
  const TEMPLATE = [ [ 'core/post-template' ] ];
30
- export function QueryContent( { attributes, setAttributes } ) {
36
+ export function QueryContent( {
37
+ attributes,
38
+ setAttributes,
39
+ openPatternSelectionModal,
40
+ } ) {
31
41
  const {
32
42
  queryId,
33
43
  query,
@@ -102,6 +112,7 @@ export function QueryContent( { attributes, setAttributes } ) {
102
112
  attributes={ attributes }
103
113
  setQuery={ updateQuery }
104
114
  setDisplayLayout={ updateDisplayLayout }
115
+ openPatternSelectionModal={ openPatternSelectionModal }
105
116
  />
106
117
  </BlockControls>
107
118
  <InspectorControls __experimentalGroup="advanced">
@@ -124,43 +135,131 @@ export function QueryContent( { attributes, setAttributes } ) {
124
135
  );
125
136
  }
126
137
 
127
- function QueryPatternSetup( props ) {
128
- const { clientId, name: blockName } = props;
138
+ function QueryPatternSetup( {
139
+ attributes,
140
+ clientId,
141
+ name,
142
+ openPatternSelectionModal,
143
+ setAttributes,
144
+ } ) {
145
+ const [ isStartingBlank, setIsStartingBlank ] = useState( false );
129
146
  const blockProps = useBlockProps();
130
- const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
131
- const onBlockPatternSelect = ( blocks ) => {
132
- const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
133
- const firstQueryClientId = getFirstQueryClientIdFromBlocks(
134
- clonedBlocks
147
+
148
+ const { blockType, allVariations, hasPatterns } = useSelect(
149
+ ( select ) => {
150
+ const { getBlockVariations, getBlockType } = select( blocksStore );
151
+ const {
152
+ getBlockRootClientId,
153
+ __experimentalGetPatternsByBlockTypes,
154
+ } = select( blockEditorStore );
155
+ const rootClientId = getBlockRootClientId( clientId );
156
+
157
+ return {
158
+ blockType: getBlockType( name ),
159
+ allVariations: getBlockVariations( name ),
160
+ hasPatterns: !! __experimentalGetPatternsByBlockTypes(
161
+ name,
162
+ rootClientId
163
+ ).length,
164
+ };
165
+ },
166
+ [ name, clientId ]
167
+ );
168
+
169
+ const matchingVariation = getMatchingVariation( attributes, allVariations );
170
+ const icon = matchingVariation?.icon || blockType?.icon?.src;
171
+ const label = matchingVariation?.title || blockType?.title;
172
+ if ( isStartingBlank ) {
173
+ return (
174
+ <QueryPlaceholder
175
+ clientId={ clientId }
176
+ name={ name }
177
+ setAttributes={ setAttributes }
178
+ icon={ icon }
179
+ label={ label }
180
+ />
135
181
  );
136
- replaceBlock( clientId, clonedBlocks );
137
- if ( firstQueryClientId ) {
138
- selectBlock( firstQueryClientId );
139
- }
140
- };
141
- // `startBlankComponent` is what to render when clicking `Start blank`
142
- // or if no matched patterns are found.
182
+ }
143
183
  return (
144
184
  <div { ...blockProps }>
145
- <BlockPatternSetup
146
- blockName={ blockName }
147
- clientId={ clientId }
148
- startBlankComponent={ <QueryPlaceholder { ...props } /> }
149
- onBlockPatternSelect={ onBlockPatternSelect }
150
- />
185
+ <Placeholder
186
+ icon={ icon }
187
+ label={ label }
188
+ instructions={ __(
189
+ 'Choose a pattern for the query loop or start blank.'
190
+ ) }
191
+ >
192
+ { !! hasPatterns && (
193
+ <Button
194
+ variant="primary"
195
+ onClick={ openPatternSelectionModal }
196
+ >
197
+ { __( 'Choose' ) }
198
+ </Button>
199
+ ) }
200
+
201
+ <Button
202
+ variant="secondary"
203
+ onClick={ () => {
204
+ setIsStartingBlank( true );
205
+ } }
206
+ >
207
+ { __( 'Start blank' ) }
208
+ </Button>
209
+ </Placeholder>
151
210
  </div>
152
211
  );
153
212
  }
154
213
 
155
214
  const QueryEdit = ( props ) => {
156
- const { clientId } = props;
215
+ const { clientId, name } = props;
216
+ const [
217
+ isPatternSelectionModalOpen,
218
+ setIsPatternSelectionModalOpen,
219
+ ] = useState( false );
220
+ const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
157
221
  const hasInnerBlocks = useSelect(
158
222
  ( select ) =>
159
223
  !! select( blockEditorStore ).getBlocks( clientId ).length,
160
224
  [ clientId ]
161
225
  );
162
226
  const Component = hasInnerBlocks ? QueryContent : QueryPatternSetup;
163
- return <Component { ...props } />;
227
+ const onBlockPatternSelect = ( blocks ) => {
228
+ const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
229
+ const firstQueryClientId = getFirstQueryClientIdFromBlocks(
230
+ clonedBlocks
231
+ );
232
+ replaceBlock( clientId, clonedBlocks );
233
+ if ( firstQueryClientId ) {
234
+ selectBlock( firstQueryClientId );
235
+ }
236
+ };
237
+ return (
238
+ <>
239
+ <Component
240
+ { ...props }
241
+ openPatternSelectionModal={ () =>
242
+ setIsPatternSelectionModalOpen( true )
243
+ }
244
+ />
245
+ { isPatternSelectionModalOpen && (
246
+ <Modal
247
+ className="block-editor-query-pattern__selection-modal"
248
+ title={ __( 'Choose a pattern' ) }
249
+ closeLabel={ __( 'Cancel' ) }
250
+ onRequestClose={ () =>
251
+ setIsPatternSelectionModalOpen( false )
252
+ }
253
+ >
254
+ <BlockPatternSetup
255
+ blockName={ name }
256
+ clientId={ clientId }
257
+ onBlockPatternSelect={ onBlockPatternSelect }
258
+ />
259
+ </Modal>
260
+ ) }
261
+ </>
262
+ );
164
263
  };
165
264
 
166
265
  export default QueryEdit;
@@ -5,7 +5,6 @@ import { useSelect, useDispatch } from '@wordpress/data';
5
5
  import {
6
6
  useBlockProps,
7
7
  __experimentalBlockVariationPicker,
8
- __experimentalGetMatchingVariation as getMatchingVariation,
9
8
  store as blockEditorStore,
10
9
  } from '@wordpress/block-editor';
11
10
  import {
@@ -13,13 +12,8 @@ import {
13
12
  store as blocksStore,
14
13
  } from '@wordpress/blocks';
15
14
 
16
- const QueryPlaceholder = ( { clientId, name, attributes, setAttributes } ) => {
17
- const {
18
- blockType,
19
- defaultVariation,
20
- scopeVariations,
21
- allVariations,
22
- } = useSelect(
15
+ function QueryPlaceholder( { clientId, name, setAttributes, icon, label } ) {
16
+ const { defaultVariation, scopeVariations } = useSelect(
23
17
  ( select ) => {
24
18
  const {
25
19
  getBlockVariations,
@@ -31,16 +25,12 @@ const QueryPlaceholder = ( { clientId, name, attributes, setAttributes } ) => {
31
25
  blockType: getBlockType( name ),
32
26
  defaultVariation: getDefaultBlockVariation( name, 'block' ),
33
27
  scopeVariations: getBlockVariations( name, 'block' ),
34
- allVariations: getBlockVariations( name ),
35
28
  };
36
29
  },
37
30
  [ name ]
38
31
  );
39
32
  const { replaceInnerBlocks } = useDispatch( blockEditorStore );
40
33
  const blockProps = useBlockProps();
41
- const matchingVariation = getMatchingVariation( attributes, allVariations );
42
- const icon = matchingVariation?.icon || blockType?.icon?.src;
43
- const label = matchingVariation?.title || blockType?.title;
44
34
  return (
45
35
  <div { ...blockProps }>
46
36
  <__experimentalBlockVariationPicker
@@ -64,6 +54,6 @@ const QueryPlaceholder = ( { clientId, name, attributes, setAttributes } ) => {
64
54
  />
65
55
  </div>
66
56
  );
67
- };
57
+ }
68
58
 
69
59
  export default QueryPlaceholder;
@@ -16,6 +16,7 @@ export default function QueryToolbar( {
16
16
  attributes: { query, displayLayout },
17
17
  setQuery,
18
18
  setDisplayLayout,
19
+ openPatternSelectionModal,
19
20
  } ) {
20
21
  const maxPageInputId = useInstanceId(
21
22
  QueryToolbar,
@@ -128,6 +129,11 @@ export default function QueryToolbar( {
128
129
  />
129
130
  </ToolbarGroup>
130
131
  ) }
132
+ <ToolbarGroup className="wp-block-template-part__block-control-group">
133
+ <ToolbarButton onClick={ openPatternSelectionModal }>
134
+ { __( 'Replace' ) }
135
+ </ToolbarButton>
136
+ </ToolbarGroup>
131
137
  <ToolbarGroup controls={ displayLayoutControls } />
132
138
  </>
133
139
  );
@@ -5,3 +5,41 @@
5
5
  .wp-block-query__create-new-link {
6
6
  padding: 0 $grid-unit-20 $grid-unit-20 56px;
7
7
  }
8
+
9
+ .block-library-query__pattern-selection-content .block-editor-block-patterns-list {
10
+ display: grid;
11
+ grid-template-columns: 1fr 1fr 1fr;
12
+ grid-gap: $grid-unit-10;
13
+
14
+ .block-editor-block-patterns-list__list-item {
15
+ margin-bottom: 0;
16
+ .block-editor-block-preview__container {
17
+ max-height: 250px;
18
+ }
19
+ }
20
+ }
21
+
22
+ .block-editor-query-pattern__selection-modal .components-modal__content {
23
+ overflow: hidden;
24
+ padding: 0;
25
+ &::before {
26
+ margin-bottom: 0;
27
+ }
28
+ }
29
+
30
+ .block-editor-query-pattern__selection-modal {
31
+ // To keep modal dimensions consistent as subsections are navigated, width
32
+ // and height are used instead of max-(width/height).
33
+ @include break-small() {
34
+ width: calc(100% - #{ $grid-unit-20 * 2 });
35
+ height: calc(100% - #{ $header-height * 2 });
36
+ }
37
+ @include break-medium() {
38
+ width: $break-medium - $grid-unit-20 * 2;
39
+ }
40
+ @include break-large() {
41
+ height: 80%;
42
+ width: 80%;
43
+ max-height: none;
44
+ }
45
+ }