Skip to content

Commit 45ae95a

Browse files
authored
feat: allow disabling of default responsive image styles (#13851)
* feat: allow disabling of default responsive image styles * Format
1 parent f5f33f3 commit 45ae95a

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

.changeset/open-tables-sit.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Allows disabling default styles for responsive images
6+
7+
This change adds a new `image.experimentalDefaultStyles` option that allows you to disable the default styles applied to responsive images.
8+
9+
When using experimental responsive images, Astro applies default styles to ensure the images resize correctly. In most cases this is what you want – and they are applied with low specificity so your own styles override them. However in some cases you may want to disable these default styles entirely. This is particularly useful when using Tailwind 4, because it uses CSS cascade layers to apply styles, making it difficult to override the default styles.
10+
11+
`image.experimentalDefaultStyles` is a boolean option that defaults to `true`, so you can change it in your Astro config file like this:
12+
13+
```js
14+
export default {
15+
image: {
16+
experimentalDefaultStyles: false,
17+
},
18+
experimental: {
19+
responsiveImages: true,
20+
},
21+
};
22+
```

packages/astro/src/assets/vite-plugin-assets.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ export default function assets({ fs, settings, sync, logger }: Options): vite.Pl
109109
referencedImages: new Set(),
110110
};
111111

112-
const imageComponentPrefix = settings.config.experimental.responsiveImages ? 'Responsive' : '';
112+
const imageComponentPrefix =
113+
settings.config.experimental.responsiveImages && settings.config.image.experimentalDefaultStyles
114+
? 'Responsive'
115+
: '';
113116
return [
114117
// Expose the components and different utilities from `astro:assets`
115118
{

packages/astro/src/core/config/schemas/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
6767
image: {
6868
endpoint: { entrypoint: undefined, route: '/_image' },
6969
service: { entrypoint: 'astro/assets/services/sharp', config: {} },
70+
experimentalDefaultStyles: true,
7071
},
7172
devToolbar: {
7273
enabled: true,
@@ -274,6 +275,9 @@ export const AstroConfigSchema = z.object({
274275
experimentalObjectFit: z.string().optional(),
275276
experimentalObjectPosition: z.string().optional(),
276277
experimentalBreakpoints: z.array(z.number()).optional(),
278+
experimentalDefaultStyles: z
279+
.boolean()
280+
.default(ASTRO_CONFIG_DEFAULTS.image.experimentalDefaultStyles),
277281
})
278282
.default(ASTRO_CONFIG_DEFAULTS.image),
279283
devToolbar: z

packages/astro/src/types/public/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,16 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
13671367
* the more comprehensive list is used, because only the required sizes are generated. For local services, the list is shorter to reduce the number of images generated.
13681368
*/
13691369
experimentalBreakpoints?: number[];
1370+
/**
1371+
* @docs
1372+
* @name experimentalDefaultStyles
1373+
* @type {boolean}
1374+
* @default `true`
1375+
* @description
1376+
* Whether to automatically add global styles to ensure that experimental images resize correctly. This is enabled by default, but can be disabled if you want to manage the styles yourself.
1377+
* This option is only used when the `experimental.responsiveImages` flag is enabled.
1378+
*/
1379+
experimentalDefaultStyles?: boolean;
13701380
};
13711381

13721382
/**

packages/astro/test/core-image-layout.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ describe('astro:image:layout', () => {
584584

585585
await fixture.build();
586586
});
587-
588587
describe('basics', () => {
589588
let $;
590589
let html;
@@ -684,5 +683,22 @@ describe('astro:image:layout', () => {
684683
assert.match(style, /\[data-astro-image\]/);
685684
});
686685
});
686+
describe('disabling global styles', async () => {
687+
it('allows disabling global styles', async () => {
688+
const fixtureWithoutStyles = await loadFixture({
689+
root: './fixtures/core-image-layout/',
690+
image: {
691+
service: testImageService({ foo: 'bar' }),
692+
domains: ['avatars.githubusercontent.com'],
693+
experimentalDefaultStyles: false,
694+
},
695+
});
696+
await fixtureWithoutStyles.build();
697+
const html = await fixtureWithoutStyles.readFile('/index.html');
698+
const $ = cheerio.load(html);
699+
const style = $('style').text();
700+
assert.ok(!style.includes('[data-astro-image]'));
701+
});
702+
});
687703
});
688704
});

0 commit comments

Comments
 (0)