@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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@foundrykit/hooks",
3
- "version": "2.0.6",
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
- "eslint": "^9.0.0",
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/eslint-config": "1.0.5",
33
- "@foundrykit/types": "2.0.6",
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
- "lint": "eslint . --max-warnings 0",
45
- "type-check": "tsc --noEmit"
48
+ "type-check": "tsc --noEmit",
49
+ "verify:pack": "publint && attw --pack . --profile esm-only"
46
50
  }
47
51
  }
@@ -1,5 +1,5 @@
1
- import * as React from 'react';
2
1
  import type { SearchResult } from '@foundrykit/types';
2
+ import * as React from 'react';
3
3
 
4
4
  /**
5
5
  * Defines the behavior when search results are empty or query is too short.
@@ -1,4 +1,4 @@
1
- import { useEffect, RefObject } from 'react';
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
- handler(event);
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, handler]);
64
+ }, [ref]);
58
65
  }
@@ -1,4 +1,4 @@
1
- import { useState, useEffect } from 'react';
1
+ import { useEffect, useState } from 'react';
2
2
 
3
3
  /**
4
4
  * Hook that returns a debounced version of the provided value.
@@ -1,9 +1,9 @@
1
1
  import * as React from 'react';
2
2
  import {
3
- useFormContext,
4
- useFormState,
5
3
  type FieldPath,
6
4
  type FieldValues,
5
+ useFormContext,
6
+ useFormState,
7
7
  } from 'react-hook-form';
8
8
 
9
9
  export type FormFieldContextValue<
@@ -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
- // Get from local storage then parse stored json or return initialValue
48
- const [storedValue, setStoredValue] = useState<T>(() => {
49
- if (typeof window === 'undefined') {
50
- return initialValue;
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
- return item ? JSON.parse(item) : initialValue;
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 { useState, useEffect } from 'react';
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
- const [matches, setMatches] = useState(false);
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
- window.addEventListener('resize', listener);
49
- return () => window.removeEventListener('resize', listener);
50
- }, [matches, query]);
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
- export { useDebouncedValue } from './hooks/use-debounced-value.js';
3
- export { useLocalStorage } from './hooks/use-local-storage.js';
4
- export { useMediaQuery } from './hooks/use-media-query.js';
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 { useIsMobile } from './hooks/use-mobile.js';
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
- useAdvancedSearch,
16
- SearchContext,
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
  });
@@ -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
@@ -1,4 +0,0 @@
1
-
2
- > @foundrykit/hooks@2.0.5 type-check /Users/isj/Desktop/work/projects/foundry/packages/hooks
3
- > tsc --noEmit
4
-
package/eslint.config.js DELETED
@@ -1,4 +0,0 @@
1
- import { config } from "@foundrykit/eslint-config/react-internal"
2
-
3
- /** @type {import("eslint").Linter.Config} */
4
- export default config