kitzo 2.1.10 → 2.1.12

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/README.md CHANGED
@@ -26,7 +26,7 @@ npm i kitzo
26
26
  or
27
27
 
28
28
  ```javascript
29
- <script src="https://cdn.jsdelivr.net/npm/kitzo@2.1.10/dist/kitzo.umd.min.js"></script>
29
+ <script src="https://cdn.jsdelivr.net/npm/kitzo@2.1.12/dist/kitzo.umd.min.js"></script>
30
30
  ```
31
31
 
32
32
  > Vanilla: Attach this script tag in the html head tag and you are good to go.
package/dist/react.d.ts CHANGED
@@ -1,8 +1,10 @@
1
- import { type ReactNode, type CSSProperties, type JSX, type PropsWithChildren } from 'react';
1
+ import type { ReactNode, CSSProperties, JSX, PropsWithChildren, RefObject } from 'react';
2
+
3
+ //! Toast API types declaration
4
+ export type Positions = 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
2
5
 
3
- // Toast API types declaration
4
6
  export interface ToastOptions {
5
- position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
7
+ position?: Positions;
6
8
  duration?: number;
7
9
  style?: CSSProperties;
8
10
  showIcon?: boolean;
@@ -12,25 +14,29 @@ export type CustomContent = string | ReactNode | ((dismiss: () => void) => React
12
14
 
13
15
  export interface ToastAPI {
14
16
  (text?: string, options?: ToastOptions): void;
17
+
15
18
  success(text?: string, options?: ToastOptions): void;
19
+
16
20
  error(text?: string, options?: ToastOptions): void;
21
+
17
22
  promise<T>(
18
23
  callback: Promise<T>,
19
24
  msgs?: {
20
25
  loading?: string;
21
- success?: string | ((res: T) => string);
22
- error?: string | ((err: Error) => string);
26
+ success?: string | ((res: T) => string | Promise<string>);
27
+ error?: string | ((err: any) => string | Promise<string>);
23
28
  },
24
29
  options?: ToastOptions,
25
- ): Promise<T>;
26
- custom(content: CustomContent, options?: { duration?: number; exitDelay?: number; position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' }): void;
30
+ ): void;
31
+
32
+ custom(content: CustomContent, options?: { duration?: number; exitDelay?: number; position?: Positions }): void;
27
33
  }
28
34
 
29
35
  export declare const toast: ToastAPI;
30
36
 
31
- export declare function ToastContainer(props: { position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'; gap?: number }): JSX.Element;
37
+ export declare function ToastContainer(props: { position?: Positions; gap?: number }): JSX.Element;
32
38
 
33
- // Tooltip API types declaration
39
+ //! Tooltip API types declaration
34
40
  export interface TooltipCoreProps {
35
41
  content: string | ReactNode;
36
42
  position?: 'top' | 'right' | 'bottom' | 'left';
@@ -41,3 +47,16 @@ export interface TooltipCoreProps {
41
47
  export type TooltipProps = PropsWithChildren<TooltipCoreProps>;
42
48
 
43
49
  export declare function Tooltip(props: TooltipProps): JSX.Element;
50
+
51
+ //! useScrollRestoration hook types declaration
52
+ type ScrollHookOptions = {
53
+ behavior?: 'auto' | 'smooth' | 'instant';
54
+ delay?: number;
55
+ };
56
+
57
+ type LocationLike = {
58
+ pathname: string;
59
+ [key: string]: any;
60
+ };
61
+
62
+ export declare function useScrollRestoration(path: LocationLike | string, key: string | number, options?: ScrollHookOptions): RefObject<HTMLElement | null>;
package/dist/react.esm.js CHANGED
@@ -690,4 +690,73 @@ function Tooltip({
690
690
  }, content) : /*#__PURE__*/React.createElement(React.Fragment, null, content)));
691
691
  }
692
692
 
693
- export { ToastContainer, Tooltip, toast };
693
+ function useScrollRestoration(path, key, options = {}) {
694
+ if (!path || typeof path !== 'object' && typeof path !== 'string') {
695
+ throw new Error('kitzo/react: useScrollRestoration(path, ...) expect location object from the react useLocation hook or unique path string(location.pathname)');
696
+ }
697
+ if (!key || typeof key !== 'string' && typeof key !== 'number') {
698
+ throw new Error('kitzo/react: useScrollRestoration(..., key) expect unique string or number');
699
+ }
700
+ const pathname = typeof path === 'object' ? path.pathname : path;
701
+ const behavior = options?.behavior ? options.behavior : 'instant';
702
+ const delay = options?.delay ? isNaN(Number(options.delay)) ? 0 : Number(options.delay) : 0;
703
+
704
+ // hook management
705
+ const ref = useRef(null);
706
+ const history = useRef(new Map());
707
+
708
+ // hook logic
709
+ useEffect(() => {
710
+ const element = ref.current;
711
+ const target = element || window;
712
+ const isWindow = target === window;
713
+ const mapKey = `${pathname}::${key}`;
714
+ const saved = history.current.get(mapKey);
715
+ const restore = () => {
716
+ if (!saved) return;
717
+ if (isWindow) {
718
+ window.scrollTo({
719
+ top: saved.top,
720
+ left: saved.left,
721
+ behavior
722
+ });
723
+ } else if (element) {
724
+ element.scrollTo({
725
+ top: saved.top,
726
+ left: saved.left,
727
+ behavior
728
+ });
729
+ }
730
+ };
731
+ if (saved) {
732
+ if (delay > 0) {
733
+ const timeoutId = setTimeout(restore, delay);
734
+ return () => clearTimeout(timeoutId);
735
+ } else {
736
+ restore();
737
+ }
738
+ }
739
+ let timeoutId = null;
740
+ const save = () => {
741
+ clearTimeout(timeoutId);
742
+ timeoutId = setTimeout(() => {
743
+ const top = isWindow ? window.scrollY : element?.scrollTop ?? 0;
744
+ const left = isWindow ? window.scrollX : element?.scrollLeft ?? 0;
745
+ history.current.set(mapKey, {
746
+ top,
747
+ left
748
+ });
749
+ }, 50);
750
+ };
751
+ target.addEventListener('scroll', save, {
752
+ passive: true
753
+ });
754
+ return () => {
755
+ target.removeEventListener('scroll', save);
756
+ clearTimeout(timeoutId);
757
+ };
758
+ }, [pathname, key, behavior, delay]);
759
+ return ref;
760
+ }
761
+
762
+ export { ToastContainer, Tooltip, toast, useScrollRestoration };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.1.10",
3
+ "version": "2.1.12",
4
4
  "description": "A lightweight JavaScript UI micro-library.",
5
5
  "type": "module",
6
6
  "main": "./dist/kitzo.umd.js",