@goodnight-dev/react-hooks 0.0.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/LICENSE +21 -0
- package/README.md +103 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/use-theme.d.mts +36 -0
- package/dist/use-theme.mjs +44 -0
- package/package.json +100 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ian Goodnight
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @goodnight-dev/react-hooks
|
|
2
|
+
|
|
3
|
+
Small, strict, modern React hooks — the ones you end up writing in every
|
|
4
|
+
project, packaged the right way.
|
|
5
|
+
|
|
6
|
+
[](https://www.npmjs.com/package/@goodnight-dev/react-hooks)
|
|
7
|
+
[](https://github.com/goodnight-dev/react-hooks/actions/workflows/ci.yml)
|
|
8
|
+
[](https://goodnight-dev.github.io/react-hooks/)
|
|
9
|
+
|
|
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.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pnpm add @goodnight-dev/react-hooks
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Requires React `>=19`.
|
|
21
|
+
|
|
22
|
+
## Two ways to import
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// 1. Barrel — everything from one entry point.
|
|
26
|
+
import { useTheme } from '@goodnight-dev/react-hooks';
|
|
27
|
+
|
|
28
|
+
// 2. Subpath — smallest possible import, no barrel involved at all.
|
|
29
|
+
import { useTheme } from '@goodnight-dev/react-hooks/useTheme';
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Both are fully typed, ESM-only, and tree-shakable.
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
| Hook | Description |
|
|
37
|
+
| ------------ | ------------------------------------------------------------- |
|
|
38
|
+
| `useTheme()` | Reads the OS `prefers-color-scheme`, live-updating on change. |
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import { useTheme } from '@goodnight-dev/react-hooks';
|
|
42
|
+
|
|
43
|
+
function App() {
|
|
44
|
+
const { theme, isDarkMode } = useTheme();
|
|
45
|
+
if (theme === undefined) return null; // avoid a flash of the wrong theme
|
|
46
|
+
return <div data-theme={theme}>{isDarkMode ? '🌙' : '☀️'}</div>;
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
See [`src/use-theme.md`](./src/use-theme.md) for the design rationale.
|
|
51
|
+
|
|
52
|
+
## Project goals
|
|
53
|
+
|
|
54
|
+
- 100% TypeScript with declaration files generated for consumers
|
|
55
|
+
- Modern ESM only, React `>=19` peer dependency
|
|
56
|
+
- SSR-safe and concurrent-safe: every hook is built on `useSyncExternalStore`
|
|
57
|
+
where it subscribes to something outside React
|
|
58
|
+
- Strictest practical linting + formatting (typescript-eslint
|
|
59
|
+
`strictTypeChecked`, `eslint-plugin-react-hooks`, Prettier)
|
|
60
|
+
- Importable as a whole or by tree-shakable subpath, per hook
|
|
61
|
+
- Comprehensive generated API docs
|
|
62
|
+
- **Zero third-party runtime dependencies**
|
|
63
|
+
|
|
64
|
+
## Principles
|
|
65
|
+
|
|
66
|
+
This repo is opinionated about _how_ the code is written. See
|
|
67
|
+
[CONTRIBUTING.md](./CONTRIBUTING.md) for the full rationale; in short:
|
|
68
|
+
|
|
69
|
+
- **SSR-safe and concurrent-safe by default.** External state (browser APIs,
|
|
70
|
+
storage, sockets) is read through `useSyncExternalStore`, never `useEffect` +
|
|
71
|
+
`useState`. A server render never guesses a client-only value.
|
|
72
|
+
- **Zero third-party runtime dependencies.** Installing this package adds
|
|
73
|
+
nothing to your `node_modules` but our own code — React itself is a peer
|
|
74
|
+
dependency.
|
|
75
|
+
- **Per-hook notes.** Hooks carry a sibling `*.md` (repo-only, academic)
|
|
76
|
+
explaining alternative implementations and why we chose ours — e.g.
|
|
77
|
+
[`use-theme.md`](./src/use-theme.md).
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
This is a [pnpm](https://pnpm.io) project. Requires Node `>=22`.
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
pnpm install # install dependencies
|
|
85
|
+
pnpm build # build the package (tsdown)
|
|
86
|
+
pnpm test # run the test suite (vitest)
|
|
87
|
+
pnpm typecheck # tsc --noEmit
|
|
88
|
+
pnpm lint # eslint
|
|
89
|
+
pnpm format # prettier --check
|
|
90
|
+
pnpm check # everything: format, lint, build, typecheck, test, exports
|
|
91
|
+
pnpm docs:build # generate the API docs site (TypeDoc)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Documentation
|
|
95
|
+
|
|
96
|
+
- **[API reference](https://goodnight-dev.github.io/react-hooks/)** — generated
|
|
97
|
+
from TSDoc comments.
|
|
98
|
+
- **[`docs/`](./docs/)** — recipes (adding a hook, cutting a release) and
|
|
99
|
+
architecture decision records.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
[MIT](./LICENSE) © Ian Goodnight
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/use-theme.d.ts
|
|
2
|
+
type Theme = 'dark' | 'light';
|
|
3
|
+
interface ThemePreference {
|
|
4
|
+
isDarkMode: boolean;
|
|
5
|
+
isLightMode: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* `'dark'` or `'light'`, or `undefined` before the client has reported its
|
|
8
|
+
* actual preference (always the case during SSR).
|
|
9
|
+
*/
|
|
10
|
+
theme: Theme | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Read the user's OS-level color scheme preference
|
|
14
|
+
* (`prefers-color-scheme`), and stay in sync as they change it.
|
|
15
|
+
*
|
|
16
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
17
|
+
* and does not tear. `theme` is `undefined` on the server — and on the client
|
|
18
|
+
* until the first `matchMedia` read commits — render a neutral state for that
|
|
19
|
+
* case rather than branching on `isDarkMode` / `isLightMode`, which are both
|
|
20
|
+
* `false` until `theme` resolves (there are only two real themes, so "both
|
|
21
|
+
* false" is unambiguous evidence that the preference isn't known yet).
|
|
22
|
+
*
|
|
23
|
+
* @returns `theme` plus `isDarkMode` / `isLightMode` convenience booleans.
|
|
24
|
+
* The returned object is memoized, so its identity only changes when `theme`
|
|
25
|
+
* does.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* const { theme, isDarkMode } = useTheme();
|
|
30
|
+
* if (theme === undefined) return null; // avoid a flash of the wrong theme
|
|
31
|
+
* return <div data-theme={theme}>{isDarkMode ? '🌙' : '☀️'}</div>;
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function useTheme(): ThemePreference;
|
|
35
|
+
//#endregion
|
|
36
|
+
export { Theme, ThemePreference, useTheme };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useMemo, useSyncExternalStore } from "react";
|
|
2
|
+
//#region src/use-theme.ts
|
|
3
|
+
const QUERY = "(prefers-color-scheme: dark)";
|
|
4
|
+
const getSnapshot = () => window.matchMedia(QUERY).matches ? "dark" : "light";
|
|
5
|
+
const getServerSnapshot = () => void 0;
|
|
6
|
+
const subscribe = (onStoreChange) => {
|
|
7
|
+
const mediaQueryList = window.matchMedia(QUERY);
|
|
8
|
+
mediaQueryList.addEventListener("change", onStoreChange);
|
|
9
|
+
return () => {
|
|
10
|
+
mediaQueryList.removeEventListener("change", onStoreChange);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Read the user's OS-level color scheme preference
|
|
15
|
+
* (`prefers-color-scheme`), and stay in sync as they change it.
|
|
16
|
+
*
|
|
17
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
18
|
+
* and does not tear. `theme` is `undefined` on the server — and on the client
|
|
19
|
+
* until the first `matchMedia` read commits — render a neutral state for that
|
|
20
|
+
* case rather than branching on `isDarkMode` / `isLightMode`, which are both
|
|
21
|
+
* `false` until `theme` resolves (there are only two real themes, so "both
|
|
22
|
+
* false" is unambiguous evidence that the preference isn't known yet).
|
|
23
|
+
*
|
|
24
|
+
* @returns `theme` plus `isDarkMode` / `isLightMode` convenience booleans.
|
|
25
|
+
* The returned object is memoized, so its identity only changes when `theme`
|
|
26
|
+
* does.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```tsx
|
|
30
|
+
* const { theme, isDarkMode } = useTheme();
|
|
31
|
+
* if (theme === undefined) return null; // avoid a flash of the wrong theme
|
|
32
|
+
* return <div data-theme={theme}>{isDarkMode ? '🌙' : '☀️'}</div>;
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function useTheme() {
|
|
36
|
+
const theme = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
37
|
+
return useMemo(() => ({
|
|
38
|
+
theme,
|
|
39
|
+
isDarkMode: theme === "dark",
|
|
40
|
+
isLightMode: theme === "light"
|
|
41
|
+
}), [theme]);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { useTheme };
|
package/package.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@goodnight-dev/react-hooks",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Small, strict, tree-shakeable React hooks — the ones you end up writing in every project, packaged the right way.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"react-hooks",
|
|
8
|
+
"hooks",
|
|
9
|
+
"typescript",
|
|
10
|
+
"esm",
|
|
11
|
+
"tree-shakable"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Ian Goodnight",
|
|
15
|
+
"homepage": "https://github.com/goodnight-dev/react-hooks#readme",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/goodnight-dev/react-hooks.git"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/goodnight-dev/react-hooks/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"import": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./useTheme": {
|
|
31
|
+
"types": "./dist/use-theme.d.mts",
|
|
32
|
+
"import": "./dist/use-theme.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"react": ">=19.0.0"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"packageManager": "pnpm@11.9.0+sha512.bd682d5d03fe525ef7c9fd6780c6884d1e756ac4c9c9fe00c538782824310dcf90e3ddc4f53835f06dfaebd5085e41855e0bcbb3b60de2ac5bbab89e5036f03b",
|
|
49
|
+
"scripts": {
|
|
50
|
+
"prepare": "lefthook install",
|
|
51
|
+
"build": "tsdown",
|
|
52
|
+
"docs:build": "typedoc",
|
|
53
|
+
"docs:watch": "typedoc --watch",
|
|
54
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"test:watch": "vitest",
|
|
57
|
+
"test:coverage": "vitest run --coverage",
|
|
58
|
+
"lint": "eslint .",
|
|
59
|
+
"lint:fix": "eslint . --fix",
|
|
60
|
+
"format": "prettier --check .",
|
|
61
|
+
"format:write": "prettier --write .",
|
|
62
|
+
"publint": "publint",
|
|
63
|
+
"attw": "attw --pack --profile esm-only .",
|
|
64
|
+
"check:exports": "pnpm run publint && pnpm run attw",
|
|
65
|
+
"check": "pnpm run format && pnpm run lint && pnpm run build && pnpm run typecheck && pnpm run test && pnpm run check:exports",
|
|
66
|
+
"changeset": "changeset",
|
|
67
|
+
"version-packages": "changeset version",
|
|
68
|
+
"release": "pnpm run build && node scripts/release-publish.mjs"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@arethetypeswrong/cli": "^0.18.4",
|
|
72
|
+
"@changesets/cli": "^2.31.0",
|
|
73
|
+
"@commitlint/cli": "^21.2.0",
|
|
74
|
+
"@commitlint/config-conventional": "^21.2.0",
|
|
75
|
+
"@eslint/js": "^10.0.1",
|
|
76
|
+
"@testing-library/react": "^16.3.2",
|
|
77
|
+
"@types/node": "^26.1.0",
|
|
78
|
+
"@types/react": "^19.2.7",
|
|
79
|
+
"@types/react-dom": "^19.2.3",
|
|
80
|
+
"@vitest/coverage-v8": "^4.1.9",
|
|
81
|
+
"esbuild": "^0.28.1",
|
|
82
|
+
"eslint": "^10.6.0",
|
|
83
|
+
"eslint-config-prettier": "^10.1.8",
|
|
84
|
+
"eslint-plugin-perfectionist": "^5.9.1",
|
|
85
|
+
"eslint-plugin-react-hooks": "^7.1.0",
|
|
86
|
+
"globals": "^17.7.0",
|
|
87
|
+
"jsdom": "^29.1.1",
|
|
88
|
+
"lefthook": "^2.1.9",
|
|
89
|
+
"prettier": "^3.9.4",
|
|
90
|
+
"publint": "^0.3.21",
|
|
91
|
+
"react": "^19.2.0",
|
|
92
|
+
"react-dom": "^19.2.0",
|
|
93
|
+
"tsdown": "^0.22.3",
|
|
94
|
+
"typedoc": "^0.28.19",
|
|
95
|
+
"typescript": "^6.0.3",
|
|
96
|
+
"typescript-eslint": "^8.62.1",
|
|
97
|
+
"unrun": "^0.3.1",
|
|
98
|
+
"vitest": "^4.1.9"
|
|
99
|
+
}
|
|
100
|
+
}
|