import { useFullscreen } from "ahooks";
import { MutableRefObject, useCallback, useState } from "react";
type UseFullscreenCustomization = {
ref: MutableRefObject<null>;
keyboardLock?: Iterable<string>;
};
export function useFullscreenCustomization({
ref,
keyboardLock,
}: UseFullscreenCustomization) {
const [iosFullscreen, setIosFullscreen] = useState<boolean>(false);
const [isFullscreen, { enterFullscreen, exitFullscreen }] = useFullscreen(
ref,
{
onExit: () => {
if (
keyboardLock &&
"keyboard" in navigator &&
"lock" in navigator.keyboard
) {
navigator.keyboard.unlock();
}
setIosFullscreen(false);
},
}
);
const handleFullscreen = useCallback(() => {
if (isFullscreen) {
exitFullscreen();
if (
keyboardLock &&
"keyboard" in navigator &&
"lock" in navigator.keyboard
) {
navigator.keyboard.unlock();
}
} else {
if (
keyboardLock &&
"keyboard" in navigator &&
"lock" in navigator.keyboard
) {
navigator.keyboard.lock(keyboardLock);
}
enterFullscreen();
}
setIosFullscreen(!iosFullscreen);
}, [
enterFullscreen,
exitFullscreen,
isFullscreen,
iosFullscreen,
keyboardLock,
]);
return {
iosFullscreen,
isFullscreen,
handleFullscreen,
};
}