-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: isolate image with modal (#136)
- Isolates image with modal component used in annex search and question editor.
- Loading branch information
Showing
9 changed files
with
238 additions
and
194 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
38 changes: 38 additions & 0 deletions
38
libs/react/components/src/image-with-modal/image-with-modal.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,38 @@ | ||
import { ImageWithModal } from "./image-with-modal"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
type Story = StoryObj<typeof ImageWithModal>; | ||
|
||
const prefix = | ||
"https://www.chair-flight.com/content/content-question-bank-atpl/media"; | ||
|
||
export const Playground: Story = { | ||
args: { | ||
href: "image 1", | ||
width: 200, | ||
height: 200, | ||
}, | ||
argTypes: { | ||
href: { | ||
defaultValue: "image 1", | ||
options: ["image 1", "image 2", "image 3", "image 4"], | ||
mapping: { | ||
"image 1": `${prefix}/facd988e59bbec4bfd2c2ab4427a45ed.jpg`, | ||
"image 2": `${prefix}/ede35b3c1eb70d78a1ed95c8d1410148.jpg`, | ||
"image 3": `${prefix}/eb38917314ded07e0c84adfe1a2c84c1.jpg`, | ||
"Not Found Image": `${prefix}/potato.jpg`, | ||
}, | ||
control: { | ||
type: "select", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const meta: Meta<typeof ImageWithModal> = { | ||
title: "Components/ImageWithModal", | ||
component: ImageWithModal, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; |
88 changes: 88 additions & 0 deletions
88
libs/react/components/src/image-with-modal/image-with-modal.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,88 @@ | ||
import { forwardRef, useState } from "react"; | ||
import { default as Image } from "next/image"; | ||
import { Sheet, Modal, ModalClose, ModalDialog, Stack } from "@mui/joy"; | ||
import { useDisclose } from "../hooks/use-disclose"; | ||
import { Ups } from "../ups"; | ||
import type { SxProps } from "@mui/joy/styles/types"; | ||
|
||
export type ImageWithModalProps = { | ||
href: string; | ||
alt: string; | ||
width: number; | ||
height: number; | ||
sx?: SxProps; | ||
}; | ||
|
||
/** | ||
* Image component that, when clicks, pops out a modal with an image as big | ||
* as it can possibly be. | ||
*/ | ||
export const ImageWithModal = forwardRef<HTMLDivElement, ImageWithModalProps>( | ||
({ href, alt, width, height, sx }, ref) => { | ||
const [error, setError] = useState(false); | ||
const imageModal = useDisclose(); | ||
|
||
return ( | ||
<Sheet | ||
ref={ref} | ||
sx={{ | ||
width, | ||
height, | ||
p: 0.5, | ||
cursor: "pointer", | ||
backgroundColor: "background.paper", | ||
"& > img": { objectFit: "contain" }, | ||
...sx, | ||
}} | ||
> | ||
{error ? ( | ||
<Ups | ||
sx={{ | ||
flex: "initial", | ||
width: "100%", | ||
height: "100%", | ||
minHeight: 0, | ||
"& svg": { | ||
width: width / 2, | ||
height: height / 2, | ||
}, | ||
"& h3": { | ||
fontSize: "18px", | ||
}, | ||
}} | ||
message="Not Found" | ||
/> | ||
) : ( | ||
<Image | ||
onClick={imageModal.open} | ||
src={href} | ||
alt={alt} | ||
width={width - 10} | ||
height={height - 10} | ||
onError={() => setError(true)} | ||
/> | ||
)} | ||
<Modal open={imageModal.isOpen} onClose={imageModal.close}> | ||
<ModalDialog> | ||
<ModalClose variant="solid" /> | ||
<Stack> | ||
<img | ||
src={href} | ||
alt={alt} | ||
style={{ | ||
objectFit: "contain", | ||
maxWidth: "90vw", | ||
maxHeight: "90vh", | ||
width: "auto", | ||
height: "auto", | ||
}} | ||
/> | ||
</Stack> | ||
</ModalDialog> | ||
</Modal> | ||
</Sheet> | ||
); | ||
}, | ||
); | ||
|
||
ImageWithModal.displayName = "ImageWithModal"; |
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 @@ | ||
export { ImageWithModal } from "./image-with-modal"; |
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
149 changes: 0 additions & 149 deletions
149
libs/react/containers/src/annexes/annex-search/annex-search-item.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.