Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<template>
<div
ref="containerEl"
class="tw-overflow-hidden"
:class="{
'tw-aspect-square': square,
}"
>
<img
v-if="image"
:alt="image.altText"
class="tw-object-cover tw-w-full tw-max-w-full tw-max-h-full"
:src="image.image"
:sizes="`${containerWidth}px`"
:srcset="srcset"
:class="{
'tw-h-full': square,
}"
/>
</div>
</template>
<script lang="ts" setup>
import { Image } from '@/stores/images'
import { computed, ref } from 'vue'
import { useElementSize } from '@vueuse/core'
defineOptions({ compatConfig: { MODE: 3 } })
const props = defineProps<{ image: Image | null | undefined; square?: boolean }>()
const containerEl = ref<HTMLDivElement>()
const { width: containerWidth } = useElementSize(containerEl)
const srcset = computed(() => {
const image = props.image
if (!image) return ''
let sources = [
{ width: image.width, height: image.height, url: image.image },
...image.thumbnails,
]
if (props.square) sources = sources.filter((img) => img.width === img.height)
return sources.map(({ width, url }) => `${url} ${width}w`).join(',')
})
</script>