From 38c5ec53160501ef1a5236bbcc85f6ff613e0e8a Mon Sep 17 00:00:00 2001 From: Thomas Cristina de Carvalho Date: Fri, 26 Apr 2024 16:06:26 -0400 Subject: [PATCH 1/2] Update SanityImage jsdoc --- .../sanity-web/src/components/SanityImage.tsx | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/sanity-web/src/components/SanityImage.tsx b/packages/sanity-web/src/components/SanityImage.tsx index 6ae034fb..3b59354e 100644 --- a/packages/sanity-web/src/components/SanityImage.tsx +++ b/packages/sanity-web/src/components/SanityImage.tsx @@ -4,6 +4,8 @@ import { getImageDimensions, getExtension } from "@sanity/asset-utils"; import imageUrlBuilder from "@sanity/image-url"; import React from "react"; +const isDev = process.env.NODE_ENV === "development"; + type ImageCrop = { _type: "sanity.imageCrop"; top?: number; @@ -33,6 +35,12 @@ export type SanityImageProps = { projectId: string; }; className?: string; + /** + * Set to `true` to enable LQIP (Low Quality Image Placeholder). + * The LQIP image is used as a placeholder for images that are too large to load and + * is cropped to the aspect ratio of the original image. + * It renders as a blurred background while the original image is loading. + */ lqip?: boolean; data: { _type: "image"; @@ -46,12 +54,39 @@ export type SanityImageProps = { } | null; } & React.ComponentPropsWithRef<"img">; +/** + * Sanity’s Image component is a wrapper around the HTML image element. + * It supports the same props as the HTML `img` element, but automatically + * generates the srcSet and sizes attributes for you. For most use cases, + * you’ll want to set the `aspectRatio` prop to ensure the image is sized + * correctly. + * + * @remarks + * - `decoding` is set to `async` by default. + * - `loading` is set to `lazy` by default. + * - `alt` will automatically be set to the `altText` if passed in the `data` prop. + * - `src` will automatically be set to the `url` if passed in the `data` prop. + * - `lqip` is set to `true` by default. + * + * @example + * A responsive image with a 4:5 aspect ratio: + * ``` + * + * ``` + */ const SanityImage = React.forwardRef( ( { aspectRatio, className, data, + decoding = "async", + loading = "lazy", + sizes, style, config, lqip = true, @@ -68,7 +103,7 @@ const SanityImage = React.forwardRef( const extension = getExtension(_ref); const aspectRatioValues = aspectRatio?.split("/"); - if (aspectRatio && aspectRatioValues?.length !== 2) { + if (aspectRatio && aspectRatioValues?.length !== 2 && isDev) { console.warn( `Invalid aspect ratio: ${aspectRatio}. Using the original aspect ratio. The aspect ratio should be in the format "width/height".` ); @@ -105,6 +140,16 @@ const SanityImage = React.forwardRef( width, }); + if (isDev && !sizes) { + console.warn( + [ + "No sizes prop provided to SanityImage component,", + "you may be loading unnecessarily large images.", + `Image used is ${urlDefault || _ref || "unknown"}`, + ].join(" ") + ); + } + // Create srcset attribute const srcSet = srcSetValues .filter((value) => value < width) @@ -133,10 +178,13 @@ const SanityImage = React.forwardRef( width: 30, }); + // Don't use LQIP if the image is a PNG or SVG + if (extension === "svg" || extension === "png") { + lqip = false; + } + const LQIP = lqip && - extension !== "svg" && - extension !== "png" && ({ background: `url(${blurDataUrl})`, backgroundPositionX: `var(--focalX)`, @@ -162,6 +210,8 @@ const SanityImage = React.forwardRef( return ( {data.alt( } as React.CSSProperties } width={aspectRatioWidth ? aspectRatioWidth * 100 : width} + sizes={sizes} {...passthroughProps} /> ); From 8575999d4aa1ae7730dd4ff5b7dae02ae4f165fd Mon Sep 17 00:00:00 2001 From: Thomas Cristina de Carvalho Date: Mon, 29 Apr 2024 14:35:41 -0400 Subject: [PATCH 2/2] Add changeset --- .changeset/warm-timers-breathe.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/warm-timers-breathe.md diff --git a/.changeset/warm-timers-breathe.md b/.changeset/warm-timers-breathe.md new file mode 100644 index 00000000..a66f8d7b --- /dev/null +++ b/.changeset/warm-timers-breathe.md @@ -0,0 +1,5 @@ +--- +"@tinloof/sanity-web": patch +--- + +Add SanityImage jsdoc