2021-06-10 09:41:07 +02:00
|
|
|
<template>
|
2022-10-11 17:48:58 +02:00
|
|
|
<div ref="wrapper" class="flex-1" v-bind="$attrs">
|
|
|
|
<div class="h-full w-full max-w-100 min-h-[10rem]">
|
2021-06-10 09:41:07 +02:00
|
|
|
<!-- Show the placeholder as background -->
|
|
|
|
<blurhash-img
|
|
|
|
v-if="blurhash"
|
|
|
|
:hash="blurhash"
|
|
|
|
:aspect-ratio="height / width"
|
2022-07-12 10:55:28 +02:00
|
|
|
class="transition-opacity duration-500"
|
|
|
|
:class="blurhashOpacity"
|
2021-06-10 09:41:07 +02:00
|
|
|
/>
|
|
|
|
|
|
|
|
<!-- Show the real image on the top and fade in after loading -->
|
|
|
|
<img
|
|
|
|
ref="image"
|
2022-10-11 17:48:58 +02:00
|
|
|
class="transition-opacity duration-500 rounded-lg object-cover mx-auto h-full"
|
2022-07-12 10:55:28 +02:00
|
|
|
:class="imageOpacity"
|
2021-06-10 09:41:07 +02:00
|
|
|
alt=""
|
2021-11-07 14:59:20 +01:00
|
|
|
src=""
|
2022-11-04 16:11:36 +01:00
|
|
|
loading="lazy"
|
2021-06-10 09:41:07 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
<script lang="ts" setup>
|
2021-06-10 09:41:07 +02:00
|
|
|
import BlurhashImg from "./BlurhashImg.vue";
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
import { computed, ref, onMounted, onUnmounted, watchEffect } from "vue";
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
src: string;
|
|
|
|
blurhash?: string | null;
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
|
|
|
rounded?: boolean;
|
|
|
|
}>(),
|
|
|
|
{ blurhash: null, width: 100, height: 100, rounded: false }
|
|
|
|
);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const isLoaded = ref(false);
|
|
|
|
const observer = ref<IntersectionObserver | null>(null);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const wrapper = ref<HTMLElement | null>(null);
|
|
|
|
const image = ref<HTMLImageElement | null>(null);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const src = computed(() => props.src);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const isIntersecting = ref(false);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const blurhashOpacity = computed(() =>
|
|
|
|
isLoaded.value ? "opacity-0 hidden" : "opacity-100"
|
|
|
|
);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const imageOpacity = computed(() =>
|
|
|
|
isLoaded.value ? "opacity-100" : "opacity-0"
|
|
|
|
);
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
onMounted(() => {
|
|
|
|
observer.value = new IntersectionObserver((entries) => {
|
|
|
|
isIntersecting.value = entries[0].isIntersecting;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (wrapper.value) {
|
|
|
|
observer.value.observe(wrapper.value);
|
2021-06-10 09:41:07 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
});
|
2021-06-10 09:41:07 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
onUnmounted(() => {
|
|
|
|
if (observer.value) {
|
|
|
|
observer.value.disconnect();
|
2021-06-10 09:41:07 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
// Image is visible (means: has entered the viewport),
|
|
|
|
// so start loading by setting the src attribute
|
|
|
|
if (image.value) {
|
|
|
|
image.value.src = src.value;
|
|
|
|
|
|
|
|
image.value.onload = () => {
|
|
|
|
// Image is loaded, so start fading in
|
|
|
|
isLoaded.value = true;
|
|
|
|
};
|
2021-11-02 19:47:54 +01:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
});
|
|
|
|
</script>
|