Skip to content

Commit

Permalink
remove settimeout from the controller and use requestAnimationFrame i…
Browse files Browse the repository at this point in the history
…nstead
  • Loading branch information
bsatarnejad committed Dec 10, 2024
1 parent 8775574 commit 2c34853
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Controller } from '@hotwired/stimulus';

export default class ScrollController extends Controller {
static values = { targetId: String };
declare targetIdValue:string;

connect() {
// Wait to ensure DOM is fully loaded
setTimeout(() => {
if (this.targetIdValue) {
const element = document.querySelector(`[data-test-selector='op-relation-row-${this.targetIdValue}']`) as HTMLElement;
if (element) {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
});
this.waitForRenderAndAct();
}

waitForRenderAndAct() {
const element = document.querySelector('[data-scroll-active="true"]');

if (element) {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
} else {
requestAnimationFrame(this.waitForRenderAndAct.bind(this));
}
}
}

0 comments on commit 2c34853

Please sign in to comment.