Files
web/packages/design-system/lib/components/Image/index.tsx
Erik Tiekstra 3a38e99a71 Feat/BOOK-63 hotel subpages branding
* feat(BOOK-63): Replaced css variables and components to apply hotel branding on subpages
* feat(BOOK-63): Replaced css variables and components to apply hotel branding on hotel page map view

Approved-by: Christel Westerberg
Approved-by: Matilda Landström
2025-11-05 08:30:55 +00:00

60 lines
1.2 KiB
TypeScript

'use client'
import NextImage, { ImageProps as NextImageProps } from 'next/image'
import ImageFallback from '../ImageFallback'
import { useState, type CSSProperties, type SyntheticEvent } from 'react'
import { imageLoader } from './imageLoader'
type FocalPoint = {
x: number
y: number
}
export type ImageProps = NextImageProps & {
focalPoint?: FocalPoint
dimensions?: { width: number; height: number }
}
// Next/Image adds & instead of ? before the params
export default function Image({
focalPoint,
dimensions,
style,
src,
onError,
...props
}: ImageProps) {
const [imageError, setImageError] = useState(false)
const styles: CSSProperties = focalPoint
? {
objectFit: 'cover',
objectPosition: `${focalPoint.x}% ${focalPoint.y}%`,
...style,
}
: { ...style }
function handleError(error: SyntheticEvent<HTMLImageElement, Event>) {
if (onError) {
onError(error)
} else {
setImageError(true)
}
}
if (!src || imageError) {
return <ImageFallback />
}
return (
<NextImage
{...props}
src={src}
style={styles}
onError={handleError}
loader={imageLoader({ dimensions, focalPoint })}
/>
)
}