diff --git a/src/components/FileTree.tsx b/src/components/FileTree.tsx index 4b51e28..1684a61 100644 --- a/src/components/FileTree.tsx +++ b/src/components/FileTree.tsx @@ -83,9 +83,15 @@ export const HeadingNode: Component<{ const navigate = useNavigate(); const anchor = props.node.id || ""; const href = `${props.basePath}#${anchor}`; + const hasChildren = !!props.node.children; + // 默认收起,除非当前锚点在该节点内 + const [isExpanded, setIsExpanded] = createSignal(false); const handleClick = (e: MouseEvent) => { e.preventDefault(); + if (hasChildren) { + setIsExpanded(!isExpanded()); + } navigate(href); // 滚动到目标元素,考虑导航栏高度偏移 requestAnimationFrame(() => { @@ -105,13 +111,18 @@ export const HeadingNode: Component<{