1
+ 'use client' ;
2
+
3
+ import * as React from "react" ;
4
+ import { useTheme } from "next-themes" ;
5
+
6
+ import { useMounted } from '@/lib/hooks/use-mounted' ;
7
+
1
8
import { StyledIframe } from "./video-player.styles" ;
2
9
3
10
const urlOptions = new URLSearchParams ( {
@@ -14,23 +21,47 @@ export const VideoPlayer = (props: {
14
21
lightId : string ,
15
22
aspectRatio : string
16
23
} ) => {
24
+ const { isMounted } = useMounted ( ) ;
25
+ const { resolvedTheme : theme } = useTheme ( ) ;
26
+
17
27
const darkUrl = `https://iframe.mediadelivery.net/embed/${ props . libraryId } /${ props . darkId } ?${ urlOptions } ` ;
18
28
const lightUrl = `https://iframe.mediadelivery.net/embed/${ props . libraryId } /${ props . lightId } ?${ urlOptions } ` ;
19
29
30
+ const darkVisibility = isMounted
31
+ ? theme === 'dark'
32
+ : undefined ;
33
+ const lightVisibility = isMounted
34
+ ? theme === 'light'
35
+ : undefined ;
36
+
20
37
return < >
21
- < StyledIframe
22
- src = { lightUrl }
23
- data-hide-on-theme = "dark"
24
- aspectRatio = { props . aspectRatio }
25
- allow = "autoplay;picture-in-picture;"
26
- allowFullScreen = { true }
27
- />
28
- < StyledIframe
29
- src = { darkUrl }
30
- data-hide-on-theme = "light"
31
- aspectRatio = { props . aspectRatio }
32
- allow = "autoplay;picture-in-picture;"
33
- allowFullScreen = { true }
34
- />
38
+ { ( ! isMounted || theme === 'dark' ) &&
39
+ < StyledIframe
40
+ src = { darkUrl }
41
+
42
+ loading = "lazy"
43
+ // Hide if wrong theme
44
+ data-hide-on-theme = "light"
45
+ $visible = { darkVisibility }
46
+
47
+ $aspectRatio = { props . aspectRatio }
48
+ allow = "autoplay;picture-in-picture;"
49
+ allowFullScreen = { true }
50
+ />
51
+ }
52
+ { ( ! isMounted || theme === 'light' ) &&
53
+ < StyledIframe
54
+ src = { lightUrl }
55
+
56
+ loading = "lazy"
57
+ // Hide if wrong theme
58
+ data-hide-on-theme = "dark"
59
+ $visible = { lightVisibility }
60
+
61
+ $aspectRatio = { props . aspectRatio }
62
+ allow = "autoplay;picture-in-picture;"
63
+ allowFullScreen = { true }
64
+ />
65
+ }
35
66
</ > ;
36
67
} ;
0 commit comments