Unverified Commit 900afdc2 authored by Yamagishi Kazutoshi's avatar Yamagishi Kazutoshi Committed by GitHub

Add support number quality on Image Component (#18224)

Co-authored-by: 's avatarTim Neutkens <timneutkens@me.com>
parent 080b91f9
...@@ -26,7 +26,7 @@ type ImageProps = Omit< ...@@ -26,7 +26,7 @@ type ImageProps = Omit<
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading' 'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading'
> & { > & {
src: string src: string
quality?: string quality?: number | string
priority?: boolean priority?: boolean
loading?: LoadingValue loading?: LoadingValue
unoptimized?: boolean unoptimized?: boolean
...@@ -104,8 +104,8 @@ function getDeviceSizes(width: number | undefined): number[] { ...@@ -104,8 +104,8 @@ function getDeviceSizes(width: number | undefined): number[] {
function computeSrc( function computeSrc(
src: string, src: string,
unoptimized: boolean, unoptimized: boolean,
width: number | undefined, width?: number,
quality?: string quality?: number
): string { ): string {
if (unoptimized) { if (unoptimized) {
return src return src
...@@ -118,7 +118,7 @@ function computeSrc( ...@@ -118,7 +118,7 @@ function computeSrc(
type CallLoaderProps = { type CallLoaderProps = {
src: string src: string
width: number width: number
quality?: string quality?: number
} }
function callLoader(loaderProps: CallLoaderProps) { function callLoader(loaderProps: CallLoaderProps) {
...@@ -129,8 +129,8 @@ function callLoader(loaderProps: CallLoaderProps) { ...@@ -129,8 +129,8 @@ function callLoader(loaderProps: CallLoaderProps) {
type SrcSetData = { type SrcSetData = {
src: string src: string
unoptimized: boolean unoptimized: boolean
width: number | undefined width?: number
quality: string | undefined quality?: number
} }
function generateSrcSet({ function generateSrcSet({
...@@ -155,7 +155,7 @@ type PreloadData = { ...@@ -155,7 +155,7 @@ type PreloadData = {
unoptimized: boolean unoptimized: boolean
width: number | undefined width: number | undefined
sizes?: string sizes?: string
quality?: string quality?: number
} }
function generatePreload({ function generatePreload({
...@@ -251,8 +251,10 @@ export default function Image({ ...@@ -251,8 +251,10 @@ export default function Image({
} }
}, [thisEl, lazy]) }, [thisEl, lazy])
let widthInt = getInt(width) const widthInt = getInt(width)
let heightInt = getInt(height) const heightInt = getInt(height)
const qualityInt = getInt(quality)
let divStyle: React.CSSProperties | undefined let divStyle: React.CSSProperties | undefined
let imgStyle: React.CSSProperties | undefined let imgStyle: React.CSSProperties | undefined
let wrapperStyle: React.CSSProperties | undefined let wrapperStyle: React.CSSProperties | undefined
...@@ -305,12 +307,12 @@ export default function Image({ ...@@ -305,12 +307,12 @@ export default function Image({
} }
// Generate attribute values // Generate attribute values
const imgSrc = computeSrc(src, unoptimized, widthInt, quality) const imgSrc = computeSrc(src, unoptimized, widthInt, qualityInt)
const imgSrcSet = generateSrcSet({ const imgSrcSet = generateSrcSet({
src, src,
width: widthInt, width: widthInt,
unoptimized, unoptimized,
quality, quality: qualityInt,
}) })
let imgAttributes: let imgAttributes:
...@@ -352,7 +354,7 @@ export default function Image({ ...@@ -352,7 +354,7 @@ export default function Image({
width: widthInt, width: widthInt,
unoptimized, unoptimized,
sizes, sizes,
quality, quality: qualityInt,
}) })
: ''} : ''}
<img <img
...@@ -442,7 +444,5 @@ function defaultLoader({ root, src, width, quality }: LoaderProps): string { ...@@ -442,7 +444,5 @@ function defaultLoader({ root, src, width, quality }: LoaderProps): string {
} }
} }
return `${root}?url=${encodeURIComponent(src)}&w=${width}&q=${ return `${root}?url=${encodeURIComponent(src)}&w=${width}&q=${quality || 75}`
quality || '75'
}`
} }
...@@ -22,6 +22,18 @@ const Page = () => { ...@@ -22,6 +22,18 @@ const Page = () => {
src="https://via.placeholder.com/100" src="https://via.placeholder.com/100"
unsized unsized
></Image> ></Image>
<Image
id="quality-num"
src="https://via.placeholder.com/500"
quality={80}
unsized
></Image>
<Image
id="quality-str"
src="https://via.placeholder.com/500"
quality="80"
unsized
></Image>
<p id="stubtext">This is valid usage of the Image component</p> <p id="stubtext">This is valid usage of the Image component</p>
</div> </div>
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment