Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate SpinnerImage to Mui 5 #97

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions src/components/reader/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 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 makeStyles from '@mui/styles/makeStyles';
import React, { useEffect, useRef } from 'react';
import SpinnerImage from 'components/util/SpinnerImage';
import useLocalStorage from 'util/useLocalStorage';
import Box from '@mui/system/Box';

function imageStyle(settings: IReaderSettings): any {
const [dimensions, setDimensions] = React.useState({
Expand Down Expand Up @@ -53,22 +53,6 @@ function imageStyle(settings: IReaderSettings): any {
};
}

const useStyles = (settings: IReaderSettings) => makeStyles({
loading: {
margin: '100px auto',
height: '100vh',
width: '100vw',
},
loadingImage: {
height: '100vh',
width: '70vw',
padding: '50px calc(50% - 20px)',
backgroundColor: '#525252',
marginBottom: 10,
},
image: imageStyle(settings),
});

interface IProps {
src: string
index: number
Expand All @@ -84,7 +68,6 @@ const Page = React.forwardRef((props: IProps, ref: any) => {

const [useCache] = useLocalStorage<boolean>('useCache', true);

const classes = useStyles(settings)();
const imgRef = useRef<HTMLImageElement>(null);

const handleVerticalScroll = () => {
Expand Down Expand Up @@ -120,17 +103,26 @@ const Page = React.forwardRef((props: IProps, ref: any) => {
}
}, [handleVerticalScroll]);

const imgStyle = imageStyle(settings);

return (
<div ref={ref} style={{ margin: 'auto' }}>
<Box ref={ref} sx={{ margin: 'auto' }}>
<SpinnerImage
src={`${src}?useCache=${useCache}`}
onImageLoad={onImageLoad}
alt={`Page #${index}`}
imgRef={imgRef}
spinnerClassName={`${classes.image} ${classes.loadingImage}`}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging the two might have been wrong in the first place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yes, cause they both might have conflicting css. Having different styling objects for both makes more sense

imgClassName={classes.image}
spinnerStyle={{
...imgStyle,
height: '100vh',
width: '70vw',
padding: '50px calc(50% - 20px)',
backgroundColor: '#525252',
marginBottom: 10,
}}
imgStyle={imgStyle}
/>
</div>
</Box>
);
});

Expand Down
11 changes: 3 additions & 8 deletions src/components/util/SpinnerImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ interface IProps {

imgRef?: React.RefObject<HTMLImageElement>

spinnerClassName?: string
spinnerStyle?: SxProps<Theme>
imgClassName?: string
imgStyle?: CSSProperties

onImageLoad?: () => void
}

export default function SpinnerImage(props: IProps) {
const {
src, alt, onImageLoad, imgRef, spinnerClassName, imgClassName, spinnerStyle, imgStyle,
src, alt, onImageLoad, imgRef, spinnerStyle, imgStyle,
} = props;
const [imageSrc, setImagsrc] = useState<string>('');

Expand All @@ -53,19 +51,18 @@ export default function SpinnerImage(props: IProps) {

if (imageSrc.length === 0) {
return (
<Box className={spinnerClassName} sx={spinnerStyle}>
<Box sx={spinnerStyle}>
<CircularProgress thickness={5} />
</Box>
);
}

if (imageSrc === 'Not Found') {
return <Box className={spinnerClassName} sx={spinnerStyle} />;
return <Box sx={spinnerStyle} />;
}

return (
<img
className={imgClassName}
style={imgStyle}
ref={imgRef}
src={imageSrc}
Expand All @@ -75,8 +72,6 @@ export default function SpinnerImage(props: IProps) {
}

SpinnerImage.defaultProps = {
spinnerClassName: '',
imgClassName: '',
spinnerStyle: {},
imgStyle: {},
onImageLoad: () => {},
Expand Down