File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 2424 // Listen for the loaded event
2525 window . addEventListener ( "zones-loaded" , console . log ( "zones are loaded" ) ) ;
2626
27+ // Listen for the intersection event
28+ window . addEventListener ( "zone" , ( e ) => console . log ( "%s showing" , e . detail . id ) ) ;
29+
2730 // Demo-only show vips
2831 import { getValidInsertionPoints } from "/lib/zones.js" ;
2932 const vips = getValidInsertionPoints ( ) ;
Original file line number Diff line number Diff line change 55// Internals
66let map = new Map ( ) ;
77let fragment = new DocumentFragment ( ) ;
8+ let observer = new IntersectionObserver ( handleIntersection , { rootMargin : "500px" } )
89
910// Locker
1011export let locker ;
@@ -129,6 +130,9 @@ export class Zone {
129130 this . element . dataset . distributed = true ;
130131 this . vip . insertAdjacentElement ( loc , this . element ) ;
131132 this . log ( this . msg || "zone moved into new location" ) ;
133+
134+ // Observer for performance team
135+ observer . observe ( this . element ) ;
132136 }
133137 }
134138
@@ -450,3 +454,26 @@ export async function loadConfig(url) {
450454
451455 return config ;
452456}
457+
458+ /*
459+ * IntersectionObserver callback
460+ */
461+
462+ function handleIntersection ( entries , observer ) {
463+ entries . forEach ( ( entry ) => {
464+ if ( entry . isIntersecting ) {
465+ // Build the event
466+ const loadZone = new CustomEvent ( "zone" , {
467+ detail : {
468+ id : entry . target . id
469+ }
470+ } ) ;
471+
472+ // Send it
473+ window . dispatchEvent ( loadZone ) ;
474+
475+ // Only do it once
476+ observer . unobserve ( entry . target ) ;
477+ }
478+ } ) ;
479+ }
You can’t perform that action at this time.
0 commit comments