Skip to content

Commit

Permalink
fix: use ResizeObserver to watch for element size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
josias-r committed Nov 2, 2022
1 parent 1bb3bd3 commit cbf835a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib/boarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Boarding {
private currentStep: number;
private currentMovePrevented: boolean;

private resizeObserver: ResizeObserver;

private overlay: Overlay;

constructor(options?: BoardingOptions) {
Expand Down Expand Up @@ -69,6 +71,10 @@ class Boarding {
onReset: this.options.onReset,
});

this.resizeObserver = new ResizeObserver(() => {
this.refresh();
});

// bind this class to eventHandlers
this.onResize = this.onResize.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
Expand All @@ -95,6 +101,8 @@ class Boarding {

// attach eventListeners BEFORE setting highlighting element
this.attachEventListeners();
// attach resizeObserver for size changes
this.updateResizeObserver();

this.isActivated = true;
this.currentStep = index;
Expand All @@ -118,6 +126,8 @@ class Boarding {
}
// attach eventListeners BEFORE setting highlighting element
this.attachEventListeners();
// attach resizeObserver for size changes
this.updateResizeObserver();

this.isActivated = true;
this.overlay.highlight(element);
Expand All @@ -142,6 +152,8 @@ class Boarding {
}

this.overlay.highlight(previousElem);
// attach resizeObserver for size changes
this.updateResizeObserver();
this.currentStep -= 1;
}

Expand All @@ -165,6 +177,8 @@ class Boarding {
}

this.overlay.highlight(nextElem);
// attach resizeObserver for size changes
this.updateResizeObserver();
this.currentStep += 1;
}

Expand All @@ -189,6 +203,11 @@ class Boarding {
public reset(immediate = false) {
this.currentStep = 0;
this.isActivated = false;
const domElement = this.overlay.currentHighlightedElement?.getElement();
if (domElement) {
this.resizeObserver.unobserve(domElement);
}

this.overlay.clear(immediate);
this.removeEventListeners();
}
Expand Down Expand Up @@ -246,6 +265,7 @@ class Boarding {
window.addEventListener("touchstart", this.onClick, false);
}
}

/**
* Removes all DOM events listeners
*/
Expand All @@ -258,6 +278,17 @@ class Boarding {
window.removeEventListener("touchstart", this.onClick, false);
}

private updateResizeObserver() {
const previousElement = this.overlay.previouslyHighlightedElement;
if (previousElement) {
this.resizeObserver.unobserve(previousElement.getElement());
}
const element = this.overlay.currentHighlightedElement;
if (element) {
this.resizeObserver.observe(element.getElement());
}
}

/**
* Removes the popover if clicked outside the highlighted element
* or outside the
Expand Down

0 comments on commit cbf835a

Please sign in to comment.