Unverified Commit 753bb9fd authored by Yamagishi Kazutoshi's avatar Yamagishi Kazutoshi Committed by GitHub

Fix types for lazy image component (#17984)

Follow-up #17916

Correcting type errors and organizing redundant descriptions.
parent bbdebd4f
import React, { ReactElement, useEffect } from 'react' import React, { ReactElement, useEffect, useRef } from 'react'
import Head from '../next-server/lib/head' import Head from '../next-server/lib/head'
const loaders: { [key: string]: (props: LoaderProps) => string } = { const loaders: { [key: string]: (props: LoaderProps) => string } = {
...@@ -6,23 +6,25 @@ const loaders: { [key: string]: (props: LoaderProps) => string } = { ...@@ -6,23 +6,25 @@ const loaders: { [key: string]: (props: LoaderProps) => string } = {
cloudinary: cloudinaryLoader, cloudinary: cloudinaryLoader,
default: defaultLoader, default: defaultLoader,
} }
type ImageData = { type ImageData = {
sizes?: number[]
hosts: { hosts: {
[key: string]: { [key: string]: {
path: string path: string
loader: string loader: string
} }
} }
breakpoints?: number[]
} }
type ImageProps = Omit<JSX.IntrinsicElements['img'], 'src' | 'sizes'> & { type ImageProps = Omit<
JSX.IntrinsicElements['img'],
'src' | 'srcSet' | 'ref'
> & {
src: string src: string
host?: string host?: string
sizes?: string
priority?: boolean priority?: boolean
lazy: boolean lazy?: boolean
className: string
unoptimized?: boolean unoptimized?: boolean
} }
...@@ -143,10 +145,12 @@ export default function Image({ ...@@ -143,10 +145,12 @@ export default function Image({
sizes, sizes,
unoptimized = false, unoptimized = false,
priority = false, priority = false,
lazy, lazy = false,
className, className,
...rest ...rest
}: ImageProps) { }: ImageProps) {
const thisEl = useRef<HTMLImageElement>(null)
// Sanity Checks: // Sanity Checks:
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
if (unoptimized && host) { if (unoptimized && host) {
...@@ -178,15 +182,17 @@ export default function Image({ ...@@ -178,15 +182,17 @@ export default function Image({
src = src.slice(1) src = src.slice(1)
} }
let thisEl: any
useEffect(() => { useEffect(() => {
if (lazy) { const target = thisEl.current
if (target && lazy) {
const observer = getObserver() const observer = getObserver()
if (observer) { if (observer) {
observer.observe(thisEl) observer.observe(target)
return () => { return () => {
observer.unobserve(thisEl) observer.unobserve(target)
} }
} }
} }
...@@ -194,30 +200,36 @@ export default function Image({ ...@@ -194,30 +200,36 @@ export default function Image({
// Generate attribute values // Generate attribute values
const imgSrc = computeSrc(src, host, unoptimized) const imgSrc = computeSrc(src, host, unoptimized)
let imgSrcset = null const imgSrcSet = !unoptimized
if (!unoptimized) { ? generateSrcSet({
imgSrcset = generateSrcSet({ src,
src, host: host,
host: host, widths: breakpoints,
widths: breakpoints, })
}) : undefined
}
const imgAttributes: { let imgAttributes:
src?: string | {
srcSet?: string src: string
'data-src'?: string srcSet?: string
'data-srcset'?: string }
} = {} | {
'data-src': string
'data-srcset'?: string
}
if (!lazy) { if (!lazy) {
imgAttributes.src = imgSrc imgAttributes = {
if (imgSrcset) { src: imgSrc,
imgAttributes.srcSet = imgSrcset }
if (imgSrcSet) {
imgAttributes.srcSet = imgSrcSet
} }
} else { } else {
imgAttributes['data-src'] = imgSrc imgAttributes = {
if (imgSrcset) { 'data-src': imgSrc,
imgAttributes['data-srcset'] = imgSrcset }
if (imgSrcSet) {
imgAttributes['data-srcset'] = imgSrcSet
} }
className = className ? className + ' __lazy' : '__lazy' className = className ? className + ' __lazy' : '__lazy'
} }
...@@ -226,32 +238,6 @@ export default function Image({ ...@@ -226,32 +238,6 @@ export default function Image({
// it's too late for preloads // it's too late for preloads
const shouldPreload = priority && typeof window === 'undefined' const shouldPreload = priority && typeof window === 'undefined'
let imgElement
if (className) {
imgElement = (
<img
{...rest}
ref={(el) => {
thisEl = el
}}
{...imgAttributes}
className={className}
sizes={sizes}
/>
)
} else {
imgElement = (
<img
ref={(el) => {
thisEl = el
}}
{...rest}
{...imgAttributes}
sizes={sizes}
/>
)
}
return ( return (
<div> <div>
{shouldPreload {shouldPreload
...@@ -263,7 +249,13 @@ export default function Image({ ...@@ -263,7 +249,13 @@ export default function Image({
sizes, sizes,
}) })
: ''} : ''}
{imgElement} <img
{...rest}
{...imgAttributes}
className={className}
sizes={sizes}
ref={thisEl}
/>
</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