bigbuffet-rw/app/soapbox/features/ui/util/fullscreen.ts

59 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
// APIs for normalizing fullscreen operations. Note that Edge uses
// the WebKit-prefixed APIs currently (as of Edge 16).
2022-09-30 17:52:41 -07:00
export const isFullscreen = (): boolean => {
return Boolean(
document.fullscreenElement ||
// @ts-ignore
document.webkitFullscreenElement ||
// @ts-ignore
document.mozFullScreenElement,
);
};
2020-03-27 13:59:38 -07:00
2022-09-30 17:52:41 -07:00
export const exitFullscreen = (): void => {
2020-03-27 13:59:38 -07:00
if (document.exitFullscreen) {
document.exitFullscreen();
2022-09-30 17:52:41 -07:00
} else if ('webkitExitFullscreen' in document) {
// @ts-ignore
2020-03-27 13:59:38 -07:00
document.webkitExitFullscreen();
2022-09-30 17:52:41 -07:00
} else if ('mozCancelFullScreen' in document) {
// @ts-ignore
2020-03-27 13:59:38 -07:00
document.mozCancelFullScreen();
}
};
2022-09-30 17:52:41 -07:00
export const requestFullscreen = (el: Element): void => {
2020-03-27 13:59:38 -07:00
if (el.requestFullscreen) {
el.requestFullscreen();
2022-09-30 17:52:41 -07:00
} else if ('webkitRequestFullscreen' in el) {
// @ts-ignore
2020-03-27 13:59:38 -07:00
el.webkitRequestFullscreen();
2022-09-30 17:52:41 -07:00
} else if ('mozRequestFullScreen' in el) {
// @ts-ignore
2020-03-27 13:59:38 -07:00
el.mozRequestFullScreen();
}
};
2022-09-30 17:52:41 -07:00
type FullscreenListener = (this: Document, ev: Event) => void;
export const attachFullscreenListener = (listener: FullscreenListener): void => {
2020-03-27 13:59:38 -07:00
if ('onfullscreenchange' in document) {
document.addEventListener('fullscreenchange', listener);
} else if ('onwebkitfullscreenchange' in document) {
document.addEventListener('webkitfullscreenchange', listener);
} else if ('onmozfullscreenchange' in document) {
document.addEventListener('mozfullscreenchange', listener);
}
};
2022-09-30 17:52:41 -07:00
export const detachFullscreenListener = (listener: FullscreenListener): void => {
2020-03-27 13:59:38 -07:00
if ('onfullscreenchange' in document) {
document.removeEventListener('fullscreenchange', listener);
} else if ('onwebkitfullscreenchange' in document) {
document.removeEventListener('webkitfullscreenchange', listener);
} else if ('onmozfullscreenchange' in document) {
document.removeEventListener('mozfullscreenchange', listener);
}
};