index.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React, { useRef, useCallback } from 'react'
  2. import styles from './index.module.scss'
  3. import 'media-chrome'
  4. import { baseOssUrl } from '@/utils/http'
  5. function Zvideo({ src, objFit = 'fill' ,style}: { src: string; objFit?: string,style?: React.CSSProperties }) {
  6. const videoRef = useRef<HTMLVideoElement>(null)
  7. const handleTogglePlay = useCallback(() => {
  8. const video = videoRef.current
  9. if (!video) return
  10. if (video.paused) {
  11. video.play()
  12. } else {
  13. video.pause()
  14. }
  15. }, [])
  16. return (
  17. <div
  18. className={styles.Zvideo}
  19. style={style}
  20. onKeyDown={(e) => { if (e.key === ' ' || e.key === 'Enter') { e.preventDefault(); handleTogglePlay() } }}
  21. role="button"
  22. tabIndex={0}
  23. >
  24. <media-controller class='custom-media-controller' gesturesdisabled={true}>
  25. <video
  26. ref={videoRef}
  27. slot='media'
  28. src={baseOssUrl + src}
  29. className='modal-video'
  30. style={{ objectFit: objFit as any }}
  31. onClick={(e) => {
  32. e.stopPropagation()
  33. handleTogglePlay()
  34. }}
  35. ></video>
  36. <media-control-bar>
  37. <media-play-button></media-play-button>
  38. <div className='progress'>
  39. <media-time-range></media-time-range>
  40. </div>
  41. <div className='time'>
  42. <media-time-display></media-time-display>/
  43. <media-duration-display></media-duration-display>
  44. </div>
  45. </media-control-bar>
  46. </media-controller>
  47. </div>
  48. )
  49. }
  50. const MemoZvideo = React.memo(Zvideo)
  51. export default MemoZvideo