@codefast/ui 0.3.11 → 0.3.12-canary.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.
Files changed (246) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +4 -0
  3. package/dist/components/accordion.d.ts +11 -29
  4. package/dist/components/accordion.d.ts.map +1 -0
  5. package/dist/components/accordion.js +10 -37
  6. package/dist/components/alert-dialog.d.ts +25 -60
  7. package/dist/components/alert-dialog.d.ts.map +1 -0
  8. package/dist/components/alert-dialog.js +16 -69
  9. package/dist/components/alert.d.ts +16 -27
  10. package/dist/components/alert.d.ts.map +1 -0
  11. package/dist/components/alert.js +22 -30
  12. package/dist/components/aspect-ratio.d.ts +5 -8
  13. package/dist/components/aspect-ratio.d.ts.map +1 -0
  14. package/dist/components/aspect-ratio.js +5 -7
  15. package/dist/components/avatar.d.ts +7 -17
  16. package/dist/components/avatar.d.ts.map +1 -0
  17. package/dist/components/avatar.js +7 -18
  18. package/dist/components/badge.d.ts +15 -21
  19. package/dist/components/badge.d.ts.map +1 -0
  20. package/dist/components/badge.js +23 -20
  21. package/dist/components/breadcrumb.d.ts +13 -35
  22. package/dist/components/breadcrumb.d.ts.map +1 -0
  23. package/dist/components/breadcrumb.js +13 -49
  24. package/dist/components/button-group.d.ts +15 -29
  25. package/dist/components/button-group.d.ts.map +1 -0
  26. package/dist/components/button-group.js +22 -27
  27. package/dist/components/button.d.ts +26 -35
  28. package/dist/components/button.d.ts.map +1 -0
  29. package/dist/components/button.js +43 -51
  30. package/dist/components/calendar.d.ts +10 -24
  31. package/dist/components/calendar.d.ts.map +1 -0
  32. package/dist/components/calendar.js +75 -92
  33. package/dist/components/card.d.ts +11 -34
  34. package/dist/components/card.d.ts.map +1 -0
  35. package/dist/components/card.js +11 -39
  36. package/dist/components/carousel.d.ts +25 -55
  37. package/dist/components/carousel.d.ts.map +1 -0
  38. package/dist/components/carousel.js +82 -114
  39. package/dist/components/chart.d.ts +37 -72
  40. package/dist/components/chart.d.ts.map +1 -0
  41. package/dist/components/chart.js +161 -150
  42. package/dist/components/checkbox-cards.d.ts +9 -15
  43. package/dist/components/checkbox-cards.d.ts.map +1 -0
  44. package/dist/components/checkbox-cards.js +8 -22
  45. package/dist/components/checkbox-group.d.ts +9 -16
  46. package/dist/components/checkbox-group.d.ts.map +1 -0
  47. package/dist/components/checkbox-group.js +7 -19
  48. package/dist/components/checkbox.d.ts +5 -9
  49. package/dist/components/checkbox.d.ts.map +1 -0
  50. package/dist/components/checkbox.js +6 -14
  51. package/dist/components/collapsible.d.ts +7 -14
  52. package/dist/components/collapsible.d.ts.map +1 -0
  53. package/dist/components/collapsible.js +7 -15
  54. package/dist/components/command.d.ts +24 -55
  55. package/dist/components/command.d.ts.map +1 -0
  56. package/dist/components/command.js +18 -69
  57. package/dist/components/context-menu.d.ts +23 -72
  58. package/dist/components/context-menu.d.ts.map +1 -0
  59. package/dist/components/context-menu.js +21 -89
  60. package/dist/components/dialog.d.ts +23 -53
  61. package/dist/components/dialog.d.ts.map +1 -0
  62. package/dist/components/dialog.js +20 -72
  63. package/dist/components/drawer.d.ts +29 -60
  64. package/dist/components/drawer.d.ts.map +1 -0
  65. package/dist/components/drawer.js +16 -58
  66. package/dist/components/dropdown-menu.d.ts +23 -73
  67. package/dist/components/dropdown-menu.d.ts.map +1 -0
  68. package/dist/components/dropdown-menu.js +21 -96
  69. package/dist/components/empty.d.ts +16 -37
  70. package/dist/components/empty.d.ts.map +1 -0
  71. package/dist/components/empty.js +24 -44
  72. package/dist/components/field.d.ts +26 -63
  73. package/dist/components/field.d.ts.map +1 -0
  74. package/dist/components/field.js +46 -89
  75. package/dist/components/form.d.ts +16 -37
  76. package/dist/components/form.d.ts.map +1 -0
  77. package/dist/components/form.js +50 -66
  78. package/dist/components/hover-card.d.ts +8 -21
  79. package/dist/components/hover-card.d.ts.map +1 -0
  80. package/dist/components/hover-card.js +8 -23
  81. package/dist/components/input-group.d.ts +29 -53
  82. package/dist/components/input-group.d.ts.map +1 -0
  83. package/dist/components/input-group.js +59 -66
  84. package/dist/components/input-number.d.ts +8 -28
  85. package/dist/components/input-number.d.ts.map +1 -0
  86. package/dist/components/input-number.js +14 -55
  87. package/dist/components/input-otp.d.ts +11 -24
  88. package/dist/components/input-otp.d.ts.map +1 -0
  89. package/dist/components/input-otp.js +13 -34
  90. package/dist/components/input-password.d.ts +6 -12
  91. package/dist/components/input-password.d.ts.map +1 -0
  92. package/dist/components/input-password.js +10 -31
  93. package/dist/components/input-search.d.ts +9 -18
  94. package/dist/components/input-search.d.ts.map +1 -0
  95. package/dist/components/input-search.js +16 -44
  96. package/dist/components/input.d.ts +5 -10
  97. package/dist/components/input.d.ts.map +1 -0
  98. package/dist/components/input.js +5 -9
  99. package/dist/components/item.d.ts +33 -69
  100. package/dist/components/item.d.ts.map +1 -0
  101. package/dist/components/item.js +47 -90
  102. package/dist/components/kbd.d.ts +6 -13
  103. package/dist/components/kbd.d.ts.map +1 -0
  104. package/dist/components/kbd.js +6 -13
  105. package/dist/components/label.d.ts +5 -9
  106. package/dist/components/label.d.ts.map +1 -0
  107. package/dist/components/label.js +5 -8
  108. package/dist/components/menubar.d.ts +24 -80
  109. package/dist/components/menubar.d.ts.map +1 -0
  110. package/dist/components/menubar.js +23 -99
  111. package/dist/components/native-select.d.ts +7 -16
  112. package/dist/components/native-select.d.ts.map +1 -0
  113. package/dist/components/native-select.js +7 -25
  114. package/dist/components/navigation-menu.d.ts +13 -36
  115. package/dist/components/navigation-menu.d.ts.map +1 -0
  116. package/dist/components/navigation-menu.js +25 -61
  117. package/dist/components/pagination.d.ts +15 -35
  118. package/dist/components/pagination.d.ts.map +1 -0
  119. package/dist/components/pagination.js +13 -55
  120. package/dist/components/popover.d.ts +9 -24
  121. package/dist/components/popover.d.ts.map +1 -0
  122. package/dist/components/popover.js +9 -27
  123. package/dist/components/progress-circle.d.ts +89 -106
  124. package/dist/components/progress-circle.d.ts.map +1 -0
  125. package/dist/components/progress-circle.js +70 -103
  126. package/dist/components/progress.d.ts +5 -10
  127. package/dist/components/progress.d.ts.map +1 -0
  128. package/dist/components/progress.js +7 -13
  129. package/dist/components/radio-cards.d.ts +5 -10
  130. package/dist/components/radio-cards.d.ts.map +1 -0
  131. package/dist/components/radio-cards.js +7 -21
  132. package/dist/components/radio-group.d.ts +6 -13
  133. package/dist/components/radio-group.d.ts.map +1 -0
  134. package/dist/components/radio-group.js +6 -17
  135. package/dist/components/radio.d.ts +6 -12
  136. package/dist/components/radio.d.ts.map +1 -0
  137. package/dist/components/radio.js +5 -10
  138. package/dist/components/resizable.d.ts +8 -18
  139. package/dist/components/resizable.d.ts.map +1 -0
  140. package/dist/components/resizable.js +7 -21
  141. package/dist/components/scroll-area.d.ts +21 -33
  142. package/dist/components/scroll-area.d.ts.map +1 -0
  143. package/dist/components/scroll-area.js +64 -84
  144. package/dist/components/select.d.ts +17 -50
  145. package/dist/components/select.d.ts.map +1 -0
  146. package/dist/components/select.js +26 -78
  147. package/dist/components/separator.d.ts +20 -29
  148. package/dist/components/separator.d.ts.map +1 -0
  149. package/dist/components/separator.js +27 -36
  150. package/dist/components/sheet.d.ts +30 -62
  151. package/dist/components/sheet.d.ts.map +1 -0
  152. package/dist/components/sheet.js +36 -80
  153. package/dist/components/sidebar.d.ts +67 -160
  154. package/dist/components/sidebar.d.ts.map +1 -0
  155. package/dist/components/sidebar.js +166 -301
  156. package/dist/components/skeleton.d.ts +5 -9
  157. package/dist/components/skeleton.d.ts.map +1 -0
  158. package/dist/components/skeleton.js +5 -8
  159. package/dist/components/slider.d.ts +5 -13
  160. package/dist/components/slider.d.ts.map +1 -0
  161. package/dist/components/slider.js +12 -34
  162. package/dist/components/sonner.d.ts +8 -10
  163. package/dist/components/sonner.d.ts.map +1 -0
  164. package/dist/components/sonner.js +13 -16
  165. package/dist/components/spinner.d.ts +6 -12
  166. package/dist/components/spinner.d.ts.map +1 -0
  167. package/dist/components/spinner.js +20 -33
  168. package/dist/components/switch.d.ts +5 -9
  169. package/dist/components/switch.d.ts.map +1 -0
  170. package/dist/components/switch.js +5 -12
  171. package/dist/components/table.d.ts +12 -37
  172. package/dist/components/table.d.ts.map +1 -0
  173. package/dist/components/table.js +12 -47
  174. package/dist/components/tabs.d.ts +8 -21
  175. package/dist/components/tabs.d.ts.map +1 -0
  176. package/dist/components/tabs.js +8 -23
  177. package/dist/components/textarea.d.ts +5 -9
  178. package/dist/components/textarea.d.ts.map +1 -0
  179. package/dist/components/textarea.js +5 -8
  180. package/dist/components/toggle-group.d.ts +10 -24
  181. package/dist/components/toggle-group.d.ts.map +1 -0
  182. package/dist/components/toggle-group.js +20 -38
  183. package/dist/components/toggle.d.ts +18 -25
  184. package/dist/components/toggle.d.ts.map +1 -0
  185. package/dist/components/toggle.js +24 -29
  186. package/dist/components/tooltip.d.ts +9 -24
  187. package/dist/components/tooltip.d.ts.map +1 -0
  188. package/dist/components/tooltip.js +9 -28
  189. package/dist/hooks/use-animated-value.d.ts +2 -4
  190. package/dist/hooks/use-animated-value.d.ts.map +1 -0
  191. package/dist/hooks/use-animated-value.js +67 -58
  192. package/dist/hooks/use-copy-to-clipboard.d.ts +6 -11
  193. package/dist/hooks/use-copy-to-clipboard.d.ts.map +1 -0
  194. package/dist/hooks/use-copy-to-clipboard.js +42 -39
  195. package/dist/hooks/use-is-mobile.d.ts +2 -4
  196. package/dist/hooks/use-is-mobile.d.ts.map +1 -0
  197. package/dist/hooks/use-is-mobile.js +20 -23
  198. package/dist/hooks/use-media-query.d.ts +2 -4
  199. package/dist/hooks/use-media-query.d.ts.map +1 -0
  200. package/dist/hooks/use-media-query.js +49 -46
  201. package/dist/hooks/use-mutation-observer.d.ts +3 -6
  202. package/dist/hooks/use-mutation-observer.d.ts.map +1 -0
  203. package/dist/hooks/use-mutation-observer.js +33 -34
  204. package/dist/hooks/use-pagination.d.ts +15 -22
  205. package/dist/hooks/use-pagination.d.ts.map +1 -0
  206. package/dist/hooks/use-pagination.js +99 -99
  207. package/dist/index.d.ts +131 -69
  208. package/dist/index.d.ts.map +1 -0
  209. package/dist/index.js +68 -69
  210. package/dist/primitives/checkbox-group.d.ts +84 -105
  211. package/dist/primitives/checkbox-group.d.ts.map +1 -0
  212. package/dist/primitives/checkbox-group.js +91 -105
  213. package/dist/primitives/input-number.d.ts +41 -51
  214. package/dist/primitives/input-number.d.ts.map +1 -0
  215. package/dist/primitives/input-number.js +417 -473
  216. package/dist/primitives/input.d.ts +43 -48
  217. package/dist/primitives/input.d.ts.map +1 -0
  218. package/dist/primitives/input.js +67 -66
  219. package/dist/primitives/progress-circle.d.ts +49 -79
  220. package/dist/primitives/progress-circle.d.ts.map +1 -0
  221. package/dist/primitives/progress-circle.js +134 -149
  222. package/package.json +17 -10
  223. package/{dist → src}/css/preset.css +1 -1
  224. /package/{dist → src}/css/amber.css +0 -0
  225. /package/{dist → src}/css/blue.css +0 -0
  226. /package/{dist → src}/css/cyan.css +0 -0
  227. /package/{dist → src}/css/emerald.css +0 -0
  228. /package/{dist → src}/css/fuchsia.css +0 -0
  229. /package/{dist → src}/css/gray.css +0 -0
  230. /package/{dist → src}/css/green.css +0 -0
  231. /package/{dist → src}/css/indigo.css +0 -0
  232. /package/{dist → src}/css/lime.css +0 -0
  233. /package/{dist → src}/css/neutral.css +0 -0
  234. /package/{dist → src}/css/orange.css +0 -0
  235. /package/{dist → src}/css/pink.css +0 -0
  236. /package/{dist → src}/css/purple.css +0 -0
  237. /package/{dist → src}/css/red.css +0 -0
  238. /package/{dist → src}/css/rose.css +0 -0
  239. /package/{dist → src}/css/sky.css +0 -0
  240. /package/{dist → src}/css/slate.css +0 -0
  241. /package/{dist → src}/css/stone.css +0 -0
  242. /package/{dist → src}/css/style.css +0 -0
  243. /package/{dist → src}/css/teal.css +0 -0
  244. /package/{dist → src}/css/violet.css +0 -0
  245. /package/{dist → src}/css/yellow.css +0 -0
  246. /package/{dist → src}/css/zinc.css +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-media-query.d.ts","sourceRoot":"","sources":["../../src/hooks/use-media-query.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CA2CpD"}
