-
Notifications
You must be signed in to change notification settings - Fork 1
Image component and magnified image #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gfrn
wants to merge
10
commits into
main
Choose a base branch
from
feature/image-and-magnified-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+526
−2
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
82be3fe
Upgrade to MUI v7
gfrn 8f2fb6c
Split package into navigation, themes
gfrn 5f5cdae
Export controls, fix typos
gfrn d5b5c9b
Update export order to make Vitest happy
gfrn 282683c
Add image and magnified image componets
gfrn 26096be
Merge branch 'code-splitting' into feature/image-and-magnified-image
gfrn f076635
Update component descriptions
gfrn 5c73f01
Add max width
gfrn 922b73f
Fix zoom window positioning on phone screens
gfrn 0536e89
Merge branch 'main' into feature/image-and-magnified-image
gfrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,42 @@ | ||
| import { Meta, StoryObj } from "@storybook/react"; | ||
| import { Image } from "./Image"; | ||
|
|
||
| import diamond from "../../public/images/diamond.jpg"; | ||
|
|
||
| const meta: Meta<typeof Image> = { | ||
| title: "Components/Controls/Image", | ||
| component: Image, | ||
| tags: ["autodocs"], | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| component: "Image with placeholder, fallback and loading indicator", | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const BasicImage: Story = { | ||
| args: { src: diamond, style: { width: "20vw" } }, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| story: "Basic Image", | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const ErrorImage: Story = { | ||
| args: { src: "doesnotexist.jpg", style: { width: "20vw" } }, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| story: "Image displayed when original image fails to load", | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or 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,27 @@ | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { Image } from "./Image"; | ||
|
|
||
| import placeholderStaticImport from "../../public/images/diamond.jpg"; | ||
|
|
||
| describe("Image", () => { | ||
| it("should render spinner while image isn't loaded", () => { | ||
| render(<Image src={placeholderStaticImport} alt={"foo"} />); | ||
|
|
||
| const image = screen.getByAltText("foo"); | ||
|
|
||
| expect(image).toHaveAttribute("aria-busy", "true"); | ||
|
|
||
| fireEvent.load(image); | ||
|
|
||
| expect(image).toHaveAttribute("aria-busy", "false"); | ||
| }); | ||
|
|
||
| it("should render placeholder image if an error occurs while loading image", () => { | ||
| render(<Image src={placeholderStaticImport} alt={"foo"} />); | ||
|
|
||
| const image = screen.getByAltText("foo"); | ||
| fireEvent.error(image); | ||
|
|
||
| expect(image).toHaveAttribute("aria-errormessage", "Image not available"); | ||
| }); | ||
| }); |
This file contains hidden or 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,87 @@ | ||
| "use client"; | ||
| import { | ||
| DetailedHTMLProps, | ||
| ImgHTMLAttributes, | ||
| SyntheticEvent, | ||
| useState, | ||
| } from "react"; | ||
| import placeholder from "../../public/generic/no-image.png"; | ||
| import CircularProgress from "@mui/material/CircularProgress"; | ||
| import Box from "@mui/material/Box"; | ||
|
|
||
| export interface ImageProps | ||
| extends Omit< | ||
| Omit< | ||
| DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, | ||
| "onLoad" | "onError" | ||
| >, | ||
| "src" | ||
| > { | ||
| src?: string | null; | ||
| onLoad?: () => void; | ||
| onError?: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * Smart image component that displays a placeholder on error, and a loading indicator if the image is still loading | ||
| */ | ||
| export const Image = ({ src, alt, onLoad, onError, ...props }: ImageProps) => { | ||
| const [isLoading, setIsLoading] = useState(true); | ||
| const [isError, setIsError] = useState(false); | ||
|
|
||
| const handleError = (e: SyntheticEvent<HTMLImageElement>) => { | ||
| if (onError) { | ||
| onError(); | ||
| } | ||
|
|
||
| e.currentTarget.src = placeholder; | ||
| setIsError(true); | ||
| }; | ||
|
|
||
| const handleLoad = () => { | ||
| if (onLoad) { | ||
| onLoad(); | ||
| } | ||
|
|
||
| setIsLoading(false); | ||
| }; | ||
|
|
||
| return ( | ||
| <Box | ||
| display="flex" | ||
| alignItems="center" | ||
| justifyContent="center" | ||
| width={props.style?.width} | ||
| height={props.style?.height} | ||
| > | ||
| {isLoading && ( | ||
| <Box | ||
| display="flex" | ||
| width={props.style?.width} | ||
| paddingY="3em" | ||
| justifyContent="center" | ||
| alignItems="center" | ||
| > | ||
| <CircularProgress /> | ||
| </Box> | ||
| )} | ||
| <img | ||
| aria-busy={isLoading} | ||
| src={src ?? placeholder} | ||
| aria-errormessage={isError ? "Image not available" : undefined} | ||
| onError={handleError} | ||
| onLoad={handleLoad} | ||
| alt={alt ?? "Placeholder Image"} | ||
| {...props} | ||
| style={{ | ||
| width: "100%", | ||
| height: "auto", | ||
| maxHeight: "100%", | ||
| objectFit: "contain", | ||
| display: isLoading ? "none" : "block", | ||
| ...props.style, | ||
| }} | ||
| /> | ||
| </Box> | ||
| ); | ||
| }; |
This file contains hidden or 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,42 @@ | ||
| import { Meta, StoryObj } from "@storybook/react"; | ||
| import { ImageWithZoom } from "./ImageWithZoom"; | ||
|
|
||
| import diamond from "../../public/images/diamond.jpg"; | ||
|
|
||
| const meta: Meta<typeof ImageWithZoom> = { | ||
| title: "Components/Controls/ImageWithZoom", | ||
| component: ImageWithZoom, | ||
| tags: ["autodocs"], | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| component: "Image with user-controlled magnified area", | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const BasicImage: Story = { | ||
| args: { src: diamond, alt: "Diamond" }, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| story: "Basic image with magnified view on side", | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const Brightness: Story = { | ||
| args: { src: diamond, alt: "Diamond", brightness: 0.5 }, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| story: "Image with brightness filter applied", | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or 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,68 @@ | ||
| import { ImageWithZoom } from "./ImageWithZoom"; | ||
| import { render, screen } from "@testing-library/react"; | ||
|
|
||
| /** | ||
| * This is particularly hard to test without visual testing (screenshot matching) | ||
| * With unit tests, refs don't work properly, nor is it particularly useful because there might be visual changes, | ||
| * but CSS remains the same. We should revisit this once we implement visual matching through Playwright/Vitest browser mode. | ||
| */ | ||
|
|
||
| vi.mock("./Image", () => ({ | ||
| Image: ({ | ||
| onLoad, | ||
| alt, | ||
| src, | ||
| onClick, | ||
| }: { | ||
| onClick?: (e: Record<string, unknown>) => void; | ||
| onLoad?: () => void; | ||
| alt: string; | ||
| src: string; | ||
| }) => { | ||
| if (onLoad) { | ||
| onLoad(); | ||
| } | ||
| if (onClick) { | ||
| onClick({ | ||
| currentTarget: { | ||
| getBoundingClientRect: () => ({ | ||
| left: 0, | ||
| top: 0, | ||
| width: 100, | ||
| height: 100, | ||
| }), | ||
| }, | ||
| }); | ||
| } | ||
| return <img alt={alt} src={src} />; | ||
| }, | ||
| })); | ||
|
|
||
| describe("Image with Zoom Viewer", () => { | ||
| it("should update brightness/contrast", () => { | ||
| render( | ||
| <ImageWithZoom src="foo.jpg" alt="foo" brightness={1.5} contrast={0.5} />, | ||
| ); | ||
|
|
||
| // https://github.com/vitest-dev/vitest/issues/9797 | ||
| const zoomView = screen.getByLabelText("Zoom View"); | ||
| expect(zoomView).toHaveAttribute( | ||
| "style", | ||
| expect.stringContaining("brightness(1.5)"), | ||
| ); | ||
| expect(zoomView).toHaveAttribute( | ||
| "style", | ||
| expect.stringContaining("contrast(0.5)"), | ||
| ); | ||
| }); | ||
|
|
||
| it("should update image colour inversion", () => { | ||
| render(<ImageWithZoom src="foo.jpg" alt="foo" invert={true} />); | ||
|
|
||
| const zoomView = screen.getByLabelText("Zoom View"); | ||
| expect(zoomView).toHaveAttribute( | ||
| "style", | ||
| expect.stringContaining("invert(1)"), | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has got duplicated