|
4 | 4 | import { useInView } from 'react-intersection-observer' |
5 | 5 | import { useMediaQueries } from '@react-hook/media-query' |
6 | 6 | import { useEffect, type ReactElement, useRef, useCallback, type MutableRefObject } from 'react' |
7 | | -import type { LazyVideoProps } from './types/lazyVideoTypes'; |
8 | | -import { fillStyles, transparentGif } from './lib/styles' |
| 7 | +import type { LazyVideoProps } from '../types/lazyVideoTypes'; |
| 8 | +import { fillStyles, transparentGif } from '../lib/styles' |
9 | 9 |
|
10 | | -type VideoSourceProps = { |
11 | | - src: Required<LazyVideoProps>['src'] |
12 | | - videoLoader: LazyVideoProps['videoLoader'] |
| 10 | +type LazyVideoClientProps = Omit<LazyVideoProps, |
| 11 | + 'videoLoader' | 'src' | 'sourceMedia' |
| 12 | +> & { |
| 13 | + srcUrl?: string |
| 14 | + mediaSrcs?: Record<string, string> |
13 | 15 | } |
14 | 16 |
|
15 | | -type ResponsiveVideoSourceProps = Pick<Required<LazyVideoProps>, |
16 | | - 'src' | 'videoLoader' | 'sourceMedia' |
17 | | -> & { |
| 17 | +type ResponsiveVideoSourceProps = { |
| 18 | + mediaSrcs: Required<LazyVideoClientProps>['mediaSrcs'] |
18 | 19 | videoRef: VideoRef |
19 | 20 | } |
20 | 21 |
|
21 | 22 | type VideoRef = MutableRefObject<HTMLVideoElement | undefined> |
22 | 23 |
|
23 | 24 | // An video rendered within a Visual that supports lazy loading |
24 | | -export default function LazyVideo({ |
25 | | - src, sourceMedia, videoLoader, |
| 25 | +export default function LazyVideoClient({ |
| 26 | + srcUrl, mediaSrcs, |
26 | 27 | alt, fit, position, priority, noPoster, paused, |
27 | | -}: LazyVideoProps): ReactElement { |
| 28 | +}: LazyVideoClientProps): ReactElement { |
28 | 29 |
|
29 | 30 | // Make a ref to the video so it can be controlled |
30 | 31 | const videoRef = useRef<HTMLVideoElement>() |
@@ -67,11 +68,6 @@ export default function LazyVideo({ |
67 | 68 | // Simplify logic for whether to load sources |
68 | 69 | const shouldLoad = priority || inView |
69 | 70 |
|
70 | | - // Multiple media queries and a loader func are necessary for responsive |
71 | | - const useResponsiveSource = sourceMedia |
72 | | - && sourceMedia?.length > 1 |
73 | | - && !!videoLoader |
74 | | - |
75 | 71 | // Render video tag |
76 | 72 | return ( |
77 | 73 | <video |
@@ -100,39 +96,21 @@ export default function LazyVideo({ |
100 | 96 | }}> |
101 | 97 |
|
102 | 98 | {/* Implement lazy loading by not adding the source until ready */} |
103 | | - { shouldLoad && (useResponsiveSource ? |
104 | | - <ResponsiveSource { ...{ src, videoLoader, sourceMedia, videoRef }} /> : |
105 | | - <Source {...{ src, videoLoader }} /> |
| 99 | + { shouldLoad && (mediaSrcs ? |
| 100 | + <ResponsiveSource { ...{ mediaSrcs, videoRef }} /> : |
| 101 | + <source src={ srcUrl } type='video/mp4' /> |
106 | 102 | )} |
107 | 103 | </video> |
108 | 104 | ) |
109 | 105 | } |
110 | 106 |
|
111 | | -// Return a simple source element |
112 | | -function Source({ |
113 | | - src, videoLoader |
114 | | -}: VideoSourceProps): ReactElement | undefined { |
115 | | - let srcUrl |
116 | | - if (videoLoader) srcUrl = videoLoader({ src }) |
117 | | - else if (typeof src == 'string') srcUrl = src |
118 | | - if (!srcUrl) return |
119 | | - return (<source src={ srcUrl } type='video/mp4' />) |
120 | | -} |
121 | | - |
122 | 107 | // Switch the video asset depending on media queries |
123 | 108 | function ResponsiveSource({ |
124 | | - src, videoLoader, sourceMedia, videoRef |
| 109 | + mediaSrcs, videoRef |
125 | 110 | }: ResponsiveVideoSourceProps): ReactElement | undefined { |
126 | 111 |
|
127 | | - // Prepare a hash of source URLs and their media query constraint in the |
128 | | - // style expected by useMediaQueries |
129 | | - const queries = Object.fromEntries(sourceMedia.map(media => { |
130 | | - const url = videoLoader({ src, media }) |
131 | | - return [url, media] |
132 | | - })) |
133 | | - |
134 | 112 | // Find the src url that is currently active |
135 | | - const { matches } = useMediaQueries(queries) |
| 113 | + const { matches } = useMediaQueries(mediaSrcs) |
136 | 114 | const srcUrl = getFirstMatch(matches) |
137 | 115 |
|
138 | 116 | // Reload the video since the source changed |
|
0 commit comments