@goodnight-dev/react-hooks 0.2.0 → 0.4.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 CHANGED
@@ -8,8 +8,8 @@ project, packaged the right way.
8
8
  [![docs](https://img.shields.io/badge/docs-online-blue)](https://goodnight-dev.github.io/react-hooks/)
9
9
 
10
10
  > **Status:** bootstrapping — the packaging, CI, docs, and release pipeline are
11
- > in place; the hook surface is still one hook and growing. As much a showcase
12
- > of hook-packaging best practices as a hooks library.
11
+ > in place; the hook surface is still small and growing. As much a showcase of
12
+ > hook-packaging best practices as a hooks library.
13
13
 
14
14
  ## Install
15
15
 
@@ -38,6 +38,8 @@ Both are fully typed, ESM-only, and tree-shakable.
38
38
  | ------------------------------- | ---------------------------------------------------------------------------------- |
39
39
  | `useTheme()` | Reads the OS `prefers-color-scheme`, live-updating on change. |
40
40
  | `useLocalStorage(key, initial)` | Reads and writes a JSON-serializable value in `localStorage`, syncing across tabs. |
41
+ | `useWindowSize()` | Reads the viewport `width` / `height`, live-updating on resize. |
42
+ | `useOnlineStatus()` | Reads whether the browser is online (`navigator.onLine`), live-updating. |
41
43
 
42
44
  ```tsx
43
45
  import { useLocalStorage, useTheme } from '@goodnight-dev/react-hooks';
@@ -65,8 +67,10 @@ function App() {
65
67
  }
66
68
  ```
67
69
 
68
- See [`src/use-theme.md`](./src/use-theme.md) and
69
- [`src/use-local-storage.md`](./src/use-local-storage.md) for the design
70
+ See [`src/use-theme.md`](./src/use-theme.md),
71
+ [`src/use-local-storage.md`](./src/use-local-storage.md),
72
+ [`src/use-window-size.md`](./src/use-window-size.md), and
73
+ [`src/use-online-status.md`](./src/use-online-status.md) for the design
70
74
  rationale behind each.
71
75
 
72
76
  ## Project goals
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
1
  import { useLocalStorage } from "./use-local-storage.mjs";
2
+ import { useOnlineStatus } from "./use-online-status.mjs";
2
3
  import { Theme, ThemePreference, useTheme } from "./use-theme.mjs";
3
- export { type Theme, type ThemePreference, useLocalStorage, useTheme };
4
+ import { WindowSize, useWindowSize } from "./use-window-size.mjs";
5
+ export { type Theme, type ThemePreference, type WindowSize, useLocalStorage, useOnlineStatus, useTheme, useWindowSize };
package/dist/index.mjs CHANGED
@@ -1,3 +1,5 @@
1
1
  import { useLocalStorage } from "./use-local-storage.mjs";
2
+ import { useOnlineStatus } from "./use-online-status.mjs";
2
3
  import { useTheme } from "./use-theme.mjs";
3
- export { useLocalStorage, useTheme };
4
+ import { useWindowSize } from "./use-window-size.mjs";
5
+ export { useLocalStorage, useOnlineStatus, useTheme, useWindowSize };
@@ -1,4 +1,3 @@
1
- //#region src/use-local-storage.d.ts
2
1
  type SetValue<T> = T | ((previous: T) => T);
3
2
  /**
4
3
  * Read and write a JSON-serializable value in `localStorage`, keyed by
@@ -28,6 +27,5 @@ type SetValue<T> = T | ((previous: T) => T);
28
27
  * setSettings((previous) => ({ ...previous, theme: 'dark' }));
29
28
  * ```
30
29
  */
31
- declare function useLocalStorage<T>(key: string, initialValue: T): [T, (value: SetValue<T>) => void];
32
- //#endregion
33
- export { useLocalStorage };
30
+ export declare function useLocalStorage<T>(key: string, initialValue: T): [T, (value: SetValue<T>) => void];
31
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useRef, useSyncExternalStore } from "react";
2
- //#region src/use-local-storage.ts
2
+ // src/use-local-storage.ts
3
3
  const SAME_TAB_EVENT = "goodnight-dev:use-local-storage";
4
4
  function readRaw(key) {
5
5
  try {
@@ -56,7 +56,7 @@ function useLocalStorage(key, initialValue) {
56
56
  return value;
57
57
  }, [key, initialValue]);
58
58
  const getServerSnapshot = useCallback(() => initialValue, [initialValue]);
59
- return [useSyncExternalStore(useCallback((onStoreChange) => {
59
+ const subscribe = useCallback((onStoreChange) => {
60
60
  const handleStorage = (event) => {
61
61
  if (event.key === null || event.key === key) onStoreChange();
62
62
  };
@@ -69,7 +69,8 @@ function useLocalStorage(key, initialValue) {
69
69
  window.removeEventListener("storage", handleStorage);
70
70
  window.removeEventListener(SAME_TAB_EVENT, handleSameTab);
71
71
  };
72
- }, [key]), getSnapshot, getServerSnapshot), useCallback((next) => {
72
+ }, [key]);
73
+ return [useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot), useCallback((next) => {
73
74
  const resolved = typeof next === "function" ? next(getSnapshot()) : next;
74
75
  try {
75
76
  window.localStorage.setItem(key, JSON.stringify(resolved));
@@ -80,5 +81,4 @@ function useLocalStorage(key, initialValue) {
80
81
  window.dispatchEvent(new CustomEvent(SAME_TAB_EVENT, { detail: key }));
81
82
  }, [key, getSnapshot])];
82
83
  }
83
- //#endregion
84
84
  export { useLocalStorage };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Read whether the browser currently has a network connection
3
+ * (`navigator.onLine`), staying in sync as it connects and disconnects.
4
+ *
5
+ * Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
6
+ * and does not tear. The value is `undefined` on the server — and on the
7
+ * client until the first `navigator.onLine` read commits — the same "unknown
8
+ * until proven otherwise" contract `useTheme` uses for `theme`: there is no
9
+ * honest value to render for a client whose connectivity the server cannot
10
+ * know, so render a neutral state for `undefined` rather than assuming online.
11
+ *
12
+ * `navigator.onLine` is necessary but not sufficient: `false` means the
13
+ * browser knows it is offline, but `true` only means it has *a* network
14
+ * interface, not that the internet is actually reachable — a captive portal
15
+ * or a dead upstream link still reads `true`. Treat it as "definitely
16
+ * offline" vs. "probably online," not a reachability check; confirm with a
17
+ * real request if you need certainty.
18
+ *
19
+ * @returns `true` when the browser reports a connection, `false` when it
20
+ * reports none, and `undefined` until the client has read the real value
21
+ * (always the case during SSR).
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * const isOnline = useOnlineStatus();
26
+ * if (isOnline === undefined) return null; // don't assume online before we know
27
+ * return isOnline ? <App /> : <OfflineBanner />;
28
+ * ```
29
+ */
30
+ export declare function useOnlineStatus(): boolean | undefined;
@@ -0,0 +1,45 @@
1
+ import { useSyncExternalStore } from "react";
2
+ // src/use-online-status.ts
3
+ const subscribe = (onStoreChange) => {
4
+ window.addEventListener("online", onStoreChange);
5
+ window.addEventListener("offline", onStoreChange);
6
+ return () => {
7
+ window.removeEventListener("online", onStoreChange);
8
+ window.removeEventListener("offline", onStoreChange);
9
+ };
10
+ };
11
+ const getSnapshot = () => navigator.onLine;
12
+ const getServerSnapshot = () => void 0;
13
+ /**
14
+ * Read whether the browser currently has a network connection
15
+ * (`navigator.onLine`), staying in sync as it connects and disconnects.
16
+ *
17
+ * Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
18
+ * and does not tear. The value is `undefined` on the server — and on the
19
+ * client until the first `navigator.onLine` read commits — the same "unknown
20
+ * until proven otherwise" contract `useTheme` uses for `theme`: there is no
21
+ * honest value to render for a client whose connectivity the server cannot
22
+ * know, so render a neutral state for `undefined` rather than assuming online.
23
+ *
24
+ * `navigator.onLine` is necessary but not sufficient: `false` means the
25
+ * browser knows it is offline, but `true` only means it has *a* network
26
+ * interface, not that the internet is actually reachable — a captive portal
27
+ * or a dead upstream link still reads `true`. Treat it as "definitely
28
+ * offline" vs. "probably online," not a reachability check; confirm with a
29
+ * real request if you need certainty.
30
+ *
31
+ * @returns `true` when the browser reports a connection, `false` when it
32
+ * reports none, and `undefined` until the client has read the real value
33
+ * (always the case during SSR).
34
+ *
35
+ * @example
36
+ * ```tsx
37
+ * const isOnline = useOnlineStatus();
38
+ * if (isOnline === undefined) return null; // don't assume online before we know
39
+ * return isOnline ? <App /> : <OfflineBanner />;
40
+ * ```
41
+ */
42
+ function useOnlineStatus() {
43
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
44
+ }
45
+ export { useOnlineStatus };
@@ -1,13 +1,12 @@
1
- //#region src/use-theme.d.ts
2
- type Theme = 'dark' | 'light';
3
- interface ThemePreference {
4
- isDarkMode: boolean;
5
- isLightMode: boolean;
1
+ export type Theme = 'dark' | 'light';
2
+ export interface ThemePreference {
3
+ readonly isDarkMode: boolean;
4
+ readonly isLightMode: boolean;
6
5
  /**
7
6
  * `'dark'` or `'light'`, or `undefined` before the client has reported its
8
7
  * actual preference (always the case during SSR).
9
8
  */
10
- theme: Theme | undefined;
9
+ readonly theme: Theme | undefined;
11
10
  }
12
11
  /**
13
12
  * Read the user's OS-level color scheme preference
@@ -31,6 +30,4 @@ interface ThemePreference {
31
30
  * return <div data-theme={theme}>{isDarkMode ? '🌙' : '☀️'}</div>;
32
31
  * ```
33
32
  */
34
- declare function useTheme(): ThemePreference;
35
- //#endregion
36
- export { Theme, ThemePreference, useTheme };
33
+ export declare function useTheme(): Readonly<ThemePreference>;
@@ -1,5 +1,5 @@
1
1
  import { useMemo, useSyncExternalStore } from "react";
2
- //#region src/use-theme.ts
2
+ // src/use-theme.ts
3
3
  const QUERY = "(prefers-color-scheme: dark)";
4
4
  const getSnapshot = () => window.matchMedia(QUERY).matches ? "dark" : "light";
5
5
  const getServerSnapshot = () => void 0;
@@ -40,5 +40,4 @@ function useTheme() {
40
40
  isLightMode: theme === "light"
41
41
  }), [theme]);
42
42
  }
43
- //#endregion
44
43
  export { useTheme };
@@ -0,0 +1,34 @@
1
+ export interface WindowSize {
2
+ /**
3
+ * The viewport width in CSS pixels, or `undefined` before the client has
4
+ * reported its actual size (always the case during SSR).
5
+ */
6
+ readonly width: number | undefined;
7
+ /**
8
+ * The viewport height in CSS pixels, or `undefined` before the client has
9
+ * reported its actual size (always the case during SSR).
10
+ */
11
+ readonly height: number | undefined;
12
+ }
13
+ /**
14
+ * Read the browser viewport's `width` and `height` (in CSS pixels), staying
15
+ * in sync as the window is resized.
16
+ *
17
+ * Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
18
+ * and does not tear. `width` and `height` are `undefined` on the server —
19
+ * and on the client until the first resize-store read commits — the same
20
+ * "unknown until proven otherwise" contract `useTheme` uses for `theme`,
21
+ * applied to both fields here since neither has a safe default to guess.
22
+ *
23
+ * @returns `width` and `height` in CSS pixels, each `undefined` until the
24
+ * client has reported the real viewport size. The returned object is
25
+ * memoized, so its identity only changes when a dimension does.
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * const { width, height } = useWindowSize();
30
+ * if (width === undefined || height === undefined) return null; // avoid laying out against an unknown size
31
+ * return <div>{width} × {height}</div>;
32
+ * ```
33
+ */
34
+ export declare function useWindowSize(): Readonly<WindowSize>;
@@ -0,0 +1,51 @@
1
+ import { useCallback, useRef, useSyncExternalStore } from "react";
2
+ // src/use-window-size.ts
3
+ const SERVER_SNAPSHOT = {
4
+ width: void 0,
5
+ height: void 0
6
+ };
7
+ const getServerSnapshot = () => SERVER_SNAPSHOT;
8
+ const subscribe = (onStoreChange) => {
9
+ window.addEventListener("resize", onStoreChange);
10
+ return () => {
11
+ window.removeEventListener("resize", onStoreChange);
12
+ };
13
+ };
14
+ /**
15
+ * Read the browser viewport's `width` and `height` (in CSS pixels), staying
16
+ * in sync as the window is resized.
17
+ *
18
+ * Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
19
+ * and does not tear. `width` and `height` are `undefined` on the server —
20
+ * and on the client until the first resize-store read commits — the same
21
+ * "unknown until proven otherwise" contract `useTheme` uses for `theme`,
22
+ * applied to both fields here since neither has a safe default to guess.
23
+ *
24
+ * @returns `width` and `height` in CSS pixels, each `undefined` until the
25
+ * client has reported the real viewport size. The returned object is
26
+ * memoized, so its identity only changes when a dimension does.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * const { width, height } = useWindowSize();
31
+ * if (width === undefined || height === undefined) return null; // avoid laying out against an unknown size
32
+ * return <div>{width} × {height}</div>;
33
+ * ```
34
+ */
35
+ function useWindowSize() {
36
+ const cacheRef = useRef(null);
37
+ const getSnapshot = useCallback(() => {
38
+ const width = window.innerWidth;
39
+ const height = window.innerHeight;
40
+ const cache = cacheRef.current;
41
+ if (cache !== null && cache.width === width && cache.height === height) return cache;
42
+ const next = {
43
+ width,
44
+ height
45
+ };
46
+ cacheRef.current = next;
47
+ return next;
48
+ }, []);
49
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
50
+ }
51
+ export { useWindowSize };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodnight-dev/react-hooks",
3
- "version": "0.2.0",
3
+ "version": "0.4.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",
@@ -31,10 +31,18 @@
31
31
  "types": "./dist/use-local-storage.d.mts",
32
32
  "import": "./dist/use-local-storage.mjs"
33
33
  },
34
+ "./useOnlineStatus": {
35
+ "types": "./dist/use-online-status.d.mts",
36
+ "import": "./dist/use-online-status.mjs"
37
+ },
34
38
  "./useTheme": {
35
39
  "types": "./dist/use-theme.d.mts",
36
40
  "import": "./dist/use-theme.mjs"
37
41
  },
42
+ "./useWindowSize": {
43
+ "types": "./dist/use-window-size.d.mts",
44
+ "import": "./dist/use-window-size.mjs"
45
+ },
38
46
  "./package.json": "./package.json"
39
47
  },
