Skip to content

Commit

Permalink
feat(index): add isPictureInPictureAvailable in return object
Browse files Browse the repository at this point in the history
  • Loading branch information
DawChihLiou committed Aug 30, 2020
1 parent b0dd44a commit 4c6f3c7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
13 changes: 10 additions & 3 deletions README.md
Expand Up @@ -38,6 +38,7 @@ function VideoPlayer() {
const videoRef = useRef(null)
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef)

Expand All @@ -46,9 +47,13 @@ function VideoPlayer() {
<video ref={videoRef} autoPlay muted controls loop width="100%">
<source src="video-sample.mp4" />
</video>
<button onClick={() => togglePictureInPicture(!isPictureInPictureActive)}>
{isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in Picture
</button>
{isPictureInPictureAvailable && (
<button
onClick={() => togglePictureInPicture(!isPictureInPictureActive)}
>
{isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in Picture
</button>
)}
</div>
)
}
Expand All @@ -59,6 +64,7 @@ function VideoPlayer() {
```ts
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef, options)
```
Expand All @@ -71,6 +77,7 @@ const {
### Return Values

- `isPictureInPictureActive = false`: `boolean` that signals whether picture in picture mode is active or inactive
- `isPictureInPictureAvailable`: `boolean` that signals whether picture in picture mode is supported and enabled
- `togglePictureInPicture(isActive: boolean) => void`: function that provide you toggle picture in picture mode

### Options (Optional)
Expand Down
13 changes: 10 additions & 3 deletions example/public/DOC.md
Expand Up @@ -36,6 +36,7 @@ function VideoPlayer() {
const videoRef = useRef(null)
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef)

Expand All @@ -44,9 +45,13 @@ function VideoPlayer() {
<video ref={videoRef} autoPlay muted controls loop width="100%">
<source src="video-sample.mp4" />
</video>
<button onClick={() => togglePictureInPicture(!isPictureInPictureActive)}>
{isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in Picture
</button>
{isPictureInPictureAvailable && (
<button
onClick={() => togglePictureInPicture(!isPictureInPictureActive)}
>
{isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in Picture
</button>
)}
</div>
)
}
Expand All @@ -57,6 +62,7 @@ function VideoPlayer() {
```ts
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef, options)
```
Expand All @@ -69,6 +75,7 @@ const {
### Return Values

- `isPictureInPictureActive = false`: `boolean` that signals whether picture in picture mode is active or inactive
- `isPictureInPictureAvailable`: `boolean` that signals whether picture in picture mode is supported and enabled
- `togglePictureInPicture(isActive: boolean) => void`: function that provide you toggle picture in picture mode

### Options (Optional)
Expand Down
18 changes: 13 additions & 5 deletions example/src/App.tsx
Expand Up @@ -24,15 +24,15 @@ const App = () => {
const videoRef = useRef<ExtendedHTMLVideoElement | null>(null)
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef, {
onEnterPictureInPicture: (e) => console.log('enter picture in picture', e),
onLeavePictureInPicture: (e) => console.log('leave picture in picture', e),
})

const handleClick = () => togglePictureInPicture(!isPictureInPictureActive)

const [markdown, setMarkdown] = useState<string>()

useEffect(() => {
fetchMarkdown(setMarkdown)
}, [])
Expand All @@ -59,9 +59,17 @@ const App = () => {
<source src="video-sample.mp4" />
</video>
<div className="action-row">
<button onClick={handleClick} className="control-button">
{isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in Picture
</button>
{isPictureInPictureAvailable && (
<button onClick={handleClick} className="control-button">
{isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in
Picture
</button>
)}
{!isPictureInPictureAvailable && (
<p>
Picture in Picture feature API is not available in your browser.
</p>
)}
</div>
<ReactMarkdown source={markdown} renderers={{ code: CodeBlock }} />
</div>
Expand Down
26 changes: 21 additions & 5 deletions src/index.ts
Expand Up @@ -14,17 +14,22 @@ export default function usePictureInPicture(
videoRef: VideoRefType,
options?: usePictureInPictureOptions
): usePictureInPictureReturnType {
const [isPictureInPictureActive, togglePictureInPicture] = useState<boolean>(
false
)

const {
onEnterPictureInPicture,
onLeavePictureInPicture,
onRequestPictureInPictureError,
onExitPictureInPictureError,
} = options || {}

const [isPictureInPictureActive, togglePictureInPicture] = useState<boolean>(
false
)

const [
isPictureInPictureAvailable,
setIsPictureInPictureAvailable,
] = useState<boolean>(false)

useEffect(() => {
handlePictureInPicture(
videoRef,
Expand All @@ -43,6 +48,13 @@ export default function usePictureInPicture(
if (videoRef.current === null) {
return
}

setIsPictureInPictureAvailable(
(isWebkitPictureInPictureSupported(videoRef.current) ||
isPictureInPictureSupported()) &&
!isPictureInPictureDisabled(videoRef.current)
)

if (
onEnterPictureInPicture &&
typeof onEnterPictureInPicture === 'function'
Expand Down Expand Up @@ -87,7 +99,11 @@ export default function usePictureInPicture(
}
}, [])

return { isPictureInPictureActive, togglePictureInPicture }
return {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
}
}

async function handlePictureInPicture(
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Expand Up @@ -48,5 +48,6 @@ export interface usePictureInPictureOptions {

export interface usePictureInPictureReturnType {
isPictureInPictureActive: boolean
isPictureInPictureAvailable: boolean
togglePictureInPicture: Dispatch<SetStateAction<boolean>>
}

0 comments on commit 4c6f3c7

Please sign in to comment.