Skip to content

Commit

Permalink
feat(app): improve image loading experience (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Nov 20, 2023
1 parent 23a087b commit 1ff4063
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app/frontend/components/image-views.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import React, { useState } from 'react';
import { RouterOutput } from '../utils/trpc';
import { PrimaryButton, SecondaryButton } from './buttons';
import { Loader, LoaderViews } from './loader';

type Images = RouterOutput['fetchCurrentPage']['images'];
interface ImageViewChildProps {
Expand Down Expand Up @@ -42,7 +43,7 @@ export const SingleImageView: React.FC<SingleImageViewProps> = ({
);
})}
</div>
<img src={selectedImage.url} alt={selectedImage.name} />
<Image src={selectedImage.url} alt={selectedImage.name} />
</div>
);
};
Expand All @@ -69,9 +70,29 @@ export const SideBySideImageView: React.FC<ImageViewChildProps> = ({
{images.map(image => (
<div key={image.name}>
<h2 className="text-center">{image.name}</h2>
<img src={image.url} alt={image.name} />
<Image src={image.url} alt={image.name} />
</div>
))}
</div>
);
};

const Image = (
props: React.DetailedHTMLProps<
React.ImgHTMLAttributes<HTMLImageElement>,
HTMLImageElement
>
) => {
const [isLoading, setIsLoading] = useState(true);

return (
<>
{isLoading && <Loader view={LoaderViews.PARTIAL} />}
<img
{...props}
onLoad={() => setIsLoading(false)}
className={isLoading ? 'hidden' : ''}
/>
</>
);
};

0 comments on commit 1ff4063

Please sign in to comment.