All files / src/components ImageViewer.tsx

100% Statements 1/1
100% Branches 0/0
100% Functions 1/1
100% Lines 1/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                  12x                                                                                                                                                                                          
// An image (a receipt or a document) full screen (Design canvas: "Kuitti koko näytöllä"), with
// Close and Download. PDFs open in the browser's own viewer instead.
import Box from '@mui/material/Box'
import Dialog from '@mui/material/Dialog'
import IconButton from '@mui/material/IconButton'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { colors } from '#/theme/tokens'
 
interface Props {
  /** Where the image is, and where it is downloaded from. */
  src: string
  downloadHref: string
  /** The file's name, shown at the top. */
  filename: string
  /** The dialog's name, and the image's description. */
  title: string
  alt: string
  closeLabel: string
  downloadLabel: string
  open: boolean
  onClose: () => void
}
 
export function ImageViewer(props: Props) {
  return (
    <Dialog
      fullScreen
      open={props.open}
      onClose={props.onClose}
      slotProps={{
        paper: {
          'aria-label': props.title,
          sx: { bgcolor: colors.plum, color: colors.cream },
        },
      }}
    >
      <Stack
        direction="row"
        sx={{
          alignItems: 'center',
          justifyContent: 'space-between',
          px: 1.5,
          pt: 2,
          pb: 1,
        }}
      >
        <IconButton
          aria-label={props.closeLabel}
          onClick={props.onClose}
          sx={{ color: 'inherit' }}
        >
          <svg
            width="22"
            height="22"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2.6"
            strokeLinecap="round"
            aria-hidden="true"
          >
            <path d="M6 6 L18 18 M18 6 L6 18" />
          </svg>
        </IconButton>
        <Typography
          noWrap
          sx={{ fontSize: 14, fontWeight: 800, minWidth: 0, px: 1 }}
        >
          {props.filename}
        </Typography>
        <IconButton
          component="a"
          href={props.downloadHref}
          aria-label={props.downloadLabel}
          sx={{ color: 'inherit' }}
        >
          <svg
            width="22"
            height="22"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2.4"
            strokeLinecap="round"
            strokeLinejoin="round"
            aria-hidden="true"
          >
            <path d="M12 4 V15 M7 10 L12 15 L17 10 M5 20 H19" />
          </svg>
        </IconButton>
      </Stack>
      <Box
        sx={{
          flex: 1,
          minHeight: 0,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          p: 2,
          overflow: 'auto',
        }}
      >
        <Box
          component="img"
          src={props.src}
          alt={props.alt}
          sx={{
            maxWidth: '100%',
            maxHeight: '100%',
            objectFit: 'contain',
            bgcolor: colors.white,
          }}
        />
      </Box>
    </Dialog>
  )
}