@@ -1,50 +1,53 @@
1
1
  "use client";
2
2
  import { useEffect, useState } from "react";
3
- //#region src/hooks/use-media-query.ts
4
3
  /**
5
- * Subscribe to a CSS media query and receive its match state.
6
- *
7
- * Evaluates the query immediately (when supported) and updates on changes
8
- * via an event listener.
9
- *
10
- * @param query - A valid media query string (e.g., "(max-width: 768px)").
11
- * @returns true when the media query currently matches; otherwise false.
12
- *
13
- * @example
14
- * ```tsx
15
- * const isNarrow = useMediaQuery("(max-width: 768px)");
16
- * ```
17
- */
18
- function useMediaQuery(query) {
19
- /**
20
- * State to store whether the query matches.
21
- * The initial value is calculated based on the current window state.
22
- */
23
- const [matches, setMatches] = useState(() => {
24
- if (typeof window !== "undefined" && typeof window.matchMedia === "function") return window.matchMedia(query).matches;
25
- return false;
26
- });
27
- useEffect(() => {
28
- if (typeof window === "undefined") return;
29
- /**
30
- * MediaQueryList to evaluate and observe the provided query.
31
- */
32
- const mediaQueryList = window.matchMedia(query);
33
- /**
34
- * Update state when the media query status changes.
35
- */
36
- const onChange = (event) => {
37
- setMatches(event.matches);
38
- };
39
- mediaQueryList.addEventListener("change", onChange);
40
- /**
41
- * Remove the event listener on unmount or when the query changes.
42
- */
43
- return () => {
44
- mediaQueryList.removeEventListener("change", onChange);
45
- };
46
- }, [query]);
47
- return matches;
4
+ * Subscribe to a CSS media query and receive its match state.
5
+ *
6
+ * Evaluates the query immediately (when supported) and updates on changes
7
+ * via an event listener.
8
+ *
9
+ * @param query - A valid media query string (e.g., "(max-width: 768px)").
10
+ * @returns true when the media query currently matches; otherwise false.
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * const isNarrow = useMediaQuery("(max-width: 768px)");
15
+ * ```
16
+ */
17
+ export function useMediaQuery(query) {
18
+ /**
19
+ * State to store whether the query matches.
20
+ * The initial value is calculated based on the current window state.
21
+ */
22
+ const [matches, setMatches] = useState(() => {
23
+ // Ensure initial state matches current media query status
24
+ if (typeof window !== "undefined" && typeof window.matchMedia === "function") {
25
+ return window.matchMedia(query).matches;
26
+ }
27
+ return false;
28
+ });
29
+ useEffect(() => {
30
+ // Only run in a browser environment where matchMedia is available
31
+ if (typeof window === "undefined") {
32
+ return;
33
+ }
34
+ /**
35
+ * MediaQueryList to evaluate and observe the provided query.
36
+ */
37
+ const mediaQueryList = window.matchMedia(query);
38
+ /**
39
+ * Update state when the media query status changes.
40
+ */
41
+ const onChange = (event) => {
42
+ setMatches(event.matches);
43
+ };
44
+ mediaQueryList.addEventListener("change", onChange);
45
+ /**
46
+ * Remove the event listener on unmount or when the query changes.
47
+ */
48
+ return () => {
49
+ mediaQueryList.removeEventListener("change", onChange);
50
+ };
51
+ }, [query]);
52
+ return matches;
48
53
  }
49
- //#endregion
50
- export { useMediaQuery };
@@ -1,6 +1,4 @@
1
- import { RefObject } from "react";
2
-
3
- //#region src/hooks/use-mutation-observer.d.ts
1
+ import type { RefObject } from "react";
4
2
  /**
5
3
  * Observe DOM mutations on a referenced element and invoke a callback.
6
4
  *
@@ -14,6 +12,5 @@ import { RefObject } from "react";
14
12
  *
15
13
  * @see [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
16
14
  */
17
- declare function useMutationObserver(ref: RefObject<HTMLElement | null>, callback: MutationCallback, options?: MutationObserverInit): void;
18
- //#endregion
19
- export { useMutationObserver };
15
+ export declare function useMutationObserver(ref: RefObject<HTMLElement | null>, callback: MutationCallback, options?: MutationObserverInit): void;
16
+ //# sourceMappingURL=use-mutation-observer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-mutation-observer.d.ts","sourceRoot":"","sources":["../../src/hooks/use-mutation-observer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAcvC;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,EAClC,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,GAAE,oBAAqC,GAC7C,IAAI,CAkBN"}
@@ -1,41 +1,40 @@
1
1
  "use client";
2
2
  import { useEffect } from "react";
3
- //#region src/hooks/use-mutation-observer.ts
4
3
  /**
5
- * Default options for MutationObserver observing common mutation types.
6
- */
4
+ * Default options for MutationObserver observing common mutation types.
5
+ */
7
6
  const defaultOptions = {
8
- attributes: true,
9
- characterData: true,
10
- childList: true,
11
- subtree: true
7
+ attributes: true,
8
+ characterData: true,
9
+ childList: true,
10
+ subtree: true,
12
11
  };
13
12
  /**
14
- * Observe DOM mutations on a referenced element and invoke a callback.
15
- *
16
- * Attaches a MutationObserver to the provided element reference with the given
17
- * options and calls the callback whenever mutations occur.
18
- *
19
- * @param ref - Ref to the target HTMLElement to observe.
20
- * @param callback - Mutation callback invoked with observed records.
21
- * @param options - Observer configuration. Defaults watch attributes, characterData, childList, subtree.
22
- * @returns void
23
- *
24
- * @see [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
25
- */
26
- function useMutationObserver(ref, callback, options = defaultOptions) {
27
- useEffect(() => {
28
- if (!ref.current) return;
29
- const observer = new MutationObserver(callback);
30
- observer.observe(ref.current, options);
31
- return () => {
32
- observer.disconnect();
33
- };
34
- }, [
35
- ref,
36
- callback,
37
- options
38
- ]);
13
+ * Observe DOM mutations on a referenced element and invoke a callback.
14
+ *
15
+ * Attaches a MutationObserver to the provided element reference with the given
16
+ * options and calls the callback whenever mutations occur.
17
+ *
18
+ * @param ref - Ref to the target HTMLElement to observe.
19
+ * @param callback - Mutation callback invoked with observed records.
20
+ * @param options - Observer configuration. Defaults watch attributes, characterData, childList, subtree.
21
+ * @returns void
22
+ *
23
+ * @see [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
24
+ */
25
+ export function useMutationObserver(ref, callback, options = defaultOptions) {
26
+ useEffect(() => {
27
+ // Abort if ref is not attached
28
+ if (!ref.current) {
29
+ return;
30
+ }
31
+ // Create observer
32
+ const observer = new MutationObserver(callback);
33
+ // Observe with provided options
34
+ observer.observe(ref.current, options);
35
+ // Cleanup on unmount or when deps change
36
+ return () => {
37
+ observer.disconnect();
38
+ };
39
+ }, [ref, callback, options]);
39
40
  }
40
- //#endregion
41
- export { useMutationObserver };
@@ -1,19 +1,18 @@
1
- //#region src/hooks/use-pagination.d.ts
2
- interface UsePaginationProps {
3
- /** Current active page number. */
4
- currentPage: number;
5
- /** Number of results displayed per page. */
6
- resultsPerPage: number;
7
- /**
8
- * Number of sibling pages to show on each side of the current page.
9
- * Defaults to 1.
10
- */
11
- siblingPagesCount?: number;
12
- /** Total number of results across all pages. */
13
- totalResults: number;
1
+ export interface UsePaginationProps {
2
+ /** Current active page number. */
3
+ currentPage: number;
4
+ /** Number of results displayed per page. */
5
+ resultsPerPage: number;
6
+ /**
7
+ * Number of sibling pages to show on each side of the current page.
8
+ * Defaults to 1.
9
+ */
10
+ siblingPagesCount?: number;
11
+ /** Total number of results across all pages. */
12
+ totalResults: number;
14
13
  }
15
14
  /** Ellipsis marker used to collapse ranges in pagination output. */
16
- declare const ELLIPSIS = "\u2022\u2022\u2022";
15
+ export declare const ELLIPSIS = "\u2022\u2022\u2022";
17
16
  /**
18
17
  * Compute a pagination structure for result sets.
19
18
  *
@@ -34,11 +33,5 @@ declare const ELLIPSIS = "\u2022\u2022\u2022";
34
33
  * });
35
34
  * ```
36
35
  */
37
- declare function usePagination({
38
- currentPage,
39
- resultsPerPage,
40
- siblingPagesCount,
41
- totalResults
42
- }: UsePaginationProps): (number | string)[];
43
- //#endregion
44
- export { ELLIPSIS, UsePaginationProps, usePagination };
36
+ export declare function usePagination({ currentPage, resultsPerPage, siblingPagesCount, totalResults, }: UsePaginationProps): (number | string)[];
37
+ //# sourceMappingURL=use-pagination.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-pagination.d.ts","sourceRoot":"","sources":["../../src/hooks/use-pagination.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,kBAAkB;IACjC,kCAAkC;IAClC,WAAW,EAAE,MAAM,CAAC;IACpB,4CAA4C;IAC5C,cAAc,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,gDAAgD;IAChD,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,oEAAoE;AACpE,eAAO,MAAM,QAAQ,uBAAQ,CAAC;AAe9B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,EAC5B,WAAW,EACX,cAAc,EACd,iBAAqB,EACrB,YAAY,GACb,EAAE,kBAAkB,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAuF1C"}
@@ -1,107 +1,107 @@
1
1
  "use client";
2
2
  import { useMemo } from "react";
3
- //#region src/hooks/use-pagination.ts
4
3
  /** Ellipsis marker used to collapse ranges in pagination output. */
5
- const ELLIPSIS = "•••";
4
+ export const ELLIPSIS = "•••";
6
5
  /**
7
- * Generate a numeric range from start to end inclusive.
8
- *
9
- * @param start - Starting number (inclusive).
10
- * @param end - Ending number (inclusive).
11
- * @returns Array of numbers from start to end.
12
- */
6
+ * Generate a numeric range from start to end inclusive.
7
+ *
8
+ * @param start - Starting number (inclusive).
9
+ * @param end - Ending number (inclusive).
10
+ * @returns Array of numbers from start to end.
11
+ */
13
12
  const createRange = (start, end) => {
14
- const length = end - start + 1;
15
- return Array.from({ length }, (_, index) => start + index);
13
+ const length = end - start + 1;
14
+ return Array.from({ length }, (_, index) => start + index);
16
15
  };
17
16
  /**
18
- * Compute a pagination structure for result sets.
19
- *
20
- * Returns a mixed array of page numbers and the `ELLIPSIS` marker representing
21
- * collapsed ranges. The shape adapts to the total pages and the requested
22
- * sibling window around the current page.
23
- *
24
- * @param props - Pagination options. See {@link UsePaginationProps}.
25
- * @returns Array of page numbers and `ELLIPSIS` representing the pagination model.
26
- *
27
- * @example
28
- * ```tsx
29
- * const paginationRange = usePagination({
30
- * currentPage: 3,
31
- * resultsPerPage: 10,
32
- * siblingPagesCount: 1,
33
- * totalResults: 100
34
- * });
35
- * ```
36
- */
37
- function usePagination({ currentPage, resultsPerPage, siblingPagesCount = 1, totalResults }) {
38
- return useMemo(() => {
39
- const totalPages = Math.ceil(totalResults / Math.floor(resultsPerPage));
40
- if (totalPages <= 0) return [];
41
- /**
42
- * Case 1: If the number of pages is less than or equal to the visible page
43
- * numbers, return an array of all page numbers.
44
- */
45
- if (siblingPagesCount + 5 >= totalPages) return createRange(1, totalPages);
46
- /**
47
- * Left sibling boundary (min 1).
48
- */
49
- const leftSiblingIndex = Math.max(currentPage - siblingPagesCount, 1);
50
- /**
51
- * Right sibling boundary (max totalPages).
52
- */
53
- const rightSiblingIndex = Math.min(currentPage + siblingPagesCount, totalPages);
54
- /**
55
- * Whether a left-side ellipsis is needed.
56
- */
57
- const shouldShowLeftEllipsis = leftSiblingIndex > 2;
58
- /**
59
- * Whether a right-side ellipsis is needed.
60
- */
61
- const shouldShowRightEllipsis = rightSiblingIndex < totalPages - 2;
62
- /** First page number (always 1). */
63
- const firstPage = 1;
64
- /** Last page number (equals totalPages). */
65
- const lastPage = totalPages;
66
- /**
67
- * Case 2: No left ellipsis, but right ellipsis is necessary.
68
- * This occurs when the current page is close to the start of the
69
- * pagination range.
70
- */
71
- if (!shouldShowLeftEllipsis && shouldShowRightEllipsis) return [
72
- ...createRange(1, 3 + 2 * siblingPagesCount),
73
- ELLIPSIS,
74
- lastPage
75
- ];
76
- /**
77
- * Case 3: No right ellipsis, but left ellipsis is necessary.
78
- * This occurs when the current page is close to the end of the pagination
79
- * range.
80
- */
81
- if (shouldShowLeftEllipsis && !shouldShowRightEllipsis) return [
82
- firstPage,
83
- ELLIPSIS,
84
- ...createRange(totalPages - (3 + 2 * siblingPagesCount) + 1, totalPages)
85
- ];
86
- /**
87
- * Case 4: Both left and right ellipsis are necessary.
88
- * This occurs when the current page is far enough from both the start and
89
- * end of the pagination range.
90
- */
91
- if (shouldShowLeftEllipsis && shouldShowRightEllipsis) return [
92
- firstPage,
93
- ELLIPSIS,
94
- ...createRange(leftSiblingIndex, rightSiblingIndex),
95
- ELLIPSIS,
96
- lastPage
97
- ];
98
- return [];
99
- }, [
100
- totalResults,
101
- resultsPerPage,
102
- siblingPagesCount,
103
- currentPage
104
- ]);
17
+ * Compute a pagination structure for result sets.
18
+ *
19
+ * Returns a mixed array of page numbers and the `ELLIPSIS` marker representing
20
+ * collapsed ranges. The shape adapts to the total pages and the requested
21
+ * sibling window around the current page.
22
+ *
23
+ * @param props - Pagination options. See {@link UsePaginationProps}.
24
+ * @returns Array of page numbers and `ELLIPSIS` representing the pagination model.
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * const paginationRange = usePagination({
29
+ * currentPage: 3,
30
+ * resultsPerPage: 10,
31
+ * siblingPagesCount: 1,
32
+ * totalResults: 100
33
+ * });
34
+ * ```
35
+ */
36
+ export function usePagination({ currentPage, resultsPerPage, siblingPagesCount = 1, totalResults, }) {
37
+ return useMemo(() => {
38
+ // Total pages derived from results and page size
39
+ const totalPages = Math.ceil(totalResults / Math.floor(resultsPerPage));
40
+ // Return an empty array if there are no pages to display
41
+ if (totalPages <= 0) {
42
+ return [];
43
+ }
44
+ /**
45
+ * Total visible items including: first, last, current, siblings, and two ellipses.
46
+ */
47
+ const visiblePageNumbers = siblingPagesCount + 5;
48
+ /**
49
+ * Case 1: If the number of pages is less than or equal to the visible page
50
+ * numbers, return an array of all page numbers.
51
+ */
52
+ if (visiblePageNumbers >= totalPages) {
53
+ return createRange(1, totalPages);
54
+ }
55
+ /**
56
+ * Left sibling boundary (min 1).
57
+ */
58
+ const leftSiblingIndex = Math.max(currentPage - siblingPagesCount, 1);
59
+ /**
60
+ * Right sibling boundary (max totalPages).
61
+ */
62
+ const rightSiblingIndex = Math.min(currentPage + siblingPagesCount, totalPages);
63
+ /**
64
+ * Whether a left-side ellipsis is needed.
65
+ */
66
+ const shouldShowLeftEllipsis = leftSiblingIndex > 2;
67
+ /**
68
+ * Whether a right-side ellipsis is needed.
69
+ */
70
+ const shouldShowRightEllipsis = rightSiblingIndex < totalPages - 2;
71
+ /** First page number (always 1). */
72
+ const firstPage = 1;
73
+ /** Last page number (equals totalPages). */
74
+ const lastPage = totalPages;
75
+ /**
76
+ * Case 2: No left ellipsis, but right ellipsis is necessary.
77
+ * This occurs when the current page is close to the start of the
78
+ * pagination range.
79
+ */
80
+ if (!shouldShowLeftEllipsis && shouldShowRightEllipsis) {
81
+ // Range starting from page 1 through the right sibling window
82
+ const leftRange = createRange(1, 3 + 2 * siblingPagesCount);
83
+ return [...leftRange, ELLIPSIS, lastPage];
84
+ }
85
+ /**
86
+ * Case 3: No right ellipsis, but left ellipsis is necessary.
87
+ * This occurs when the current page is close to the end of the pagination
88
+ * range.
89
+ */
90
+ if (shouldShowLeftEllipsis && !shouldShowRightEllipsis) {
91
+ // Range ending at last page through the left sibling window
92
+ const rightRange = createRange(totalPages - (3 + 2 * siblingPagesCount) + 1, totalPages);
93
+ return [firstPage, ELLIPSIS, ...rightRange];
94
+ }
95
+ /**
96
+ * Case 4: Both left and right ellipsis are necessary.
97
+ * This occurs when the current page is far enough from both the start and
98
+ * end of the pagination range.
99
+ */
100
+ if (shouldShowLeftEllipsis && shouldShowRightEllipsis) {
101
+ // Middle window from left to right siblings
102
+ const middleRange = createRange(leftSiblingIndex, rightSiblingIndex);
103
+ return [firstPage, ELLIPSIS, ...middleRange, ELLIPSIS, lastPage];
104
+ }
105
+ return [];
106
+ }, [totalResults, resultsPerPage, siblingPagesCount, currentPage]);
105
107
  }
106
- //#endregion
107
- export { ELLIPSIS, usePagination };