mtxuilib 0.1.506 → 0.1.507

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare function useSnapScroll(): ((node: HTMLDivElement | null) => void)[];
@@ -0,0 +1,45 @@
1
+ "use client";
2
+ import { useCallback, useRef } from "react";
3
+ export function useSnapScroll() {
4
+ const autoScrollRef = useRef(true);
5
+ const scrollNodeRef = useRef();
6
+ const onScrollRef = useRef();
7
+ const observerRef = useRef();
8
+ const messageRef = useCallback((node) => {
9
+ if (node) {
10
+ const observer = new ResizeObserver(() => {
11
+ if (autoScrollRef.current && scrollNodeRef.current) {
12
+ const { scrollHeight, clientHeight } = scrollNodeRef.current;
13
+ const scrollTarget = scrollHeight - clientHeight;
14
+ scrollNodeRef.current.scrollTo({
15
+ top: scrollTarget,
16
+ });
17
+ }
18
+ });
19
+ observer.observe(node);
20
+ }
21
+ else {
22
+ observerRef.current?.disconnect();
23
+ observerRef.current = undefined;
24
+ }
25
+ }, []);
26
+ const scrollRef = useCallback((node) => {
27
+ if (node) {
28
+ onScrollRef.current = () => {
29
+ const { scrollTop, scrollHeight, clientHeight } = node;
30
+ const scrollTarget = scrollHeight - clientHeight;
31
+ autoScrollRef.current = Math.abs(scrollTop - scrollTarget) <= 10;
32
+ };
33
+ node.addEventListener("scroll", onScrollRef.current);
34
+ scrollNodeRef.current = node;
35
+ }
36
+ else {
37
+ if (onScrollRef.current) {
38
+ scrollNodeRef.current?.removeEventListener("scroll", onScrollRef.current);
39
+ }
40
+ scrollNodeRef.current = undefined;
41
+ onScrollRef.current = undefined;
42
+ }
43
+ }, []);
44
+ return [messageRef, scrollRef];
45
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mtxuilib",
3
3
  "private": false,
4
- "version": "0.1.506",
4
+ "version": "0.1.507",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },