-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add loading state to ImageGallery (#3462)
- Loading branch information
1 parent
5305ff1
commit 6c0c3e1
Showing
6 changed files
with
260 additions
and
44 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
127 changes: 127 additions & 0 deletions
127
packages/components/src/ImageGalleryThumbnail/ImageGalleryThumbnail.stories.tsx
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,127 @@ | ||
import { ImageGalleryThumbnail } from './ImageGalleryThumbnail' | ||
|
||
import type { Meta, StoryFn } from '@storybook/react' | ||
import type { ImageGalleryThumbnailProps } from './ImageGalleryThumbnail' | ||
|
||
export default { | ||
title: 'Layout/ImageGallery/ImageGalleryThumbnail', | ||
component: ImageGalleryThumbnail, | ||
} as Meta<typeof ImageGalleryThumbnail> | ||
|
||
export const Default: StoryFn<typeof ImageGalleryThumbnail> = ( | ||
props: ImageGalleryThumbnailProps, | ||
) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={0} | ||
allowPortrait={false} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://picsum.photos/id/29/150/150" | ||
/> | ||
) | ||
} | ||
|
||
export const AllowPortrait: StoryFn<typeof ImageGalleryThumbnail> = ( | ||
props: ImageGalleryThumbnailProps, | ||
) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={0} | ||
allowPortrait={true} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://picsum.photos/id/29/150/150" | ||
/> | ||
) | ||
} | ||
|
||
export const DisallowPortrait: StoryFn<typeof ImageGalleryThumbnail> = ( | ||
props: ImageGalleryThumbnailProps, | ||
) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={0} | ||
allowPortrait={false} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://picsum.photos/id/29/150/150" | ||
/> | ||
) | ||
} | ||
|
||
export const ImageIsActive: StoryFn<typeof ImageGalleryThumbnail> = ( | ||
props: ImageGalleryThumbnailProps, | ||
) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={0} | ||
allowPortrait={false} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://picsum.photos/id/29/150/150" | ||
/> | ||
) | ||
} | ||
|
||
export const ImageIsNotActive: StoryFn<typeof ImageGalleryThumbnail> = ( | ||
props: ImageGalleryThumbnailProps, | ||
) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={1} | ||
allowPortrait={false} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://picsum.photos/id/29/150/150" | ||
/> | ||
) | ||
} | ||
|
||
export const ThumbnailUrlInvalidAltText: StoryFn< | ||
typeof ImageGalleryThumbnail | ||
> = (props: ImageGalleryThumbnailProps) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={1} | ||
allowPortrait={false} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://fastly.picsum.photos/404" | ||
/> | ||
) | ||
} | ||
|
||
export const ThumbnailUrlInvalidNameText: StoryFn< | ||
typeof ImageGalleryThumbnail | ||
> = (props: ImageGalleryThumbnailProps) => { | ||
return ( | ||
<ImageGalleryThumbnail | ||
{...props} | ||
activeImageIndex={1} | ||
allowPortrait={false} | ||
name="name" | ||
index={0} | ||
setActiveIndex={() => {}} | ||
thumbnailUrl="https://fastly.picsum.photos/404" | ||
/> | ||
) | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/components/src/ImageGalleryThumbnail/ImageGalleryThumbnail.test.tsx
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,25 @@ | ||
import '@testing-library/jest-dom/vitest' | ||
|
||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { render } from '../test/utils' | ||
import { ImageGalleryThumbnail } from './ImageGalleryThumbnail' | ||
|
||
describe('ImageGalleryThumbnail', () => { | ||
it('calls Callback, when image clicked', () => { | ||
const mockFn = vi.fn() | ||
const { getByTestId } = render( | ||
<ImageGalleryThumbnail | ||
activeImageIndex={0} | ||
allowPortrait={false} | ||
alt="alt" | ||
name="name" | ||
index={0} | ||
setActiveIndex={mockFn} | ||
thumbnailUrl="https://picsum.photos/id/29/150/150" | ||
/>, | ||
) | ||
getByTestId('thumbnail').click() | ||
expect(mockFn).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
65 changes: 65 additions & 0 deletions
65
packages/components/src/ImageGalleryThumbnail/ImageGalleryThumbnail.tsx
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,65 @@ | ||
import React, { useState } from 'react' | ||
import styled from '@emotion/styled' | ||
import { Box, Image as ThemeImage } from 'theme-ui' | ||
|
||
import { Loader } from '../Loader/Loader' | ||
|
||
import type { CardProps } from 'theme-ui' | ||
|
||
import 'photoswipe/style.css' | ||
|
||
export interface ImageGalleryThumbnailProps { | ||
setActiveIndex: (index: number) => void | ||
allowPortrait: boolean | ||
activeImageIndex: number | ||
thumbnailUrl: string | ||
index: number | ||
alt?: string | ||
name?: string | ||
} | ||
|
||
export const ThumbCard = styled<CardProps & React.ComponentProps<any>>(Box)` | ||
cursor: pointer; | ||
padding: 5px; | ||
overflow: hidden; | ||
transition: 0.2s ease-in-out; | ||
&:hover { | ||
transform: translateY(-5px); | ||
} | ||
` | ||
|
||
export const ImageGalleryThumbnail = (props: ImageGalleryThumbnailProps) => { | ||
const [thumbnailLoaded, setThumbnailLoaded] = useState<boolean>(false) | ||
|
||
return ( | ||
<> | ||
{!thumbnailLoaded && ( | ||
<Loader sx={{ mb: 3, mt: 4, width: 100, height: 67 }} /> | ||
)} | ||
<ThumbCard | ||
data-cy="thumbnail" | ||
data-testid="thumbnail" | ||
mb={3} | ||
mt={4} | ||
opacity={props.index === props.activeImageIndex ? 1.0 : 0.5} | ||
onClick={() => props.setActiveIndex(props.index)} | ||
> | ||
<ThemeImage | ||
loading="lazy" | ||
src={props.thumbnailUrl} | ||
alt={props.alt ?? props.name} | ||
onLoad={() => setThumbnailLoaded(true)} | ||
onError={() => setThumbnailLoaded(true)} | ||
sx={{ | ||
width: thumbnailLoaded ? 100 : 0, | ||
height: 67, | ||
objectFit: props.allowPortrait ? 'contain' : 'cover', | ||
borderRadius: 1, | ||
border: '1px solid offWhite', | ||
}} | ||
crossOrigin="" | ||
/> | ||
</ThumbCard> | ||
</> | ||
) | ||
} |
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