@pushwoosh/smart-components-kit 1.2.0 → 1.2.2
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/hooks/index.d.ts +1 -0
- package/hooks/index.js +1 -0
- package/hooks/useBack.d.ts +9 -0
- package/hooks/useBack.js +26 -0
- package/package.json +3 -3
package/hooks/index.d.ts
CHANGED
package/hooks/index.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a "back" handler that navigates to `pathname`.
|
|
3
|
+
*
|
|
4
|
+
* - if the previous page in the navigation stack matches `pathname`
|
|
5
|
+
* (only the path is compared, without the query and hash) — calls `history.goBack()`,
|
|
6
|
+
* which preserves the scroll position and the state of the previous page;
|
|
7
|
+
* - otherwise — navigates via `history.push(pathname)`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useBack(pathname: string): () => void;
|
package/hooks/useBack.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useHistory } from 'react-router-dom';
|
|
3
|
+
import { readNavigationHistory } from '@pushwoosh/kit-helpers';
|
|
4
|
+
const getPathname = entry => entry.split(/[?#]/)[0];
|
|
5
|
+
/**
|
|
6
|
+
* Returns a "back" handler that navigates to `pathname`.
|
|
7
|
+
*
|
|
8
|
+
* - if the previous page in the navigation stack matches `pathname`
|
|
9
|
+
* (only the path is compared, without the query and hash) — calls `history.goBack()`,
|
|
10
|
+
* which preserves the scroll position and the state of the previous page;
|
|
11
|
+
* - otherwise — navigates via `history.push(pathname)`.
|
|
12
|
+
*/
|
|
13
|
+
export function useBack(pathname) {
|
|
14
|
+
const history = useHistory();
|
|
15
|
+
return useCallback(() => {
|
|
16
|
+
const targetPathname = getPathname(pathname);
|
|
17
|
+
const entries = readNavigationHistory();
|
|
18
|
+
const previousEntry = entries[entries.length - 2];
|
|
19
|
+
const previousPathname = previousEntry ? getPathname(previousEntry) : undefined;
|
|
20
|
+
if (previousPathname === targetPathname) {
|
|
21
|
+
history.goBack();
|
|
22
|
+
} else {
|
|
23
|
+
history.push(pathname);
|
|
24
|
+
}
|
|
25
|
+
}, [pathname, history]);
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@floating-ui/react-dom": "^2.1.2",
|
|
24
24
|
"@pushwoosh/dumb-components": "^1.1.140",
|
|
25
25
|
"@pushwoosh/kit-constants": "^1.6.9",
|
|
26
|
-
"@pushwoosh/kit-helpers": "^4.11.
|
|
26
|
+
"@pushwoosh/kit-helpers": "^4.11.1",
|
|
27
27
|
"@pushwoosh/kit-icons": "^2.1.6",
|
|
28
28
|
"@pushwoosh/kit-typography": "^1.7.3",
|
|
29
29
|
"@pushwoosh/rpc-v2-http-api-data": "^0.2.7",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"react-router-dom": "^5.3.4"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@pushwoosh/frontend-builder-engine": "^2.1.
|
|
36
|
+
"@pushwoosh/frontend-builder-engine": "^2.1.1",
|
|
37
37
|
"@svgr/webpack": "^8.1.0",
|
|
38
38
|
"@types/react": "^18.3.5",
|
|
39
39
|
"@types/react-dom": "^18.3.0",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"react": "^16.14.0 || ^18.0.0",
|
|
49
49
|
"react-dom": "^16.14.0 || ^18.0.0"
|
|
50
50
|
},
|
|
51
|
-
"version": "1.2.
|
|
51
|
+
"version": "1.2.2"
|
|
52
52
|
}
|