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