@goodnight-dev/react-hooks 0.2.0 → 0.3.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
@@ -38,6 +38,7 @@ 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. |
41
42
 
42
43
  ```tsx
43
44
  import { useLocalStorage, useTheme } from '@goodnight-dev/react-hooks';
@@ -65,9 +66,10 @@ function App() {
65
66
  }
66
67
  ```
67
68
 
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
- rationale behind each.
69
+ See [`src/use-theme.md`](./src/use-theme.md),
70
+ [`src/use-local-storage.md`](./src/use-local-storage.md), and
71
+ [`src/use-window-size.md`](./src/use-window-size.md) for the design rationale
72
+ behind each.
71
73
 
72
74
  ## Project goals
73
75
 
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { useLocalStorage } from "./use-local-storage.mjs";
2
2
  import { Theme, ThemePreference, useTheme } from "./use-theme.mjs";
3
- export { type Theme, type ThemePreference, useLocalStorage, useTheme };
3
+ import { WindowSize, useWindowSize } from "./use-window-size.mjs";
4
+ export { type Theme, type ThemePreference, type WindowSize, useLocalStorage, useTheme, useWindowSize };
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  import { useLocalStorage } from "./use-local-storage.mjs";
2
2
  import { useTheme } from "./use-theme.mjs";
3
- export { useLocalStorage, useTheme };
3
+ import { useWindowSize } from "./use-window-size.mjs";
4
+ export { useLocalStorage, useTheme, useWindowSize };
@@ -1,13 +1,13 @@
1
1
  //#region src/use-theme.d.ts
2
2
  type Theme = 'dark' | 'light';
3
3
  interface ThemePreference {
4
- isDarkMode: boolean;
5
- isLightMode: boolean;
4
+ readonly isDarkMode: boolean;
5
+ readonly isLightMode: boolean;
6
6
  /**
7
7
  * `'dark'` or `'light'`, or `undefined` before the client has reported its
8
8
  * actual preference (always the case during SSR).
9
9
  */
10
- theme: Theme | undefined;
10
+ readonly theme: Theme | undefined;
11
11
  }
12
12
  /**
13
13
  * Read the user's OS-level color scheme preference
@@ -31,6 +31,6 @@ interface ThemePreference {
31
31
  * return <div data-theme={theme}>{isDarkMode ? '🌙' : '☀️'}</div>;
32
32
  * ```
33
33
  */
34
- declare function useTheme(): ThemePreference;
34
+ declare function useTheme(): Readonly<ThemePreference>;
35
35
  //#endregion
36
36
  export { Theme, ThemePreference, useTheme };
@@ -0,0 +1,37 @@
1
+ //#region src/use-window-size.d.ts
2
+ interface WindowSize {
3
+ /**
4
+ * The viewport width in CSS pixels, or `undefined` before the client has
5
+ * reported its actual size (always the case during SSR).
6
+ */
7
+ readonly width: number | undefined;
8
+ /**
9
+ * The viewport height in CSS pixels, or `undefined` before the client has
10
+ * reported its actual size (always the case during SSR).
11
+ */
12
+ readonly height: number | undefined;
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
+ declare function useWindowSize(): Readonly<WindowSize>;
36
+ //#endregion
37
+ export { WindowSize, useWindowSize };
@@ -0,0 +1,52 @@
1
+ import { useCallback, useRef, useSyncExternalStore } from "react";
2
+ //#region 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
+ //#endregion
52
+ 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.3.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",
@@ -35,6 +35,10 @@
35
35
  "types": "./dist/use-theme.d.mts",
36
36
  "import": "./dist/use-theme.mjs"
37
37
  },
38
+ "./useWindowSize": {
39
+ "types": "./dist/use-window-size.d.mts",
40
+ "import": "./dist/use-window-size.mjs"
41
+ },
38
42
  "./package.json": "./package.json"
39
43
  },
40
44
  "files": [
@@ -63,6 +67,7 @@
63
67
  "esbuild": "^0.28.1",
64
68
  "eslint": "^10.6.0",
65
69
  "eslint-config-prettier": "^10.1.8",
70
+ "eslint-plugin-functional": "^10.0.0",
66
71
  "eslint-plugin-perfectionist": "^5.9.1",
67
72
  "eslint-plugin-react-hooks": "^7.1.0",
68
73
  "globals": "^17.7.0",