-
Hello Máximo, me again. :) This time I would like to ask you about some internals of the @islands/images module. Such a cool module! The I would like to add custom CSS classes to the The original When I use my
There is no automatic asset transform. As I might have to handle quite a few images this way, handing over the asset path directly instead of first importing it would be a nice efficiency gain. What I triedI tried to create a custom module based on your Thank you very much for showing a way to perform that asset path transformation in a custom Lucas |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@lutuh You are looking for the transform Vue performs to extract Since Vite doesn't seem to combine these options, you might need to provide all of the ones you intend to use, in addition to your An example custom component I'm using in some of my sites, which uses the <script setup lang="ts">
import { useAttrs } from 'vue'
const attrs = useAttrs()
const props = defineProps({
src: { type: [String, Array], required: true },
narrow: { type: Boolean, default: false },
square: { type: Boolean, default: false },
wide: { type: Boolean, default: false },
})
let title = $computed(() => attrs.title || attrs.alt)
</script>
<template>
<Picture
class="img"
loading="lazy"
:src="src"
:class="{ narrow, square, wide, captioned: $slots.default }"
v-bind="$attrs"
/>
<Caption v-if="$slots.default">
<slot/>
</Caption>
</template> I recommend this pattern, since it abstracts any details about |
Beta Was this translation helpful? Give feedback.
-
Hello Máximo, thank you very much for your explanation and the example component. I added the asset transform to my export default defineConfig({
// ...
vue: {
template: {
transformAssetUrls: {
tags: {
CustomPicture: ['src'],
},
},
},
},
}) I am going to refactor my custom component as well. I can do the styling from outside the Thanks again! |
Beta Was this translation helpful? Give feedback.
@lutuh You are looking for the transform Vue performs to extract
src
andsrcset
urls as imports, which can be configured manually by providingtemplate.transformAssetUrls.tags
to@vitejs/plugin-vue
.Since Vite doesn't seem to combine these options, you might need to provide all of the ones you intend to use, in addition to your
CustomPicture: ['src'],
.An example custom component I'm using in some of my sites, which uses the
Picture
component internally: