103 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-05-29 17:53:27 +08:00
import { css, cx } from '@emotion/css'
import { AnimatePresence, motion, useAnimation } from 'framer-motion'
2022-06-11 00:19:07 +08:00
import { useEffect, useState } from 'react'
2022-05-29 17:53:27 +08:00
import { ease } from '@/web/utils/const'
2022-06-11 00:19:07 +08:00
import useIsMobile from '@/web/hooks/useIsMobile'
2022-05-29 17:53:27 +08:00
const Image = ({
src,
srcSet,
className,
alt,
lazyLoad = true,
sizes,
placeholder = 'blank',
onClick,
2022-06-06 01:00:25 +08:00
onMouseOver,
2022-06-11 00:19:07 +08:00
animation = true,
2022-05-29 17:53:27 +08:00
}: {
src?: string
srcSet?: string
sizes?: string
className?: string
alt: string
lazyLoad?: boolean
2022-06-12 15:29:14 +08:00
placeholder?: 'artist' | 'album' | 'playlist' | 'podcast' | 'blank' | false
2022-05-29 17:53:27 +08:00
onClick?: (e: React.MouseEvent<HTMLImageElement>) => void
2022-06-06 01:00:25 +08:00
onMouseOver?: (e: React.MouseEvent<HTMLImageElement>) => void
2022-06-11 00:19:07 +08:00
animation?: boolean
2022-05-29 17:53:27 +08:00
}) => {
const [loaded, setLoaded] = useState(false)
const [error, setError] = useState(false)
const animate = useAnimation()
2022-06-11 00:19:07 +08:00
const isMobile = useIsMobile()
const isAnimate = animation && !isMobile
2022-05-29 17:53:27 +08:00
useEffect(() => setError(false), [src])
2022-06-11 00:19:07 +08:00
const onLoad = async () => {
2022-05-29 17:53:27 +08:00
setLoaded(true)
2022-06-11 00:19:07 +08:00
if (isAnimate) animate.start({ opacity: 1 })
2022-05-29 17:53:27 +08:00
}
const onError = () => {
setError(true)
}
const hidden = error || !loaded
2022-06-11 00:19:07 +08:00
const transition = { duration: 0.6, ease }
const motionProps = isAnimate
? {
animate,
initial: { opacity: 0 },
2022-06-12 15:29:14 +08:00
exit: { opacity: 0 },
2022-06-11 00:19:07 +08:00
transition,
}
: {}
const placeholderMotionProps = isAnimate
? {
initial: { opacity: 1 },
exit: { opacity: 0 },
transition,
}
: {}
2022-05-29 17:53:27 +08:00
return (
2022-06-12 15:29:14 +08:00
<div
className={cx(
'overflow-hidden',
className,
className?.includes('absolute') === false && 'relative'
)}
>
2022-05-29 17:53:27 +08:00
{/* Image */}
2022-06-12 15:29:14 +08:00
<AnimatePresence>
<motion.img
alt={alt}
className='absolute inset-0 h-full w-full'
src={src}
srcSet={srcSet}
sizes={sizes}
decoding='async'
loading={lazyLoad ? 'lazy' : undefined}
onError={onError}
onLoad={onLoad}
onClick={onClick}
onMouseOver={onMouseOver}
{...motionProps}
/>
</AnimatePresence>
2022-05-29 17:53:27 +08:00
{/* Placeholder / Error fallback */}
<AnimatePresence>
{hidden && placeholder && (
<motion.div
2022-06-11 00:19:07 +08:00
{...placeholderMotionProps}
2022-05-29 17:53:27 +08:00
className='absolute inset-0 h-full w-full bg-white dark:bg-neutral-800'
></motion.div>
)}
</AnimatePresence>
</div>
)
}
export default Image