40
48
  "files": [
@@ -51,7 +59,7 @@
51
59
  },
52
60
  "devDependencies": {
53
61
  "@arethetypeswrong/cli": "^0.18.4",
54
- "@changesets/cli": "^2.31.0",
62
+ "@changesets/cli": "^3.0.0",
55
63
  "@commitlint/cli": "^21.2.0",
56
64
  "@commitlint/config-conventional": "^21.2.0",
57
65
  "@eslint/js": "^10.0.1",
@@ -63,16 +71,17 @@
63
71
  "esbuild": "^0.28.1",
64
72
  "eslint": "^10.6.0",
65
73
  "eslint-config-prettier": "^10.1.8",
74
+ "eslint-plugin-functional": "^10.0.0",
66
75
  "eslint-plugin-perfectionist": "^5.9.1",
67
76
  "eslint-plugin-react-hooks": "^7.1.0",
68
77
  "globals": "^17.7.0",
69
- "jsdom": "^29.1.1",
78
+ "jsdom": "^31.0.0",
70
79
  "lefthook": "^2.1.9",
71
80
  "prettier": "^3.9.4",
72
81
  "publint": "^0.3.21",
73
82
  "react": "^19.2.0",
74
83
  "react-dom": "^19.2.0",
75
- "tsdown": "^0.22.3",
84
+ "tsdown": "^0.23.0",
76
85
  "typedoc": "^0.28.19",
77
86
  "typescript": "^6.0.3",
78
87
  "typescript-eslint": "^8.62.1",