Reader progress bars

This commit is contained in:
schroda
2024-09-28 03:24:16 +02:00
parent 51fb660c7b
commit 7434ed0152
42 changed files with 1587 additions and 34 deletions

View File

@@ -0,0 +1,15 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { Theme } from '@mui/material/styles';
// use CSSObject instead of SxProps<Theme> because this completely fucks over typescript by causing an out of memory error during compilation
type CSSObject = ReturnType<Theme['applyStyles']>;
const emptyStyle = {};
export const applyStyles = (isActive: boolean, styling: CSSObject): CSSObject => (isActive ? styling : emptyStyle);

View File

@@ -27,18 +27,45 @@ export class MediaQuery {
return this.useIsBelowWidth('sm');
}
static useGetScrollbarSize(type: 'height' | 'width'): number {
private static getScrollbarSize(type: 'height' | 'width'): number {
const outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.overflow = 'scroll';
document.body.appendChild(outer);
const inner = document.createElement('div');
inner.style.width = '100%';
inner.style.height = '100%';
outer.appendChild(inner);
const width = outer.offsetWidth - inner.offsetWidth;
const height = outer.offsetHeight - inner.offsetHeight;
outer.parentNode?.removeChild(outer);
return type === 'height' ? height : width;
}
static useGetScrollbarSize(
type: 'height' | 'width',
element: HTMLElement | null = document.documentElement,
): number {
const [scrollbarSize, setScrollbarSize] = useState(0);
useResizeObserver(
document.documentElement,
element,
useCallback(() => {
const height = window.innerHeight - document.documentElement.clientHeight;
const width = window.innerWidth - document.documentElement.clientWidth;
const size = type === 'height' ? height : width;
const hasYScrollbar = !!(element!.scrollHeight - element!.clientHeight);
const hasXScrollbar = !!(element!.scrollWidth - element!.clientWidth);
setScrollbarSize(size);
}, []),
const hasScrollbar = (type === 'height' && hasYScrollbar) || (type === 'width' && hasXScrollbar);
if (hasScrollbar) {
setScrollbarSize(this.getScrollbarSize(type));
return;
}
setScrollbarSize(0);
}, [element]),
);
return scrollbarSize;