laif-ds
Version:
Design System di Laif con componenti React basati su principi di Atomic Design
38 lines (37 loc) • 1.02 kB
JavaScript
"use client";
import { useRef as l, useState as h, useEffect as s } from "react";
const H = 50, m = 10;
function D(u) {
const e = l(null), t = l(null), [n, r] = h(!0), c = () => {
typeof window > "u" || e.current && (e.current.scrollTop = e.current.scrollHeight);
}, i = () => {
if (!(typeof window > "u") && e.current) {
const { scrollTop: o, scrollHeight: d, clientHeight: p } = e.current, S = Math.abs(
d - o - p
), a = t.current ? o < t.current : !1, T = t.current ? t.current - o : 0;
if (a && T > m)
r(!1);
else {
const w = S < H;
r(w);
}
t.current = o;
}
}, f = () => {
typeof window > "u" || r(!1);
};
return s(() => {
typeof window > "u" || e.current && (t.current = e.current.scrollTop);
}, []), s(() => {
typeof window > "u" || n && c();
}, u), {
containerRef: e,
scrollToBottom: c,
handleScroll: i,
shouldAutoScroll: n,
handleTouchStart: f
};
}
export {
D as useAutoScroll
};