fumadocs-core 13.0.4 → 13.0.6

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.
@@ -0,0 +1,19 @@
1
+ // src/utils/use-debounce.ts
2
+ import { useRef, useState } from "react";
3
+ function useDebounce(value, delayMs = 1e3) {
4
+ const [debouncedValue, setDebouncedValue] = useState(value);
5
+ const timer = useRef();
6
+ if (delayMs === 0) return value;
7
+ if (value !== debouncedValue && timer.current?.value !== value) {
8
+ if (timer.current) clearTimeout(timer.current.handler);
9
+ const handler = window.setTimeout(() => {
10
+ setDebouncedValue(value);
11
+ }, delayMs);
12
+ timer.current = { value, handler };
13
+ }
14
+ return debouncedValue;
15
+ }
16
+
17
+ export {
18
+ useDebounce
19
+ };
@@ -1,7 +1,10 @@
1
+ import {
2
+ useDebounce
3
+ } from "../chunk-NREWOIVI.js";
1
4
  import "../chunk-MLKGABMK.js";
2
5
 
3
6
  // src/search/client.ts
4
- import { useEffect, useState } from "react";
7
+ import { useState } from "react";
5
8
  import useSWR from "swr";
6
9
  async function fetchDocs(api, query, locale, tag) {
7
10
  if (query.length === 0) return "empty";
@@ -25,18 +28,6 @@ function useDocsSearch(locale, tag, api = "/api/search", delayMs = 100) {
25
28
  );
26
29
  return { search, setSearch, query };
27
30
  }
28
- function useDebounce(value, delayMs = 1e3) {
29
- const [debouncedValue, setDebouncedValue] = useState(value);
30
- useEffect(() => {
31
- const handler = setTimeout(() => {
32
- setDebouncedValue(value);
33
- }, delayMs);
34
- return () => {
35
- clearTimeout(handler);
36
- };
37
- }, [value, delayMs]);
38
- return debouncedValue;
39
- }
40
31
  export {
41
32
  useDocsSearch
42
33
  };
@@ -15,6 +15,12 @@ interface Options extends SearchOptions {
15
15
  * @defaultValue true
16
16
  */
17
17
  allowEmpty?: boolean;
18
+ /**
19
+ * Delay to debounce (in ms)
20
+ *
21
+ * @defaultValue 300
22
+ */
23
+ delay?: number;
18
24
  }
19
25
  declare function groupResults(hits: Hit<BaseIndex>[]): SortedResult[];
20
26
  declare function searchDocs(index: SearchIndex, query: string, options?: SearchOptions): Promise<SortedResult[]>;
@@ -25,6 +31,6 @@ interface UseAlgoliaSearch {
25
31
  keepPreviousData: true;
26
32
  }>;
27
33
  }
28
- declare function useAlgoliaSearch(index: SearchIndex, { allowEmpty, ...options }?: Options): UseAlgoliaSearch;
34
+ declare function useAlgoliaSearch(index: SearchIndex, { allowEmpty, delay, ...options }?: Options): UseAlgoliaSearch;
29
35
 
30
36
  export { type Options, groupResults, searchDocs, useAlgoliaSearch };
@@ -1,3 +1,6 @@
1
+ import {
2
+ useDebounce
3
+ } from "../chunk-NREWOIVI.js";
1
4
  import "../chunk-MLKGABMK.js";
2
5
 
3
6
  // src/search-algolia/client.ts
@@ -41,13 +44,14 @@ async function searchDocs(index, query, options) {
41
44
  });
42
45
  return groupResults(result.hits);
43
46
  }
44
- function useAlgoliaSearch(index, { allowEmpty = true, ...options } = {}) {
47
+ function useAlgoliaSearch(index, { allowEmpty = true, delay = 150, ...options } = {}) {
45
48
  const [search, setSearch] = useState("");
49
+ const debouncedValue = useDebounce(search, delay);
46
50
  const query = useSWR(
47
- ["algolia-search", search, allowEmpty, options],
51
+ ["algolia-search", debouncedValue, allowEmpty, options],
48
52
  async () => {
49
- if (allowEmpty && search.length === 0) return "empty";
50
- return searchDocs(index, search, options);
53
+ if (allowEmpty && debouncedValue.length === 0) return "empty";
54
+ return searchDocs(index, debouncedValue, options);
51
55
  },
52
56
  {
53
57
  keepPreviousData: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "13.0.4",
3
+ "version": "13.0.6",
4
4
  "description": "The library for building a documentation website in Next.js",
5
5
  "keywords": [
6
6
  "NextJs",