Skip to content

Commit

Permalink
Reset styles on window resize (#55077)
Browse files Browse the repository at this point in the history
Co-authored-by: Ricardo Artemio Morales <[email protected]>
  • Loading branch information
SantosGuillamot and artemiomorales authored Oct 10, 2023
1 parent d55093a commit 1d0f3c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$w->set_attribute( 'data-wp-init', 'effects.core.image.setCurrentSrc' );
$w->set_attribute( 'data-wp-on--load', 'actions.core.image.handleLoad' );
$w->set_attribute( 'data-wp-effect', 'effects.core.image.setButtonStyles' );
$w->set_attribute( 'data-wp-effect--setStylesOnResize', 'effects.core.image.setStylesOnResize' );
$body_content = $w->get_updated_html();

// Wrap the image in the body content with a button.
Expand Down
21 changes: 16 additions & 5 deletions packages/block-library/src/image/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ store(
context.core.image.scrollDelta = 0;

context.core.image.lightboxEnabled = true;
setStyles( context, event );
setStyles(
context,
event.target.previousElementSibling
);

context.core.image.scrollTopReset =
window.pageYOffset ||
Expand Down Expand Up @@ -338,6 +341,15 @@ store(
context.core.image.imageButtonHeight = offsetHeight;
}
},
setStylesOnResize: ( { state, context, ref } ) => {
if (
context.core.image.lightboxEnabled &&
( state.core.image.windowWidth ||
state.core.image.windowHeight )
) {
setStyles( context, ref );
}
},
},
},
},
Expand All @@ -362,17 +374,16 @@ store(
* @param {Object} context - An Interactivity API context
* @param {Object} event - A triggering event
*/
function setStyles( context, event ) {
function setStyles( context, ref ) {
// The reference img element lies adjacent
// to the event target button in the DOM.
let {
naturalWidth,
naturalHeight,
offsetWidth: originalWidth,
offsetHeight: originalHeight,
} = event.target.previousElementSibling;
let { x: screenPosX, y: screenPosY } =
event.target.previousElementSibling.getBoundingClientRect();
} = ref;
let { x: screenPosX, y: screenPosY } = ref.getBoundingClientRect();

// Natural ratio of the image clicked to open the lightbox.
const naturalRatio = naturalWidth / naturalHeight;
Expand Down

0 comments on commit 1d0f3c4

Please sign in to comment.