I am trying to determine whether a slot element has a scrollbar. If it does I want to add a show all/show fewer link next to the slot which will expand or contract it accordingly.
I am using IntersectionObserver because it seems designed for this very use case. The slot contains a ul which itself has a variable number of li children. The ul is not populated until after the function that adds the show more/show link is fired - hence the use of IntersectionObserver. The function I wrote should return a boolean:
_isOverflowing( rootElem, childElem ) {
// rootElem is the slot; childElem is the ul
let observer;
const options = {
root: rootElem,
threshold: 1.0
};
function handleIntersect(entries, observer) {
entries.forEach(entry => {
return entry.intersectionRatio >= options.threshold
});
}
observer = new IntersectionObserver(handleIntersect, options);
return observer.observe( childElem );
}
What I am seeing is that the final function
observer.observe( childElem )
returns as undefined. What am I doing wrong?