-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
388 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
import React, {useState} from 'react'; | ||
import Image from 'next/image'; | ||
import {cl} from '@builtbymom/web3/utils'; | ||
import {useUpdateEffect} from '@react-hookz/web'; | ||
|
||
import type {ImageProps} from 'next/image'; | ||
import type {CSSProperties, ReactElement} from 'react'; | ||
|
||
type TImageWithFallback = ImageProps & { | ||
smWidth?: number; | ||
smHeight?: number; | ||
}; | ||
export function ImageWithFallback(props: TImageWithFallback): ReactElement { | ||
const {alt, src, smWidth, smHeight, ...rest} = props; | ||
const [imageSrc, set_imageSrc] = useState(`${src}?fallback=true`); | ||
function ImageWithFallback(props: ImageProps & {altSrc?: string}): ReactElement { | ||
const {alt, src, altSrc, ...rest} = props; | ||
const [imageSrc, set_imageSrc] = useState(altSrc ? src : `${src}?fallback=true`); | ||
const [imageStyle, set_imageStyle] = useState<CSSProperties>({}); | ||
|
||
useUpdateEffect((): void => { | ||
set_imageSrc(`${src}?fallback=true`); | ||
set_imageSrc(altSrc ? src : `${src}?fallback=true`); | ||
set_imageStyle({}); | ||
}, [src]); | ||
|
||
return ( | ||
<Image | ||
alt={alt} | ||
src={imageSrc} | ||
style={imageStyle} | ||
className={cl( | ||
`w-[${smWidth ?? rest.width}px] min-w-[${smWidth ?? rest.width}px]`, | ||
`h-[${smHeight ?? rest.height}px] min-h-[${smHeight ?? rest.height}px]`, | ||
`md:w-[${rest.width}px] md:h-[${rest.height}px]`, | ||
`md:min-w-[${rest.width}px] md:min-h-[${rest.height}px]` | ||
)} | ||
loading={'eager'} | ||
className={'animate-fadeIn'} | ||
style={{ | ||
minWidth: props.width, | ||
minHeight: props.height, | ||
maxWidth: props.width, | ||
maxHeight: props.height, | ||
...imageStyle | ||
}} | ||
onError={(): void => { | ||
if (altSrc && imageSrc !== `${altSrc}?fallback=true`) { | ||
console.warn('using placeholder'); | ||
set_imageSrc(`${altSrc}?fallback=true`); | ||
return; | ||
} | ||
set_imageSrc('/placeholder.png'); | ||
set_imageStyle({filter: 'opacity(0.2)'}); | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
} | ||
|
||
export {ImageWithFallback}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.