Fix duplicate imports in post TOC

This commit is contained in:
2025-11-18 23:31:52 +08:00
parent e73f37da76
commit 1e39647ab6

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { useEffect, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faListUl, faCircle } from '@fortawesome/free-solid-svg-icons'; import { faListUl, faCircle } from '@fortawesome/free-solid-svg-icons';
@@ -13,6 +13,9 @@ interface TocItem {
export function PostToc() { export function PostToc() {
const [items, setItems] = useState<TocItem[]>([]); const [items, setItems] = useState<TocItem[]>([]);
const [activeId, setActiveId] = useState<string | null>(null); const [activeId, setActiveId] = useState<string | null>(null);
const listRef = useRef<HTMLUListElement | null>(null);
const itemRefs = useRef<Record<string, HTMLLIElement | null>>({});
const [indicator, setIndicator] = useState({ top: 0, opacity: 0 });
useEffect(() => { useEffect(() => {
const headings = Array.from( const headings = Array.from(
@@ -50,6 +53,18 @@ export function PostToc() {
return () => observer.disconnect(); return () => observer.disconnect();
}, []); }, []);
useEffect(() => {
if (!activeId || !listRef.current) {
setIndicator({ top: 0, opacity: 0 });
return;
}
const activeEl = itemRefs.current[activeId];
if (!activeEl) return;
const listTop = listRef.current.getBoundingClientRect().top;
const { top, height } = activeEl.getBoundingClientRect();
setIndicator({ top: top - listTop + height / 2, opacity: 1 });
}, [activeId, items.length]);
const handleClick = (id: string) => (event: React.MouseEvent<HTMLAnchorElement>) => { const handleClick = (id: string) => (event: React.MouseEvent<HTMLAnchorElement>) => {
event.preventDefault(); event.preventDefault();
const el = document.getElementById(id); const el = document.getElementById(id);
@@ -82,24 +97,38 @@ export function PostToc() {
<FontAwesomeIcon icon={faListUl} className="h-3 w-3 text-slate-400" /> <FontAwesomeIcon icon={faListUl} className="h-3 w-3 text-slate-400" />
</div> </div>
<ul className="space-y-1"> <div className="relative pl-4">
{items.map((item) => ( <span className="absolute left-1 top-0 h-full w-px bg-slate-200 dark:bg-slate-800" aria-hidden="true" />
<li key={item.id} className={item.depth === 3 ? 'pl-3' : ''}> <span
<a className="absolute left-0 h-3 w-3 -translate-y-1/2 rounded-full bg-accent transition-all duration-200 ease-snappy"
href={`#${item.id}`} style={{ top: `${indicator.top}px`, opacity: indicator.opacity }}
onClick={handleClick(item.id)} aria-hidden="true"
className={`line-clamp-2 inline-flex items-center gap-2 hover:text-blue-600 dark:hover:text-blue-400 ${ />
item.id === activeId <ul ref={listRef} className="space-y-1">
? 'text-blue-600 dark:text-blue-400 font-semibold' {items.map((item) => (
: '' <li
}`} key={item.id}
ref={(el) => {
itemRefs.current[item.id] = el;
}}
className={item.depth === 3 ? 'pl-3' : ''}
> >
<FontAwesomeIcon icon={faCircle} className="h-1.5 w-1.5 text-slate-300" /> <a
{item.text} href={`#${item.id}`}
</a> onClick={handleClick(item.id)}
</li> className={`line-clamp-2 inline-flex items-center gap-2 hover:text-blue-600 dark:hover:text-blue-400 ${
))} item.id === activeId
</ul> ? 'text-blue-600 dark:text-blue-400 font-semibold'
: ''
}`}
>
<FontAwesomeIcon icon={faCircle} className="h-1.5 w-1.5 text-slate-300" />
{item.text}
</a>
</li>
))}
</ul>
</div>
</nav> </nav>
); );
} }