import React from 'react' import { getOriginalImageDimensions, imageCalculateHeight } from '../../utils/imageServices' import { LmImageProps } from './imageTypes' import LmAspectRatio from './LmAspectRatio' import LmSquareImage from '../avatar/LmSquareImage' import { ImageStoryblok } from '../../typings/generated/components-schema' import { makeStyles } from 'tss-react/mui' const useStyles = makeStyles({ name: 'Image' })( (theme, props) => ({ image: { margin: 'auto' }, imgAddons: { '&.square, &.rounded-0 img': { borderRadius: 0 }, '&.rounded img': { borderRadius: theme.shape.borderRadius }, '&.rounded-circle img': { borderRadius: '50%' }, '&.rounded-circle': { borderRadius: '50%', overflow: 'hidden' } }, customImage: { borderRadius: props.border_radius ? props.border_radius : undefined } }) ) export default function LmImage({ content, onClick }: LmImageProps): JSX.Element | null { const { classes, cx: clsx } = useStyles(content) const definedWidth = content.width const definedHeight = content.height const property = content.property || [] const imageSource = content.source const { priority, disable_lazy_loading } = content if (!imageSource) { return
// don't need to render anything } const loading = priority ? undefined : disable_lazy_loading ? 'eager' : undefined const originalDimensions = content.image_data || getOriginalImageDimensions(imageSource || '') const manualSquare = definedWidth && definedHeight && definedWidth === definedHeight const squareOrRoundedIsSet = property.includes('rounded-circle') || property.includes('square') const isSquare = manualSquare || squareOrRoundedIsSet const squareSize = isSquare ? definedHeight || definedWidth || originalDimensions.width // todo was set to 120 before, does it break things? : undefined let proportionalWidth = 0 let proportionalHeight = 0 let isProportional = false if ( !squareOrRoundedIsSet && ((definedWidth && !definedHeight) || (!definedWidth && definedHeight)) ) { isProportional = true proportionalWidth = definedWidth || 0 proportionalHeight = definedHeight || 0 } if (isSquare && squareSize) { return ( <> onClick() : undefined, alt: content.alt || 'website image', style: { objectFit: 'cover', objectPosition: 'center' }, loading, fill: true, className: classes.customImage }} /> ) } const proportionalHeightCalc = isProportional && (proportionalHeight || imageCalculateHeight(proportionalWidth, originalDimensions)) return ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-noninteractive-element-interactions,jsx-a11y/no-static-element-interactions
onClick() : undefined, alt: content.alt || 'website image', className: classes.customImage, ...(isProportional ? proportionalHeightCalc ? { style: { height: `auto`, // width: 'auto', maxHeight: `${proportionalHeightCalc}px`, maxWidth: '100%', objectFit: 'contain' } } : { style: { width: `${proportionalWidth}px`, height: 'auto', maxWidth: '100%', objectFit: 'contain' } } : { style: { width: '100%', height: 'auto' } }) }} />
) }