import React, { CSSProperties } from 'react' import { browserCheck } from '@ta-interaktiv/browsercheck' import { placeholderString, getAspectRatioPadding } from '../assets' import './styles.scss' // Image with Mobile-Desktop-Switch export interface ImageProps { /** File/s to be displayed */ file?: string | [string, string] /** Alternate text for image.If none is provided image gets role presentation */ alt?: string /** CSS class for the Container */ className?: string /** Inline css for the Image */ style?: CSSProperties /** Title to show above the Image */ title?: string | JSX.Element /** Subtitle to show below title */ sub?: string | JSX.Element /** Source to show below the image */ source?: string | JSX.Element /** Proportions for the Image f.e.: "16:9" */ aspectRatio?: string /** Specific height for the Image both | [mobile,desktop] */ height?: string | [string, string] /** makes the image stick out of the text column */ oversized?: boolean /** Child elements to be placed below subline and above image */ children?: JSX.Element /** Places title and subline below image above source */ imageFirst?: boolean /** any other properties get applied to the image */ props?: any [key: string]: any } /** Displays an image element. If two image files have been provided one is rendered on desktop/tablets and one on mobile screens. Title, subtitle and source are displayed if provided. If no files are provided a placeholder is rendered. */ export const Image = ({ file, alt, className = 'cbImage', style, title, sub, source, height, aspectRatio, oversized, children, imageFirst = false, ...props }: ImageProps) => { let src = '' let h = 'auto' if (file) { if (typeof file === 'object') { if (browserCheck.isMobile) { src = file[0].toString() } else { src = file[1].toString() } } else { src = file.toString() } } if (typeof height === 'object') { if (browserCheck.isMobile) { h = height[0] } else { h = height[1] } } else { h = height || '100%' } const ImgElement = ({ imageSrc }: { imageSrc?: string }) => { if (!imageSrc || imageSrc === '') { return ( ) } return ( {alt} ) } const titleElement =
{title}
const subElement =
{sub}
const sourceElement =
{source}
if (src === '' && !height && !aspectRatio) aspectRatio = '16:9' return (
{!imageFirst && (
{title && titleElement} {sub && subElement}
)} {children && !imageFirst && children}
{children && imageFirst && children} {imageFirst && (
{title && titleElement} {sub && subElement} {source && sourceElement}
)} {source && !imageFirst && sourceElement}
) }