Search

Back

Copy Link

Library

/

Free

/

108

Attempted Escape

108

Attempted Escape

When the user's cursor leaves the window this layer will appear

Not loading? Preview

Made by

@frameroverrides

override

click

Override

Attempted Escape

Copy

Override

Copied to Clipboard!

Instructions

  1. Add a wrapper around the content then apply withAttemptedEscape to prevent the layout from collapsing

  2. Apply withHide to your close icon to allow the user to dismiss your frame

  3. Change position from "relative" or "absolute" to "fixed" for your frame to be always in the center of your webpage

Code Preview

import React, { useEffect } from "react"
import type { ComponentType } from "react"
import { createStore } from "https://framer.com/m/framer/store.js@^1.0.0"

const useStore = createStore({
    isVisible: false,
    hasBeenShown: false, // Add a flag to track if the frame has been shown
})

export const withAttemptedEscape = (Component): ComponentType => {
    return (props) => {
        const [store, setStore] = useStore()

        useEffect(() => {
            const handleMouseOut = (event) => {
                if (
                    !event.relatedTarget &&
                    !event.toElement &&
                    !store.hasBeenShown
                ) {
                    setStore({ isVisible: true, hasBeenShown: true }) // Update the flag when shown
                }
            }

            document.addEventListener("mouseout", handleMouseOut)

            return () => {
                document.removeEventListener("mouseout", handleMouseOut)
            }
        }, [store, setStore]) // Add store to the dependency array

        return (
            <Component
                {...props}
                style={{
                    ...props.style,
                    display: store.isVisible ? "block" : "none",
                }}
            />
        )
    }
}

export const withHide = (Component): ComponentType => {
    return (props) => {
        const [store, setStore] = useStore()

        const onClick = () => {
            // Hide the frame on click
            setStore({ isVisible: false })
        }

        return (
            <Component
                {...props}
                onClick={onClick}
                style={{
                    ...props.style,
                    display: store.isVisible ? "block" : "none",
                }}
            />
        )
    }
}

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.