-
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.
OPHJOD-1320: Add LazyImage component
- Loading branch information
Showing
5 changed files
with
101 additions
and
8 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,26 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { LazyImage } from './LazyImage'; | ||
|
||
const meta = { | ||
title: 'LazyImage', | ||
component: LazyImage, | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof LazyImage>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: 'This is a lazy loading image component that preloads the image before displaying it.', | ||
}, | ||
}, | ||
}, | ||
args: { | ||
src: 'https://images.unsplash.com/photo-1523464862212-d6631d073194?q=80&w=260', | ||
alt: 'Woman standing in front of a colourful wall', | ||
}, | ||
}; |
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,23 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { LazyImage } from './LazyImage'; | ||
|
||
describe('LazyImage', () => { | ||
it('renders without crashing', () => { | ||
render(<LazyImage src="test.jpg" alt="test image" />); | ||
const imgElement = screen.getByAltText('test image'); | ||
expect(imgElement).toBeInTheDocument(); | ||
}); | ||
|
||
it('sets the alt attribute correctly', () => { | ||
render(<LazyImage src="test.jpg" alt="test image" />); | ||
const imgElement = screen.getByAltText('test image'); | ||
expect(imgElement).toHaveAttribute('alt', 'test image'); | ||
}); | ||
|
||
it('applies the correct styles when the image is not loaded', () => { | ||
render(<LazyImage src="test.jpg" alt="test image" />); | ||
const imgElement = screen.getByAltText('test image'); | ||
expect(imgElement).toHaveStyle('opacity: 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,39 @@ | ||
import React from 'react'; | ||
|
||
interface LazyImageProps extends React.ImgHTMLAttributes<HTMLImageElement> { | ||
/** The source of the image to be loaded */ | ||
src: string; | ||
/** The alt text for the image */ | ||
alt: string; | ||
} | ||
|
||
/** | ||
* LazyImage is a lazy loading image component that preloads the image before displaying it. | ||
*/ | ||
export const LazyImage: React.FC<LazyImageProps> = ({ src, alt, ...props }) => { | ||
const [loaded, setLoaded] = React.useState(false); | ||
const [isCached, setIsCached] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
const img = new Image(); | ||
img.src = src; | ||
if (img.complete) { | ||
setIsCached(true); | ||
setLoaded(true); | ||
} else { | ||
img.onload = () => setLoaded(true); | ||
} | ||
img.onload = () => setLoaded(true); | ||
}, [src]); | ||
|
||
return ( | ||
<div className={`ds:flex ds:bg-secondary-5 ${props.className}`.trim()}> | ||
<img | ||
src={loaded ? src : undefined} | ||
alt={alt} | ||
style={{ opacity: loaded ? 1 : 0, transition: isCached ? 'none' : 'opacity 0.3s' }} | ||
className="ds:w-full ds:h-full ds:object-cover" | ||
/> | ||
</div> | ||
); | ||
}; |
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