Search

Back

Copy Link

Library

/

Free

/

33

Random Gradient

33

Random Gradient

Not loading? Preview

Made by

@frameroverrides

override

hover

Override

Random Gradient

Copy

Override

Copied to Clipboard!

Instructions

Code Preview

import type { ComponentType } from "react"
import { useState } from "react"
import { motion } from "framer-motion"

export function withRandomGradientOnHover(Component): ComponentType {
    return (props) => {
        const [background, setBackground] = useState("")

        // Generate a random color
        const randomColor = () => {
            const random = (min, max) =>
                Math.floor(Math.random() * (max - min + 1) + min)
            return `hsl(${random(0, 360)}, ${random(50, 100)}%, ${random(
                40,
                60
            )}%)`
        }

        // Generate a random gradient
        const randomGradient = () => {
            const angle = Math.floor(Math.random() * 360)
            const color1 = randomColor()
            const color2 = randomColor()
            return `linear-gradient(${angle}deg, ${color1}, ${color2})`
        }

        const handleMouseEnter = () => {
            setBackground(randomGradient())
        }

        const handleMouseLeave = () => {
            setBackground("")
        }

        return (
            <Component
                {...props}
                as={motion.div}
                style={{ ...props.style, background }}
                onMouseEnter={handleMouseEnter}
                onMouseLeave={handleMouseLeave}
            />
        )
    }
}

Terms of Use for Free Assets

By accessing and using this Framer Override Library, you agree to the following terms:

All code snippets provided are available for your personal and professional use. This includes personal websites, client projects, and other website projects in Framer. You are strictly prohibited from sharing, redistributing, selling these code snippets, or creating derivative works for resale or distribution. Unauthorized sharing, distribution, or selling of these code snippets is a breach of these terms and may result in termination of your access to this library, along with potential legal action.