DevPlace CI / test (push) Failing after 28m20s
Also streamline the top navigation: drop the Tools dropdown, make Quizzes and Battles icon-only entries, and remove the Workspace, Containers and Editor entry points from the project detail page.
82 lines
2.6 KiB
JavaScript
82 lines
2.6 KiB
JavaScript
// retoor <retoor@molodetz.nl>
|
|
|
|
export class StickyScrollPane {
|
|
constructor(selectors, options = {}) {
|
|
this.selectors = Array.isArray(selectors) ? selectors : [selectors];
|
|
this.activeClass = options.activeClass || "sticky-scroll-active";
|
|
this.gapVar = options.gapVar || "--space-lg";
|
|
this.elements = new Set();
|
|
this.scheduled = false;
|
|
this.gapPx = this.resolveGapPx(this.gapVar);
|
|
this.resizeObserver = typeof ResizeObserver !== "undefined"
|
|
? new ResizeObserver(() => this.scheduleRecompute())
|
|
: null;
|
|
this.collect();
|
|
window.addEventListener("resize", () => this.scheduleRecompute(), { passive: true });
|
|
this.scheduleRecompute();
|
|
}
|
|
|
|
resolveGapPx(varName) {
|
|
const probe = document.createElement("div");
|
|
probe.style.position = "absolute";
|
|
probe.style.visibility = "hidden";
|
|
probe.style.pointerEvents = "none";
|
|
probe.style.height = `var(${varName})`;
|
|
document.body.appendChild(probe);
|
|
const px = probe.getBoundingClientRect().height;
|
|
probe.remove();
|
|
return px;
|
|
}
|
|
|
|
collect() {
|
|
this.selectors.forEach((selector) => {
|
|
document.querySelectorAll(selector).forEach((el) => {
|
|
if (this.elements.has(el)) return;
|
|
this.elements.add(el);
|
|
if (this.resizeObserver) this.resizeObserver.observe(el);
|
|
});
|
|
});
|
|
}
|
|
|
|
scheduleRecompute() {
|
|
if (this.scheduled) return;
|
|
this.scheduled = true;
|
|
requestAnimationFrame(() => {
|
|
this.scheduled = false;
|
|
this.recomputeAll();
|
|
});
|
|
}
|
|
|
|
recomputeAll() {
|
|
this.elements.forEach((el) => this.recompute(el));
|
|
}
|
|
|
|
recompute(el) {
|
|
const style = getComputedStyle(el);
|
|
if (style.position !== "sticky" || style.display === "none") {
|
|
this.disengage(el);
|
|
return;
|
|
}
|
|
const top = parseFloat(style.top) || 0;
|
|
const available = window.innerHeight - top - this.gapPx;
|
|
const natural = el.scrollHeight;
|
|
if (available > 0 && natural > available) {
|
|
this.engage(el, available);
|
|
} else {
|
|
this.disengage(el);
|
|
}
|
|
}
|
|
|
|
engage(el, availablePx) {
|
|
const height = `${Math.round(availablePx)}px`;
|
|
if (el.style.maxHeight !== height) el.style.maxHeight = height;
|
|
el.classList.add(this.activeClass);
|
|
}
|
|
|
|
disengage(el) {
|
|
if (!el.classList.contains(this.activeClass) && !el.style.maxHeight) return;
|
|
el.classList.remove(this.activeClass);
|
|
el.style.maxHeight = "";
|
|
}
|
|
}
|