prismicio / prismic-next

Helpers to integrate Prismic into Next.js apps
https://prismic.io/docs/technologies/nextjs
Apache License 2.0
57 stars 7 forks source link

feat: support `width` and `height` props in `<PrismicNextImage>` when layout is `intrinsic` or `fixed` #42

Closed angeloashmore closed 2 years ago

angeloashmore commented 2 years ago

Types of changes

Description

This PR adds support for width and height props in <PrismicNextImage> when the layout prop is intrinsic or fixed.

When using the intrinsic or fixed layout, the width and height props define the size of the rendered image. If no width or height are given, the image's source dimensions are used.

When using responsive or fill, the width and height props define the source image's dimensions. The provided Image field already contains this information, thus the width and height props are not allowed in this situation.

Examples

Default layout (intrinsic)

// The height is derived from the image's aspect ratio.
<PrismicNextImage field={myImageField} width="400" />

// The width is derived from the image's aspect ratio.
<PrismicNextImage field={myImageField} height="300" />

// The width and height are explicitly defined.
<PrismicNextImage field={myImageField} width="300" height="400" />

layout="intrinsic"

// The height is derived from the image's aspect ratio.
<PrismicNextImage field={myImageField} width="400" layout="intrinsic" />

// The width is derived from the image's aspect ratio.
<PrismicNextImage field={myImageField} height="300" layout="intrinsic"/>

// The width and height are explicitly defined.
<PrismicNextImage field={myImageField} width="300" height="400" layout="intrinsic" />

layout="fixed"

// The height is derived from the image's aspect ratio.
<PrismicNextImage field={myImageField} width="400" layout="fixed" />

// The width is derived from the image's aspect ratio.
<PrismicNextImage field={myImageField} height="300" layout="fixed"/>

// The width and height are explicitly defined.
<PrismicNextImage field={myImageField} width="300" height="400" layout="fixed" />

layout="responsive"

// The dimensions are derived from the image's metadata.
// `width` and `height` are not allowed.
<PrismicNextImage field={myImageField} layout="responsive" />

layout="fill"

// The dimensions are derived from the image's metadata.
// `width` and `height` are not allowed.
<PrismicNextImage field={myImageField} layout="fill" />

Checklist:

🐻

github-actions[bot] commented 2 years ago

size-limit report πŸ“¦

Path Size
./dist/index.mjs 4.22 KB (+3.11% πŸ”Ί)
codecov-commenter commented 2 years ago

Codecov Report

Merging #42 (b39e405) into main (dd2f06b) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #42   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           11        11           
  Lines          672       713   +41     
  Branches        33        43   +10     
=========================================
+ Hits           672       713   +41     
Impacted Files Coverage Ξ”
src/PrismicNextImage.tsx 100.00% <100.00%> (ΓΈ)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

webda2l commented 2 years ago

It will be nice to handle the https://nextjs.org/docs/api-reference/next/future/image as well because the current image component has its days numbered now.

angeloashmore commented 2 years ago

Hey @webda2l, yes, next/future/image support is planned, but it's not clear yet how it could be done.

We may need to wait until next/future/image becomes the default next/image. If you have any suggestions, I'm more than happy to listen! πŸ™‚