mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-17 13:48:02 +00:00
feat: updates
This commit is contained in:
parent
ffcc60b793
commit
dd5361b8c4
106 changed files with 11989 additions and 4143 deletions
78
packages/web/components/New/Image.tsx
Normal file
78
packages/web/components/New/Image.tsx
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { css, cx } from '@emotion/css'
|
||||
import { AnimatePresence, motion, useAnimation } from 'framer-motion'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { ease } from '@/web/utils/const'
|
||||
|
||||
const Image = ({
|
||||
src,
|
||||
srcSet,
|
||||
className,
|
||||
alt,
|
||||
lazyLoad = true,
|
||||
sizes,
|
||||
placeholder = 'blank',
|
||||
onClick,
|
||||
}: {
|
||||
src?: string
|
||||
srcSet?: string
|
||||
sizes?: string
|
||||
className?: string
|
||||
alt: string
|
||||
lazyLoad?: boolean
|
||||
placeholder?: 'artist' | 'album' | 'playlist' | 'podcast' | 'blank' | null
|
||||
onClick?: (e: React.MouseEvent<HTMLImageElement>) => void
|
||||
}) => {
|
||||
const [loaded, setLoaded] = useState(false)
|
||||
const [error, setError] = useState(false)
|
||||
const animate = useAnimation()
|
||||
const placeholderAnimate = useAnimation()
|
||||
const transition = { duration: 0.6, ease }
|
||||
|
||||
useEffect(() => setError(false), [src])
|
||||
|
||||
const onload = async () => {
|
||||
setLoaded(true)
|
||||
animate.start({ opacity: 1 })
|
||||
}
|
||||
const onError = () => {
|
||||
setError(true)
|
||||
}
|
||||
|
||||
const hidden = error || !loaded
|
||||
|
||||
return (
|
||||
<div className={cx('relative overflow-hidden', className)}>
|
||||
{/* Image */}
|
||||
<motion.img
|
||||
alt={alt}
|
||||
className={cx('absolute inset-0 h-full w-full')}
|
||||
src={src}
|
||||
srcSet={srcSet}
|
||||
sizes={sizes}
|
||||
decoding='async'
|
||||
loading={lazyLoad ? 'lazy' : undefined}
|
||||
onLoad={onload}
|
||||
onError={onError}
|
||||
animate={animate}
|
||||
initial={{ opacity: 0 }}
|
||||
transition={transition}
|
||||
onClick={onClick}
|
||||
/>
|
||||
|
||||
{/* Placeholder / Error fallback */}
|
||||
<AnimatePresence>
|
||||
{hidden && placeholder && (
|
||||
<motion.div
|
||||
animate={placeholderAnimate}
|
||||
initial={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
transition={transition}
|
||||
className='absolute inset-0 h-full w-full bg-white dark:bg-neutral-800'
|
||||
></motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Image
|
||||
Loading…
Add table
Add a link
Reference in a new issue