pinafore/routes/_components/LazyImage.html

52 lines
1.0 KiB
HTML

<div class="lazy-image" style={computedStyle} >
<img
class={className}
aria-hidden={ariaHidden}
{alt}
{title}
{width}
{height}
src={displaySrc}
ref:node
/>
</div>
<style>
.lazy-image {
overflow: hidden;
}
</style>
<script>
import { decodeImage } from '../_utils/decodeImage'
export default {
async oncreate () {
try {
await decodeImage(this.refs.node)
} catch (e) {
this.set({ error: true })
}
},
data: () => ({
error: false,
fallback: void 0,
background: '',
width: void 0,
height: void 0,
className: '',
ariaHidden: '',
alt: '',
title: ''
}),
computed: {
computedStyle: ({ width, height, background }) => {
return [
width && `width: ${width}px;`,
height && `height: ${height}px;`,
background && `background: ${background};`
].filter(Boolean).join('')
},
displaySrc: ({ error, src, fallback }) => ((error && fallback) || src)
}
}
</script>