Skip to content
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

chore: Update image API to remove maxWidth/maxHeight #28985

Merged
merged 10 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e-tests/gatsby-static-image/src/pages/constrained.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const ConstrainedPage = () => (
<div data-testid="image-constrained-limit">
<StaticImage
src="../images/citrus-fruits.jpg"
maxWidth={500}
width={500}
alt="Citrus fruits"
/>
</div>
<div data-testid="image-constrained-override">
<StaticImage
src="../images/citrus-fruits.jpg"
maxWidth={500}
width={500}
sizes="100vw"
alt="Citrus fruits"
/>
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/gatsby-static-image/src/pages/fluid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const FluidPage = () => (
<StaticImage
src="../images/citrus-fruits.jpg"
layout="fluid"
maxWidth={700}
width={700}
alt="Citrus fruits"
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/visual-regression/src/pages/images/avif.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Page = () => {
query {
file(relativePath: { eq: "cornwall.jpg" }) {
childImageSharp {
gatsbyImageData(maxWidth: 1024, layout: CONSTRAINED, formats: [AVIF])
gatsbyImageData(width: 1024, layout: CONSTRAINED, formats: [AVIF])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Page = () => {
query {
file(relativePath: { eq: "cornwall.jpg" }) {
childImageSharp {
gatsbyImageData(maxWidth: 1024, layout: CONSTRAINED)
gatsbyImageData(width: 1024, layout: CONSTRAINED)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/visual-regression/src/pages/images/fluid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Page = () => {
query {
file(relativePath: { eq: "cornwall.jpg" }) {
childImageSharp {
gatsbyImageData(maxWidth: 1024, layout: FLUID)
gatsbyImageData(width: 1024, layout: FLUID)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Page = () => {
src="../../images/cornwall.jpg"
alt="cornwall"
formats={["avif"]}
maxWidth={1024}
width={1024}
/>
</TestWrapper>
</Layout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Page = () => {
<StaticImage
src="../../images/cornwall.jpg"
alt="cornwall"
maxWidth={1024}
width={1024}
/>
</TestWrapper>
</Layout>
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/visual-regression/src/pages/static-images/fluid.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import Layout from "../../components/layout"
const Page = () => {
return (
<Layout>
<h1>Fluid, maxWidth</h1>
<h1>Fluid, width</h1>
<TestWrapper style={{ display: `block` }}>
<StaticImage
src="../../images/cornwall.jpg"
loading="eager"
layout="fluid"
maxWidth={1024}
width={1024}
alt="cornwall"
/>
</TestWrapper>
Expand Down
15 changes: 5 additions & 10 deletions packages/gatsby-plugin-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export const Dino = () => (
<StaticImage
src="trex.png"
placeholder="none"
layout="fluid"
maxWidth={200}
layout="constrained"
width={200}
alt="T-Rex"
transformOptions={{ grayscale: true }}
/>
Expand Down Expand Up @@ -294,7 +294,7 @@ JSON type, meaning you don't specify the individual fields, but are instead give
```graphql
coverImage: file(relativePath: { eq: "plant.jpg" }) {
childImageSharp {
gatsbyImageData(maxWidth: 720, layout: FLUID, placeholder: TRACED_SVG)
gatsbyImageData(width: 720, layout: CONSTRAINED, placeholder: TRACED_SVG)
}
}
```
Expand All @@ -316,13 +316,8 @@ The optional helper function `getImage` takes a file node and returns `file?.chi

These arguments can be passed to the `gatsbyImageData()` resolver:

- **width**: The display width of the generated image. The actual largest image resolution will be this value multiplied by the largest value in outputPixelDensities. Ignored if layout = FLUID or CONSTRAINED, where you should use "maxWidth" instead.
- **width**: The display width of the generated image for layout = FIXED, if layout = CONSTRAINED it's the display width of the largest generated image. The actual largest image resolution will be this value multiplied by the largest value in outputPixelDensities.
- **height**: If set, the height of the generated image. If omitted, it is calculated from the supplied width, matching the aspect ratio of the source image.
- **maxWidth**:
Maximum display width of generated files.
The actual largest image resolution will be this value multiplied by the largest value in outputPixelDensities
This only applies when layout = FLUID or CONSTRAINED. For other layout types, use "width"
- **maxHeight**: If set, the generated image is a maximum of this height, cropping if necessary. If the image layout is "constrained" then the image will be limited to this height. If the aspect ratio of the image is different than the source, then the image will be cropped.`,
- **placeholder**: Format of generated placeholder image.
- `BLURRED`: (default) a blurred, low resolution image, encoded as a base64 data URI
- `TRACED_SVG`: a low-resolution traced SVG of the image.
Expand All @@ -333,7 +328,7 @@ These arguments can be passed to the `gatsbyImageData()` resolver:
- `FIXED`: A static image size, that does not resize according to the screen width
- `FLUID`: The image resizes to fit its container. Pass a "sizes" option if it isn't going to be the full width of the screen.
- **outputPixelDensities**: A list of image pixel densities to generate, for high-resolution (retina) screens. It will never generate images larger than the source, and will always include a 1x image.
Default is `[ 0.25, 0.5, 1, 2 ]`, for fluid/constrained images, and `[ 1, 2 ]` for fixed. In this case, an image with a fluid layout and maxWidth = 400 would generate images at 100, 200, 400 and 800px wide
Default is `[ 0.25, 0.5, 1, 2 ]`, for fluid/constrained images, and `[ 1, 2 ]` for fixed.
- **sizes**: The "[sizes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images)" attribute, passed to the `<img>` tag. This describes the display size of the image. This does not affect the generated images, but is used by the browser to decide which images to download. You can leave this blank for fixed images, or if the responsive image container will be the full width of the screen. In these cases we will generate an appropriate value. If, however, you are generating responsive images that are not the full width of the screen, you should provide a sizes property for best performance. You can alternatively pass this value to the component.
- **formats**: an array of file formats to generate. The default is `[AUTO, WEBP]`, which means it will generate images in the same format as the source image, as well as in the next-generation [WebP](https://developers.google.com/speed/webp) format. We strongly recommend you do not change this option, as doing so will affect performance scores.
- **quality**: The default quality. This is overridden by any format-specific options
Expand Down
4 changes: 1 addition & 3 deletions packages/gatsby-plugin-image/src/__tests__/image-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ const args: IGatsbyImageHelperArgs = {

const fluidArgs: IGatsbyImageHelperArgs = {
...args,
width: undefined,
maxWidth: 400,
layout: `fluid`,
}

const constrainedArgs: IGatsbyImageHelperArgs = {
...fluidArgs,
...args,
layout: `constrained`,
}

Expand Down
2 changes: 0 additions & 2 deletions packages/gatsby-plugin-image/src/babel-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export const SHARP_ATTRIBUTES = new Set([
`src`,
`layout`,
`formats`,
`maxWidth`,
`maxHeight`,
`aspectRatio`,
`quality`,
`avifOptions`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export interface GatsbyImageProps
imgClassName?: string
image: IGatsbyImageData
imgStyle?: CSSProperties
backgroundColor?: string
backgroundColor?: CSSProperties["backgroundColor"]
objectFit?: CSSProperties["objectFit"]
objectPosition?: CSSProperties["objectPosition"]
onLoad?: () => void
onError?: () => void
onStartLoad?: Function
onStartLoad?: (props: { wasCached?: boolean }) => void
}

export interface IGatsbyImageData {
Expand All @@ -47,8 +47,6 @@ export interface IGatsbyImageData {
images: Pick<MainImageProps, "sources" | "fallback">
placeholder?: Pick<PlaceholderProps, "sources" | "fallback">
width?: number
maxHeight?: number
maxWidth?: number
}

let hasShownWarning = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,12 @@ import React, { FunctionComponent } from "react"
import { GatsbyImage as GatsbyImageServer } from "./gatsby-image.server"
import { GatsbyImageProps, IGatsbyImageData } from "./gatsby-image.browser"
import PropTypes from "prop-types"
import { ImageFormat, Layout, Fit } from "../image-utils"
import { ISharpGatsbyImageArgs } from "../image-utils"

export interface IStaticImageProps extends Omit<GatsbyImageProps, "image"> {
export interface IStaticImageProps
extends Omit<GatsbyImageProps, "image">,
ISharpGatsbyImageArgs {
src: string
layout?: Layout
formats?: Array<ImageFormat>
placeholder?: "tracedSVG" | "dominantColor" | "blurred" | "none"
tracedSVGOptions?: Record<string, unknown>
width?: number
height?: number
maxWidth?: number
maxHeight?: number
aspectRatio?: number
sizes?: string
quality?: number
transformOptions?: {
fit?: Fit
}
jpgOptions?: Record<string, unknown>
pngOptions?: Record<string, unknown>
webpOptions?: Record<string, unknown>
avifOptions?: Record<string, unknown>
blurredOptions?: Record<string, unknown>
}

// These values are added by Babel. Do not add them manually
Expand All @@ -43,9 +26,7 @@ export function _getStaticImage(
// We extract these because they're not meant to be passed-down to GatsbyImage
/* eslint-disable @typescript-eslint/no-unused-vars */
width,
maxWidth,
height,
maxHeight,
aspectRatio,
tracedSVGOptions,
placeholder,
Expand Down Expand Up @@ -87,25 +68,13 @@ const checkDimensionProps: PropTypes.Validator<number> = (
...rest
) => {
if (
props.layout !== `fixed` &&
props.layout === `fluid` &&
(propName === `width` || propName === `height`) &&
props[propName]
) {
return new Error(
`"${propName}" ${props[propName]} may not be passed when layout is "${
props.layout || `constrained`
}"`
`"${propName}" ${props[propName]} may not be passed when layout is fluid.`
)
} else {
if (
props.layout === `fixed` &&
(propName === `maxWidth` || propName === `maxHeight`) &&
props[propName]
) {
return new Error(
`"${propName}" may not be passed when layout is "${props.layout}"`
)
}
}
return PropTypes.number(props, propName, ...rest)
}
Expand All @@ -117,9 +86,6 @@ export const propTypes = {
alt: PropTypes.string.isRequired,
width: checkDimensionProps,
height: checkDimensionProps,
maxHeight: checkDimensionProps,
maxWidth: checkDimensionProps,
aspectRatio: checkDimensionProps,
sizes: PropTypes.string,
layout: (props: IStaticImageProps & IPrivateProps): Error | undefined => {
if (props.layout === undefined) {
Expand All @@ -130,7 +96,7 @@ export const propTypes = {
}

return new Error(
`Invalid value ${props.layout}" provided for prop "layout". Defaulting to "fixed". Valid values are "fixed", "fluid" or "constrained"`
`Invalid value ${props.layout}" provided for prop "layout". Defaulting to "constrained". Valid values are "fixed", "fluid" or "constrained"`
)
},
}
Expand Down
Loading