useFullscreen
function useFullscreen(ref: RefObject<Element | null>): Fullscreen;
interface Fullscreen {
readonly isFullscreen: boolean; // whether this element is the one shown fullscreen
readonly supported: boolean;
readonly enter: () => Promise<boolean>; // resolves whether it worked
readonly exit: () => Promise<boolean>;
readonly toggle: () => Promise<boolean>;
}
Shows an element, such as a video player, slideshow or chart, fullscreen.
- Browsers only allow
enterfrom a click or key press. It resolves whether it worked, and never rejects. isFullscreenis true only while this element is the one shown, and turns false when the user leaves with Escape.exitdoesn't touch another element's fullscreen.supportedisfalsewithout the Fullscreen API and where only videos can go fullscreen, as on iPhone. Hide the button then.
Example
import { useRef } from "react";
import { useFullscreen } from "react-honey-hooks";
function Slideshow({ slides }: { slides: Slide[] }) {
const ref = useRef<HTMLDivElement>(null);
const { isFullscreen, supported, toggle } = useFullscreen(ref);
return (
<div ref={ref}>
<Slides slides={slides} />
{supported && (
<button onClick={() => void toggle()}>
{isFullscreen ? "Exit fullscreen" : "Fullscreen"}
</button>
)}
</div>
);
}