1
0
mirror of https://github.com/nolanlawson/pinafore synced 2025-02-03 15:17:37 +01:00

59 lines
1.2 KiB
HTML
Raw Normal View History

<div class="lazy-image" style={computedStyle} >
{#if displaySrc}
2018-03-13 23:07:30 -07:00
<img
class={className}
aria-hidden={ariaHidden}
{alt}
{title}
{width}
{height}
src={displaySrc}
2018-03-13 23:07:30 -07:00
/>
{/if}
2018-03-13 23:07:30 -07:00
</div>
<style>
.lazy-image {
overflow: hidden;
}
</style>
<script>
2018-03-14 07:24:16 -07:00
import { mark, stop } from '../_utils/marks'
import { decodeImage } from '../_utils/decodeImage'
2018-03-14 07:24:16 -07:00
2018-03-13 23:07:30 -07:00
export default {
async oncreate () {
2018-03-14 07:24:16 -07:00
mark('LazyImage oncreate()')
let { src, fallback } = this.get()
try {
await decodeImage(src)
this.set({ displaySrc: src })
} catch (e) {
if (fallback) {
this.set({displaySrc: fallback})
}
2018-03-14 07:24:16 -07:00
}
stop('LazyImage oncreate()')
},
data: () => ({
displaySrc: void 0,
hidden: 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('')
}
}
2018-03-13 23:07:30 -07:00
}
</script>