Skip to content

Commit

Permalink
Scroll element into viewport when focused
Browse files Browse the repository at this point in the history
  • Loading branch information
AjayPoshak committed Nov 17, 2019
1 parent 58dc346 commit 1e047d6
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function Coachmark() {
throw new Error(
`To start using Coachmark plugin, please add data-coachmark attribute.`
);

const { top, left } = element.getBoundingClientRect();
const height = element.offsetHeight,
width = element.offsetWidth;
Expand Down Expand Up @@ -120,8 +121,11 @@ function Coachmark() {

// Add class to the element
element.classList.add("coachmark-highlight");

// Scroll element's parent container so that it is visible
element.scrollIntoView({ behavior: "smooth", block: "start" });
setTimeout(() => { // Chrome hack for scrollIntoView
element.scrollIntoView({ behavior: "smooth", block: "start" });
}, 0)

if (currentStep === steps.length - 1) {
nextBtn.style.display = "none";
Expand Down Expand Up @@ -159,6 +163,8 @@ function Coachmark() {
removeHighlight(steps[currentStep]);
const mountPoint = document.getElementById("js-coachmark");
if (mountPoint) mountPoint.remove();
if (nextBtn) nextBtn.removeEventListener("click", handleNextBtnClick);
if (skipBtn) skipBtn.removeEventListener("click", handleSkipClick);
}

function getCoachmarkElements() {
Expand Down

0 comments on commit 1e047d6

Please sign in to comment.