Skip to content

Commit

Permalink
✨ Clicking on an image opens it in a modal
Browse files Browse the repository at this point in the history
  • Loading branch information
doobry-systemli committed Apr 27, 2024
1 parent 0658ed2 commit c36c527
Showing 1 changed file with 24 additions and 3 deletions.
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

0 comments on commit c36c527

Please sign in to comment.