Component and Hook for handling full screen components
npm install react-full-screenA React component that sets its children to fullscreen using the Fullscreen API, normalized using fscreen.
bash
yarn add react-full-screen
`$3
`js
import { FullScreen, useFullScreenHandle } from "react-full-screen";
`$3
You must use one handle per full screen element.
It is not possible to start in Fullscreen. Fullscreen must be enabled from a user action such as
onClick.`jsx
import React, {useCallback} from 'react';
import { FullScreen, useFullScreenHandle } from "react-full-screen";function App() {
const handle = useFullScreenHandle();
return (
Any fullscreen content here
);
}export default App;
`When you have many elements you need one handle per element.
`jsx
import React, {useCallback} from 'react';
import { FullScreen, useFullScreenHandle } from "react-full-screen";function App() {
const screen1 = useFullScreenHandle();
const screen2 = useFullScreenHandle();
const reportChange = useCallback((state, handle) => {
if (handle === screen1) {
console.log('Screen 1 went to', state, handle);
}
if (handle === screen2) {
console.log('Screen 2 went to', state, handle);
}
}, [screen1, screen2]);
return (
First
Second
);
}export default App;
`Types
`ts
interface FullScreenHandle {
active: boolean;
// Specifies if attached element is currently full screen. enter: () => Promise;
// Requests this element to go full screen.
exit: () => Promise;
// Requests this element to exit full screen.
node: React.MutableRefObject;
// The attached DOM node
}
``ts
interface FullScreenProps {
handle: FullScreenHandle;
// Handle that helps operate the full screen state. onChange?: (state: boolean, handle: FullScreenHandle) => void;
// Optional callback that gets called when this screen changes state.
className?: string;
// Optional prop allowing you to apply a custom class name to the FullScreen container
}
`CSS
Class
fullscreen-enabled will be added to component when it goes fullscreen. If you want to alter child elements when this happens you can use a typical CSS statement.`css
.my-component {
background: #fff;
}.fullscreen-enabled .my-component {
background: #000;
}
``Used with MegamanJS