@foundrykit/hooks 2.0.6 → 2.0.8
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/CHANGELOG.md +12 -0
- package/README.md +24 -434
- package/dist/index.d.ts +150 -148
- package/dist/index.js +92 -89
- package/dist/index.js.map +1 -1
- package/package.json +13 -9
- package/src/hooks/use-advanced-search.ts +1 -1
- package/src/hooks/use-click-outside.ts +10 -3
- package/src/hooks/use-debounced-value.ts +1 -1
- package/src/hooks/use-form-field.ts +2 -2
- package/src/hooks/use-local-storage.ts +10 -10
- package/src/hooks/use-media-query.ts +12 -8
- package/src/index.ts +14 -13
- package/tsup.config.ts +3 -0
- package/.turbo/turbo-build.log +0 -17
- package/.turbo/turbo-type-check.log +0 -4
- package/eslint.config.js +0 -4
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foundrykit/hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
9
10
|
"publishConfig": {
|
|
10
11
|
"access": "public"
|
|
11
12
|
},
|
|
@@ -18,30 +19,33 @@
|
|
|
18
19
|
"./lib/*": "./src/lib/*.ts"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
|
-
"react": "^19.1.1",
|
|
22
|
-
"react-dom": "^19.1.1",
|
|
23
22
|
"react-hook-form": "^7.62.0"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
26
25
|
"@types/node": "^20.19.9",
|
|
27
26
|
"@types/react": "^19.1.9",
|
|
28
27
|
"@types/react-dom": "^19.1.7",
|
|
29
|
-
"
|
|
28
|
+
"react": "^19.1.1",
|
|
29
|
+
"react-dom": "^19.1.1",
|
|
30
30
|
"tsup": "^8.0.0",
|
|
31
31
|
"typescript": "^5.9.2",
|
|
32
|
-
"@foundrykit/
|
|
33
|
-
"@foundrykit/
|
|
34
|
-
"@foundrykit/typescript-config": "1.0.5"
|
|
32
|
+
"@foundrykit/types": "3.0.0",
|
|
33
|
+
"@foundrykit/typescript-config": "1.0.6"
|
|
35
34
|
},
|
|
36
35
|
"peerDependencies": {
|
|
37
36
|
"react": "^19.1.1",
|
|
38
37
|
"react-dom": "^19.1.1",
|
|
39
38
|
"react-hook-form": "^7.62.0"
|
|
40
39
|
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/umi-labs/foundry.git",
|
|
43
|
+
"directory": "packages/hooks"
|
|
44
|
+
},
|
|
41
45
|
"scripts": {
|
|
42
46
|
"build": "tsup",
|
|
43
47
|
"dev": "tsup --watch",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
48
|
+
"type-check": "tsc --noEmit",
|
|
49
|
+
"verify:pack": "publint && attw --pack . --profile esm-only"
|
|
46
50
|
}
|
|
47
51
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { type RefObject, useEffect, useRef } from 'react';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Hook that detects clicks outside of a specified element.
|
|
@@ -37,6 +37,13 @@ export function useClickOutside<T extends HTMLElement = HTMLElement>(
|
|
|
37
37
|
ref: RefObject<T>,
|
|
38
38
|
handler: (event: MouseEvent | TouchEvent) => void
|
|
39
39
|
) {
|
|
40
|
+
// Keep the latest handler in a ref so an inline handler (the common call
|
|
41
|
+
// site) doesn't re-add/remove the listeners on every render.
|
|
42
|
+
const handlerRef = useRef(handler);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
handlerRef.current = handler;
|
|
45
|
+
}, [handler]);
|
|
46
|
+
|
|
40
47
|
useEffect(() => {
|
|
41
48
|
const listener = (event: MouseEvent | TouchEvent) => {
|
|
42
49
|
const el = ref?.current;
|
|
@@ -44,7 +51,7 @@ export function useClickOutside<T extends HTMLElement = HTMLElement>(
|
|
|
44
51
|
return;
|
|
45
52
|
}
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
handlerRef.current(event);
|
|
48
55
|
};
|
|
49
56
|
|
|
50
57
|
document.addEventListener('mousedown', listener);
|
|
@@ -54,5 +61,5 @@ export function useClickOutside<T extends HTMLElement = HTMLElement>(
|
|
|
54
61
|
document.removeEventListener('mousedown', listener);
|
|
55
62
|
document.removeEventListener('touchstart', listener);
|
|
56
63
|
};
|
|
57
|
-
}, [ref
|
|
64
|
+
}, [ref]);
|
|
58
65
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Hook that provides localStorage functionality with React state synchronization.
|
|
5
|
-
* Automatically handles serialization/deserialization and SSR safety
|
|
5
|
+
* Automatically handles serialization/deserialization and SSR safety: the server
|
|
6
|
+
* and the first client render both use `initialValue`, and the stored value is
|
|
7
|
+
* read after mount, so hydration never mismatches.
|
|
6
8
|
*
|
|
7
9
|
* @template T - The type of the value to store
|
|
8
10
|
* @param key - The localStorage key
|
|
@@ -44,19 +46,17 @@ import { useState } from 'react';
|
|
|
44
46
|
* ```
|
|
45
47
|
*/
|
|
46
48
|
export function useLocalStorage<T>(key: string, initialValue: T) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
49
|
+
const [storedValue, setStoredValue] = useState<T>(initialValue);
|
|
50
|
+
|
|
51
|
+
// Read the stored value after mount so the first client render matches the server.
|
|
52
|
+
useEffect(() => {
|
|
52
53
|
try {
|
|
53
54
|
const item = window.localStorage.getItem(key);
|
|
54
|
-
|
|
55
|
+
if (item !== null) setStoredValue(JSON.parse(item));
|
|
55
56
|
} catch (error) {
|
|
56
57
|
console.log(error);
|
|
57
|
-
return initialValue;
|
|
58
58
|
}
|
|
59
|
-
});
|
|
59
|
+
}, [key]);
|
|
60
60
|
|
|
61
61
|
// Return a wrapped version of useState's setter function that persists the new value to localStorage
|
|
62
62
|
const setValue = (value: T | ((val: T) => T)) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Hook that tracks whether a CSS media query matches the current viewport.
|
|
@@ -37,17 +37,21 @@ import { useState, useEffect } from 'react';
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
export function useMediaQuery(query: string): boolean {
|
|
40
|
-
|
|
40
|
+
// SSR-safe initial value; hydrated to the real match in the effect.
|
|
41
|
+
const [matches, setMatches] = useState(() =>
|
|
42
|
+
typeof window !== 'undefined' ? window.matchMedia(query).matches : false
|
|
43
|
+
);
|
|
41
44
|
|
|
42
45
|
useEffect(() => {
|
|
43
46
|
const media = window.matchMedia(query);
|
|
44
|
-
if (media.matches !== matches) {
|
|
45
|
-
setMatches(media.matches);
|
|
46
|
-
}
|
|
47
47
|
const listener = () => setMatches(media.matches);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
// Subscribe to the MediaQueryList's own `change` event so non-viewport
|
|
49
|
+
// queries (prefers-color-scheme, prefers-reduced-motion, orientation, …)
|
|
50
|
+
// update too — a `resize` listener never fires for those.
|
|
51
|
+
listener();
|
|
52
|
+
media.addEventListener('change', listener);
|
|
53
|
+
return () => media.removeEventListener('change', listener);
|
|
54
|
+
}, [query]);
|
|
51
55
|
|
|
52
56
|
return matches;
|
|
53
57
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
// Export all hooks
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
2
|
+
|
|
3
|
+
export type { SearchResult, SearchSection } from '@foundrykit/types';
|
|
4
|
+
export {
|
|
5
|
+
type EmptyStateBehavior,
|
|
6
|
+
type EmptyStateBehaviour,
|
|
7
|
+
SearchContext,
|
|
8
|
+
useAdvancedSearch,
|
|
9
|
+
} from './hooks/use-advanced-search.js';
|
|
5
10
|
export { useClickOutside } from './hooks/use-click-outside.js';
|
|
6
|
-
export {
|
|
11
|
+
export { useDebouncedValue } from './hooks/use-debounced-value.js';
|
|
7
12
|
export {
|
|
8
|
-
useFormField,
|
|
9
13
|
FormFieldContext,
|
|
10
|
-
FormItemContext,
|
|
11
14
|
type FormFieldContextValue,
|
|
15
|
+
FormItemContext,
|
|
12
16
|
type FormItemContextValue,
|
|
17
|
+
useFormField,
|
|
13
18
|
} from './hooks/use-form-field.js';
|
|
14
|
-
export {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
type EmptyStateBehavior,
|
|
18
|
-
type EmptyStateBehaviour,
|
|
19
|
-
} from './hooks/use-advanced-search.js';
|
|
20
|
-
export type { SearchResult, SearchSection } from '@foundrykit/types';
|
|
19
|
+
export { useLocalStorage } from './hooks/use-local-storage.js';
|
|
20
|
+
export { useMediaQuery } from './hooks/use-media-query.js';
|
|
21
|
+
export { useIsMobile } from './hooks/use-mobile.js';
|
|
21
22
|
|
|
22
23
|
// Export utilities
|
|
23
24
|
export * from './lib/utils.js';
|
package/tsup.config.ts
CHANGED
|
@@ -7,5 +7,8 @@ export default defineConfig({
|
|
|
7
7
|
splitting: false,
|
|
8
8
|
sourcemap: true,
|
|
9
9
|
clean: true,
|
|
10
|
+
// esbuild drops module directives when bundling; every export here is
|
|
11
|
+
// client-only, so mark the whole bundle for RSC consumers.
|
|
12
|
+
banner: { js: "'use client';" },
|
|
10
13
|
external: ['react', 'react-dom'],
|
|
11
14
|
});
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @foundrykit/hooks@2.0.5 build /Users/isj/Desktop/work/projects/foundry/packages/hooks
|
|
3
|
-
> tsup
|
|
4
|
-
|
|
5
|
-
CLI Building entry: src/index.ts
|
|
6
|
-
CLI Using tsconfig: tsconfig.json
|
|
7
|
-
CLI tsup v8.5.0
|
|
8
|
-
CLI Using tsup config: /Users/isj/Desktop/work/projects/foundry/packages/hooks/tsup.config.ts
|
|
9
|
-
CLI Target: es2022
|
|
10
|
-
CLI Cleaning output folder
|
|
11
|
-
ESM Build start
|
|
12
|
-
ESM dist/index.js 4.36 KB
|
|
13
|
-
ESM dist/index.js.map 16.50 KB
|
|
14
|
-
ESM ⚡️ Build success in 137ms
|
|
15
|
-
DTS Build start
|
|
16
|
-
DTS ⚡️ Build success in 1530ms
|
|
17
|
-
DTS dist/index.d.ts 9.79 KB
|
package/eslint.config.js
DELETED