@wordpress/edit-site 6.26.0 → 6.27.0

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.
@@ -51,6 +51,7 @@ import { getExamples } from './examples';
51
51
  import { store as siteEditorStore } from '../../store';
52
52
  import { useSection } from '../sidebar-global-styles-wrapper';
53
53
  import { GlobalStylesRenderer } from '../global-styles-renderer';
54
+ import { getVariationClassName } from '../global-styles/utils';
54
55
  import {
55
56
  STYLE_BOOK_COLOR_GROUPS,
56
57
  STYLE_BOOK_PREVIEW_CATEGORIES,
@@ -215,6 +216,32 @@ export function getExamplesForSinglePageUse( examples ) {
215
216
  return examplesForSinglePageUse;
216
217
  }
217
218
 
219
+ /**
220
+ * Applies a block variation to each example by updating its attributes.
221
+ *
222
+ * @param {Array} examples Array of examples
223
+ * @param {string} variation Block variation name.
224
+ * @return {Array} Updated examples with variation applied.
225
+ */
226
+ function applyBlockVariationsToExamples( examples, variation ) {
227
+ if ( ! variation ) {
228
+ return examples;
229
+ }
230
+
231
+ return examples.map( ( example ) => ( {
232
+ ...example,
233
+ variation,
234
+ blocks: {
235
+ ...example.blocks,
236
+ attributes: {
237
+ ...example.blocks.attributes,
238
+ style: undefined,
239
+ className: getVariationClassName( variation ),
240
+ },
241
+ },
242
+ } ) );
243
+ }
244
+
218
245
  function StyleBook( {
219
246
  enableResizing = true,
220
247
  isSelected,
@@ -394,7 +421,7 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => {
394
421
  );
395
422
  };
396
423
 
397
- const onSelect = ( blockName ) => {
424
+ const onSelect = ( blockName, isBlockVariation = false ) => {
398
425
  if (
399
426
  STYLE_BOOK_COLOR_GROUPS.find(
400
427
  ( group ) => group.slug === blockName
@@ -410,6 +437,10 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => {
410
437
  return;
411
438
  }
412
439
 
440
+ if ( isBlockVariation ) {
441
+ return;
442
+ }
443
+
413
444
  // Now go to the selected block.
414
445
  onChangeSection( `/blocks/${ encodeURIComponent( blockName ) }` );
415
446
  };
@@ -419,14 +450,20 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => {
419
450
  const examplesForSinglePageUse = getExamplesForSinglePageUse( examples );
420
451
 
421
452
  let previewCategory = null;
453
+ let blockVariation = null;
422
454
  if ( section.includes( '/colors' ) ) {
423
455
  previewCategory = 'colors';
424
456
  } else if ( section.includes( '/typography' ) ) {
425
457
  previewCategory = 'text';
426
458
  } else if ( section.includes( '/blocks' ) ) {
427
459
  previewCategory = 'blocks';
428
- const blockName =
429
- decodeURIComponent( section ).split( '/blocks/' )[ 1 ];
460
+ let blockName = decodeURIComponent( section ).split( '/blocks/' )[ 1 ];
461
+
462
+ // The blockName can contain variations, if so, extract the variation.
463
+ if ( blockName?.includes( '/variations' ) ) {
464
+ [ blockName, blockVariation ] = blockName.split( '/variations/' );
465
+ }
466
+
430
467
  if (
431
468
  blockName &&
432
469
  examples.find( ( example ) => example.name === blockName )
@@ -440,21 +477,43 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => {
440
477
  ( category ) => category.slug === previewCategory
441
478
  );
442
479
 
443
- // If there's no category definition there may be a single block.
444
- const filteredExamples = categoryDefinition
445
- ? getExamplesByCategory( categoryDefinition, examples )
446
- : {
480
+ const filteredExamples = useMemo( () => {
481
+ // If there's no category definition there may be a single block.
482
+ if ( ! categoryDefinition ) {
483
+ return {
447
484
  examples: [
448
485
  examples.find(
449
486
  ( example ) => example.name === previewCategory
450
487
  ),
451
488
  ],
452
- };
489
+ };
490
+ }
491
+
492
+ return getExamplesByCategory( categoryDefinition, examples );
493
+ }, [ categoryDefinition, examples, previewCategory ] );
453
494
 
454
- // If there's no preview category, show all examples.
455
- const displayedExamples = previewCategory
456
- ? filteredExamples
457
- : { examples: examplesForSinglePageUse };
495
+ const displayedExamples = useMemo( () => {
496
+ // If there's no preview category, show all examples.
497
+ if ( ! previewCategory ) {
498
+ return { examples: examplesForSinglePageUse };
499
+ }
500
+
501
+ if ( blockVariation ) {
502
+ return {
503
+ examples: applyBlockVariationsToExamples(
504
+ filteredExamples.examples,
505
+ blockVariation
506
+ ),
507
+ };
508
+ }
509
+
510
+ return filteredExamples;
511
+ }, [
512
+ previewCategory,
513
+ examplesForSinglePageUse,
514
+ blockVariation,
515
+ filteredExamples,
516
+ ] );
458
517
 
459
518
  const { base: baseConfig } = useContext( GlobalStylesContext );
460
519
  const goTo = getStyleBookNavigationFromPath( section );
@@ -603,7 +662,11 @@ const Examples = memo(
603
662
  isSelected={ isSelected?.( example.name ) }
604
663
  onClick={
605
664
  !! onSelect
606
- ? () => onSelect( example.name )
665
+ ? () =>
666
+ onSelect(
667
+ example.name,
668
+ !! example.variation
669
+ )
607
670
  : null
608
671
  }
609
672
  />