Applied NuxtImg to pages and components

This commit is contained in:
2024-12-07 23:29:20 +09:00
parent 3ddcf5fa5e
commit e821b258c3
13 changed files with 264 additions and 170 deletions

View File

@@ -0,0 +1,30 @@
/**
* Vue Composable for optimizing image used in background-image.
* @module composables/backgroundImageOptimization
*/
/**
* Vue Composable for optimizing background-image with Nuxt Image
* @returns { WritableComputedRef<{ backgroundImage: string}, string>} writable computed value that will return the Vue style object with optimized image URL
*/
function useBackgroundImageOptimization() {
const targetImage = ref("");
const img = useImage();
const backgroundImageStyles = computed({
get: () => {
const imageURL = img(targetImage.value, {
format: "webp",
quality: 80,
});
return { backgroundImage: `url("${imageURL}")` };
},
set: (value: string) => {
targetImage.value = value;
},
});
return { backgroundImageStyles };
}
export { useBackgroundImageOptimization };