From d9777c179ad5702e8bc72d40e653f08d702551d0 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Fri, 6 Sep 2024 23:43:38 +0200 Subject: [PATCH] Extract function to get scrollbar size --- src/components/reader/Page.tsx | 12 +++++------- src/lib/ui/MediaQuery.tsx | 13 +++++++++++++ src/screens/Reader.tsx | 8 ++------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/components/reader/Page.tsx b/src/components/reader/Page.tsx index 9fd62f2d..f62288f0 100644 --- a/src/components/reader/Page.tsx +++ b/src/components/reader/Page.tsx @@ -6,13 +6,13 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { CSSProperties, forwardRef, useCallback, useState } from 'react'; +import { CSSProperties, forwardRef } from 'react'; import Box from '@mui/material/Box'; import { useTheme } from '@mui/material/styles'; import useMediaQuery from '@mui/material/useMediaQuery'; import { IReaderSettings, ReaderType } from '@/typings'; import { SpinnerImage } from '@/components/util/SpinnerImage'; -import { useResizeObserver } from '@/util/useResizeObserver'; +import { MediaQuery } from '@/lib/ui/MediaQuery.tsx'; export const isHorizontalReaderType = (readerType: ReaderType): boolean => ['ContinuesHorizontalLTR', 'ContinuesHorizontalRTL'].includes(readerType); @@ -20,11 +20,9 @@ export const isHorizontalReaderType = (readerType: ReaderType): boolean => export function imageStyle(settings: IReaderSettings): CSSProperties { const isVertical = settings.readerType === 'ContinuesVertical'; const isHorizontal = isHorizontalReaderType(settings.readerType); - const [scrollbarHeight, setScrollbarHeight] = useState(0); - useResizeObserver( - document.documentElement, - useCallback(() => setScrollbarHeight(window.innerHeight - document.documentElement.clientHeight), []), - ); + + const scrollbarHeight = MediaQuery.useGetScrollbarSize(); + const baseStyling: CSSProperties = { margin: 0, width: `${settings.readerWidth}%`, diff --git a/src/lib/ui/MediaQuery.tsx b/src/lib/ui/MediaQuery.tsx index cd197bd0..5a7ad26e 100644 --- a/src/lib/ui/MediaQuery.tsx +++ b/src/lib/ui/MediaQuery.tsx @@ -8,9 +8,11 @@ import useMediaQuery from '@mui/material/useMediaQuery'; import { Breakpoint } from '@mui/material/styles'; +import { useCallback, useState } from 'react'; import { getCurrentTheme } from '@/theme.ts'; import { ThemeMode } from '@/components/context/ThemeModeContext.tsx'; import { AppStorage } from '@/util/AppStorage.ts'; +import { useResizeObserver } from '@/util/useResizeObserver.tsx'; export class MediaQuery { static useIsTouchDevice(): boolean { @@ -25,6 +27,17 @@ export class MediaQuery { return this.useIsBelowWidth('sm'); } + static useGetScrollbarSize(): number { + const [scrollbarSize, setScrollbarSize] = useState(0); + + useResizeObserver( + document.documentElement, + useCallback(() => setScrollbarSize(window.innerHeight - document.documentElement.clientHeight), []), + ); + + return scrollbarSize; + } + static getSystemThemeMode(): Exclude { const prefersDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; return prefersDarkMode ? ThemeMode.DARK : ThemeMode.LIGHT; diff --git a/src/screens/Reader.tsx b/src/screens/Reader.tsx index 11ee2b6f..38c2b600 100644 --- a/src/screens/Reader.tsx +++ b/src/screens/Reader.tsx @@ -42,7 +42,7 @@ import { GET_CHAPTERS_READER } from '@/lib/graphql/queries/ChapterQuery.ts'; import { GET_MANGA_READER } from '@/lib/graphql/queries/MangaQuery.ts'; import { TMangaReader } from '@/lib/data/Mangas.ts'; import { CHAPTER_READER_FIELDS } from '@/lib/graphql/fragments/ChapterFragments.ts'; -import { useResizeObserver } from '@/util/useResizeObserver'; +import { MediaQuery } from '@/lib/ui/MediaQuery.tsx'; type TChapter = GetChaptersReaderQuery['chapters']['nodes'][number]; @@ -488,11 +488,7 @@ export function Reader() { openNextChapter(ChapterOffset.PREV); }, [openNextChapter]); - const [scrollbarHeight, setScrollbarHeight] = useState(0); - useResizeObserver( - document.documentElement, - useCallback(() => setScrollbarHeight(window.innerHeight - document.documentElement.clientHeight), []), - ); + const scrollbarHeight = MediaQuery.useGetScrollbarSize(); if (isLoading) { return (