Skip to content

Commit

Permalink
Image: Adopt margin block support (#63546)
Browse files Browse the repository at this point in the history
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: artemiomorales <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
5 people authored Jul 18, 2024
1 parent 88dffa0 commit 6958c80
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre

- **Name:** core/image
- **Category:** media
- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity, shadow ()
- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity, shadow (), spacing (margin)
- **Attributes:** alt, aspectRatio, blob, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
"filter": {
"duotone": true
},
"spacing": {
"margin": true
},
"__experimentalBorder": {
"color": true,
"radius": true,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,12 @@ class="wp-lightbox-overlay zoom"
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" focusable="false"><path d="m13.06 12 6.47-6.47-1.06-1.06L12 10.94 5.53 4.47 4.47 5.53 10.94 12l-6.47 6.47 1.06 1.06L12 13.06l6.47 6.47 1.06-1.06L13.06 12Z"></path></svg>
</button>
<div class="lightbox-image-container">
<figure data-wp-bind--class="state.currentImage.figureClassNames" data-wp-bind--style="state.currentImage.figureStyles">
<figure data-wp-bind--class="state.currentImage.figureClassNames" data-wp-bind--style="state.figureStyles">
<img data-wp-bind--alt="state.currentImage.alt" data-wp-bind--class="state.currentImage.imgClassNames" data-wp-bind--style="state.imgStyles" data-wp-bind--src="state.currentImage.currentSrc">
</figure>
</div>
<div class="lightbox-image-container">
<figure data-wp-bind--class="state.currentImage.figureClassNames" data-wp-bind--style="state.currentImage.figureStyles">
<figure data-wp-bind--class="state.currentImage.figureClassNames" data-wp-bind--style="state.figureStyles">
<img data-wp-bind--alt="state.currentImage.alt" data-wp-bind--class="state.currentImage.imgClassNames" data-wp-bind--style="state.imgStyles" data-wp-bind--src="state.enlargedSrc">
</figure>
</div>
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/image/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ const { state, actions, callbacks } = store(
'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
);
},
get figureStyles() {
return (
state.overlayOpened &&
`${ state.currentImage.figureStyles?.replace(
/margin[^;]*;?/g,
''
) };`
);
},
get imgStyles() {
return (
state.overlayOpened &&
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/specs/editor/blocks/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,37 @@ test.describe( 'Image - lightbox', () => {
} );
} );
} );

test.describe( 'should render as expected on front end', () => {
test( "Overlay image should not inherit content image's margins", async ( {
editor,
page,
} ) => {
await editor.setContent( `<!-- wp:image {"id":${ uploadedMedia.id },"sizeSlug":"full","linkDestination":"none",
"lightbox":{"enabled":true},"style":{"spacing":{"margin":{"top":"var:preset|spacing|40","bottom":"var:preset|spacing|40","left":"var:preset|spacing|40","right":"var:preset|spacing|40"}}}} -->
<figure class="wp-block-image size-full" style="margin-top:var(--wp--preset--spacing--40);margin-right:var(--wp--preset--spacing--40);margin-bottom:var(--wp--preset--spacing--40);margin-left:var(--wp--preset--spacing--40)">
<img src="${ uploadedMedia.source_url }" alt="" class="wp-image-${ uploadedMedia.id }"/></figure>
<!-- /wp:image --> ` );

const postId = await editor.publishPost();
await page.goto( `/?p=${ postId }` );

const lightboxImage = page.locator( '.wp-lightbox-container img' );
await expect( lightboxImage ).toBeVisible();
await lightboxImage.click();

const figure = page
.locator( '.wp-lightbox-overlay .wp-block-image' )
.first();
await expect( figure ).toBeVisible();
const margin = await figure.evaluate( ( element ) => {
return window
.getComputedStyle( element )
.getPropertyValue( 'margin' );
} );
expect( margin ).toBe( '0px' );
} );
} );
} );

// Added to prevent regressions of https://github.com/WordPress/gutenberg/pull/57040.
Expand Down

0 comments on commit 6958c80

Please sign in to comment.