@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.
- package/CHANGELOG.md +15 -0
- package/README.md +4 -0
- package/dist/components/accordion.d.ts +11 -29
- package/dist/components/accordion.d.ts.map +1 -0
- package/dist/components/accordion.js +10 -37
- package/dist/components/alert-dialog.d.ts +25 -60
- package/dist/components/alert-dialog.d.ts.map +1 -0
- package/dist/components/alert-dialog.js +16 -69
- package/dist/components/alert.d.ts +16 -27
- package/dist/components/alert.d.ts.map +1 -0
- package/dist/components/alert.js +22 -30
- package/dist/components/aspect-ratio.d.ts +5 -8
- package/dist/components/aspect-ratio.d.ts.map +1 -0
- package/dist/components/aspect-ratio.js +5 -7
- package/dist/components/avatar.d.ts +7 -17
- package/dist/components/avatar.d.ts.map +1 -0
- package/dist/components/avatar.js +7 -18
- package/dist/components/badge.d.ts +15 -21
- package/dist/components/badge.d.ts.map +1 -0
- package/dist/components/badge.js +23 -20
- package/dist/components/breadcrumb.d.ts +13 -35
- package/dist/components/breadcrumb.d.ts.map +1 -0
- package/dist/components/breadcrumb.js +13 -49
- package/dist/components/button-group.d.ts +15 -29
- package/dist/components/button-group.d.ts.map +1 -0
- package/dist/components/button-group.js +22 -27
- package/dist/components/button.d.ts +26 -35
- package/dist/components/button.d.ts.map +1 -0
- package/dist/components/button.js +43 -51
- package/dist/components/calendar.d.ts +10 -24
- package/dist/components/calendar.d.ts.map +1 -0
- package/dist/components/calendar.js +75 -92
- package/dist/components/card.d.ts +11 -34
- package/dist/components/card.d.ts.map +1 -0
- package/dist/components/card.js +11 -39
- package/dist/components/carousel.d.ts +25 -55
- package/dist/components/carousel.d.ts.map +1 -0
- package/dist/components/carousel.js +82 -114
- package/dist/components/chart.d.ts +37 -72
- package/dist/components/chart.d.ts.map +1 -0
- package/dist/components/chart.js +161 -150
- package/dist/components/checkbox-cards.d.ts +9 -15
- package/dist/components/checkbox-cards.d.ts.map +1 -0
- package/dist/components/checkbox-cards.js +8 -22
- package/dist/components/checkbox-group.d.ts +9 -16
- package/dist/components/checkbox-group.d.ts.map +1 -0
- package/dist/components/checkbox-group.js +7 -19
- package/dist/components/checkbox.d.ts +5 -9
- package/dist/components/checkbox.d.ts.map +1 -0
- package/dist/components/checkbox.js +6 -14
- package/dist/components/collapsible.d.ts +7 -14
- package/dist/components/collapsible.d.ts.map +1 -0
- package/dist/components/collapsible.js +7 -15
- package/dist/components/command.d.ts +24 -55
- package/dist/components/command.d.ts.map +1 -0
- package/dist/components/command.js +18 -69
- package/dist/components/context-menu.d.ts +23 -72
- package/dist/components/context-menu.d.ts.map +1 -0
- package/dist/components/context-menu.js +21 -89
- package/dist/components/dialog.d.ts +23 -53
- package/dist/components/dialog.d.ts.map +1 -0
- package/dist/components/dialog.js +20 -72
- package/dist/components/drawer.d.ts +29 -60
- package/dist/components/drawer.d.ts.map +1 -0
- package/dist/components/drawer.js +16 -58
- package/dist/components/dropdown-menu.d.ts +23 -73
- package/dist/components/dropdown-menu.d.ts.map +1 -0
- package/dist/components/dropdown-menu.js +21 -96
- package/dist/components/empty.d.ts +16 -37
- package/dist/components/empty.d.ts.map +1 -0
- package/dist/components/empty.js +24 -44
- package/dist/components/field.d.ts +26 -63
- package/dist/components/field.d.ts.map +1 -0
- package/dist/components/field.js +46 -89
- package/dist/components/form.d.ts +16 -37
- package/dist/components/form.d.ts.map +1 -0
- package/dist/components/form.js +50 -66
- package/dist/components/hover-card.d.ts +8 -21
- package/dist/components/hover-card.d.ts.map +1 -0
- package/dist/components/hover-card.js +8 -23
- package/dist/components/input-group.d.ts +29 -53
- package/dist/components/input-group.d.ts.map +1 -0
- package/dist/components/input-group.js +59 -66
- package/dist/components/input-number.d.ts +8 -28
- package/dist/components/input-number.d.ts.map +1 -0
- package/dist/components/input-number.js +14 -55
- package/dist/components/input-otp.d.ts +11 -24
- package/dist/components/input-otp.d.ts.map +1 -0
- package/dist/components/input-otp.js +13 -34
- package/dist/components/input-password.d.ts +6 -12
- package/dist/components/input-password.d.ts.map +1 -0
- package/dist/components/input-password.js +10 -31
- package/dist/components/input-search.d.ts +9 -18
- package/dist/components/input-search.d.ts.map +1 -0
- package/dist/components/input-search.js +16 -44
- package/dist/components/input.d.ts +5 -10
- package/dist/components/input.d.ts.map +1 -0
- package/dist/components/input.js +5 -9
- package/dist/components/item.d.ts +33 -69
- package/dist/components/item.d.ts.map +1 -0
- package/dist/components/item.js +47 -90
- package/dist/components/kbd.d.ts +6 -13
- package/dist/components/kbd.d.ts.map +1 -0
- package/dist/components/kbd.js +6 -13
- package/dist/components/label.d.ts +5 -9
- package/dist/components/label.d.ts.map +1 -0
- package/dist/components/label.js +5 -8
- package/dist/components/menubar.d.ts +24 -80
- package/dist/components/menubar.d.ts.map +1 -0
- package/dist/components/menubar.js +23 -99
- package/dist/components/native-select.d.ts +7 -16
- package/dist/components/native-select.d.ts.map +1 -0
- package/dist/components/native-select.js +7 -25
- package/dist/components/navigation-menu.d.ts +13 -36
- package/dist/components/navigation-menu.d.ts.map +1 -0
- package/dist/components/navigation-menu.js +25 -61
- package/dist/components/pagination.d.ts +15 -35
- package/dist/components/pagination.d.ts.map +1 -0
- package/dist/components/pagination.js +13 -55
- package/dist/components/popover.d.ts +9 -24
- package/dist/components/popover.d.ts.map +1 -0
- package/dist/components/popover.js +9 -27
- package/dist/components/progress-circle.d.ts +89 -106
- package/dist/components/progress-circle.d.ts.map +1 -0
- package/dist/components/progress-circle.js +70 -103
- package/dist/components/progress.d.ts +5 -10
- package/dist/components/progress.d.ts.map +1 -0
- package/dist/components/progress.js +7 -13
- package/dist/components/radio-cards.d.ts +5 -10
- package/dist/components/radio-cards.d.ts.map +1 -0
- package/dist/components/radio-cards.js +7 -21
- package/dist/components/radio-group.d.ts +6 -13
- package/dist/components/radio-group.d.ts.map +1 -0
- package/dist/components/radio-group.js +6 -17
- package/dist/components/radio.d.ts +6 -12
- package/dist/components/radio.d.ts.map +1 -0
- package/dist/components/radio.js +5 -10
- package/dist/components/resizable.d.ts +8 -18
- package/dist/components/resizable.d.ts.map +1 -0
- package/dist/components/resizable.js +7 -21
- package/dist/components/scroll-area.d.ts +21 -33
- package/dist/components/scroll-area.d.ts.map +1 -0
- package/dist/components/scroll-area.js +64 -84
- package/dist/components/select.d.ts +17 -50
- package/dist/components/select.d.ts.map +1 -0
- package/dist/components/select.js +26 -78
- package/dist/components/separator.d.ts +20 -29
- package/dist/components/separator.d.ts.map +1 -0
- package/dist/components/separator.js +27 -36
- package/dist/components/sheet.d.ts +30 -62
- package/dist/components/sheet.d.ts.map +1 -0
- package/dist/components/sheet.js +36 -80
- package/dist/components/sidebar.d.ts +67 -160
- package/dist/components/sidebar.d.ts.map +1 -0
- package/dist/components/sidebar.js +166 -301
- package/dist/components/skeleton.d.ts +5 -9
- package/dist/components/skeleton.d.ts.map +1 -0
- package/dist/components/skeleton.js +5 -8
- package/dist/components/slider.d.ts +5 -13
- package/dist/components/slider.d.ts.map +1 -0
- package/dist/components/slider.js +12 -34
- package/dist/components/sonner.d.ts +8 -10
- package/dist/components/sonner.d.ts.map +1 -0
- package/dist/components/sonner.js +13 -16
- package/dist/components/spinner.d.ts +6 -12
- package/dist/components/spinner.d.ts.map +1 -0
- package/dist/components/spinner.js +20 -33
- package/dist/components/switch.d.ts +5 -9
- package/dist/components/switch.d.ts.map +1 -0
- package/dist/components/switch.js +5 -12
- package/dist/components/table.d.ts +12 -37
- package/dist/components/table.d.ts.map +1 -0
- package/dist/components/table.js +12 -47
- package/dist/components/tabs.d.ts +8 -21
- package/dist/components/tabs.d.ts.map +1 -0
- package/dist/components/tabs.js +8 -23
- package/dist/components/textarea.d.ts +5 -9
- package/dist/components/textarea.d.ts.map +1 -0
- package/dist/components/textarea.js +5 -8
- package/dist/components/toggle-group.d.ts +10 -24
- package/dist/components/toggle-group.d.ts.map +1 -0
- package/dist/components/toggle-group.js +20 -38
- package/dist/components/toggle.d.ts +18 -25
- package/dist/components/toggle.d.ts.map +1 -0
- package/dist/components/toggle.js +24 -29
- package/dist/components/tooltip.d.ts +9 -24
- package/dist/components/tooltip.d.ts.map +1 -0
- package/dist/components/tooltip.js +9 -28
- package/dist/hooks/use-animated-value.d.ts +2 -4
- package/dist/hooks/use-animated-value.d.ts.map +1 -0
- package/dist/hooks/use-animated-value.js +67 -58
- package/dist/hooks/use-copy-to-clipboard.d.ts +6 -11
- package/dist/hooks/use-copy-to-clipboard.d.ts.map +1 -0
- package/dist/hooks/use-copy-to-clipboard.js +42 -39
- package/dist/hooks/use-is-mobile.d.ts +2 -4
- package/dist/hooks/use-is-mobile.d.ts.map +1 -0
- package/dist/hooks/use-is-mobile.js +20 -23
- package/dist/hooks/use-media-query.d.ts +2 -4
- package/dist/hooks/use-media-query.d.ts.map +1 -0
- package/dist/hooks/use-media-query.js +49 -46
- package/dist/hooks/use-mutation-observer.d.ts +3 -6
- package/dist/hooks/use-mutation-observer.d.ts.map +1 -0
- package/dist/hooks/use-mutation-observer.js +33 -34
- package/dist/hooks/use-pagination.d.ts +15 -22
- package/dist/hooks/use-pagination.d.ts.map +1 -0
- package/dist/hooks/use-pagination.js +99 -99
- package/dist/index.d.ts +131 -69
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -69
- package/dist/primitives/checkbox-group.d.ts +84 -105
- package/dist/primitives/checkbox-group.d.ts.map +1 -0
- package/dist/primitives/checkbox-group.js +91 -105
- package/dist/primitives/input-number.d.ts +41 -51
- package/dist/primitives/input-number.d.ts.map +1 -0
- package/dist/primitives/input-number.js +417 -473
- package/dist/primitives/input.d.ts +43 -48
- package/dist/primitives/input.d.ts.map +1 -0
- package/dist/primitives/input.js +67 -66
- package/dist/primitives/progress-circle.d.ts +49 -79
- package/dist/primitives/progress-circle.d.ts.map +1 -0
- package/dist/primitives/progress-circle.js +134 -149
- package/package.json +17 -10
- package/{dist → src}/css/preset.css +1 -1
- /package/{dist → src}/css/amber.css +0 -0
- /package/{dist → src}/css/blue.css +0 -0
- /package/{dist → src}/css/cyan.css +0 -0
- /package/{dist → src}/css/emerald.css +0 -0
- /package/{dist → src}/css/fuchsia.css +0 -0
- /package/{dist → src}/css/gray.css +0 -0
- /package/{dist → src}/css/green.css +0 -0
- /package/{dist → src}/css/indigo.css +0 -0
- /package/{dist → src}/css/lime.css +0 -0
- /package/{dist → src}/css/neutral.css +0 -0
- /package/{dist → src}/css/orange.css +0 -0
- /package/{dist → src}/css/pink.css +0 -0
- /package/{dist → src}/css/purple.css +0 -0
- /package/{dist → src}/css/red.css +0 -0
- /package/{dist → src}/css/rose.css +0 -0
- /package/{dist → src}/css/sky.css +0 -0
- /package/{dist → src}/css/slate.css +0 -0
- /package/{dist → src}/css/stone.css +0 -0
- /package/{dist → src}/css/style.css +0 -0
- /package/{dist → src}/css/teal.css +0 -0
- /package/{dist → src}/css/violet.css +0 -0
- /package/{dist → src}/css/yellow.css +0 -0
- /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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
//#
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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
|
-
|
|
15
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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 };
|