The following code at least starts loading only after one instance of the html component comes into view with an intersection observer, but it loads all the instances instead of only the visible one/ones.
only the visible one.
<script defer type="module">
// Set up our observer:
let observerPlayground = new IntersectionObserver((entries, observerRef) => {
entries.forEach(async (entry) => {
// `isIntersecting` will be `true` if any part of the element is currently visible
if (entry.isIntersecting) {
// We are assuming here that your Web Component is located in a file
// named after its tag name
const name = entry.target.nodeName.toLowerCase();
// Once we've observed this element come into view, we can safely remove
// the observer since we won't need to import the WC code again
observerRef.unobserve(entry.target);
try {
// Let's load the WC code
const playgroundElementsModule = await import('https://cdn.jsdelivr.net/npm/playground-elements@0.18.1/+esm');
// Use the imported module as needed
console.log(playgroundElementsModule);
} catch (error) {
console.error('Error loading playground-elements:', error);
} finally {
// Disconnect the observer to stop observing the current element
observerRef.disconnect();
}
}
});
});
// Observe all components with the desired class
const els = document.querySelectorAll('playground-ide');
els.forEach((el) => {
observerPlayground.observe(el);
});
</script>