@stimulus-library/controllers
Version:
A library of useful controllers for Stimulus
23 lines (22 loc) • 822 B
JavaScript
import { BaseController, dispatchEvent } from "@stimulus-library/utilities";
import { useIntersection } from "@stimulus-library/mixins";
export class IntersectionController extends BaseController {
get _threshold() {
if (this.hasThresholdValue) {
return this.thresholdValue.split(",").map(val => Number.parseFloat(val.trim())).filter(val => val >= 0 && val <= 1);
}
else {
return [0, 1];
}
}
connect() {
useIntersection(this, this.el, this.appear, this.disappear, { threshold: this._threshold });
}
appear(entry) {
dispatchEvent(this, this.el, this.eventName("appear"));
}
disappear(entry) {
dispatchEvent(this, this.el, this.eventName("disappear"));
}
}
IntersectionController.values = { threshold: String };