Reputation: 374
Is there a way to implement fullscreen API in the React component functions?
Upvotes: 3
Views: 19600
Reputation: 61
You can refer @chiragrupani/fullscreen-react package. It supports both class components and hooks/function components. Also, it supports allowing multiple elements to enter full screen. It's usage is simple as:
Install: npm i @chiragrupani/fullscreen-react
<button onClick={e => this.setState({isFullScreen: true})}>Go Fullscreen</button>
<FullScreen
isFullScreen={this.state.isFullScreen}
onChange={(isFullScreen) => {
this.setState({ isFullScreen });
}}
>
<div>Fullscreen</div>
</FullScreen>
For hooks, the code is similar:
export default function FSExampleHook() {
let [isFullScreen, setFullScreen] = React.useState(false);
return (
<>
<button onClick={(e) => setFullScreen(true)}>Go Fullscreen</button>
<FullScreen
isFullScreen={isFullScreen}
onChange={(isFull: boolean) => {
setFullScreen(isFull);
}}
>
<div>Fullscreen</div>
</FullScreen>
</>
);
}
PS: I am not author of this library, but I am using it in my production site.
Upvotes: 5