@stimulus-library/controllers
Version:
A library of useful controllers for Stimulus
22 lines (21 loc) • 773 B
JavaScript
import { BaseController, scrollToElement } from "@stimulus-library/utilities";
export class ScrollToController extends BaseController {
scroll() {
const target = document.querySelector(this.selectorValue);
if (!target) {
console.warn(`Could not find target for '${this.selectorValue}'`);
return;
}
scrollToElement(target, {
behavior: this.hasBehaviorValue ? this.behaviorValue : "smooth",
block: this.hasBlockValue ? this.blockValue : "center",
inline: this.hasInlineValue ? this.inlineValue : "center",
}).catch(() => target.scrollIntoView());
}
}
ScrollToController.values = {
selector: String,
behavior: String,
block: String,
inline: String,
};