@goodnight-dev/react-hooks 0.5.0 → 0.7.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/README.md +6 -2
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +3 -1
- package/dist/use-page-visibility.d.mts +35 -0
- package/dist/use-page-visibility.mjs +48 -0
- package/dist/use-window-scroll.d.mts +42 -0
- package/dist/use-window-scroll.mjs +57 -0
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -40,7 +40,9 @@ Both are fully typed, ESM-only, and tree-shakable.
|
|
|
40
40
|
| `useMediaQuery(query)` | Reads any CSS media query (`matchMedia`), live-updating — the primitive behind `useTheme`. |
|
|
41
41
|
| `useLocalStorage(key, initial)` | Reads and writes a JSON-serializable value in `localStorage`, syncing across tabs. |
|
|
42
42
|
| `useWindowSize()` | Reads the viewport `width` / `height`, live-updating on resize. |
|
|
43
|
+
| `useWindowScroll()` | Reads the window scroll offset (`x` / `y`), live-updating on scroll. |
|
|
43
44
|
| `useOnlineStatus()` | Reads whether the browser is online (`navigator.onLine`), live-updating. |
|
|
45
|
+
| `usePageVisibility()` | Reads whether the page is visible (Page Visibility API), live-updating on tab switch. |
|
|
44
46
|
|
|
45
47
|
```tsx
|
|
46
48
|
import { useLocalStorage, useTheme } from '@goodnight-dev/react-hooks';
|
|
@@ -71,8 +73,10 @@ function App() {
|
|
|
71
73
|
See [`src/use-theme.md`](./src/use-theme.md),
|
|
72
74
|
[`src/use-media-query.md`](./src/use-media-query.md),
|
|
73
75
|
[`src/use-local-storage.md`](./src/use-local-storage.md),
|
|
74
|
-
[`src/use-window-size.md`](./src/use-window-size.md),
|
|
75
|
-
[`src/use-
|
|
76
|
+
[`src/use-window-size.md`](./src/use-window-size.md),
|
|
77
|
+
[`src/use-window-scroll.md`](./src/use-window-scroll.md),
|
|
78
|
+
[`src/use-online-status.md`](./src/use-online-status.md), and
|
|
79
|
+
[`src/use-page-visibility.md`](./src/use-page-visibility.md) for the design
|
|
76
80
|
rationale behind each.
|
|
77
81
|
|
|
78
82
|
## Project goals
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { useLocalStorage } from "./use-local-storage.mjs";
|
|
2
2
|
import { useMediaQuery } from "./use-media-query.mjs";
|
|
3
3
|
import { useOnlineStatus } from "./use-online-status.mjs";
|
|
4
|
+
import { usePageVisibility } from "./use-page-visibility.mjs";
|
|
4
5
|
import { Theme, ThemePreference, useTheme } from "./use-theme.mjs";
|
|
6
|
+
import { WindowScroll, useWindowScroll } from "./use-window-scroll.mjs";
|
|
5
7
|
import { WindowSize, useWindowSize } from "./use-window-size.mjs";
|
|
6
|
-
export { type Theme, type ThemePreference, type WindowSize, useLocalStorage, useMediaQuery, useOnlineStatus, useTheme, useWindowSize };
|
|
8
|
+
export { type Theme, type ThemePreference, type WindowScroll, type WindowSize, useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useTheme, useWindowScroll, useWindowSize };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { useLocalStorage } from "./use-local-storage.mjs";
|
|
2
2
|
import { useMediaQuery } from "./use-media-query.mjs";
|
|
3
3
|
import { useOnlineStatus } from "./use-online-status.mjs";
|
|
4
|
+
import { usePageVisibility } from "./use-page-visibility.mjs";
|
|
4
5
|
import { useTheme } from "./use-theme.mjs";
|
|
6
|
+
import { useWindowScroll } from "./use-window-scroll.mjs";
|
|
5
7
|
import { useWindowSize } from "./use-window-size.mjs";
|
|
6
|
-
export { useLocalStorage, useMediaQuery, useOnlineStatus, useTheme, useWindowSize };
|
|
8
|
+
export { useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useTheme, useWindowScroll, useWindowSize };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read whether the page is currently visible — its tab foregrounded and its
|
|
3
|
+
* window not minimized — staying in sync via the Page Visibility API's
|
|
4
|
+
* `visibilitychange` event.
|
|
5
|
+
*
|
|
6
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
7
|
+
* and does not tear. The value is `undefined` on the server — and on the client
|
|
8
|
+
* until the first read commits — the same "unknown until proven otherwise"
|
|
9
|
+
* contract `useOnlineStatus` and `useTheme` use: there is no honest value to
|
|
10
|
+
* render for a tab whose focus the server cannot know, so treat `undefined` as
|
|
11
|
+
* a neutral "not yet known" rather than assuming visible.
|
|
12
|
+
*
|
|
13
|
+
* `visibilityState` is the browser's own coarse signal: it reports foreground
|
|
14
|
+
* vs. background/minimized, *not* whether the user is actually looking, whether
|
|
15
|
+
* the window is occluded by another, or whether a visible tab holds input
|
|
16
|
+
* focus. Use it to pause work that is wasteful off-screen — polling, video,
|
|
17
|
+
* animation loops — and resume it on return; reach for the `focus` / `blur`
|
|
18
|
+
* events if you need true input focus instead.
|
|
19
|
+
*
|
|
20
|
+
* @returns `true` when the page is visible, `false` when it is hidden
|
|
21
|
+
* (backgrounded tab or minimized window), and `undefined` until the client has
|
|
22
|
+
* read the real value (always the case during SSR).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* const isVisible = usePageVisibility();
|
|
27
|
+
*
|
|
28
|
+
* useEffect(() => {
|
|
29
|
+
* if (isVisible === undefined) return; // don't start or stop before we know
|
|
30
|
+
* if (isVisible) resumePolling();
|
|
31
|
+
* else pausePolling();
|
|
32
|
+
* }, [isVisible]);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function usePageVisibility(): boolean | undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
// src/use-page-visibility.ts
|
|
3
|
+
const subscribe = (onStoreChange) => {
|
|
4
|
+
document.addEventListener("visibilitychange", onStoreChange);
|
|
5
|
+
return () => {
|
|
6
|
+
document.removeEventListener("visibilitychange", onStoreChange);
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
const getSnapshot = () => document.visibilityState === "visible";
|
|
10
|
+
const getServerSnapshot = () => void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Read whether the page is currently visible — its tab foregrounded and its
|
|
13
|
+
* window not minimized — staying in sync via the Page Visibility API's
|
|
14
|
+
* `visibilitychange` event.
|
|
15
|
+
*
|
|
16
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
17
|
+
* and does not tear. The value is `undefined` on the server — and on the client
|
|
18
|
+
* until the first read commits — the same "unknown until proven otherwise"
|
|
19
|
+
* contract `useOnlineStatus` and `useTheme` use: there is no honest value to
|
|
20
|
+
* render for a tab whose focus the server cannot know, so treat `undefined` as
|
|
21
|
+
* a neutral "not yet known" rather than assuming visible.
|
|
22
|
+
*
|
|
23
|
+
* `visibilityState` is the browser's own coarse signal: it reports foreground
|
|
24
|
+
* vs. background/minimized, *not* whether the user is actually looking, whether
|
|
25
|
+
* the window is occluded by another, or whether a visible tab holds input
|
|
26
|
+
* focus. Use it to pause work that is wasteful off-screen — polling, video,
|
|
27
|
+
* animation loops — and resume it on return; reach for the `focus` / `blur`
|
|
28
|
+
* events if you need true input focus instead.
|
|
29
|
+
*
|
|
30
|
+
* @returns `true` when the page is visible, `false` when it is hidden
|
|
31
|
+
* (backgrounded tab or minimized window), and `undefined` until the client has
|
|
32
|
+
* read the real value (always the case during SSR).
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* const isVisible = usePageVisibility();
|
|
37
|
+
*
|
|
38
|
+
* useEffect(() => {
|
|
39
|
+
* if (isVisible === undefined) return; // don't start or stop before we know
|
|
40
|
+
* if (isVisible) resumePolling();
|
|
41
|
+
* else pausePolling();
|
|
42
|
+
* }, [isVisible]);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function usePageVisibility() {
|
|
46
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
47
|
+
}
|
|
48
|
+
export { usePageVisibility };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export interface WindowScroll {
|
|
2
|
+
/**
|
|
3
|
+
* The horizontal scroll offset in CSS pixels (`window.scrollX`), or
|
|
4
|
+
* `undefined` before the client has reported its real position (always the
|
|
5
|
+
* case during SSR).
|
|
6
|
+
*/
|
|
7
|
+
readonly x: number | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* The vertical scroll offset in CSS pixels (`window.scrollY`), or `undefined`
|
|
10
|
+
* before the client has reported its real position (always the case during
|
|
11
|
+
* SSR).
|
|
12
|
+
*/
|
|
13
|
+
readonly y: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Read the window's scroll offset — `x` (`window.scrollX`) and `y`
|
|
17
|
+
* (`window.scrollY`), in CSS pixels — staying in sync as the document is
|
|
18
|
+
* scrolled.
|
|
19
|
+
*
|
|
20
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
21
|
+
* and does not tear. `x` and `y` are `undefined` on the server — and on the
|
|
22
|
+
* client until the first scroll-store read commits — the same "unknown until
|
|
23
|
+
* proven otherwise" contract `useTheme` uses for `theme`, applied to both
|
|
24
|
+
* fields here since neither has a safe default to guess.
|
|
25
|
+
*
|
|
26
|
+
* The scroll sibling of [`useWindowSize`](./use-window-size.ts): both read one
|
|
27
|
+
* global, compound piece of window state, so both memoize the returned object
|
|
28
|
+
* with a per-instance cache — its identity only changes when the position
|
|
29
|
+
* actually moves.
|
|
30
|
+
*
|
|
31
|
+
* @returns `x` and `y` scroll offsets in CSS pixels, each `undefined` until the
|
|
32
|
+
* client has reported the real position. The returned object is memoized, so
|
|
33
|
+
* its identity only changes when an offset does.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* const { y } = useWindowScroll();
|
|
38
|
+
* const showBackToTop = y !== undefined && y > 400;
|
|
39
|
+
* return showBackToTop ? <BackToTopButton /> : null;
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function useWindowScroll(): Readonly<WindowScroll>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useCallback, useRef, useSyncExternalStore } from "react";
|
|
2
|
+
// src/use-window-scroll.ts
|
|
3
|
+
const SERVER_SNAPSHOT = {
|
|
4
|
+
x: void 0,
|
|
5
|
+
y: void 0
|
|
6
|
+
};
|
|
7
|
+
const getServerSnapshot = () => SERVER_SNAPSHOT;
|
|
8
|
+
const subscribe = (onStoreChange) => {
|
|
9
|
+
window.addEventListener("scroll", onStoreChange, { passive: true });
|
|
10
|
+
return () => {
|
|
11
|
+
window.removeEventListener("scroll", onStoreChange);
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Read the window's scroll offset — `x` (`window.scrollX`) and `y`
|
|
16
|
+
* (`window.scrollY`), in CSS pixels — staying in sync as the document is
|
|
17
|
+
* scrolled.
|
|
18
|
+
*
|
|
19
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
20
|
+
* and does not tear. `x` and `y` are `undefined` on the server — and on the
|
|
21
|
+
* client until the first scroll-store read commits — the same "unknown until
|
|
22
|
+
* proven otherwise" contract `useTheme` uses for `theme`, applied to both
|
|
23
|
+
* fields here since neither has a safe default to guess.
|
|
24
|
+
*
|
|
25
|
+
* The scroll sibling of [`useWindowSize`](./use-window-size.ts): both read one
|
|
26
|
+
* global, compound piece of window state, so both memoize the returned object
|
|
27
|
+
* with a per-instance cache — its identity only changes when the position
|
|
28
|
+
* actually moves.
|
|
29
|
+
*
|
|
30
|
+
* @returns `x` and `y` scroll offsets in CSS pixels, each `undefined` until the
|
|
31
|
+
* client has reported the real position. The returned object is memoized, so
|
|
32
|
+
* its identity only changes when an offset does.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* const { y } = useWindowScroll();
|
|
37
|
+
* const showBackToTop = y !== undefined && y > 400;
|
|
38
|
+
* return showBackToTop ? <BackToTopButton /> : null;
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function useWindowScroll() {
|
|
42
|
+
const cacheRef = useRef(null);
|
|
43
|
+
const getSnapshot = useCallback(() => {
|
|
44
|
+
const x = window.scrollX;
|
|
45
|
+
const y = window.scrollY;
|
|
46
|
+
const cache = cacheRef.current;
|
|
47
|
+
if (cache !== null && cache.x === x && cache.y === y) return cache;
|
|
48
|
+
const next = {
|
|
49
|
+
x,
|
|
50
|
+
y
|
|
51
|
+
};
|
|
52
|
+
cacheRef.current = next;
|
|
53
|
+
return next;
|
|
54
|
+
}, []);
|
|
55
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
56
|
+
}
|
|
57
|
+
export { useWindowScroll };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodnight-dev/react-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Small, strict, tree-shakeable React hooks — the ones you end up writing in every project, packaged the right way.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -39,10 +39,18 @@
|
|
|
39
39
|
"types": "./dist/use-online-status.d.mts",
|
|
40
40
|
"import": "./dist/use-online-status.mjs"
|
|
41
41
|
},
|
|
42
|
+
"./usePageVisibility": {
|
|
43
|
+
"types": "./dist/use-page-visibility.d.mts",
|
|
44
|
+
"import": "./dist/use-page-visibility.mjs"
|
|
45
|
+
},
|
|
42
46
|
"./useTheme": {
|
|
43
47
|
"types": "./dist/use-theme.d.mts",
|
|
44
48
|
"import": "./dist/use-theme.mjs"
|
|
45
49
|
},
|
|
50
|
+
"./useWindowScroll": {
|
|
51
|
+
"types": "./dist/use-window-scroll.d.mts",
|
|
52
|
+
"import": "./dist/use-window-scroll.mjs"
|
|
53
|
+
},
|
|
46
54
|
"./useWindowSize": {
|
|
47
55
|
"types": "./dist/use-window-size.d.mts",
|
|
48
56
|
"import": "./dist/use-window-size.mjs"
|