From fbc4fb75e85f397c96d668165a5fd20a3fbba44f Mon Sep 17 00:00:00 2001 From: monoid Date: Wed, 17 Apr 2024 00:10:46 +0900 Subject: [PATCH] feat: fullscreen exit --- packages/client/src/page/reader/comicPage.tsx | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/client/src/page/reader/comicPage.tsx b/packages/client/src/page/reader/comicPage.tsx index ee4d3c3..97c849c 100644 --- a/packages/client/src/page/reader/comicPage.tsx +++ b/packages/client/src/page/reader/comicPage.tsx @@ -4,7 +4,8 @@ import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { useGalleryDoc } from "@/hook/useGalleryDoc.ts"; import { cn } from "@/lib/utils"; -import { EnterFullScreenIcon, ExitIcon } from "@radix-ui/react-icons"; +import { EnterFullScreenIcon, ExitFullScreenIcon, ExitIcon } from "@radix-ui/react-icons"; +import { useEventListener } from "usehooks-ts"; import type { Document } from "dbtype/api"; import { useCallback, useEffect, useRef, useState } from "react"; @@ -92,12 +93,30 @@ function clip(val: number, min: number, max: number): number { return Math.max(min, Math.min(max, val)); } +function useFullScreen() { + const ref = useRef(document.documentElement); + const [isFullScreen, setIsFullScreen] = useState(false); + + const toggleFullScreen = useCallback(() => { + if (isFullScreen) { + document.exitFullscreen(); + } else { + document.documentElement.requestFullscreen(); + } + }, [isFullScreen]); + + useEventListener("fullscreenchange", () => { + setIsFullScreen(!!document.fullscreenElement); + }, ref); + return { isFullScreen, toggleFullScreen }; +} + export default function ComicPage({ params }: ComicPageProps) { const { data, error, isLoading } = useGalleryDoc(params.id); const [curPage, setCurPage] = useState(0); - + const { isFullScreen, toggleFullScreen } = useFullScreen(); if (isLoading) { // TODO: Add a loading spinner return
@@ -122,11 +141,8 @@ export default function ComicPage({ return ( }/> - } onClick={()=>{ - const elem = document.documentElement; - if (elem.requestFullscreen) { - elem.requestFullscreen(); - } + : } onClick={()=>{ + toggleFullScreen(); }} /> @@ -137,15 +153,14 @@ export default function ComicPage({ setCurPage(clip(Number.parseInt(e.target.value) - 1, 0, (data.additional.page as number) - 1))} /> -
-
}> + doc={data} + totalPage={data.additional.page as number} />
) } \ No newline at end of file