{"version":3,"file":"PlayWhenVisibleOverride.CBX52O12.mjs","names":["forwardRef","useRef","useMemo","useCallback"],"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":"+NACmM,SAAgB,EAAoB,EAAU,C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