Skip to content

Commit 57cc2a6

Browse files
committed
fixes bug & adds heading focusing
1 parent f93f2b8 commit 57cc2a6

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/app/[category]/[slug]/page.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export default function Page({ params }: { params: { category: string; slug: str
2222
);
2323
return (
2424
<div className={styles.pageWrapper}>
25-
<div className={styles.tocWrapper}>
26-
<TOC tocContent={tocContent} style={{ visibility: "hidden" }} className={styles.toc} />
27-
</div>
25+
<div className={styles.tocWrapper} />
2826
<div className={`${styles.postWrapper} container`}>
2927
<div className={styles.postHeader}>
3028
<div className={styles.postTitle} id="toc-title">

src/app/about/page.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ export default function AboutPage() {
2727
);
2828
return (
2929
<div className={styles.pageWrapper}>
30-
<div className={styles.tocWrapper}>
31-
<TOC tocContent={tocContent} style={{ visibility: "hidden" }} className={styles.toc} />
32-
</div>
30+
<div className={styles.tocWrapper} />
3331
<div className={`${styles.postWrapper} container`}>
3432
<div className={styles.header}>
3533
<div className={styles.titleContainer}>
36-
<div className={styles.title} id="toc-title">{frontMatter.title}</div>
34+
<div className={styles.title} id="toc-title">
35+
{frontMatter.title}
36+
</div>
3737
<div className={styles.description}>{frontMatter.description}</div>
3838
<div className={styles.postTime}>
3939
<span>

src/components/TOC.module.css

+4-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
display: none;
3939
}
4040

41+
.active {
42+
font-weight: 600;
43+
}
44+
4145
.tocWrapper > * {
4246
display: block;
4347
}
@@ -74,10 +78,6 @@
7478
margin: 0 8px;
7579
}
7680

77-
.toc_wrapper ul {
78-
padding-left: 16px;
79-
}
80-
8181
.toc_h1,
8282
.toc_h2,
8383
.toc_h3,
@@ -102,10 +102,6 @@
102102
text-decoration: underline;
103103
}
104104

105-
.active {
106-
font-weight: 600;
107-
}
108-
109105
.toc_h2 {
110106
margin-left: 12px;
111107
}

src/components/TOC.tsx

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import React, { useEffect, useState } from "react";
3+
import React, { useEffect, useState, useRef } from "react";
44
import Link from "next/link";
55
import { Itoc } from "@/interfaces/Post";
66
import styles from "./TOC.module.css";
@@ -9,6 +9,7 @@ const TOC = ({ tocContent, ...props }: { tocContent: Array<Itoc> } & React.HTMLA
99
const stylesList = [styles.toc_h1, styles.toc_h2, styles.toc_h3, styles.toc_h4, styles.toc_h5];
1010
const { className, ...rest } = props;
1111
const [activeId, setActiveId] = useState<string | null>(null);
12+
const containerRef = useRef<HTMLDivElement>(null)
1213

1314
useEffect(() => {
1415
const observer = new IntersectionObserver(
@@ -19,8 +20,8 @@ const TOC = ({ tocContent, ...props }: { tocContent: Array<Itoc> } & React.HTMLA
1920
},
2021
{
2122
root: null,
22-
rootMargin: "0px 0px -80% 0px",
23-
threshold: 1.0,
23+
rootMargin: "0px 0px -50% 0px",
24+
threshold: 0,
2425
},
2526
);
2627
const headingElements = tocContent.map((toc) => document.getElementById(handleSlug(toc.slug)));
@@ -34,8 +35,26 @@ const TOC = ({ tocContent, ...props }: { tocContent: Array<Itoc> } & React.HTMLA
3435
};
3536
}, [tocContent]);
3637

38+
useEffect(() => {
39+
if (containerRef.current && activeId) {
40+
const element = document.getElementsByClassName(styles.active)[0] as HTMLElement;
41+
if (element) {
42+
const container = containerRef.current;
43+
const containerHeight = container.clientHeight;
44+
const elementOffsetTop = element.offsetTop;
45+
const elementHeight = element.clientHeight;
46+
47+
const scrollTop = elementOffsetTop - (containerHeight / 2) + (elementHeight / 2);
48+
container.scrollTo({
49+
top: scrollTop,
50+
behavior: "smooth",
51+
});
52+
}
53+
}
54+
}, [activeId]);
55+
3756
return (
38-
<div className={`${styles.tocWrapper} ${className || ""}`} {...rest}>
57+
<div className={`${styles.tocWrapper} ${className || ""}`} ref={containerRef} {...rest}>
3958
<div className={styles.title}>目录</div>
4059
{tocContent.map((value, index) => {
4160
const slug = handleSlug(value.slug);

0 commit comments

Comments
 (0)