|
|
|
@ -1,6 +1,7 @@ |
|
|
|
import { |
|
|
|
import { |
|
|
|
memo, |
|
|
|
memo, |
|
|
|
RefObject, |
|
|
|
RefObject, |
|
|
|
|
|
|
|
useEffect, |
|
|
|
} from 'react' |
|
|
|
} from 'react' |
|
|
|
|
|
|
|
|
|
|
|
import styled from 'styled-components/macro' |
|
|
|
import styled from 'styled-components/macro' |
|
|
|
@ -15,10 +16,11 @@ const PipWrapper = styled.div` |
|
|
|
` |
|
|
|
` |
|
|
|
|
|
|
|
|
|
|
|
type PipProps = { |
|
|
|
type PipProps = { |
|
|
|
|
|
|
|
isPlaying: boolean, |
|
|
|
videoRef: RefObject<HTMLVideoElement>, |
|
|
|
videoRef: RefObject<HTMLVideoElement>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const PiP = memo(({ videoRef }: PipProps) => { |
|
|
|
export const PiP = memo(({ isPlaying, videoRef }: PipProps) => { |
|
|
|
const togglePip = async () => { |
|
|
|
const togglePip = async () => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
@ -33,6 +35,20 @@ export const PiP = memo(({ videoRef }: PipProps) => { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
window.addEventListener('visibilitychange', async () => { |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
document.hidden === true |
|
|
|
|
|
|
|
&& document.pictureInPictureEnabled |
|
|
|
|
|
|
|
&& videoRef.current !== document.pictureInPictureElement |
|
|
|
|
|
|
|
&& videoRef.current?.hidden === false |
|
|
|
|
|
|
|
&& isPlaying |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
await videoRef.current?.requestPictureInPicture() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}, [videoRef, isPlaying]) |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<PipWrapper> |
|
|
|
<PipWrapper> |
|
|
|
<Icon refIcon='PiP' onClick={togglePip} /> |
|
|
|
<Icon refIcon='PiP' onClick={togglePip} /> |
|
|
|
|