feat: image comp supports string dimensions

pull/3017/head
William Grant 2 years ago
parent a37c8eaf13
commit c0cfe153c6

@ -1,19 +1,19 @@
import React, { useCallback } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import React, { useCallback } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { Spinner } from '../basic/Spinner';
import { AttachmentType, AttachmentTypeWithPath } from '../../types/Attachment';
import { useEncryptedFileFetch } from '../../hooks/useEncryptedFileFetch';
import { useDisableDrag } from '../../hooks/useDisableDrag'; import { useDisableDrag } from '../../hooks/useDisableDrag';
import { useEncryptedFileFetch } from '../../hooks/useEncryptedFileFetch';
import { AttachmentType, AttachmentTypeWithPath } from '../../types/Attachment';
import { Spinner } from '../basic/Spinner';
type Props = { type Props = {
alt: string; alt: string;
attachment: AttachmentTypeWithPath | AttachmentType; attachment: AttachmentTypeWithPath | AttachmentType;
url: string | undefined; // url is undefined if the message is not visible yet url: string | undefined; // url is undefined if the message is not visible yet
height?: number; height?: number | string;
width?: number; width?: number | string;
overlayText?: string; overlayText?: string;
@ -46,7 +46,7 @@ export const Image = (props: Props) => {
attachment, attachment,
closeButton, closeButton,
darkOverlay, darkOverlay,
height, height: _height,
onClick, onClick,
onClickClose, onClickClose,
onError, onError,
@ -56,7 +56,7 @@ export const Image = (props: Props) => {
forceSquare, forceSquare,
attachmentIndex, attachmentIndex,
url, url,
width, width: _width,
} = props; } = props;
const onErrorUrlFilterering = useCallback(() => { const onErrorUrlFilterering = useCallback(() => {
@ -78,6 +78,9 @@ export const Image = (props: Props) => {
// data will be url if loading is finished and '' if not // data will be url if loading is finished and '' if not
const srcData = !loading ? urlToLoad : ''; const srcData = !loading ? urlToLoad : '';
const width = typeof _width === 'number' ? `${_width}px` : _width;
const height = typeof _height === 'number' ? `${_height}px` : _height;
return ( return (
<div <div
role={role} role={role}
@ -93,10 +96,10 @@ export const Image = (props: Props) => {
softCorners ? 'module-image--soft-corners' : null softCorners ? 'module-image--soft-corners' : null
)} )}
style={{ style={{
maxHeight: `${height}px`, maxHeight: height,
maxWidth: `${width}px`, maxWidth: width,
minHeight: `${height}px`, minHeight: height,
minWidth: `${width}px`, minWidth: width,
}} }}
data-attachmentindex={attachmentIndex} data-attachmentindex={attachmentIndex}
> >
@ -104,11 +107,11 @@ export const Image = (props: Props) => {
<div <div
className="module-image__loading-placeholder" className="module-image__loading-placeholder"
style={{ style={{
maxHeight: `${height}px`, maxHeight: height,
maxWidth: `${width}px`, maxWidth: width,
width: `${width}px`, width,
height: `${height}px`, height,
lineHeight: `${height}px`, lineHeight: height,
textAlign: 'center', textAlign: 'center',
}} }}
> >
@ -123,12 +126,12 @@ export const Image = (props: Props) => {
)} )}
alt={alt} alt={alt}
style={{ style={{
maxHeight: `${height}px`, maxHeight: height,
maxWidth: `${width}px`, maxWidth: width,
minHeight: `${height}px`, minHeight: height,
minWidth: `${width}px`, minWidth: width,
width: forceSquare ? `${width}px` : '', width: forceSquare ? width : '',
height: forceSquare ? `${height}px` : '', height: forceSquare ? height : '',
}} }}
src={srcData} src={srcData}
onDragStart={disableDrag} onDragStart={disableDrag}
@ -165,7 +168,7 @@ export const Image = (props: Props) => {
</div> </div>
) : null} ) : null}
{overlayText ? ( {overlayText ? (
<div className="module-image__text-container" style={{ lineHeight: `${height}px` }}> <div className="module-image__text-container" style={{ lineHeight: height }}>
{overlayText} {overlayText}
</div> </div>
) : null} ) : null}

Loading…
Cancel
Save