pinafore/routes/_components/LazyImage.html

52 lines
1 KiB
HTML
Raw Normal View History

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