YesPlayMusic/packages/web/components/BlurBackground.tsx

62 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-07-11 11:06:41 +08:00
import { resizeImage } from '@/web/utils/common'
import { cx, css } from '@emotion/css'
import useIsMobile from '@/web/hooks/useIsMobile'
import { useSnapshot } from 'valtio'
import uiStates from '@/web/states/uiStates'
2022-08-03 23:48:39 +08:00
import { AnimatePresence, motion, useAnimation } from 'framer-motion'
2022-07-11 11:06:41 +08:00
import { ease } from '@/web/utils/const'
2022-08-03 23:48:39 +08:00
import { useLocation } from 'react-router-dom'
2022-10-28 20:29:04 +08:00
import { useEffect, useState } from 'react'
2022-07-11 11:06:41 +08:00
2022-08-03 23:48:39 +08:00
const BlurBackground = () => {
2022-07-11 11:06:41 +08:00
const isMobile = useIsMobile()
2022-08-03 23:48:39 +08:00
const { hideTopbarBackground, blurBackgroundImage } = useSnapshot(uiStates)
const location = useLocation()
const animate = useAnimation()
useEffect(() => {
uiStates.blurBackgroundImage = null
}, [location.pathname])
2022-10-28 20:29:04 +08:00
const [isLoaded, setIsLoaded] = useState(false)
useEffect(() => {
setIsLoaded(false)
}, [blurBackgroundImage])
useEffect(() => {
if (!isMobile && blurBackgroundImage && hideTopbarBackground && isLoaded) {
animate.start({ opacity: 1 })
} else {
animate.start({ opacity: 0 })
}
}, [animate, blurBackgroundImage, hideTopbarBackground, isLoaded, isMobile])
2022-07-11 11:06:41 +08:00
return (
<AnimatePresence>
2022-10-28 20:29:04 +08:00
<motion.div
initial={{ opacity: 0 }}
animate={animate}
exit={{ opacity: 0 }}
transition={{ ease }}
>
<img
onLoad={() => setIsLoaded(true)}
2022-07-11 11:06:41 +08:00
className={cx(
'absolute z-0 object-cover opacity-70',
css`
top: -400px;
left: -370px;
width: 1572px;
height: 528px;
filter: blur(256px) saturate(1.2);
`
)}
2022-10-28 20:29:04 +08:00
src={resizeImage(blurBackgroundImage || '', 'sm')}
2022-07-11 11:06:41 +08:00
/>
2022-10-28 20:29:04 +08:00
</motion.div>
2022-07-11 11:06:41 +08:00
</AnimatePresence>
)
}
export default BlurBackground