|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from 'react' |
| 4 | + |
| 5 | +const COLORS = ['#2ABBF8', '#FA4EDF', '#FFCC02', '#00F701'] as const |
| 6 | + |
| 7 | +const ENTER_STAGGER_MS = 60 |
| 8 | +const ENTER_DURATION_MS = 300 |
| 9 | +const HOLD_MS = 3000 |
| 10 | +const EXIT_STAGGER_MS = 120 |
| 11 | +const EXIT_DURATION_MS = 500 |
| 12 | + |
| 13 | +interface BlockState { |
| 14 | + opacity: number |
| 15 | + transitioning: boolean |
| 16 | +} |
| 17 | + |
| 18 | +export function AnimatedColorBlocks() { |
| 19 | + const prefersReducedMotion = usePrefersReducedMotion() |
| 20 | + const [blocks, setBlocks] = useState<BlockState[]>( |
| 21 | + COLORS.map(() => ({ opacity: prefersReducedMotion ? 1 : 0, transitioning: false })) |
| 22 | + ) |
| 23 | + const mounted = useRef(true) |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + mounted.current = true |
| 27 | + if (prefersReducedMotion) return |
| 28 | + |
| 29 | + COLORS.forEach((_, i) => { |
| 30 | + setTimeout(() => { |
| 31 | + if (!mounted.current) return |
| 32 | + setBlocks((prev) => |
| 33 | + prev.map((b, idx) => (idx === i ? { opacity: 1, transitioning: true } : b)) |
| 34 | + ) |
| 35 | + }, i * ENTER_STAGGER_MS) |
| 36 | + }) |
| 37 | + |
| 38 | + const totalEnterMs = COLORS.length * ENTER_STAGGER_MS + ENTER_DURATION_MS + HOLD_MS |
| 39 | + const cycleTimer = setTimeout(() => { |
| 40 | + if (!mounted.current) return |
| 41 | + startCycle() |
| 42 | + }, totalEnterMs) |
| 43 | + |
| 44 | + return () => { |
| 45 | + mounted.current = false |
| 46 | + clearTimeout(cycleTimer) |
| 47 | + } |
| 48 | + }, [prefersReducedMotion]) |
| 49 | + |
| 50 | + function startCycle() { |
| 51 | + if (!mounted.current) return |
| 52 | + |
| 53 | + COLORS.forEach((_, i) => { |
| 54 | + setTimeout(() => { |
| 55 | + if (!mounted.current) return |
| 56 | + setBlocks((prev) => |
| 57 | + prev.map((b, idx) => (idx === i ? { opacity: 0.15, transitioning: true } : b)) |
| 58 | + ) |
| 59 | + }, i * EXIT_STAGGER_MS) |
| 60 | + }) |
| 61 | + |
| 62 | + const exitTotalMs = COLORS.length * EXIT_STAGGER_MS + EXIT_DURATION_MS |
| 63 | + setTimeout(() => { |
| 64 | + if (!mounted.current) return |
| 65 | + COLORS.forEach((_, i) => { |
| 66 | + setTimeout(() => { |
| 67 | + if (!mounted.current) return |
| 68 | + setBlocks((prev) => |
| 69 | + prev.map((b, idx) => |
| 70 | + idx === i ? { opacity: [1, 0.8, 0.6, 0.9][i], transitioning: true } : b |
| 71 | + ) |
| 72 | + ) |
| 73 | + }, i * ENTER_STAGGER_MS) |
| 74 | + }) |
| 75 | + }, exitTotalMs + 200) |
| 76 | + |
| 77 | + const cycleDuration = |
| 78 | + exitTotalMs + 200 + COLORS.length * ENTER_STAGGER_MS + ENTER_DURATION_MS + HOLD_MS |
| 79 | + setTimeout(() => startCycle(), cycleDuration) |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <div className='flex gap-0' aria-hidden='true'> |
| 84 | + {COLORS.map((color, i) => ( |
| 85 | + <span |
| 86 | + key={color} |
| 87 | + className='inline-block h-3 w-3' |
| 88 | + style={{ |
| 89 | + backgroundColor: color, |
| 90 | + opacity: blocks[i]?.opacity ?? 0, |
| 91 | + transition: `opacity ${blocks[i]?.transitioning ? ENTER_DURATION_MS : 0}ms ease-out`, |
| 92 | + }} |
| 93 | + /> |
| 94 | + ))} |
| 95 | + </div> |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +export function AnimatedColorBlocksVertical() { |
| 100 | + const prefersReducedMotion = usePrefersReducedMotion() |
| 101 | + const [blocks, setBlocks] = useState<BlockState[]>( |
| 102 | + COLORS.slice(0, 3).map(() => ({ |
| 103 | + opacity: prefersReducedMotion ? 1 : 0, |
| 104 | + transitioning: false, |
| 105 | + })) |
| 106 | + ) |
| 107 | + const mounted = useRef(true) |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + mounted.current = true |
| 111 | + if (prefersReducedMotion) return |
| 112 | + |
| 113 | + const baseDelay = COLORS.length * ENTER_STAGGER_MS + 100 |
| 114 | + |
| 115 | + COLORS.slice(0, 3).forEach((_, i) => { |
| 116 | + setTimeout( |
| 117 | + () => { |
| 118 | + if (!mounted.current) return |
| 119 | + setBlocks((prev) => |
| 120 | + prev.map((b, idx) => (idx === i ? { opacity: 1, transitioning: true } : b)) |
| 121 | + ) |
| 122 | + }, |
| 123 | + baseDelay + i * ENTER_STAGGER_MS |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + return () => { |
| 128 | + mounted.current = false |
| 129 | + } |
| 130 | + }, [prefersReducedMotion]) |
| 131 | + |
| 132 | + const verticalColors = [COLORS[0], COLORS[1], COLORS[2]] |
| 133 | + |
| 134 | + return ( |
| 135 | + <div className='flex flex-col gap-0' aria-hidden='true'> |
| 136 | + {verticalColors.map((color, i) => ( |
| 137 | + <span |
| 138 | + key={color} |
| 139 | + className='inline-block h-3 w-3' |
| 140 | + style={{ |
| 141 | + backgroundColor: color, |
| 142 | + opacity: blocks[i]?.opacity ?? 0, |
| 143 | + transition: `opacity ${blocks[i]?.transitioning ? ENTER_DURATION_MS : 0}ms ease-out`, |
| 144 | + }} |
| 145 | + /> |
| 146 | + ))} |
| 147 | + </div> |
| 148 | + ) |
| 149 | +} |
| 150 | + |
| 151 | +function usePrefersReducedMotion(): boolean { |
| 152 | + const [prefersReduced, setPrefersReduced] = useState(false) |
| 153 | + |
| 154 | + useEffect(() => { |
| 155 | + const mq = window.matchMedia('(prefers-reduced-motion: reduce)') |
| 156 | + setPrefersReduced(mq.matches) |
| 157 | + |
| 158 | + const handler = (e: MediaQueryListEvent) => setPrefersReduced(e.matches) |
| 159 | + mq.addEventListener('change', handler) |
| 160 | + return () => mq.removeEventListener('change', handler) |
| 161 | + }, []) |
| 162 | + |
| 163 | + return prefersReduced |
| 164 | +} |
0 commit comments