funda-ui 4.7.701 → 4.7.723

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,84 @@
1
+ /**
2
+ * Watch for to know when a document becomes visible or hidden
3
+ * @param onVisible
4
+ * @param onHidden
5
+ * @param onPageInit executed once when the page first loads visibly
6
+ */
7
+ /*
8
+ @usage:
9
+
10
+ const App = () => {
11
+ usePageVisibility(
12
+ () => console.log("🍏 Page is now visible — current tab is active."),
13
+ () => console.log("🍎 Page is hidden — switched to another tab or minimized."),
14
+ () => console.log("🎬 Page initialized while visible.")
15
+ );
16
+
17
+ return <div>Try switching tabs to see the console output.</div>;
18
+ };
19
+
20
+ */
21
+ import { useEffect, useRef } from "react";
22
+
23
+ type VisibilityCallback = () => void;
24
+
25
+ type UsePageVisibility = (
26
+ onVisible?: VisibilityCallback,
27
+ onHidden?: VisibilityCallback,
28
+ onPageInit?: VisibilityCallback
29
+ ) => void;
30
+
31
+ const usePageVisibility: UsePageVisibility = (
32
+ onVisible,
33
+ onHidden,
34
+ onPageInit
35
+ ) => {
36
+ const visibleRef = useRef<boolean>(document.visibilityState === "visible");
37
+ const onVisibleRef = useRef<VisibilityCallback | undefined>(onVisible);
38
+ const onHiddenRef = useRef<VisibilityCallback | undefined>(onHidden);
39
+ const initialVisibleTriggeredRef = useRef<boolean>(false);
40
+ const onPageInitRef = useRef<VisibilityCallback | undefined>(onPageInit);
41
+
42
+ useEffect(() => {
43
+ onVisibleRef.current = onVisible;
44
+ }, [onVisible]);
45
+
46
+ useEffect(() => {
47
+ onHiddenRef.current = onHidden;
48
+ }, [onHidden]);
49
+
50
+ useEffect(() => {
51
+ onPageInitRef.current = onPageInit;
52
+ }, [onPageInit]);
53
+
54
+ useEffect(() => {
55
+ const handleVisibilityChange = () => {
56
+ const isVisible = document.visibilityState === "visible";
57
+
58
+ if (isVisible && !visibleRef.current) {
59
+ onVisibleRef.current && onVisibleRef.current();
60
+ }
61
+
62
+ if (!isVisible && visibleRef.current) {
63
+ onHiddenRef.current && onHiddenRef.current();
64
+ }
65
+
66
+ visibleRef.current = isVisible;
67
+ };
68
+
69
+ // It retains the original switching monitor, and can ensure that onPageInit will run when the first screen is loaded.
70
+ if (visibleRef.current && !initialVisibleTriggeredRef.current) {
71
+ initialVisibleTriggeredRef.current = true;
72
+ onPageInitRef.current && onPageInitRef.current();
73
+ }
74
+
75
+ document.addEventListener("visibilitychange", handleVisibilityChange);
76
+ return () => {
77
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
78
+ };
79
+ }, []);
80
+ };
81
+
82
+
83
+ export default usePageVisibility;
84
+ export { usePageVisibility };
@@ -3,19 +3,44 @@
3
3
  *
4
4
  * @usage:
5
5
  *
6
- * const App = () => {
7
- * const { connected, messages, disconnect, reconnect } = useSSE('http://localhost:3000/sse');
8
- *
9
- * return (
10
- * <div>
11
- * <p>Status: {connected ? '✅ Connected' : '❌ Disconnected'}</p>
12
- * <button onClick={disconnect}>Disconnect</button>
13
- * <button onClick={reconnect}>Reconnect</button>
14
- * {messages.map((m, i) => <div key={i}>{m}</div>)}
15
- * </div>
16
- * );
17
- * };
6
+ const App = () => {
7
+ const { connected, messages, disconnect, reconnect } = useSSE('http://localhost:3000/sse');
8
+
9
+ return (
10
+ <div>
11
+ <p>Status: {connected ? '✅ Connected' : '❌ Disconnected'}</p>
12
+ <button onClick={disconnect}>Disconnect</button>
13
+ <button onClick={reconnect}>Reconnect</button>
14
+ {messages.map((m, i) => <div key={i}>{m}</div>)}
15
+ </div>
16
+ );
17
+ };
18
+
19
+ * It is recommended to use it in conjunction with usePageVisibility, because in HTTP mode,
20
+ * browsers allow a maximum of 6 connections; otherwise, other normal interfaces will be suspended and inaccessible.
21
+
22
+ import usePageVisibility from 'funda-ui/Utils/usePageVisibility';
23
+
24
+ const App = () => {
25
+ const { connected, messages, disconnect, reconnect } = useSSE('http://localhost:3000/sse');
26
+
27
+ // add new
28
+ usePageVisibility(
29
+ () => {
30
+ reconnect();
31
+ },
32
+ () => {
33
+ disconnect();
34
+ },
35
+ () => console.log("🎬 Page initialized while visible.")
36
+ );
37
+
38
+ return '';
39
+ };
40
+
18
41
  */
42
+
43
+
19
44
  import { useEffect, useRef, useState, useCallback } from 'react';
20
45
 
21
46
  const useSSE = (url: string | null | undefined, retryDelay: number = 3000) => {
package/package.json CHANGED
@@ -1 +1 @@
1
- {"author":"UIUX Lab","email":"uiuxlab@gmail.com","name":"funda-ui","version":"4.7.701","description":"React components using pure Bootstrap 5+ which does not contain any external style and script libraries.","repository":{"type":"git","url":"git+https://github.com/xizon/funda-ui.git"},"scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["bootstrap","react-bootstrap","react-components","components","components-react","react-bootstrap-components","react","funda-ui","fundaui","uikit","ui-kit","ui-components"],"bugs":{"url":"https://github.com/xizon/funda-ui/issues"},"homepage":"https://github.com/xizon/funda-ui#readme","main":"all.js","license":"MIT","dependencies":{"react":"^18.2.0","react-dom":"^18.2.0"},"types":"all.d.ts","publishConfig":{"directory":"lib"},"directories":{"lib":"lib"}}
1
+ {"author":"UIUX Lab","email":"uiuxlab@gmail.com","name":"funda-ui","version":"4.7.723","description":"React components using pure Bootstrap 5+ which does not contain any external style and script libraries.","repository":{"type":"git","url":"git+https://github.com/xizon/funda-ui.git"},"scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["bootstrap","react-bootstrap","react-components","components","components-react","react-bootstrap-components","react","funda-ui","fundaui","uikit","ui-kit","ui-components"],"bugs":{"url":"https://github.com/xizon/funda-ui/issues"},"homepage":"https://github.com/xizon/funda-ui#readme","main":"all.js","license":"MIT","dependencies":{"react":"^18.2.0","react-dom":"^18.2.0"},"types":"all.d.ts","publishConfig":{"directory":"lib"},"directories":{"lib":"lib"}}