Search

Back

Copy Link

Library

/

Free

/

31

Random Rotate

31

Random Rotate

Not loading? Preview

Made by

Joe

override

click

Override

Random Rotate

Copy

Override

Copied to Clipboard!

Instructions

Code Preview

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

export const withHover = (Component: ComponentType): ComponentType => {
    return (props) => {
        const [angle, setAngle] = useState(0)

        const randomRotation = () => {
            const degree = Math.floor(Math.random() * 21) + 5
            const sign = Math.random() < 0.5 ? -1 : 1
            return degree * sign
        }

        return (
            <Component
                {...props}
                whileHover={{ rotate: angle }}
                onMouseEnter={() => setAngle(randomRotation())}
            />
        )
    }
}

export const onPageLoad = (Component: ComponentType): ComponentType => {
    return (props) => {
        const randomRotation = () => {
            const degree = Math.floor(Math.random() * 10) // Now the rotation can be from 0 to 360
            const sign = Math.random() < 0.5 ? -1 : 1
            return degree * sign
        }

        // Angle is set to a random rotation immediately
        const [angle, setAngle] = useState(randomRotation())

        useEffect(() => {
            setAngle(randomRotation())
        }, [])

        return (
            <motion.div
                as={Component}
                {...props}
                initial={{ rotate: 0 }}
                animate={{ rotate: angle }}
                transition={{ type: "spring", stiffness: 200, damping: 20 }}
            />
        )
    }
}

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.