webapp/src/components/_shared/Image/Image.tsx

32 lines
790 B
TypeScript
Raw Normal View History

2023-03-24 14:51:05 +00:00
import type { JSX } from 'solid-js'
import { Link } from '@solidjs/meta'
import { splitProps } from 'solid-js'
import { getImageUrl } from '../../../utils/getImageUrl'
2023-03-24 14:51:05 +00:00
type Props = JSX.ImgHTMLAttributes<HTMLImageElement> & {
width: number
alt: string
}
export const Image = (props: Props) => {
const [local, others] = splitProps(props, ['src', 'alt'])
const imageUrl = getImageUrl(local.src, { width: others.width })
const imageSrcSet = [1, 2, 3]
.map(
(pixelDensity) =>
`${getImageUrl(local.src, { width: others.width * pixelDensity })} ${pixelDensity}x`,
)
.join(', ')
return (
<>
<Link rel="preload" as="image" imagesrcset={imageSrcSet} />
<img src={imageUrl} alt={local.alt} srcSet={imageSrcSet} {...others} />
</>
)
2023-03-24 14:51:05 +00:00
}