"use client"
import React, { useCallback, useEffect, useMemo, useState } from "react"
import { AnimatePresence, motion } from "motion/react"
type AnimationDirection = "top-to-bottom" | "bottom-to-top" | "both" | "random"
type AnimationEasing = "linear" | "easeIn" | "easeOut" | "easeInOut" | "spring"
interface AnimatedBackgroundGuidesProps {
columnCount?: number
className?: string
solidLines?: number[]
animated?: boolean
animationDuration?: number
animationDelay?: number
glowColor?: string
glowSize?: string
glowOpacity?: number
randomize?: boolean
randomInterval?: number
direction?: AnimationDirection
easing?: AnimationEasing
responsive?: boolean
minColumnWidth?: string
maxActiveColumns?: number
darkMode?: boolean
contained?: boolean
}
const easingFunctions = {
linear: [0, 0, 1, 1],
easeIn: [0.42, 0, 1, 1],
easeOut: [0, 0, 0.58, 1],
easeInOut: [0.42, 0, 0.58, 1],
spring: [0.175, 0.885, 0.32, 1.275],
}
export function StripeBgGuides({
columnCount = 6,
className = "",
solidLines = [],
animated = true,
animationDuration = 45,
animationDelay = 1.2,
glowColor = "#64FFDA", // Icy cyan
glowSize = "15vh",
glowOpacity = 0.6,
randomize = true,
randomInterval = 7000,
direction = "both",
easing = "spring",
responsive = true,
minColumnWidth = "3rem",
maxActiveColumns = 4,
darkMode = true,
contained = false,
}: AnimatedBackgroundGuidesProps) {
const [windowWidth, setWindowWidth] = useState(
typeof window !== "undefined" ? window.innerWidth : 0
)
const columns = useMemo(() => {
const count = responsive
? Math.max(Math.floor(windowWidth / parseInt(minColumnWidth)), 1)
: columnCount
return [...Array(count)]
}, [columnCount, responsive, windowWidth, minColumnWidth])
const [activeColumns, setActiveColumns] = useState<boolean[]>(
columns.map(() => true)
)
const getRandomColumns = useCallback(() => {
const newActiveColumns = columns.map(() => Math.random() < 0.4)
const activeCount = newActiveColumns.filter(Boolean).length
if (activeCount > maxActiveColumns) {
const indicesToDeactivate = newActiveColumns
.map((isActive, index) => (isActive ? index : -1))
.filter((index) => index !== -1)
.sort(() => Math.random() - 0.5)
.slice(0, activeCount - maxActiveColumns)
indicesToDeactivate.forEach((index) => {
newActiveColumns[index] = false
})
}
return newActiveColumns
}, [columns, maxActiveColumns])
useEffect(() => {
const handleResize = () => setWindowWidth(window.innerWidth)
if (typeof window !== "undefined") {
window.addEventListener("resize", handleResize)
return () => window.removeEventListener("resize", handleResize)
}
}, [])
useEffect(() => {
setActiveColumns(columns.map(() => true))
}, [columns])
useEffect(() => {
if (randomize && animated) {
const intervalId = setInterval(() => {
setActiveColumns(getRandomColumns())
}, randomInterval)
return () => clearInterval(intervalId)
} else {
setActiveColumns(columns.map(() => true))
}
}, [randomize, animated, randomInterval, getRandomColumns, columns])
const getAnimationVariants = useCallback(() => {
const variants = {
"top-to-bottom": {
initial: { top: "-120%" },
animate: { top: "120%" },
},
"bottom-to-top": {
initial: { top: "120%" },
animate: { top: "-120%" },
},
both: {
initial: { top: "120%" },
animate: { top: ["-120%", "120%"] },
},
random: {
initial: () => ({ top: Math.random() < 0.5 ? "-120%" : "120%" }),
animate: () => ({ top: Math.random() < 0.5 ? "-120%" : "120%" }),
},
}
return variants[direction] || variants["top-to-bottom"]
}, [direction])
const animationVariants = useMemo(
() => getAnimationVariants(),
[getAnimationVariants]
)
const nordicColors = useMemo(() => {
return {
// Nordic palette
darkBlue: "#1e2139",
deepBlue: "#16213e",
iceBlue: "#4A90E2",
frost: "#87CEEB",
glacial: "#B0E0E6",
aurora: "#64FFDA",
// Line colors
solid: "rgba(135, 206, 235, 0.3)", // Light ice blue
dashed: "rgba(176, 224, 230, 0.2)", // Glacial blue
background: "linear-gradient(135deg, #0B1426 0%, #1e2139 50%, #16213e 100%)",
}
}, [])
return (
<div
className={`pointer-events-none ${
contained ? "absolute inset-0" : "fixed inset-0"
} ${className}`}
aria-hidden="true"
style={{
zIndex: contained ? 0 : -1,
background: nordicColors.background,
}}
>
{/* Aurora background overlay */}
<div
className="absolute inset-0 opacity-20"
style={{
background: `
radial-gradient(ellipse at 20% 10%, rgba(100, 255, 218, 0.1) 0%, transparent 50%),
radial-gradient(ellipse at 80% 90%, rgba(135, 206, 235, 0.08) 0%, transparent 50%),
radial-gradient(ellipse at 40% 70%, rgba(176, 224, 230, 0.05) 0%, transparent 50%)
`
}}
/>
{/* Subtle grain texture */}
<div
className="absolute inset-0 opacity-30"
style={{
backgroundImage: `url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.02'%3E%3Ccircle cx='7' cy='7' r='1'/%3E%3Ccircle cx='27' cy='27' r='1'/%3E%3Ccircle cx='47' cy='47' r='1'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E")`,
}}
/>
<div className="z-0 h-full w-full px-4 sm:px-6 lg:px-24">
<div
className="mx-auto h-full w-full"
style={{
display: "grid",
gridTemplateColumns: responsive
? `repeat(auto-fit, minmax(${minColumnWidth}, 1fr))`
: `repeat(${columnCount}, minmax(0, 1fr))`,
gap: "2rem",
}}
>
{columns.map((_, index) => (
<div key={index} className="relative h-full">
<div
className={`absolute inset-y-0 ${
index === 0
? "left-0"
: index === columns.length - 1
? "right-0"
: "left-1/2"
} w-px overflow-hidden`}
style={
solidLines.includes(index + 1)
? {
background: nordicColors.solid,
boxShadow: `0 0 2px ${nordicColors.solid}`,
}
: {
backgroundImage: `linear-gradient(to bottom, ${nordicColors.dashed} 50%, transparent 50%)`,
backgroundSize: "1px 12px",
}
}
>
<AnimatePresence>
{animated && activeColumns[index] && (
<>
{/* Main glow */}
<motion.div
key={`glow-main-${index}`}
className="absolute w-full"
style={{
height: glowSize,
background: `linear-gradient(to bottom,
transparent 0%,
${glowColor}40 20%,
${glowColor}80 50%,
${glowColor}40 80%,
transparent 100%
)`,
opacity: glowOpacity,
filter: "blur(1px)",
}}
initial={
typeof animationVariants.initial === "function"
? animationVariants.initial()
: animationVariants.initial
}
animate={
typeof animationVariants.animate === "function"
? animationVariants.animate()
: animationVariants.animate
}
exit={
typeof animationVariants.initial === "function"
? animationVariants.initial()
: animationVariants.initial
}
transition={{
duration: animationDuration,
repeat: Infinity,
ease: easingFunctions[easing],
delay: index * animationDelay,
}}
/>
{/* Inner core glow */}
<motion.div
key={`glow-core-${index}`}
className="absolute w-full"
style={{
height: `calc(${glowSize} * 0.6)`,
background: `linear-gradient(to bottom,
transparent 0%,
#FFFFFF60 30%,
${glowColor}FF 50%,
#FFFFFF60 70%,
transparent 100%
)`,
opacity: glowOpacity * 0.8,
filter: "blur(0.5px)",
}}
initial={
typeof animationVariants.initial === "function"
? animationVariants.initial()
: animationVariants.initial
}
animate={
typeof animationVariants.animate === "function"
? animationVariants.animate()
: animationVariants.animate
}
exit={
typeof animationVariants.initial === "function"
? animationVariants.initial()
: animationVariants.initial
}
transition={{
duration: animationDuration,
repeat: Infinity,
ease: easingFunctions[easing],
delay: index * animationDelay + 0.1,
}}
/>
{/* Outer halo */}
<motion.div
key={`glow-halo-${index}`}
className="absolute w-full"
style={{
height: `calc(${glowSize} * 1.4)`,
background: `radial-gradient(ellipse at center,
${nordicColors.aurora}20 0%,
${nordicColors.frost}10 40%,
transparent 70%
)`,
opacity: glowOpacity * 0.4,
filter: "blur(3px)",
left: "-50px",
width: "100px",
transform: "translateX(-50%)",
}}
initial={
typeof animationVariants.initial === "function"
? animationVariants.initial()
: animationVariants.initial
}
animate={
typeof animationVariants.animate === "function"
? animationVariants.animate()
: animationVariants.animate
}
exit={
typeof animationVariants.initial === "function"
? animationVariants.initial()
: animationVariants.initial
}
transition={{
duration: animationDuration * 1.1,
repeat: Infinity,
ease: easingFunctions[easing],
delay: index * animationDelay - 0.2,
}}
/>
</>
)}
</AnimatePresence>
</div>
</div>
))}
</div>
</div>
</div>
)
}