Skip to content

Commit 4464b3a

Browse files
astro-factory[bot]factory[bot]matthewp
authored
fix(assets): forward class to picture element in Picture component (#18003) (#18004)
Co-authored-by: factory[bot] <factory[bot]@users.noreply.github.com> Co-authored-by: Matthew Phillips <matthewphillips@cloudflare.com>
1 parent 31e89a1 commit 4464b3a

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎.changeset/dull-nights-invite.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes the `Picture` component to forward the `class` prop to the `<picture>` element in addition to the `<img>` element. This allows layout-affecting CSS properties like `z-index` to work correctly when the `Picture` component is used inside flex or grid containers.

‎packages/astro/components/Picture.astro‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ if (props.alt === undefined || props.alt === null) {
3838
throw new AstroError(AstroErrorData.ImageMissingAlt);
3939
}
4040
41-
// Picture attribute inherit scoped styles from class and attributes
42-
const scopedStyleClass = props.class?.match(/\bastro-\w{8}\b/)?.[0];
43-
if (scopedStyleClass) {
41+
// Forward the user's class to <picture> so layout-affecting CSS (z-index, order, etc.)
42+
// applies to the flex/grid item. The class is also kept on <img> for visual styling.
43+
if (props.class) {
4444
if (pictureAttributes.class) {
45-
pictureAttributes.class = `${pictureAttributes.class} ${scopedStyleClass}`;
45+
pictureAttributes.class = `${pictureAttributes.class} ${props.class}`;
4646
} else {
47-
pictureAttributes.class = scopedStyleClass;
47+
pictureAttributes.class = props.class;
4848
}
4949
}
5050

‎packages/astro/test/core-image-layout.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ describe('astro:image:layout', () => {
270270
assert.ok($picture.attr('class')!.includes('picture-comp'));
271271
});
272272

273+
it('forwards class to picture element', () => {
274+
const $picture = $('#picture-class-forwarded picture');
275+
assert.ok($picture.attr('class')!.includes('my-class'));
276+
const $img = $('#picture-class-forwarded img');
277+
assert.ok($img.attr('class')!.includes('my-class'));
278+
});
279+
280+
it('merges class with pictureAttributes class on picture element', () => {
281+
const $picture = $('#picture-class-merged picture');
282+
assert.ok($picture.attr('class')!.includes('pic-class'));
283+
assert.ok($picture.attr('class')!.includes('my-class'));
284+
const $img = $('#picture-class-merged img');
285+
assert.ok($img.attr('class')!.includes('my-class'));
286+
});
287+
273288
it('adds data attributes instead of inline styles', () => {
274289
const $img = $('#picture-attributes img');
275290
// Should have data attributes for CSP compliance

‎packages/astro/test/fixtures/core-image-layout/src/pages/picture.astro‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ import myImage from "../assets/penguin.jpg";
5050
}}/>
5151
</div>
5252

53+
<div id="picture-class-forwarded">
54+
<Picture src={myImage} fallbackFormat="jpeg" alt="A penguin" class="my-class" />
55+
</div>
56+
57+
<div id="picture-class-merged">
58+
<Picture src={myImage} fallbackFormat="jpeg" alt="A penguin" class="my-class" pictureAttributes={{ class: 'pic-class' }} />
59+
</div>
60+
5361

5462
<style>
5563
.img-comp {

0 commit comments

Comments
 (0)