import React, { useRef, useMemo } from 'react'; import { getWebProps } from 'react-native-unistyles/web'; import { videoStyles } from './Video.styles'; import type { VideoProps, VideoSource } from './types'; import useMergeRefs from '../hooks/useMergeRefs'; import { getWebAriaProps } from '../utils/accessibility'; const Video: React.FC = ({ source, poster, width, height, aspectRatio, controls = true, autoPlay = false, loop = false, muted = false, playsInline = true, preload = 'metadata', onLoad, onError, onPlay, onPause, onEnd, onProgress, borderRadius, style, testID, id, // Accessibility props accessibilityLabel, accessibilityHint, accessibilityRole, accessibilityHidden, }) => { // Generate ARIA props const ariaProps = useMemo(() => { return getWebAriaProps({ accessibilityLabel: accessibilityLabel ?? 'Video player', accessibilityHint, accessibilityRole, accessibilityHidden, }); }, [accessibilityLabel, accessibilityHint, accessibilityRole, accessibilityHidden]); const videoRef = useRef(null); const containerProps = getWebProps([videoStyles.container, style as any]); const videoProps = getWebProps([videoStyles.video as any]); const videoSource = typeof source === 'string' ? source : (source as VideoSource).uri; const videoType = typeof source === 'object' ? (source as VideoSource).type : undefined; const handleLoadedMetadata = () => { onLoad?.(); }; const handleError = (e: React.SyntheticEvent) => { onError?.(e); }; const handlePlay = () => { onPlay?.(); }; const handlePause = () => { onPause?.(); }; const handleEnded = () => { onEnd?.(); }; const handleTimeUpdate = () => { if (videoRef.current && onProgress) { onProgress({ currentTime: videoRef.current.currentTime, playableDuration: videoRef.current.duration || 0, }); } }; const containerStyle: React.CSSProperties = { width: width || '100%', height: height || 'auto', aspectRatio: aspectRatio ? String(aspectRatio) : undefined, borderRadius: borderRadius ? `${borderRadius}px` : undefined, }; const vidStyle: React.CSSProperties = { borderRadius: borderRadius ? `${borderRadius}px` : undefined, }; const mergedVideoRef = useMergeRefs(videoRef, videoProps.ref); return (
); }; export default Video;