Skip to content

Commit

Permalink
fullscreen api
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmanueldaza committed Dec 2, 2024
1 parent 2f0b8d8 commit e5d5973
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
16 changes: 14 additions & 2 deletions src/components/Slideshow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const images: SlideImage[] = [
];

const Slideshow: React.FC = () => {
const containerRef = useRef<HTMLDivElement>(null);
const {
currentSlide,
showSlide,
Expand All @@ -25,9 +26,20 @@ const Slideshow: React.FC = () => {
handleWheel,
isFullscreen,
toggleFullscreen,
} = useSlideshow(images);
} = useSlideshow(images, containerRef);

const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const elem = document.documentElement;
if (
!elem.requestFullscreen &&
!elem.mozRequestFullScreen && // Firefox
!elem.webkitRequestFullscreen && // Chrome, Safari & Opera
!elem.msRequestFullscreen
) {
// IE/Edge
console.warn("Fullscreen API is not supported in this browser");
}
}, []);

useEffect(() => {
const container = containerRef.current;
Expand Down
40 changes: 37 additions & 3 deletions src/hooks/useSlideshow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useState, useCallback, useEffect, useRef } from "react";
import type { SlideImage } from "../types";

export const useSlideshow = (images: SlideImage[]) => {
export const useSlideshow = (
images: SlideImage[],
containerRef: React.RefObject<HTMLDivElement>,
) => {
const [currentSlide, setCurrentSlide] = useState<number>(0);
const [isPaused, setIsPaused] = useState<boolean>(false);
const [scale, setScale] = useState<number>(1);
Expand Down Expand Up @@ -59,8 +62,39 @@ export const useSlideshow = (images: SlideImage[]) => {
);

const toggleFullscreen = useCallback(() => {
setIsFullscreen((prev) => !prev);
setScale(1); // Reset scale when toggling fullscreen
if (!document.fullscreenElement && containerRef.current) {
containerRef.current
.requestFullscreen()
.then(() => {
setIsFullscreen(true);
})
.catch((err) => {
console.error(
`Error attempting to enable fullscreen: ${err.message}`,
);
});
} else if (document.fullscreenElement) {
document
.exitFullscreen()
.then(() => {
setIsFullscreen(false);
})
.catch((err) => {
console.error(`Error attempting to exit fullscreen: ${err.message}`);
});
}
setScale(1);
}, [containerRef]);

useEffect(() => {
const handleFullscreenChange = () => {
setIsFullscreen(!!document.fullscreenElement);
};

document.addEventListener("fullscreenchange", handleFullscreenChange);
return () => {
document.removeEventListener("fullscreenchange", handleFullscreenChange);
};
}, []);

useEffect(() => {
Expand Down
25 changes: 17 additions & 8 deletions src/styles/Slideshow.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@
transition: all 0.3s ease;
}

.fullscreen {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.95);
z-index: 1000;
.slideshowContainer:fullscreen {
padding: 0;
background: #000;
}

.slideshowContainer:-webkit-full-screen {
padding: 0;
background: #000;
}

.slideshowContainer:-moz-full-screen {
padding: 0;
background: #000;
}

.slideshowContainer:-ms-fullscreen {
padding: 0;
background: #000;
}

.slide {
Expand Down
16 changes: 16 additions & 0 deletions src/types/fullscreen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface Document {
mozCancelFullScreen?: () => Promise<void>;
webkitExitFullscreen?: () => Promise<void>;
msExitFullscreen?: () => Promise<void>;
mozFullScreenElement?: Element;
webkitFullscreenElement?: Element;
msFullscreenElement?: Element;
fullscreenElement: Element | null;
}

interface HTMLElement {
mozRequestFullScreen?: () => Promise<void>;
webkitRequestFullscreen?: () => Promise<void>;
msRequestFullscreen?: () => Promise<void>;
requestFullscreen: () => Promise<void>;
}

0 comments on commit e5d5973

Please sign in to comment.