Skip to content

Commit 5b3f4a3

Browse files
committed
Add InteresectionObserver to ensure that the preview map is checked on when the mapmlify-layer intersects the viewport for the first time. It remains on after that unless turned off.
1 parent 9d450b2 commit 5b3f4a3

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/script/mapmlify-layer.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class MapmlifyLayer extends HTMLElement {
3131
#sourceCodeTextarea = null;
3232
#sourceCodeVisible = false;
3333
#moveendHandler = null;
34+
#lazyLoadObserver = null;
3435

3536
set layerConfig(value) {
3637
this.#config = value;
@@ -47,11 +48,42 @@ class MapmlifyLayer extends HTMLElement {
4748
if (this.#config) {
4849
this.#initDefaults();
4950
this.#render();
51+
this.#setupLazyLoading();
5052
}
5153
}
5254

5355
disconnectedCallback() {
5456
this.#removeViewer();
57+
if (this.#lazyLoadObserver) {
58+
this.#lazyLoadObserver.disconnect();
59+
this.#lazyLoadObserver = null;
60+
}
61+
}
62+
63+
#setupLazyLoading() {
64+
const options = {
65+
root: null,
66+
rootMargin: '100px',
67+
threshold: 0.1,
68+
};
69+
70+
this.#lazyLoadObserver = new IntersectionObserver((entries) => {
71+
entries.forEach((entry) => {
72+
if (
73+
entry.isIntersecting &&
74+
this.#layerCheckbox &&
75+
!this.#layerCheckbox.checked
76+
) {
77+
this.#layerCheckbox.checked = true;
78+
this.#layerCheckbox.dispatchEvent(new Event('change'));
79+
80+
this.#lazyLoadObserver.disconnect();
81+
this.#lazyLoadObserver = null;
82+
}
83+
});
84+
}, options);
85+
86+
this.#lazyLoadObserver.observe(this);
5587
}
5688

5789
#initDefaults() {

tests/helpers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ export function getLayerElements(page) {
9090
*/
9191
export async function activateLayer(page, index) {
9292
const layer = page.locator('mapmlify-layer').nth(index);
93-
await layer.locator('.layer-checkbox').check();
93+
const cb = layer.locator('.layer-checkbox');
94+
// The IntersectionObserver may have already checked this box
95+
const alreadyChecked = await cb.isChecked();
96+
if (!alreadyChecked) {
97+
await cb.check();
98+
}
9499
// Wait for the mapml-viewer to appear inside this layer
95100
await layer.locator('mapml-viewer').waitFor({ timeout: 15000 });
96101
}

0 commit comments

Comments
 (0)