|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useContext, useState } from 'react'; |
| 2 | +import { createPortal } from 'react-dom'; |
| 3 | +import { useHotkeys } from 'react-hotkeys-hook'; |
2 | 4 |
|
3 | 5 | import styled from 'styled-components'; |
| 6 | +import { DialogPortalContext } from './Dialog/dialogContext'; |
| 7 | + |
| 8 | +interface ImageViewerProps { |
| 9 | + src: string; |
| 10 | + alt?: string; |
| 11 | + className?: string; |
| 12 | +} |
4 | 13 |
|
5 | 14 | /** Shows an image that becomes fullscreen on click */ |
6 | | -export function ImageViewer(props): JSX.Element { |
| 15 | +export function ImageViewer({ |
| 16 | + src, |
| 17 | + alt, |
| 18 | + className, |
| 19 | +}: ImageViewerProps): JSX.Element { |
7 | 20 | const [showFull, setShowFull] = useState(false); |
| 21 | + const portalRef = useContext(DialogPortalContext); |
| 22 | + |
| 23 | + useHotkeys('esc', () => setShowFull(false), { enabled: showFull }); |
| 24 | + |
| 25 | + if (!portalRef.current) { |
| 26 | + return <></>; |
| 27 | + } |
8 | 28 |
|
9 | 29 | return ( |
10 | | - <ImageViewerStyled |
11 | | - data-test={`image-viewer`} |
12 | | - onClick={() => setShowFull(!showFull)} |
13 | | - {...props} |
| 30 | + <WrapperButton |
14 | 31 | showFull={showFull} |
15 | | - /> |
| 32 | + title='Click to enlarge' |
| 33 | + onClick={() => setShowFull(prev => !prev)} |
| 34 | + > |
| 35 | + {!showFull && ( |
| 36 | + <img |
| 37 | + src={src} |
| 38 | + alt={alt ?? ''} |
| 39 | + className={className} |
| 40 | + data-test={`image-viewer`} |
| 41 | + /> |
| 42 | + )} |
| 43 | + {showFull && |
| 44 | + createPortal( |
| 45 | + <Viewer> |
| 46 | + <img src={src} alt={alt ?? ''} data-test={`image-viewer`} /> |
| 47 | + </Viewer>, |
| 48 | + portalRef.current, |
| 49 | + )} |
| 50 | + </WrapperButton> |
16 | 51 | ); |
17 | 52 | } |
18 | 53 |
|
19 | | -interface Props { |
| 54 | +interface WrapperProps { |
20 | 55 | showFull: boolean; |
21 | 56 | } |
22 | 57 |
|
23 | | -const ImageViewerStyled = styled.img<Props>` |
24 | | - max-width: 100%; |
25 | | - max-height: 100%; |
26 | | - position: ${t => (t.showFull ? 'fixed' : 'relative')}; |
| 58 | +const WrapperButton = styled.button<WrapperProps>` |
27 | 59 | cursor: ${t => (t.showFull ? 'zoom-out' : 'zoom-in')}; |
28 | | - width: ${t => (t.showFull ? '100%' : 'auto')}; |
29 | | - z-index: ${t => (t.showFull ? '100' : 'auto')}; |
30 | | - object-fit: contain; |
31 | | - /* Depends on navbarheight */ |
32 | | - /* top: ${t => (t.showFull ? '2.5rem' : '0')}; */ |
33 | | - top: 0; |
34 | | - left: 0; |
35 | | - right: 0; |
36 | | - bottom: 0; |
37 | | - background-color: ${t => t.theme.colors.bg}; |
| 60 | + border: none; |
| 61 | + padding: 0; |
| 62 | + width: fit-content; |
| 63 | + height: fit-content; |
| 64 | + user-select: none; |
| 65 | + border-radius: ${p => p.theme.radius}; |
| 66 | + background-color: transparent; |
| 67 | +
|
| 68 | + &:hover, |
| 69 | + &:focus { |
| 70 | + outline: 2px solid ${p => p.theme.colors.main}; |
| 71 | + } |
| 72 | +
|
| 73 | + & img { |
| 74 | + border-radius: ${p => p.theme.radius}; |
| 75 | + vertical-align: sub; |
| 76 | + } |
| 77 | +`; |
| 78 | + |
| 79 | +const Viewer = styled.div` |
| 80 | + position: fixed; |
| 81 | + inset: 0; |
| 82 | + width: 100vw; |
| 83 | + height: 100%; |
| 84 | + max-height: 100vh; |
| 85 | + max-height: 100dvh; |
| 86 | + display: grid; |
| 87 | + place-items: center; |
| 88 | + padding: ${p => p.theme.margin}rem; |
| 89 | + z-index: 200; |
| 90 | + background-color: rgba(0, 0, 0, 0.85); |
| 91 | + cursor: zoom-out; |
| 92 | + backdrop-filter: blur(5px); |
| 93 | +
|
| 94 | + & img { |
| 95 | + height: 90%; |
| 96 | + max-width: 100%; |
| 97 | + max-height: 100vh; |
| 98 | + object-fit: contain; |
| 99 | + border-radius: ${p => p.theme.radius}; |
| 100 | + } |
38 | 101 | `; |
0 commit comments