1"use client"
2
3import React, { useCallback, useEffect, useMemo, useState } from "react"
4import { AnimatePresence, motion } from "motion/react"
5
6type AnimationDirection = "top-to-bottom" | "bottom-to-top" | "both" | "random"
7type AnimationEasing = "linear" | "easeIn" | "easeOut" | "easeInOut" | "spring"
8
9interface AnimatedBackgroundGuidesProps {
10 columnCount?: number
11 className?: string
12 solidLines?: number[]
13 animated?: boolean
14 animationDuration?: number
15 animationDelay?: number
16 glowColor?: string
17 glowSize?: string
18 glowOpacity?: number
19 randomize?: boolean
20 randomInterval?: number
21 direction?: AnimationDirection
22 easing?: AnimationEasing
23 responsive?: boolean
24 minColumnWidth?: string
25 maxActiveColumns?: number
26 darkMode?: boolean
27 contained?: boolean
28}
29
30const easingFunctions = {
31 linear: [0, 0, 1, 1] as const,
32 easeIn: [0.42, 0, 1, 1] as const,
33 easeOut: [0, 0, 0.58, 1] as const,
34 easeInOut: [0.42, 0, 0.58, 1] as const,
35 spring: [0.175, 0.885, 0.32, 1.275] as const,
36}
37
38export function StripeBgGuides({
39 columnCount = 4,
40 className = "",
41 solidLines = [],
42 animated = true,
43 animationDuration = 62,
44 animationDelay = 0.8,
45 glowColor = "hsl(var(--accent))",
46 // glowColor = "#D2F583",
47 glowSize = "10vh",
48 glowOpacity = 0.4,
49 randomize = true,
50 randomInterval = 9000,
51 direction = "both",
52 easing = "spring",
53 responsive = false,
54 minColumnWidth = "4rem",
55 maxActiveColumns = 3,
56 darkMode = false,
57 contained = false,
58}: AnimatedBackgroundGuidesProps) {
59 const [windowWidth, setWindowWidth] = useState(
60 typeof window !== "undefined" ? window.innerWidth : 0
61 )
62
63 const columns = useMemo(() => {
64 const count = responsive
65 ? Math.max(Math.floor(windowWidth / parseInt(minColumnWidth)), 1)
66 : columnCount
67 return [...Array(count)]
68 }, [columnCount, responsive, windowWidth, minColumnWidth])
69
70 const [activeColumns, setActiveColumns] = useState<boolean[]>(
71 columns.map(() => true)
72 )
73
74 const getRandomColumns = useCallback(() => {
75 const newActiveColumns = columns.map(() => Math.random() < 0.5)
76 const activeCount = newActiveColumns.filter(Boolean).length
77 if (activeCount > maxActiveColumns) {
78 const indicesToDeactivate = newActiveColumns
79 .map((isActive, index) => (isActive ? index : -1))
80 .filter((index) => index !== -1)
81 .sort(() => Math.random() - 0.5)
82 .slice(0, activeCount - maxActiveColumns)
83 indicesToDeactivate.forEach((index) => {
84 newActiveColumns[index] = false
85 })
86 }
87 return newActiveColumns
88 }, [columns, maxActiveColumns])
89
90 useEffect(() => {
91 const handleResize = () => setWindowWidth(window.innerWidth)
92 if (typeof window !== "undefined") {
93 window.addEventListener("resize", handleResize)
94 return () => window.removeEventListener("resize", handleResize)
95 }
96 }, [])
97
98 useEffect(() => {
99 setActiveColumns(columns.map(() => true))
100 }, [columns])
101
102 useEffect(() => {
103 if (randomize && animated) {
104 const intervalId = setInterval(() => {
105 setActiveColumns(getRandomColumns())
106 }, randomInterval)
107 return () => clearInterval(intervalId)
108 } else {
109 setActiveColumns(columns.map(() => true))
110 }
111 }, [randomize, animated, randomInterval, getRandomColumns, columns])
112
113 const getAnimationVariants = useCallback(() => {
114 const variants = {
115 "top-to-bottom": {
116 initial: { top: "-100%" },
117 animate: { top: "100%" },
118 },
119 "bottom-to-top": {
120 initial: { top: "100%" },
121 animate: { top: "-100%" },
122 },
123 both: {
124 initial: { top: "100%" },
125 animate: { top: ["-100%", "100%"] },
126 },
127 random: {
128 initial: () => ({ top: Math.random() < 0.5 ? "-100%" : "100%" }),
129 animate: () => ({ top: Math.random() < 0.5 ? "-100%" : "100%" }),
130 },
131 }
132 return variants[direction] || variants["top-to-bottom"]
133 }, [direction])
134
135 const animationVariants = useMemo(
136 () => getAnimationVariants(),
137 [getAnimationVariants]
138 )
139
140 const lineColors = useMemo(() => {
141 return {
142 solid: darkMode ? "hsl(233 14% 13%)" : "hsl(233 14.1% 96.1%)",
143 dashed: darkMode ? "hsl(233 14% 20%)" : "hsl(233 14% 93%)",
144 }
145 }, [darkMode])
146
147 return (
148 <div
149 className={`pointer-events-none ${
150 contained ? "absolute inset-0" : "fixed inset-0"
151 } ${className}`}
152 aria-hidden="true"
153 style={{ zIndex: contained ? 0 : -1 }}
154 >
155 <div className="z-0 h-full w-full px-4 sm:px-6 lg:px-24">
156 <div
157 className="mx-auto h-full w-full"
158 style={{
159 display: "grid",
160 gridTemplateColumns: responsive
161 ? `repeat(auto-fit, minmax(${minColumnWidth}, 1fr))`
162 : `repeat(${columnCount}, minmax(0, 1fr))`,
163 gap: "2rem",
164 }}
165 >
166 {columns.map((_, index) => (
167 <div key={index} className="relative h-full">
168 <div
169 className={`absolute inset-y-0 ${
170 index === 0
171 ? "left-0"
172 : index === columns.length - 1
173 ? "right-0"
174 : "left-1/2"
175 } w-px ${
176 solidLines.includes(index + 1)
177 ? "bg-gray-300"
178 : "bg-gradient-to-b"
179 } overflow-hidden`}
180 style={
181 solidLines.includes(index + 1)
182 ? { background: lineColors.solid }
183 : {
184 backgroundImage: `linear-gradient(to bottom, ${lineColors.dashed} 50%, transparent 50%)`,
185 backgroundSize: "1px 8px",
186 }
187 }
188 >
189 <AnimatePresence>
190 {animated && activeColumns[index] && (
191 <motion.div
192 key={`glow-${index}`}
193 className="absolute w-full"
194 style={{
195 height: glowSize,
196 background: `linear-gradient(to bottom, transparent, ${glowColor}, ${
197 darkMode ? "black" : "white"
198 })`,
199 opacity: glowOpacity,
200 }}
201 initial={
202 typeof animationVariants.initial === "function"
203 ? animationVariants.initial()
204 : animationVariants.initial
205 }
206 animate={
207 typeof animationVariants.animate === "function"
208 ? animationVariants.animate()
209 : animationVariants.animate
210 }
211 exit={
212 typeof animationVariants.initial === "function"
213 ? animationVariants.initial()
214 : animationVariants.initial
215 }
216 transition={{
217 duration: animationDuration,
218 repeat: Infinity,
219 ease: easingFunctions[easing],
220 delay: index * animationDelay,
221 }}
222 />
223 )}
224 </AnimatePresence>
225 </div>
226 </div>
227 ))}
228 </div>
229 </div>
230 </div>
231 )
232}
233