Skip to content

Commit

Permalink
Slideshow block: Ensure max-width is defined when within the Stack bl…
Browse files Browse the repository at this point in the history
…ock (#40383)
  • Loading branch information
coder-karen authored Dec 19, 2024
1 parent cab5e22 commit b4e6eac
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Slideshow block: Fix block display when added within a Stack block.
9 changes: 9 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/slideshow/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
import { DropZone, FormFileUpload, withNotices } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
import domReady from '@wordpress/dom-ready';
import { mediaUpload } from '@wordpress/editor';
import { useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { get, map, pick } from 'lodash';
import metadata from './block.json';
import { PanelControls, ToolbarControls } from './controls';
import Slideshow from './slideshow';
import applyPaddingForStackBlock from './utils';

import './editor.scss';

Expand Down Expand Up @@ -54,6 +56,13 @@ export const SlideshowEdit = ( {
ids: imgs.map( ( { id } ) => parseInt( id, 10 ) ),
} );
};
useEffect( () => {
if ( typeof window !== 'undefined' ) {
domReady( function () {
applyPaddingForStackBlock();
} );
}
}, [] );

const onSelectImages = imgs =>
setImages( imgs.map( image => pickRelevantMediaFiles( image, sizeSlug ) ) );
Expand Down
13 changes: 13 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/slideshow/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,16 @@
padding-top: 0;
}
}

.entry-content:has(.wp-block-group.is-vertical:not(.is-layout-constrained)) .wp-block-group.is-vertical:not(.is-layout-constrained),
#editor:has(.wp-block-group.is-vertical:not(.is-layout-constrained)) .wp-block-group.is-vertical:not(.is-layout-constrained) {
.wp-block-jetpack-slideshow {
max-width: inherit;
}
}

div:not(.entry-content):not(.block-editor__container) > .wp-block-group.is-vertical:not(.is-layout-constrained) {
.wp-block-jetpack-slideshow {
max-width: 100vw;
}
}
43 changes: 43 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/slideshow/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export default function applyPaddingForStackBlock() {
// Helper function to apply styles
const applyPaddingStyles = ( doc, isIframe ) => {
const parentElement = doc.querySelector( '.wp-site-blocks' );
if ( ! parentElement ) return;

const { paddingLeft, paddingRight } = window.getComputedStyle( parentElement );
const totalPadding = parseFloat( paddingLeft ) + parseFloat( paddingRight );

const targetElements = doc.querySelectorAll(
isIframe
? '.wp-block-group.is-vertical:not(.is-layout-constrained) .wp-block-jetpack-slideshow'
: '.wp-block-group.is-vertical:not(.is-layout-constrained) .wp-block-jetpack-slideshow:not(.entry-content .wp-block-jetpack-slideshow)'
);

targetElements.forEach( element => {
element.style.maxWidth = `calc(100vw - ${ totalPadding }px)`;
} );
};

// Apply styles to the main document
applyPaddingStyles( document, false );

// Find all iframes and apply styles to their content
const iframes = document.querySelectorAll( 'iframe' );
iframes.forEach( iframe => {
const iframeDoc = iframe.contentDocument;

// Ensure iframe's parent is #editor before applying styles
const iframeParent = iframe.closest( '#editor' );
if ( iframeDoc && iframeParent ) {
const targetElements = iframeDoc.querySelectorAll(
'.wp-block-group.is-vertical:not(.is-layout-constrained) .wp-block-jetpack-slideshow'
);
targetElements.forEach( element => {
element.style.maxWidth = 'inherit'; // Explicit style for this case
} );
} else if ( iframeDoc ) {
// If the iframe is not in the editor, apply the styles to the iframe itself
applyPaddingStyles( iframeDoc, true );
}
} );
}
3 changes: 3 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/slideshow/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
swiperPaginationRender,
swiperResize,
} from './swiper-callbacks';
import applyPaddingForStackBlock from './utils';

if ( typeof window !== 'undefined' ) {
domReady( function () {
applyPaddingForStackBlock();

const slideshowBlocks = document.getElementsByClassName( 'wp-block-jetpack-slideshow' );

Array.from( slideshowBlocks ).forEach( slideshowBlock => {
Expand Down

0 comments on commit b4e6eac

Please sign in to comment.