Skip to content

Commit 02e5a3e

Browse files
committed
优化自动跳转组件
1 parent 032e709 commit 02e5a3e

File tree

2 files changed

+82
-38
lines changed

2 files changed

+82
-38
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from "react";
2+
3+
export type Props = {
4+
delay: number;
5+
text: string;
6+
href: string;
7+
};
8+
9+
const AutoRedirect: React.FC<Props> = ({ delay, text, href }) => {
10+
const [visible, setVisible] = React.useState(true);
11+
const [countdown, setCountdown] = React.useState<number>(delay);
12+
13+
const timerRef = React.useRef<number | null>(null);
14+
15+
React.useEffect(() => {
16+
if (delay > 0) {
17+
timerRef.current = window.setInterval(() => {
18+
setCountdown((prev) => {
19+
if (prev <= 1) {
20+
clear();
21+
location.href = href;
22+
return 0;
23+
}
24+
return prev - 1;
25+
});
26+
}, 1000);
27+
return clear;
28+
}
29+
}, []);
30+
31+
const clear = () => {
32+
if (timerRef.current !== null) {
33+
clearInterval(timerRef.current);
34+
timerRef.current = null;
35+
}
36+
};
37+
38+
const cancel = () => {
39+
clear();
40+
setVisible(false);
41+
};
42+
43+
if (!visible || delay <= 0) {
44+
return null;
45+
}
46+
47+
return (
48+
<div
49+
style={{
50+
padding: "0.75em 1em",
51+
border: "1px solid var(--sl-color-orange)",
52+
backgroundColor: "var(--sl-color-orange-low)",
53+
}}
54+
>
55+
{"页面将在 "}
56+
<span id="auto-redirect-countdown">{countdown}</span>
57+
{" 秒后自动跳转到"}
58+
<a href={href}>{text}</a>
59+
{",您也可以手动"}
60+
<a
61+
style={{
62+
cursor: "pointer",
63+
color: "-webkit-link",
64+
textDecoration: "underline",
65+
}}
66+
onClick={cancel}
67+
>
68+
{"取消跳转"}
69+
</a>
70+
71+
</div>
72+
);
73+
};
74+
75+
export default AutoRedirect;
Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
import HitsInfo from "@components/react/HitsInfo";
3-
import { Aside } from "@astrojs/starlight/components";
3+
import AutoRedirect from "@components/react/AutoRedirect";
44
import DefaultPageTitle from "@astrojs/starlight/components/PageTitle.astro";
55
66
let tag = Astro.url.href;
@@ -10,25 +10,16 @@ if (!tag.endsWith("/") && !tag.endsWith(".html")) {
1010
1111
const { autoRedirect, contributors } = Astro.locals.starlightRoute.entry.data;
1212
const showContributors = Array.isArray(contributors) && contributors.length > 0;
13-
14-
const clientVars = autoRedirect ?? { delay: 0, href: "" };
1513
---
1614

1715
{
1816
autoRedirect && (
19-
<div id="auto-redirect">
20-
<Aside type="tip">
21-
{"页面将在 "}
22-
<span id="auto-redirect-countdown">{autoRedirect.delay}</span>
23-
{" 秒后自动跳转到"}
24-
<a href={autoRedirect.href}>{autoRedirect.text}</a>
25-
{",您也可以手动"}
26-
<a href="javascript:;" id="auto-redirect-cancel">
27-
{"取消跳转"}
28-
</a>
29-
30-
</Aside>
31-
</div>
17+
<AutoRedirect
18+
client:only="react"
19+
delay={autoRedirect.delay}
20+
text={autoRedirect.text}
21+
href={autoRedirect.href}
22+
/>
3223
)
3324
}
3425

@@ -55,25 +46,3 @@ const clientVars = autoRedirect ?? { delay: 0, href: "" };
5546
)
5647
}
5748
</div>
58-
59-
<script is:inline define:vars={clientVars}>
60-
const autoRedirect = document.getElementById("auto-redirect");
61-
if (!autoRedirect) return;
62-
const autoRedirectCountdown = document.getElementById("auto-redirect-countdown");
63-
if (autoRedirectCountdown && delay > 0) {
64-
let countdown = delay;
65-
const autoRedirectCancel = document.getElementById("auto-redirect-cancel");
66-
const interval = setInterval(() => {
67-
if (countdown > 0) {
68-
autoRedirectCountdown.textContent = String(--countdown);
69-
} else {
70-
clearInterval(interval);
71-
location.href = href;
72-
}
73-
}, 1000);
74-
autoRedirectCancel.addEventListener("click", () => {
75-
clearInterval(interval);
76-
autoRedirect.style.display = "none";
77-
});
78-
}
79-
</script>

0 commit comments

Comments
 (0)