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

[@mantine/core] Image: fix possible permanent placeholder after hydration #630

Merged
merged 1 commit into from Jan 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nvmrc
@@ -1 +1 @@
v14.9.0
v14.17.0
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason to change node version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was stuck with an error stating that a dependency required node >= 14.17 during my first yarn install

Copy link
Member

Choose a reason for hiding this comment

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

Alright, then let's leave it in 14.17 then

18 changes: 14 additions & 4 deletions src/mantine-core/src/components/Image/Image.tsx
@@ -1,6 +1,6 @@
import React, { useState, forwardRef } from 'react';
import React, { useState, forwardRef, useEffect, useRef } from 'react';
import { DefaultProps, MantineNumberSize, ClassNames } from '@mantine/styles';
import { useDidUpdate } from '@mantine/hooks';
import { useDidUpdate, useMergedRef } from '@mantine/hooks';
import { Text } from '../Text';
import { Box } from '../Box';
import { ImageIcon } from './ImageIcon';
Expand Down Expand Up @@ -70,6 +70,16 @@ export const Image = forwardRef<HTMLDivElement, ImageProps>(
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState(!src);
const isPlaceholder = withPlaceholder && (!loaded || error);
const internalImgRef = useRef<HTMLImageElement>(null);
const mergedImgRef = useMergedRef(...[...(imageRef ? [imageRef] : []), internalImgRef]);

useEffect(() => {
const { current } = internalImgRef;
if (current?.complete) {
setError(current.naturalHeight === 0);
setLoaded(current.naturalHeight !== 0);
}
}, [src]);

useDidUpdate(() => {
setLoaded(false);
Expand All @@ -85,7 +95,8 @@ export const Image = forwardRef<HTMLDivElement, ImageProps>(
src={src}
alt={alt}
style={{ objectFit: fit, width, height }}
ref={imageRef}
ref={mergedImgRef}
{...imageProps}
onLoad={(event) => {
setLoaded(true);
typeof imageProps?.onLoad === 'function' && imageProps.onLoad(event);
Expand All @@ -94,7 +105,6 @@ export const Image = forwardRef<HTMLDivElement, ImageProps>(
setError(true);
typeof imageProps?.onError === 'function' && imageProps.onError(event);
}}
{...imageProps}
/>

{isPlaceholder && (
Expand Down