-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from perimetre/feature/add-thumbnail-to-file-…
…input feat: added imageLoader and preview for file upload
- Loading branch information
Showing
7 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// also exported from '@storybook/react' if you can deal with breaking changes in 6.1 | ||
import { Meta, Story } from '@storybook/react/types-6-0'; | ||
import React from 'react'; | ||
import { ImageLoader, ImageLoaderProps } from '.'; | ||
|
||
export default { | ||
title: 'Components/ImageLoader', | ||
component: ImageLoader, | ||
argTypes: { | ||
src: { defaultValue: 'https://cataas.com/cat' }, | ||
alt: { defaultValue: 'An image of a cat' }, | ||
className: { defaultValue: 'w-64 h-64 object-cover' }, | ||
loaderClassName: { defaultValue: 'w-64 h-64' } | ||
} | ||
} as Meta; | ||
|
||
/** | ||
* A story that displays a ImageLoader example | ||
* | ||
* @param props the story props | ||
*/ | ||
const Template: Story<ImageLoaderProps> = (props) => <ImageLoader {...props} />; | ||
|
||
export const Default = Template.bind({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* eslint-disable jsx-a11y/alt-text */ | ||
import classnames from 'classnames'; | ||
import React, { useLayoutEffect, useRef, useState } from 'react'; | ||
|
||
export type ImageLoaderProps = React.DetailedHTMLProps<React.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement> & { | ||
/** | ||
* The image alt tag | ||
*/ | ||
alt: string; // Never make "alt" optional. | ||
/** | ||
* Class name for the loader | ||
*/ | ||
loaderClassName: string; // Required because the user must provide desired height/width | ||
/** | ||
* Class name for the container | ||
*/ | ||
containerClassName?: string; | ||
/** | ||
* Options for the intersection observer | ||
*/ | ||
observerOptions?: IntersectionObserverInit; | ||
}; | ||
|
||
/** | ||
* Wraps the image tag with a loader placeholder that shows up until the image is loaded | ||
* | ||
* @param props The image props | ||
* @param props.src The image src tag | ||
* @param props.observerOptions The options for the intersection observer | ||
* @param props.loaderClassName The class name for the loader | ||
* @param props.containerClassName The class name for the container | ||
*/ | ||
export const ImageLoader: React.FC<ImageLoaderProps> = ({ | ||
src, | ||
observerOptions, | ||
loaderClassName, | ||
containerClassName, | ||
...props | ||
}) => { | ||
const imgRef = useRef<HTMLImageElement | null>(null); | ||
const [imgLoaded, setImgLoaded] = useState(false); | ||
|
||
useLayoutEffect(() => { | ||
const img = imgRef.current; | ||
|
||
if (!!img) { | ||
// Creates a new intersection observer to check if the image is visible or not | ||
// Image only gets loaded if it's in screen | ||
const observer = new IntersectionObserver( | ||
() => { | ||
/** | ||
* Callback for when the image is loaded | ||
*/ | ||
img.onload = () => setImgLoaded(true); | ||
img.src = src || ''; | ||
}, | ||
{ | ||
threshold: 0.2, // Only trigger with this threshold | ||
...observerOptions | ||
} | ||
); | ||
|
||
observer.observe(img); | ||
|
||
return () => { | ||
observer.unobserve(img); | ||
observer.disconnect(); | ||
}; | ||
} | ||
|
||
return; | ||
// Don't need to listen for observerOptions changes | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [imgRef, src]); | ||
|
||
return ( | ||
<span className={containerClassName}> | ||
{!imgLoaded && <div className={classnames('pui-skeleton', loaderClassName)} />} | ||
<img style={{ display: !imgLoaded ? 'none' : undefined }} ref={imgRef} {...props} /> | ||
</span> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters