From 900afdc22892338f461b05ab894b5c4f45907a52 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Tue, 27 Oct 2020 20:37:55 +0900 Subject: [PATCH] Add support number quality on Image Component (#18224) Co-authored-by: Tim Neutkens --- packages/next/client/image.tsx | 30 +++++++++---------- .../typescript/pages/valid.tsx | 12 ++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/next/client/image.tsx b/packages/next/client/image.tsx index a3a2e5e4d1c6132..083c26945781b23 100644 --- a/packages/next/client/image.tsx +++ b/packages/next/client/image.tsx @@ -26,7 +26,7 @@ type ImageProps = Omit< 'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading' > & { src: string - quality?: string + quality?: number | string priority?: boolean loading?: LoadingValue unoptimized?: boolean @@ -104,8 +104,8 @@ function getDeviceSizes(width: number | undefined): number[] { function computeSrc( src: string, unoptimized: boolean, - width: number | undefined, - quality?: string + width?: number, + quality?: number ): string { if (unoptimized) { return src @@ -118,7 +118,7 @@ function computeSrc( type CallLoaderProps = { src: string width: number - quality?: string + quality?: number } function callLoader(loaderProps: CallLoaderProps) { @@ -129,8 +129,8 @@ function callLoader(loaderProps: CallLoaderProps) { type SrcSetData = { src: string unoptimized: boolean - width: number | undefined - quality: string | undefined + width?: number + quality?: number } function generateSrcSet({ @@ -155,7 +155,7 @@ type PreloadData = { unoptimized: boolean width: number | undefined sizes?: string - quality?: string + quality?: number } function generatePreload({ @@ -251,8 +251,10 @@ export default function Image({ } }, [thisEl, lazy]) - let widthInt = getInt(width) - let heightInt = getInt(height) + const widthInt = getInt(width) + const heightInt = getInt(height) + const qualityInt = getInt(quality) + let divStyle: React.CSSProperties | undefined let imgStyle: React.CSSProperties | undefined let wrapperStyle: React.CSSProperties | undefined @@ -305,12 +307,12 @@ export default function Image({ } // Generate attribute values - const imgSrc = computeSrc(src, unoptimized, widthInt, quality) + const imgSrc = computeSrc(src, unoptimized, widthInt, qualityInt) const imgSrcSet = generateSrcSet({ src, width: widthInt, unoptimized, - quality, + quality: qualityInt, }) let imgAttributes: @@ -352,7 +354,7 @@ export default function Image({ width: widthInt, unoptimized, sizes, - quality, + quality: qualityInt, }) : ''} { src="https://via.placeholder.com/100" unsized > + +

This is valid usage of the Image component

)