| 1 |
import { useMemo } from '@wordpress/element'; |
| 2 |
import { colord } from 'colord'; |
| 3 |
import { motion, useReducedMotion, useTime, useTransform } from 'framer-motion'; |
| 4 |
|
| 5 |
export const MovingGradient = () => { |
| 6 |
const time = useTime(); |
| 7 |
const shouldReduceMotion = useReducedMotion(); |
| 8 |
|
| 9 |
const bannerMain = useMemo(() => { |
| 10 |
return getComputedStyle(document.documentElement) |
| 11 |
.getPropertyValue('--ext-banner-main') |
| 12 |
.trim(); |
| 13 |
}, []); |
| 14 |
|
| 15 |
const bannerMainWashed = useMemo(() => { |
| 16 |
return colord(bannerMain) |
| 17 |
.desaturate(0.3) |
| 18 |
.lighten(0.4) |
| 19 |
.alpha(0.25) |
| 20 |
.toRgbString(); |
| 21 |
}, [bannerMain]); |
| 22 |
|
| 23 |
const isLight = useMemo(() => colord(bannerMain).isLight(), [bannerMain]); |
| 24 |
|
| 25 |
const designMain = useMemo(() => { |
| 26 |
return getComputedStyle(document.documentElement) |
| 27 |
.getPropertyValue('--ext-design-main') |
| 28 |
.trim(); |
| 29 |
}, []); |
| 30 |
|
| 31 |
const mainColorLike = useMemo(() => { |
| 32 |
return colord(designMain).desaturate(0.3).alpha(0.25).toRgbString(); |
| 33 |
}, [designMain]); |
| 34 |
|
| 35 |
// stable per-mount random start, 0..1 |
| 36 |
const phase = useMemo(() => Math.random(), []); |
| 37 |
const t = useTransform(time, (ms) => (ms / 24000 + phase) % 1); |
| 38 |
|
| 39 |
const x = useTransform(t, (v) => Math.sin(v * Math.PI * 2) * 180); |
| 40 |
const y = useTransform(t, (v) => Math.cos(v * Math.PI * 2) * 180); |
| 41 |
|
| 42 |
if (shouldReduceMotion) return null; |
| 43 |
|
| 44 |
if (isLight) { |
| 45 |
return ( |
| 46 |
<motion.div |
| 47 |
style={{ |
| 48 |
background: `radial-gradient(circle at center, ${mainColorLike}, transparent 40%)`, |
| 49 |
x, |
| 50 |
y, |
| 51 |
}} |
| 52 |
className="pointer-events-none absolute inset-0 h-full w-full scale-200" |
| 53 |
/> |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
return ( |
| 58 |
<motion.div |
| 59 |
style={{ |
| 60 |
background: `radial-gradient(circle at center, ${bannerMainWashed}, transparent 40%)`, |
| 61 |
x, |
| 62 |
y, |
| 63 |
}} |
| 64 |
className="pointer-events-none absolute inset-0 h-full w-full scale-200" |
| 65 |
/> |
| 66 |
); |
| 67 |
}; |
| 68 |
|