-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: rename layout and refactor styles #13677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Simplifies styles for experimental responsive images | ||
|
|
||
| :warning: **BREAKING CHANGE FOR EXPERIMENTAL RESPONSIVE IMAGES ONLY** :warning: | ||
|
|
||
| The generated styles for image layouts are now simpler and easier to override. Previously it used CSS to set the size and aspect ratio of the images, but this is no longer needed. Now it just sets `object-fit` and `object-position`, and sets `max-width: 100%` for constrained images and `width: 100%` for full-width images. | ||
|
|
||
| The styles now use the [`:where()` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:where), which has a [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_cascade/Specificity) of 0, meaning that it is easy to override with your own styles. You can now be sure that your own classes will always override the applied styles, as will global styles on `img`. | ||
|
|
||
| An exception is Tailwind 4, which uses [cascade layers](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer), meaning the rules are always lower specificity. Astro supports browsers that do not support cascade layers, so we cannot use this. If you need to override the styles using Tailwind 4, you must use `!important` classes. Do check if this is needed though: there may be a layout that is more appropriate for your use case. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Renames experimental responsive image layout option from "responsive" to "constrained" | ||
|
|
||
| :warning: **BREAKING CHANGE FOR EXPERIMENTAL RESPONSIVE IMAGES ONLY** :warning: | ||
|
|
||
| The layout option called `"responsive"` is renamed to `"constrained"` to better reflect its behavior. | ||
|
|
||
| The previous name was causing confusion, because it is also the name of the feature. The `responsive` layout option is specifically for images that are displayed at the requested size, unless they do not fit the width of their container, at which point they would be scaled down to fit. They do not get scaled beyond the intrinsic size of the source image, or the `width` prop if provided. | ||
|
|
||
| It became clear from user feedback that many people (understandably) thought that they needed to set `layout` to `responsive` if they wanted to use responsive images. They then struggled with overriding styles to make the image scale up for full-width hero images, for example, when they should have been using `full-width` layout. Renaming the layout to `constrained` should make it clearer that this layout is for when you want to constrain the maximum size of the image, but allow it to scale-down. | ||
|
|
||
| ### Upgrading | ||
|
|
||
| If you set a default `image.experimentalLayout` in your `astro.config.mjs`, or set it on a per-image basis using the `layout` prop, you will need to change all occurences to `constrained`: | ||
|
|
||
| ```diff lang="ts" | ||
| // astro.config.mjs | ||
| export default { | ||
| image: { | ||
| - experimentalLayout: 'responsive', | ||
| + experimentalLayout: 'constrained', | ||
| }, | ||
| } | ||
| ``` | ||
|
|
||
| ```diff lang="astro" | ||
| // src/pages/index.astro | ||
| --- | ||
| import { Image } from 'astro:assets'; | ||
| --- | ||
| - <Image src="/image.jpg" layout="responsive" /> | ||
| + <Image src="/image.jpg" layout="constrained" /> | ||
| ``` | ||
|
|
||
| Please [give feedback on the RFC](https://github.com/withastro/roadmap/pull/1051) if you have any questions or comments about the responsive images API. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,10 @@ | ||
| [data-astro-image] { | ||
| width: 100%; | ||
| height: auto; | ||
| :where([data-astro-image]) { | ||
| object-fit: var(--fit); | ||
| object-position: var(--pos); | ||
| aspect-ratio: var(--w) / var(--h); | ||
| } | ||
| /* Styles for responsive layout */ | ||
| [data-astro-image='responsive'] { | ||
| max-width: calc(var(--w) * 1px); | ||
| max-height: calc(var(--h) * 1px); | ||
| :where([data-astro-image='full-width']) { | ||
| width: 100%; | ||
| } | ||
| /* Styles for fixed layout */ | ||
| [data-astro-image='fixed'] { | ||
| width: calc(var(--w) * 1px); | ||
| height: calc(var(--h) * 1px); | ||
| :where([data-astro-image='constrained']) { | ||
| max-width: 100%; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great explanation of what's happening! My only nit is not being super clear on what "it" is referring to. (If it's "generated styles" then should be "they" for plural? If it's "layouts" that's also plural unless you just want to say "your layout used CSS". If it's Astro, then we haven't introduced that antecedent at all yet!)
Not a huge deal, but enough that I went, "wait a minute, what is "it" here?" so might be enough to be a bit distracting to readers.
Also, there's little guidance here as to what to do about this breaking change (if anything?). We could add something after this paragraph as another standalone paragraph so it stands out, like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about now