{"version":3,"file":"PlayWhenVisibleOverride.Ck4oHeNx.mjs","names":["forwardRef","useRef","useMemo","useCallback","useEffect","_jsx","__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_ChujFWHC","RenderTarget","withPlayWhenVisible","Component","props","ref","rootMargin","pauseWhenHidden","rest","isStatic","isCanvas","shouldRun","rootRef","videosRef","rafRef","mutationObserverRef","intersectionObserverRef","isNearRef","activatedRef","safeRootMargin","setRefs","node","applyPlayback","shouldPlay","video","result","resolveVideos","root","nextVideos","shouldPlayNow","attempts","stopped","stopRaf","pollForVideos","count","entries","entry","isNear","c","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,SAAgBqB,EAAoBC,EAAU,CAAo5F,MAAx3FtB,kBAAW,SAA6BuB,EAAMC,EAAI,CAAC,GAAK,CAACC,aAAW,oBAAoBC,kBAAgB,GAAK,GAAGC,GAAMJ,EAAYK,EAASZ,EAAoB,EAAQa,EAAST,EAAa,QAAQ,IAAIA,EAAa,OAAaU,EAAU,CAACF,GAAU,CAACC,EAAeE,EAAQ9B,EAAO,IAAI,EAAQ+B,EAAU/B,EAAO,CAAC,CAAC,EAAQgC,EAAOhC,EAAO,IAAI,EAAQiC,EAAoBjC,EAAO,IAAI,EAAQkC,EAAwBlC,EAAO,IAAI,EAAQmC,EAAUnC,EAAO,EAAK,EAAQoC,EAAapC,EAAO,EAAK,EAAQqC,EAAepC,MAAoBuB,GAAY,KAAK,EAAEA,EAAW,oBAAsB,CAACA,CAAU,CAAC,EAAQc,EAAQpC,EAAYqC,GAAM,CAACT,EAAQ,QAAQS,EAAQ,OAAOhB,GAAM,WAAYA,EAAIgB,CAAI,EAAWhB,GAAK,YAAYA,IAAKA,EAAI,QAAQgB,EAAM,EAAE,CAAChB,CAAG,CAAC,EAAQiB,EAActC,EAAYuC,GAAY,CAAC,IAAI,IAAMC,KAASX,EAAU,QAAS,GAAGU,EAAW,CAAC,IAAME,EAAOD,EAAM,KAAK,EAAKC,GAAQ,OAAOA,EAAO,OAAQ,YAAYA,EAAO,UAAU,CAAC,CAAC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