UNPKG

@stimulus-library/controllers

Version:

A library of useful controllers for Stimulus

56 lines (55 loc) 2.32 kB
import { BaseController, scrollAbsoluteBottom, scrollAbsoluteLeft, scrollAbsoluteTop, scrollDown, scrollLeft, scrollRight, scrollUp } from "@stimulus-library/utilities"; export class ScrollContainerController extends BaseController { get _increment() { return this.hasIncrementValue ? this.incrementValue : 50; } get _behaviour() { if (this.hasBehaviourValue) { if (["auto", "smooth"].includes(this.behaviourValue)) { return this.behaviourValue; } else { throw new Error(`'${this.behaviourValue}' is not a recognised scroll behaviour`); } } else { return "auto"; } } async scrollTop(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollAbsoluteTop(this.el, { behavior: this._behaviour }); } async scrollBottom(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollAbsoluteBottom(this.el, { behavior: this._behaviour }); } async scrollLeft(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollAbsoluteLeft(this.el, { behavior: this._behaviour }); } async scrollRight(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollAbsoluteLeft(this.el, { behavior: this._behaviour }); } async up(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollUp(this.el, this._increment, { behavior: this._behaviour }); } async down(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollDown(this.el, this._increment, { behavior: this._behaviour }); } async left(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollLeft(this.el, this._increment, { behavior: this._behaviour }); } async right(event) { event === null || event === void 0 ? void 0 : event.preventDefault(); await scrollRight(this.el, this._increment, { behavior: this._behaviour }); } } ScrollContainerController.values = { behaviour: String, increment: Number, };