forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# `useFullscreen` | ||
|
||
Display an element full-screen, optional fallback for fullscreen video on iOS. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import React, { useRef } from 'react'; | ||
import { useFullscreen } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const ref = useRef(null) | ||
const videoRef = useRef(null) | ||
const [fullscreen, toggle] = useFullscreen(ref, videoRef); | ||
|
||
return ( | ||
<div ref={ref} style={{backgroundColor: 'white'}}> | ||
<div>{fullscreen ? 'Fullscreen' : 'Not fullscreen'}</div> | ||
<button onClick={() => toggle()}>Toggle</button> | ||
<button onClick={() => toggle(true)}>set ON</button> | ||
<button onClick={() => toggle(false)}>set OFF</button> | ||
<video ref={videoRef} src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" autoPlay /> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
useFullscreen(ref: RefObject<Element>, videoRef?: RefObject<HTMLVideoElement>); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {useFullscreen} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const ref = React.useRef(null) | ||
const videoRef = React.useRef(null) | ||
const [fullscreen, toggle] = useFullscreen(ref, videoRef); | ||
|
||
return ( | ||
<div ref={ref} style={{backgroundColor: 'white'}}> | ||
<div>{fullscreen ? 'Fullscreen' : 'Not fullscreen'}</div> | ||
<button onClick={() => toggle()}>Toggle</button> | ||
<button onClick={() => toggle(true)}>set ON</button> | ||
<button onClick={() => toggle(false)}>set OFF</button> | ||
<video ref={videoRef} src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" autoPlay /> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects|useFullscreen', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useFullscreen.md')} />) | ||
.add('Demo', () => <Demo />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {useEffect, RefObject, useCallback} from 'react'; | ||
import screenfull from 'screenfull'; | ||
import useToggle from './useToggle' | ||
|
||
export interface State { | ||
fullscreen: boolean | ||
} | ||
|
||
const useFullscreen = (ref: RefObject<Element>, videoRef?: RefObject<HTMLVideoElement>): [boolean, (value?: boolean) => void] => { | ||
const [fullscreen, toggle] = useToggle(false); | ||
|
||
useEffect(() => { | ||
const onChange = () => { | ||
if (screenfull) { | ||
toggle(screenfull.isFullscreen) | ||
} | ||
} | ||
|
||
if (screenfull && screenfull.enabled) { | ||
screenfull.on('change', onChange); | ||
} | ||
|
||
return () => { | ||
if (screenfull && screenfull.enabled) { | ||
screenfull.off('change', onChange); | ||
} | ||
} | ||
}, []); | ||
|
||
const toggleFullscreen = useCallback(async (nextValue?: boolean) => { | ||
nextValue = typeof nextValue === 'undefined' ? !fullscreen : nextValue; | ||
|
||
if (screenfull && screenfull.enabled) { | ||
try { | ||
if (nextValue) { | ||
await screenfull.request(ref.current || undefined); | ||
} else { | ||
await screenfull.exit(); | ||
} | ||
toggle(nextValue); | ||
} catch {} | ||
} else { | ||
if (videoRef && videoRef.current) { | ||
if (nextValue) { | ||
if (videoRef.current.webkitEnterFullscreen) { | ||
const onWebkitEndFullscreen = () => { | ||
if (videoRef.current) { | ||
videoRef.current.removeEventListener('webkitendfullscreen', onWebkitEndFullscreen); | ||
toggle(false) | ||
} | ||
}; | ||
|
||
videoRef.current.webkitEnterFullscreen(); | ||
toggle(true) | ||
videoRef.current.addEventListener('webkitendfullscreen', onWebkitEndFullscreen); | ||
} | ||
} else if (videoRef.current.webkitExitFullscreen) { | ||
videoRef.current.webkitExitFullscreen(); | ||
toggle(false) | ||
} | ||
} | ||
} | ||
}, [fullscreen, toggle]) | ||
|
||
return [fullscreen, toggleFullscreen] | ||
}; | ||
|
||
export default useFullscreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters