@indietabletop/appkit 3.4.0 → 3.5.0

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.
package/lib/index.ts CHANGED
@@ -18,6 +18,7 @@ export * from "./use-is-installed.ts";
18
18
  export * from "./use-media-query.ts";
19
19
  export * from "./use-reverting-state.ts";
20
20
  export * from "./use-scroll-restoration.ts";
21
+ export * from "./useIsVisible.ts";
21
22
 
22
23
  // Utils
23
24
  export * from "./append-copy-to-text.ts";
@@ -0,0 +1,27 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+
3
+ export function useIsVisible<T extends HTMLElement>(
4
+ initialState = false,
5
+ options?: IntersectionObserverInit,
6
+ ) {
7
+ const ref = useRef<T | null>(null);
8
+ const [isVisible, setVisible] = useState(initialState);
9
+
10
+ useEffect(() => {
11
+ if (ref.current) {
12
+ const observer = new IntersectionObserver(([entry]) => {
13
+ if (entry) {
14
+ setVisible(entry.isIntersecting);
15
+ }
16
+ }, options);
17
+
18
+ observer.observe(ref.current);
19
+
20
+ return () => {
21
+ observer.disconnect();
22
+ };
23
+ }
24
+ });
25
+
26
+ return { ref, isVisible };
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indietabletop/appkit",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "A collection of modules used in apps built by Indie Tabletop Club",
5
5
  "private": false,
6
6
  "type": "module",