@ichaingo/link 1.3.99 → 1.4.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/dist/index.js +67 -38
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,49 +1,78 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx
|
|
3
|
-
import
|
|
4
|
-
import { usePathname
|
|
5
|
-
import { useLocale
|
|
6
|
-
import { useRouter
|
|
7
|
-
import { useTransition
|
|
8
|
-
import { useProgressBar
|
|
9
|
-
const
|
|
10
|
-
href
|
|
11
|
-
isOldProject
|
|
12
|
-
withLocale
|
|
13
|
-
baseUrl
|
|
14
|
-
children
|
|
15
|
-
target
|
|
16
|
-
onClick
|
|
17
|
-
prefetch
|
|
18
|
-
...
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import Link from "next/link";
|
|
4
|
+
import { usePathname } from "@ichaingo/i18n/navigation";
|
|
5
|
+
import { useLocale } from "next-intl";
|
|
6
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
7
|
+
import { useTransition, useMemo } from "react";
|
|
8
|
+
import { useProgressBar } from "@ichaingo/providers/TopProgressBarProvider";
|
|
9
|
+
const LinkBar = ({
|
|
10
|
+
href,
|
|
11
|
+
isOldProject,
|
|
12
|
+
withLocale = true,
|
|
13
|
+
baseUrl = "",
|
|
14
|
+
children,
|
|
15
|
+
target = "_self",
|
|
16
|
+
onClick,
|
|
17
|
+
prefetch = false,
|
|
18
|
+
...rest
|
|
19
19
|
}) => {
|
|
20
|
-
const
|
|
21
|
-
|
|
20
|
+
const router = useRouter();
|
|
21
|
+
const pathName = usePathname();
|
|
22
|
+
const searchParams = useSearchParams();
|
|
23
|
+
const [, startTransition] = useTransition();
|
|
24
|
+
const { start } = useProgressBar();
|
|
25
|
+
const locale = useLocale();
|
|
26
|
+
const isExternalLink = (url) => {
|
|
27
|
+
if (baseUrl.includes(".ichaingo.com") || url.includes(".ichaingo.com")) return false;
|
|
28
|
+
if (baseUrl === "" && !/^(https?:\/\/|\/\/)/.test(url)) return false;
|
|
29
|
+
return /^(https?:\/\/|\/\/)/.test(url);
|
|
30
|
+
};
|
|
31
|
+
const isCurrentPage = (url) => {
|
|
32
|
+
return url.replace(`/${locale}`, "") === pathName.replace(`/${locale}`, "") + searchParams.toString();
|
|
33
|
+
};
|
|
34
|
+
const handleClick = (e, href2) => {
|
|
35
|
+
onClick == null ? void 0 : onClick(e);
|
|
36
|
+
e.stopPropagation();
|
|
37
|
+
e.preventDefault();
|
|
38
|
+
if (isCurrentPage(href2)) {
|
|
22
39
|
window.location.reload();
|
|
23
40
|
return;
|
|
24
41
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
42
|
+
if (isOldProject || isExternalLink(href2)) {
|
|
43
|
+
window.open(href2, target === "_blank" || isExternalLink(href2) ? "_blank" : target);
|
|
44
|
+
} else {
|
|
45
|
+
start();
|
|
46
|
+
startTransition(() => {
|
|
47
|
+
router.push(href2);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const friendlyHerf = useMemo(() => {
|
|
52
|
+
if (isExternalLink(href)) return href;
|
|
53
|
+
const localePrefix = `/${locale}`;
|
|
54
|
+
if (href === localePrefix || href.startsWith(`${localePrefix}/`) || !withLocale) {
|
|
55
|
+
return baseUrl + href;
|
|
56
|
+
}
|
|
57
|
+
if (href.startsWith("/")) {
|
|
58
|
+
return `${baseUrl}${localePrefix}${href}`;
|
|
59
|
+
}
|
|
60
|
+
return href;
|
|
61
|
+
}, [href, locale, baseUrl]);
|
|
62
|
+
return /* @__PURE__ */ jsx(
|
|
63
|
+
Link,
|
|
35
64
|
{
|
|
36
|
-
suppressHydrationWarning:
|
|
37
|
-
prefetch
|
|
38
|
-
href:
|
|
39
|
-
target:
|
|
40
|
-
onClick: (
|
|
41
|
-
rel:
|
|
42
|
-
...
|
|
43
|
-
children
|
|
65
|
+
suppressHydrationWarning: true,
|
|
66
|
+
prefetch,
|
|
67
|
+
href: friendlyHerf,
|
|
68
|
+
target: isExternalLink(href) ? "_blank" : target,
|
|
69
|
+
onClick: (e) => handleClick(e, friendlyHerf),
|
|
70
|
+
rel: isExternalLink(href) ? "noopener noreferrer" : void 0,
|
|
71
|
+
...rest,
|
|
72
|
+
children
|
|
44
73
|
}
|
|
45
74
|
);
|
|
46
75
|
};
|
|
47
76
|
export {
|
|
48
|
-
|
|
77
|
+
LinkBar as default
|
|
49
78
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/link",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"next": "^15.4.5",
|
|
21
21
|
"next-intl": "^4.3.4",
|
|
22
22
|
"react": "^19.1.0",
|
|
23
|
-
"@ichaingo/
|
|
24
|
-
"@ichaingo/
|
|
23
|
+
"@ichaingo/i18n": "1.4.0",
|
|
24
|
+
"@ichaingo/providers": "1.4.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"rollup-plugin-preserve-use-client": "^3.0.1"
|