@goodnight-dev/react-hooks 0.7.0 → 0.8.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 +4 -3
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/use-hash.d.mts +28 -0
- package/dist/use-hash.mjs +41 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -43,6 +43,7 @@ Both are fully typed, ESM-only, and tree-shakable.
|
|
|
43
43
|
| `useWindowScroll()` | Reads the window scroll offset (`x` / `y`), live-updating on scroll. |
|
|
44
44
|
| `useOnlineStatus()` | Reads whether the browser is online (`navigator.onLine`), live-updating. |
|
|
45
45
|
| `usePageVisibility()` | Reads whether the page is visible (Page Visibility API), live-updating on tab switch. |
|
|
46
|
+
| `useHash()` | Reads the URL fragment (`location.hash`), live-updating on `hashchange`. |
|
|
46
47
|
|
|
47
48
|
```tsx
|
|
48
49
|
import { useLocalStorage, useTheme } from '@goodnight-dev/react-hooks';
|
|
@@ -75,9 +76,9 @@ See [`src/use-theme.md`](./src/use-theme.md),
|
|
|
75
76
|
[`src/use-local-storage.md`](./src/use-local-storage.md),
|
|
76
77
|
[`src/use-window-size.md`](./src/use-window-size.md),
|
|
77
78
|
[`src/use-window-scroll.md`](./src/use-window-scroll.md),
|
|
78
|
-
[`src/use-online-status.md`](./src/use-online-status.md),
|
|
79
|
-
[`src/use-page-visibility.md`](./src/use-page-visibility.md)
|
|
80
|
-
rationale behind each.
|
|
79
|
+
[`src/use-online-status.md`](./src/use-online-status.md),
|
|
80
|
+
[`src/use-page-visibility.md`](./src/use-page-visibility.md), and
|
|
81
|
+
[`src/use-hash.md`](./src/use-hash.md) for the design rationale behind each.
|
|
81
82
|
|
|
82
83
|
## Project goals
|
|
83
84
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useHash } from "./use-hash.mjs";
|
|
1
2
|
import { useLocalStorage } from "./use-local-storage.mjs";
|
|
2
3
|
import { useMediaQuery } from "./use-media-query.mjs";
|
|
3
4
|
import { useOnlineStatus } from "./use-online-status.mjs";
|
|
@@ -5,4 +6,4 @@ import { usePageVisibility } from "./use-page-visibility.mjs";
|
|
|
5
6
|
import { Theme, ThemePreference, useTheme } from "./use-theme.mjs";
|
|
6
7
|
import { WindowScroll, useWindowScroll } from "./use-window-scroll.mjs";
|
|
7
8
|
import { WindowSize, useWindowSize } from "./use-window-size.mjs";
|
|
8
|
-
export { type Theme, type ThemePreference, type WindowScroll, type WindowSize, useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useTheme, useWindowScroll, useWindowSize };
|
|
9
|
+
export { type Theme, type ThemePreference, type WindowScroll, type WindowSize, useHash, useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useTheme, useWindowScroll, useWindowSize };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useHash } from "./use-hash.mjs";
|
|
1
2
|
import { useLocalStorage } from "./use-local-storage.mjs";
|
|
2
3
|
import { useMediaQuery } from "./use-media-query.mjs";
|
|
3
4
|
import { useOnlineStatus } from "./use-online-status.mjs";
|
|
@@ -5,4 +6,4 @@ import { usePageVisibility } from "./use-page-visibility.mjs";
|
|
|
5
6
|
import { useTheme } from "./use-theme.mjs";
|
|
6
7
|
import { useWindowScroll } from "./use-window-scroll.mjs";
|
|
7
8
|
import { useWindowSize } from "./use-window-size.mjs";
|
|
8
|
-
export { useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useTheme, useWindowScroll, useWindowSize };
|
|
9
|
+
export { useHash, useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useTheme, useWindowScroll, useWindowSize };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read the URL's fragment identifier (`window.location.hash`), staying in sync
|
|
3
|
+
* as it changes via the `hashchange` event.
|
|
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 client
|
|
7
|
+
* until the first read commits — the same "unknown until proven otherwise"
|
|
8
|
+
* contract the other hooks use. For the fragment this is not just caution: it is
|
|
9
|
+
* never transmitted to the server (the browser drops it before the request), so
|
|
10
|
+
* there is no server-side value to have.
|
|
11
|
+
*
|
|
12
|
+
* Returned raw from `location.hash`: the leading `#` is included (`'#section'`),
|
|
13
|
+
* and it is the empty string `''` when the URL has no fragment — distinct from
|
|
14
|
+
* `undefined`, which means "not known yet." Read-only by design; set the
|
|
15
|
+
* fragment with a native `<a href="#…">` or `location.hash = …` (see
|
|
16
|
+
* use-hash.md for why there is no setter).
|
|
17
|
+
*
|
|
18
|
+
* @returns the fragment including its leading `#`, `''` when there is none, or
|
|
19
|
+
* `undefined` until the client has read the real value (always during SSR).
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* const hash = useHash(); // '#pricing', '' (no fragment), or undefined (SSR)
|
|
24
|
+
* const section = hash?.slice(1) || 'home'; // '#pricing' -> 'pricing'
|
|
25
|
+
* return <main data-section={section} />;
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function useHash(): string | undefined;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
// src/use-hash.ts
|
|
3
|
+
const subscribe = (onStoreChange) => {
|
|
4
|
+
window.addEventListener("hashchange", onStoreChange);
|
|
5
|
+
return () => {
|
|
6
|
+
window.removeEventListener("hashchange", onStoreChange);
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
const getSnapshot = () => window.location.hash;
|
|
10
|
+
const getServerSnapshot = () => void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Read the URL's fragment identifier (`window.location.hash`), staying in sync
|
|
13
|
+
* as it changes via the `hashchange` event.
|
|
14
|
+
*
|
|
15
|
+
* Backed by `useSyncExternalStore`, so it is safe under concurrent rendering
|
|
16
|
+
* and does not tear. The value is `undefined` on the server — and on the client
|
|
17
|
+
* until the first read commits — the same "unknown until proven otherwise"
|
|
18
|
+
* contract the other hooks use. For the fragment this is not just caution: it is
|
|
19
|
+
* never transmitted to the server (the browser drops it before the request), so
|
|
20
|
+
* there is no server-side value to have.
|
|
21
|
+
*
|
|
22
|
+
* Returned raw from `location.hash`: the leading `#` is included (`'#section'`),
|
|
23
|
+
* and it is the empty string `''` when the URL has no fragment — distinct from
|
|
24
|
+
* `undefined`, which means "not known yet." Read-only by design; set the
|
|
25
|
+
* fragment with a native `<a href="#…">` or `location.hash = …` (see
|
|
26
|
+
* use-hash.md for why there is no setter).
|
|
27
|
+
*
|
|
28
|
+
* @returns the fragment including its leading `#`, `''` when there is none, or
|
|
29
|
+
* `undefined` until the client has read the real value (always during SSR).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const hash = useHash(); // '#pricing', '' (no fragment), or undefined (SSR)
|
|
34
|
+
* const section = hash?.slice(1) || 'home'; // '#pricing' -> 'pricing'
|
|
35
|
+
* return <main data-section={section} />;
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function useHash() {
|
|
39
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
40
|
+
}
|
|
41
|
+
export { useHash };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodnight-dev/react-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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",
|
|
@@ -27,6 +27,10 @@
|
|
|
27
27
|
"types": "./dist/index.d.mts",
|
|
28
28
|
"import": "./dist/index.mjs"
|
|
29
29
|
},
|
|
30
|
+
"./useHash": {
|
|
31
|
+
"types": "./dist/use-hash.d.mts",
|
|
32
|
+
"import": "./dist/use-hash.mjs"
|
|
33
|
+
},
|
|
30
34
|
"./useLocalStorage": {
|
|
31
35
|
"types": "./dist/use-local-storage.d.mts",
|
|
32
36
|
"import": "./dist/use-local-storage.mjs"
|