Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RLL-000: fix lazy loading in iframe #164

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/LazyLoad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export default class LazyLoad extends Component<Props, State> {

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);
}
}
Expand All @@ -73,14 +73,26 @@ export default class LazyLoad extends Component<Props, State> {
}

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[]) => {
Expand All @@ -96,8 +108,8 @@ export default class LazyLoad extends Component<Props, State> {
});

// Stop observing
const node = this.wrapper?.current;
if (node && node instanceof HTMLElement) {
const node = this.getRefElement();
if (node) {
this.elementObserver?.unobserve(node);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down