diff --git a/src/LazyLoad.tsx b/src/LazyLoad.tsx index 1ec3488..30d2184 100644 --- a/src/LazyLoad.tsx +++ b/src/LazyLoad.tsx @@ -61,9 +61,9 @@ export default class LazyLoad extends Component { this.elementObserver = new IntersectionObserver(this.lazyLoadHandler, options); - const node = this.wrapper?.current; + const node = this.getRefElement(); - if (node instanceof HTMLElement) { + if (node) { this.elementObserver.observe(node); } } @@ -73,14 +73,26 @@ export default class LazyLoad extends Component { } componentWillUnmount() { - const node = this.wrapper?.current; - if (node && node instanceof HTMLElement) { + const node = this.getRefElement(); + if (node) { this.elementObserver?.unobserve(node); } } getEventNode() { - return scrollParent(this.wrapper?.current); + return scrollParent(this.getRefElement()); + } + + getRefElement(): HTMLElement | null { + const node = this.wrapper?.current as HTMLElement | null; + const regExp = /\[object HTML[\S]*Element\]/i; + const nodeToString = Object.prototype.toString.call(node); + + if (regExp.test(nodeToString)) { + return node as HTMLElement; + } + + return null; } lazyLoadHandler = (entries: IntersectionObserverEntry[]) => { @@ -96,8 +108,8 @@ export default class LazyLoad extends Component { }); // Stop observing - const node = this.wrapper?.current; - if (node && node instanceof HTMLElement) { + const node = this.getRefElement(); + if (node) { this.elementObserver?.unobserve(node); } } diff --git a/src/utils/index.js b/src/utils/index.js index 5c624a2..f47fed1 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -5,7 +5,7 @@ const style = (element, prop) => (typeof getComputedStyle !== 'undefined' const overflow = element => style(element, 'overflow') + style(element, 'overflow-y') + style(element, 'overflow-x'); export default element => { - if (!(element instanceof HTMLElement)) { + if (!element) { return window; }