{"version":3,"file":"PlayWhenVisibleOverride.BqOhuTdF.mjs","names":["forwardRef","useRef","useMemo","useCallback","__esmMin","ne","init_npm_react_18_3_1","pe","window","de","J","init_jsx_runtime","ce","init_ssg_sandbox_shims","useIsStaticRenderer","addPropertyControls","ControlType","init_framer_Cb9Z_4vX","RenderTarget","withPlayWhenVisible","rest","props","isStatic","isCanvas","shouldRun","rootRef","videosRef","rafRef","mutationObserverRef","intersectionObserverRef","isNearRef","activatedRef","safeRootMargin","rootMargin","setRefs","node","ref","applyPlayback","video","shouldPlay","result","pauseWhenHidden","resolveVideos","root","nextVideos","shouldPlayNow","attempts","stopped","stopRaf","pollForVideos","count","entry","entries","isNear","Component","init_PlayWhenVisibleOverride"],"sources":["https:/framerusercontent.com/modules/gyeGRNLJ6EwYCGlses6v/XPJXwu5PdqJTx412XImZ/PlayWhenVisibleOverride.js"],"sourcesContent":["// User request: Create a `withPlayWhenVisible` code override that plays native videos near viewport (one fold early) and pauses when far offscreen, with SSR/canvas safety and controls for rootMargin + pauseWhenHidden.\nimport{jsx as _jsx}from\"react/jsx-runtime\";import{forwardRef,useCallback,useEffect,useMemo,useRef}from\"react\";import{addPropertyControls,ControlType,RenderTarget,useIsStaticRenderer}from\"framer\";export function withPlayWhenVisible(Component){const Wrapped=/*#__PURE__*/forwardRef(function WithPlayWhenVisible(props,ref){const{rootMargin=\"100% 0px 100% 0px\",pauseWhenHidden=true,...rest}=props;const isStatic=useIsStaticRenderer();const isCanvas=RenderTarget.current()===RenderTarget.canvas;const shouldRun=!isStatic&&!isCanvas;const rootRef=useRef(null);const videosRef=useRef([]);const rafRef=useRef(null);const mutationObserverRef=useRef(null);const intersectionObserverRef=useRef(null);const isNearRef=useRef(false);const activatedRef=useRef(false);const safeRootMargin=useMemo(()=>{return rootMargin?.trim()?rootMargin:\"100% 0px 100% 0px\";},[rootMargin]);const setRefs=useCallback(node=>{rootRef.current=node;if(typeof ref===\"function\"){ref(node);}else if(ref&&\"current\"in ref){ref.current=node;}},[ref]);const applyPlayback=useCallback(shouldPlay=>{for(const video of videosRef.current){if(shouldPlay){const result=video.play();if(result&&typeof result.catch===\"function\"){result.catch(()=>{});}}else if(pauseWhenHidden){video.pause();}}},[pauseWhenHidden]);const resolveVideos=useCallback(()=>{const root=rootRef.current;if(!root)return 0;const nextVideos=Array.from(root.querySelectorAll(\"video\"));videosRef.current=nextVideos;const shouldPlayNow=isNearRef.current||!pauseWhenHidden&&activatedRef.current;applyPlayback(shouldPlayNow);return nextVideos.length;},[applyPlayback,pauseWhenHidden]);useEffect(()=>{if(typeof window===\"undefined\"||typeof document===\"undefined\"){return;}if(!shouldRun)return;const root=rootRef.current;if(!root)return;let attempts=0;let stopped=false;const stopRaf=()=>{if(rafRef.current!==null){window.cancelAnimationFrame(rafRef.current);rafRef.current=null;}};const pollForVideos=()=>{if(stopped)return;const count=resolveVideos();attempts+=1;if(count===0&&attempts<90){rafRef.current=window.requestAnimationFrame(pollForVideos);}};pollForVideos();if(typeof window.MutationObserver!==\"undefined\"){mutationObserverRef.current=new window.MutationObserver(()=>{resolveVideos();});mutationObserverRef.current.observe(root,{childList:true,subtree:true});}if(typeof window.IntersectionObserver!==\"undefined\"){intersectionObserverRef.current=new window.IntersectionObserver(entries=>{for(const entry of entries){const isNear=entry.isIntersecting;isNearRef.current=isNear;if(isNear)activatedRef.current=true;const shouldPlayNow=isNear||!pauseWhenHidden&&activatedRef.current;applyPlayback(shouldPlayNow);}},{root:null,rootMargin:safeRootMargin,threshold:0});intersectionObserverRef.current.observe(root);}else{isNearRef.current=true;activatedRef.current=true;applyPlayback(true);}return()=>{stopped=true;stopRaf();if(mutationObserverRef.current){mutationObserverRef.current.disconnect();mutationObserverRef.current=null;}if(intersectionObserverRef.current){intersectionObserverRef.current.disconnect();intersectionObserverRef.current=null;}};},[applyPlayback,resolveVideos,safeRootMargin,shouldRun,pauseWhenHidden]);return /*#__PURE__*/_jsx(Component,{...rest,ref:setRefs});});return Wrapped;}addPropertyControls(withPlayWhenVisible,{rootMargin:{type:ControlType.String,title:\"Preload Margin\",defaultValue:\"100% 0px 100% 0px\"},pauseWhenHidden:{type:ControlType.Boolean,title:\"Pause When Hidden\",defaultValue:true,enabledTitle:\"On\",disabledTitle:\"Off\"}});\nexport const __FramerMetadata__ = {\"exports\":{\"withPlayWhenVisible\":{\"type\":\"reactHoc\",\"name\":\"withPlayWhenVisible\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./PlayWhenVisibleOverride.map"],"mappings":"A,gOACmM,SAAgBmB,EAAoB,EAAU,CAAo5F,MAAx3FnB,kBAAW,SAA6B,EAAM,EAAI,CAAC,GAAK,CAAC,aAAW,oBAAoB,kBAAgB,GAAK,GAAGoB,GAAMC,EAAYC,EAASR,EAAoB,EAAQS,EAASL,EAAa,QAAQ,IAAIA,EAAa,OAAaM,EAAU,CAACF,GAAU,CAACC,EAAeE,EAAQxB,EAAO,IAAI,EAAQyB,EAAUzB,EAAO,CAAC,CAAC,EAAQ0B,EAAO1B,EAAO,IAAI,EAAQ2B,EAAoB3B,EAAO,IAAI,EAAQ4B,EAAwB5B,EAAO,IAAI,EAAQ6B,EAAU7B,EAAO,EAAK,EAAQ8B,EAAa9B,EAAO,EAAK,EAAQ+B,EAAe9B,MAAoB+B,GAAY,KAAK,EAAEA,EAAW,oBAAsB,CAACA,CAAU,CAAC,EAAQC,EAAQ/B,EAAY,GAAM,CAAC,EAAQ,QAAQgC,EAAQ,OAAOC,GAAM,WAAYA,EAAID,CAAI,EAAWC,GAAK,YAAYA,IAAK,EAAI,QAAQD,EAAM,EAAE,CAACC,CAAG,CAAC,EAAQC,EAAclC,EAAY,GAAY,CAAC,IAAI,IAAMmC,KAASZ,EAAU,QAAS,GAAGa,EAAW,CAAC,IAAMC,EAAOF,EAAM,KAAK,EAAKE,GAAQ,OAAOA,EAAO,OAAQ,YAAYA,EAAO,UAAU,CAAC,CAAC,CAAG,MAASC,GAAiB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