How to Install and Apply the Code Override

Follow these quick steps to add the SmartSession override to your SmartPreloader component.

import type { ComponentType } from "react"
import React, { useState, useEffect, useLayoutEffect } from "react"
import { RenderTarget } from "framer"

export function withSmartSession(Component): ComponentType {
    return (props) => {
        const isCanvas = RenderTarget.current() === RenderTarget.canvas

        const [shouldRender, setShouldRender] = useState(true)

        // Sync this value with startDelayMs + totalRevealMs + fadeMs from SmartPreloader
        const animationDuration = 2600

        useLayoutEffect(() => {
            if (!isCanvas && sessionStorage.getItem("preloaderPlayed")) {
                setShouldRender(false)
            }
        }, [isCanvas])

        useEffect(() => {
            if (isCanvas || !shouldRender) return

            const timer = setTimeout(() => {
                setShouldRender(false)
                sessionStorage.setItem("preloaderPlayed", "true")
            }, animationDuration)

            return () => clearTimeout(timer)
        }, [isCanvas, shouldRender, animationDuration])

        if (!shouldRender && !isCanvas) {
            return (
                <Component
                    {...props}
                    style={{
                        ...props.style,
                        display: "none",
                        pointerEvents: "none",
                    }}
                />
            )
        }

        return (
            <React.Fragment>
                {!isCanvas && (
                    <script
                        dangerouslySetInnerHTML={{
                            __html: `
                                (function(){
                                    try {
                                        if (sessionStorage.getItem("preloaderPlayed")) {
                                            var me = document.currentScript;
                                            if (me && me.parentElement) {
                                                me.parentElement.style.display = 'none';
                                            }
                                        }
                                    } catch(e) {}
                                })();
                            `,
                        }}
                    />
                )}
                <Component {...props} />
            </React.Fragment>
        )
    }
}

01

Create a New Code File

From the left sidebar, select Assets, find Code, and click the + sign, then select New Code File and New Override.

Name it as PreloaderLogic.

02

Paste the Code

Delete any boilerplate code, paste the code override, then save the file with Ctrl + S or Cmd + S.

03

Select the Component

Return to the canvas and select your SmartPreloader component.

Make sure your Smart Preloader component is on top of every layer. In the right sidebar, find Styles, set Z-index to 10, and Pointer Events to None.

04

Assign the Override

In the right sidebar, locate the Code Overrides section.

Under File, choose the PreloaderLogic code file. Under Function, select withSmartSession.

I disappear so well that even Framer’s invisible ghost layers forgot I was there.

I disappear so well that even Framer’s invisible ghost layers forgot I was there.

Create a free website with Framer, the website builder loved by startups, designers and agencies.