11'use client' ;
22
33import { motion } from 'framer-motion' ;
4+ import { useEffect , useRef , useState } from 'react' ;
45
56export default function Statistics ( ) {
67 const stats = [
@@ -10,6 +11,40 @@ export default function Statistics() {
1011 { number : "50+" , label : "Service Locations" }
1112 ] ;
1213
14+ const videoRef = useRef < HTMLVideoElement > ( null ) ;
15+ const [ isInView , setIsInView ] = useState ( false ) ;
16+ const [ hasPlayed , setHasPlayed ] = useState ( false ) ;
17+
18+ useEffect ( ( ) => {
19+ const video = videoRef . current ;
20+ if ( ! video ) return ;
21+
22+ const observer = new IntersectionObserver (
23+ ( entries ) => {
24+ entries . forEach ( ( entry ) => {
25+ if ( entry . isIntersecting && ! hasPlayed ) {
26+ setIsInView ( true ) ;
27+ setHasPlayed ( true ) ;
28+ // Start playing when in view
29+ video . play ( ) . catch ( ( error ) => {
30+ console . warn ( 'Video autoplay failed:' , error ) ;
31+ } ) ;
32+ }
33+ } ) ;
34+ } ,
35+ {
36+ threshold : 0.1 , // Trigger when 10% of video is visible
37+ rootMargin : '50px' // Start loading 50px before entering viewport
38+ }
39+ ) ;
40+
41+ observer . observe ( video ) ;
42+
43+ return ( ) => {
44+ observer . disconnect ( ) ;
45+ } ;
46+ } , [ hasPlayed ] ) ;
47+
1348 return (
1449 < section className = "py-20 md:py-28 bg-white" >
1550 < div className = "max-w-[1400px] mx-auto px-6 md:px-12" >
@@ -25,12 +60,14 @@ export default function Statistics() {
2560 >
2661 < div className = "h-48 sm:h-64 md:h-80 lg:aspect-[16/9] rounded-2xl overflow-hidden shadow-2xl bg-black" >
2762 < video
28- autoPlay
63+ ref = { videoRef }
2964 loop
3065 muted
3166 playsInline
67+ preload = { isInView ? "metadata" : "none" }
3268 className = "w-full h-full object-cover"
3369 poster = "/hero-1.jpg"
70+ onError = { ( e ) => console . error ( 'Video failed to load:' , e ) }
3471 >
3572 < source src = "/ambe-service-video.mp4" type = "video/mp4" />
3673 Your browser does not support the video tag.
0 commit comments