Last active
February 17, 2021 21:04
-
-
Save vhoyer/76c9cdad5bf8917d41d2e7c4ce1067ed to your computer and use it in GitHub Desktop.
lazy-google-maps.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<component | |
:is="tag" | |
:src="mapUrl" | |
class="w-full h-96 animate-skeleton" | |
/> | |
</template> | |
<script> | |
export default { | |
name: 'ui-map', | |
props: { | |
key: { | |
type: String, | |
required: true | |
}, | |
address: { | |
type: String, | |
required: true | |
} | |
}, | |
data: () => ({ | |
observer: null, | |
tag: 'div' | |
}), | |
computed: { | |
mapUrl () { | |
return `https://www.google.com/maps/embed/v1/place?key=${this.key}&q=${encodeURIComponent(this.address)}` | |
} | |
}, | |
mounted () { | |
this.observer = new IntersectionObserver(([entry]) => { | |
if (!entry.isIntersecting) return | |
// stop observing if has already trigger | |
this.observer.unobserve(entry.target) | |
this.tag = 'iframe' | |
}, { | |
rootMargin: '50px' | |
}) | |
this.observer.observe(this.$el) | |
}, | |
beforeDestroy () { | |
this.observer.unobserve(this.$el) | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment