Skip to content
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

✨ Clicking on an image opens it in a modal #570

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/components/Attachments.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FC } from 'react'
import { FC, useState } from 'react'
import { ButtonBack, ButtonNext, CarouselProvider, Dot, Image, Slide, Slider } from 'pure-react-carousel'
import styled from 'styled-components'
import { Attachment } from '../lib/types'
import 'pure-react-carousel/dist/react-carousel.es.css'
import { Button } from 'semantic-ui-react'
import { Button, Modal, ModalContent } from 'semantic-ui-react'

const Wrapper = styled(CarouselProvider)`
position: relative;
Expand All @@ -22,6 +22,9 @@ interface Props {
}

const Attachments: FC<Props> = props => {
const [modalOpen, setModalOpen] = useState<boolean>(false)
const [modalImageSrc, setModalImageSrc] = useState<string>('')

const renderButtonsAndDots = () => {
// FIXME: This doesn't feel like a good pattern.
// Tried to use styled components instead which didn't work right away.
Expand All @@ -46,16 +49,34 @@ const Attachments: FC<Props> = props => {
)
}

const handleImageClick = (event: React.MouseEvent) => {
if (!event.target?.src) {
return
}
setModalImageSrc(event.target.src)
setModalOpen(true)
}

const handleModalClose = () => {
setModalOpen(false)
setModalImageSrc('')
}

return (
<Wrapper naturalSlideHeight={0.75} naturalSlideWidth={1} totalSlides={props.attachments.length}>
<Slider>
{props.attachments.map((image, key) => (
<Slide key={key} index={key}>
<Image hasMasterSpinner={false} src={image.url} style={{ objectFit: 'scale-down' }} />
<Image hasMasterSpinner={false} src={image.url} onClick={handleImageClick} style={{ objectFit: 'scale-down' }} />
</Slide>
))}
</Slider>
{props.attachments.length > 1 && renderButtonsAndDots()}
<Modal closeIcon onClose={handleModalClose} open={modalOpen}>
<ModalContent>
<Image hasMasterSpinner={false} src={modalImageSrc} style={{ objectFit: 'scale-down' }} />
</ModalContent>
</Modal>
</Wrapper>
)
}
Expand Down
Loading