kitzo 2.1.11 → 2.1.13
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 +1 -1
- package/dist/react.d.ts +29 -3
- package/dist/react.esm.js +70 -1
- package/package.json +1 -1
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.
|
|
29
|
+
<script src="https://cdn.jsdelivr.net/npm/kitzo@2.1.13/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,7 +1,13 @@
|
|
|
1
|
-
import type { ReactNode, CSSProperties, JSX, PropsWithChildren } from 'react';
|
|
1
|
+
import type { ReactNode, CSSProperties, JSX, PropsWithChildren, RefObject } from 'react';
|
|
2
2
|
|
|
3
3
|
//! Toast API types declaration
|
|
4
|
-
export type Positions =
|
|
4
|
+
export type Positions =
|
|
5
|
+
| 'top-left'
|
|
6
|
+
| 'top-center'
|
|
7
|
+
| 'top-right'
|
|
8
|
+
| 'bottom-left'
|
|
9
|
+
| 'bottom-center'
|
|
10
|
+
| 'bottom-right';
|
|
5
11
|
|
|
6
12
|
export interface ToastOptions {
|
|
7
13
|
position?: Positions;
|
|
@@ -29,7 +35,10 @@ export interface ToastAPI {
|
|
|
29
35
|
options?: ToastOptions,
|
|
30
36
|
): void;
|
|
31
37
|
|
|
32
|
-
custom(
|
|
38
|
+
custom(
|
|
39
|
+
content: CustomContent,
|
|
40
|
+
options?: { duration?: number; exitDelay?: number; position?: Positions },
|
|
41
|
+
): void;
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
export declare const toast: ToastAPI;
|
|
@@ -47,3 +56,20 @@ export interface TooltipCoreProps {
|
|
|
47
56
|
export type TooltipProps = PropsWithChildren<TooltipCoreProps>;
|
|
48
57
|
|
|
49
58
|
export declare function Tooltip(props: TooltipProps): JSX.Element;
|
|
59
|
+
|
|
60
|
+
//! useScrollRestoration hook types declaration
|
|
61
|
+
type ScrollHookOptions = {
|
|
62
|
+
behavior?: 'auto' | 'smooth' | 'instant';
|
|
63
|
+
delay?: number;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type LocationLike = {
|
|
67
|
+
pathname: string;
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export declare function useScrollRestoration<T extends HTMLElement = HTMLElement>(
|
|
72
|
+
path: LocationLike | string,
|
|
73
|
+
key: string | number,
|
|
74
|
+
options?: ScrollHookOptions,
|
|
75
|
+
): RefObject<T | 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
|
-
|
|
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 };
|