-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03ec590
commit dc95cfc
Showing
10 changed files
with
210 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import observerOptions from "./options"; | ||
import Wrapper from "./Wrapper"; | ||
import { ImageProps } from "./types"; | ||
|
||
const BackgroundImage: React.FC<ImageProps> = ({ src, ...otherProps }) => { | ||
const imgRef = useRef(null); | ||
|
||
useEffect(() => { | ||
const img = (imgRef.current as unknown) as HTMLElement; | ||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
const { isIntersecting } = entry; | ||
if (isIntersecting) { | ||
img.style.backgroundImage = `url("${src}")`; | ||
observer.disconnect(); | ||
} | ||
}); | ||
}, observerOptions); | ||
observer.observe(img); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [src]); | ||
|
||
return <Wrapper ref={imgRef} {...otherProps} />; | ||
}; | ||
|
||
export default BackgroundImage; |
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,53 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import styled from "styled-components"; | ||
import observerOptions from "./options"; | ||
import Wrapper from "./Wrapper"; | ||
import { ImageProps } from "./types"; | ||
|
||
const StyledImage = styled.img` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
max-width: 100%; | ||
`; | ||
|
||
const Placeholder = styled.div` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
`; | ||
|
||
const Image: React.FC<ImageProps> = ({ src, alt, ...otherProps }) => { | ||
const imgRef = useRef(null); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
const img = (imgRef.current as unknown) as HTMLImageElement; | ||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
const { isIntersecting } = entry; | ||
if (isIntersecting) { | ||
setIsLoaded(true); | ||
observer.disconnect(); | ||
} | ||
}); | ||
}, observerOptions); | ||
observer.observe(img); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [src]); | ||
|
||
return ( | ||
<Wrapper ref={imgRef} {...otherProps}> | ||
{isLoaded ? <StyledImage src={src} alt={alt} /> : <Placeholder />} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default Image; |
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,18 @@ | ||
import styled from "styled-components"; | ||
import { space } from "styled-system"; | ||
import { ContainerProps } from "./types"; | ||
|
||
const Wrapper = styled.div<ContainerProps>` | ||
position: relative; | ||
background-position: center center; | ||
background-repeat: no-repeat; | ||
background-size: cover; | ||
height: ${({ height, responsive }) => (responsive ? 0 : height)}px; | ||
max-width: ${({ width }) => width}px; | ||
max-height: ${({ height }) => height}px; | ||
width: 100%; | ||
padding-top: ${({ width, height, responsive }) => (responsive ? (height / width) * 100 : 0)}%; | ||
${space} | ||
`; | ||
|
||
export default Wrapper; |
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,80 @@ | ||
import React from "react"; | ||
import times from "lodash/times"; | ||
import Flex from "../Flex/Flex"; | ||
import BackgroundImage from "./BackgroundImage"; | ||
import Img from "./Image"; | ||
|
||
export default { | ||
title: "Components/Image", | ||
argTypes: {}, | ||
}; | ||
|
||
export const Image: React.FC = () => { | ||
return ( | ||
<div> | ||
<Img src="https://via.placeholder.com/800x400" width={800} height={400} alt="test" /> | ||
<div>Image</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const ImageResponsive: React.FC = () => { | ||
return ( | ||
<div> | ||
<Img src="https://via.placeholder.com/800x400" width={800} height={400} responsive /> | ||
<div>Image</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Background: React.FC = () => { | ||
return ( | ||
<div> | ||
<BackgroundImage src="https://via.placeholder.com/800x400" width={800} height={400} mr="16px" /> | ||
<div>Background Image</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const BackgroundResponsive: React.FC = () => { | ||
return ( | ||
<div> | ||
<BackgroundImage src="https://via.placeholder.com/800x400" width={800} height={400} responsive mr="16px" /> | ||
<div>Background Image</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const LazyImages: React.FC = () => { | ||
return ( | ||
<Flex flexWrap="wrap"> | ||
{times(40, (index) => ( | ||
<Img | ||
key={index} | ||
src={`https://via.placeholder.com/${150 + index}`} | ||
width={150} | ||
height={150} | ||
mb="16px" | ||
mr="16px" | ||
/> | ||
))} | ||
</Flex> | ||
); | ||
}; | ||
|
||
export const LazyBackgrounds: React.FC = () => { | ||
return ( | ||
<Flex flexWrap="wrap"> | ||
{times(40, (index) => ( | ||
<BackgroundImage | ||
key={index} | ||
src={`https://via.placeholder.com/${150 + index}`} | ||
width={150} | ||
height={150} | ||
mb="16px" | ||
mr="16px" | ||
/> | ||
))} | ||
</Flex> | ||
); | ||
}; |
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,2 @@ | ||
export { default as BackgroundImage } from "./BackgroundImage"; | ||
export { default as Image } from "./Image"; |
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,5 @@ | ||
export default { | ||
root: null, | ||
rootMargin: "200px", | ||
threshold: 0, | ||
}; |
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,12 @@ | ||
import { SpaceProps } from "styled-system"; | ||
|
||
export interface ContainerProps { | ||
width: number; | ||
height: number; | ||
responsive?: boolean; | ||
} | ||
|
||
export interface ImageProps extends ContainerProps, SpaceProps { | ||
src: string; | ||
alt?: string; | ||
} |
This file was deleted.
Oops, something went wrong.
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