Files
zhaoguiyang.site/src/layouts/AboutLayout.astro
joyzhao 8fbeec7dc8 feat(table-of-contents): implement scrollable TOC with radix-ui
- Add @radix-ui/react-scroll-area dependency
- Create scroll-area component with custom styling
- Refactor TOC layout to use scroll-area component
- Improve TOC styling and active state transitions
- Apply consistent TOC styling across blog and about layouts
2025-06-19 12:36:35 +08:00

267 lines
10 KiB
Plaintext

---
import type { MarkdownLayoutProps } from 'astro';
import { type Lang, type FrontmatterProps } from '@/types';
import { defaultLang } from '@/i18n/ui';
import GlassHeader from '@/components/GlassHeader';
import Footer from '@/components/Footer';
import "../styles/global.css";
// Use Astro's MarkdownLayoutProps for proper type safety
export type Props = MarkdownLayoutProps<FrontmatterProps>;
// Access frontmatter data correctly for markdown layouts
const { frontmatter } = Astro.props;
const { title, description } = frontmatter;
const lang = Astro.currentLocale as Lang || defaultLang;
---
<!doctype html>
<html lang={lang}>
<head>
<meta charset="UTF-8" />
<meta name="description" content={description} />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap"
rel="stylesheet"
/>
</head>
<body class="min-h-screen bg-background font-sans antialiased selection:bg-purple-500/20 selection:text-purple-500">
<!-- Enhanced background with gradient overlay -->
<div class="fixed inset-0 -z-10 h-full w-full bg-background">
<!-- Base radial gradient -->
<div class="absolute inset-0 bg-[radial-gradient(ellipse_80%_80%_at_50%_-20%,rgba(120,119,198,0.3),rgba(255,255,255,0))] dark:bg-[radial-gradient(ellipse_80%_80%_at_50%_-20%,rgba(120,119,198,0.2),rgba(0,0,0,0))]">
</div>
<!-- Additional subtle gradient for about page -->
<div class="absolute inset-0 bg-gradient-to-br from-purple-50/30 via-transparent to-blue-50/20 dark:from-purple-950/20 dark:via-transparent dark:to-blue-950/10">
</div>
</div>
<!-- Glass Header with navigation -->
<GlassHeader lang={lang} client:load />
<!-- Main content with proper spacing for fixed header -->
<div class="pt-16 sm:pt-20">
<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6 sm:py-8">
<div class="max-w-7xl mx-auto">
<div class="grid grid-cols-1 xl:grid-cols-4 gap-6 lg:gap-8">
<!-- Main Content -->
<main class="xl:col-span-3 order-2 xl:order-1">
<!-- About page header -->
<header class="mb-10">
<h1 class="text-3xl sm:text-4xl lg:text-5xl font-bold text-foreground mb-6 leading-tight tracking-tight">
✨ {title}
</h1>
{description && (
<p class="text-lg sm:text-xl text-muted-foreground mb-8 leading-relaxed max-w-4xl">
📝 {description}
</p>
)}
</header>
<!-- About page content with typography styles -->
<article class="prose prose-lg dark:prose-invert max-w-none prose-headings:scroll-mt-24 prose-headings:font-bold prose-headings:tracking-tight prose-h1:text-2xl sm:prose-h1:text-3xl prose-h2:text-xl sm:prose-h2:text-2xl prose-h3:text-lg sm:prose-h3:text-xl prose-h4:text-base sm:prose-h4:text-lg prose-p:text-base sm:prose-p:text-lg prose-p:leading-relaxed prose-p:mb-6 prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-strong:text-foreground prose-code:text-sm prose-code:px-2 prose-code:py-1 prose-code:rounded prose-pre:bg-muted prose-pre:border prose-pre:text-sm prose-blockquote:border-l-primary prose-blockquote:bg-muted/30 prose-blockquote:text-base prose-li:text-base sm:prose-li:text-lg prose-li:leading-relaxed">
<slot />
</article>
</main>
<!-- Sidebar with Table of Contents -->
<aside class="xl:col-span-1 order-1 xl:order-2">
<div class="xl:sticky xl:top-24 space-y-6 sm:space-y-8 xl:space-y-12 xl:max-h-[calc(100vh-6rem)] xl:overflow-y-auto">
<!-- Table of Contents -->
<div class="max-xl:hidden">
<div id="nav-content" class="sticky xl:w-72 w-full top-14">
<div class="flex flex-col gap-3 p-4">
<h3 class="dark:text-zinc-200 text-blacktext font-bold tracking-wide text-sm sm:text-base uppercase flex items-center">
<svg class="w-4 h-4 sm:w-5 sm:h-5 mr-2 text-purple-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h16" />
</svg>
{lang === 'zh' ? '目录' : 'Table of Contents'}
</h3>
<div class="text-neutral-500 dark:text-neutral-300 pr-4">
<ul id="toc-list" class="leading-relaxed text-sm sm:text-base border-l dark:border-neutral-500/20 border-blacktext/20 mt-4">
</ul>
</div>
</div>
</div>
</div>
</div>
</aside>
</div>
</div>
</div>
</div>
<!-- Footer -->
<Footer lang={lang} client:load />
</body>
</html>
<script>
/**
* Initialize table of contents functionality
* Extracts headings from article content and creates navigation links
*/
document.addEventListener("DOMContentLoaded", function () {
const tocList = document.getElementById("toc-list");
const content = document.querySelector("article");
if (!tocList || !content) return;
// Extract h1, h2, h3, h4 headings from article content
const headers = content.querySelectorAll("h1, h2, h3, h4");
let currentUl = tocList;
/**
* Process each heading and create corresponding TOC entry
*/
headers.forEach((header, index) => {
// Generate ID if not present
if (!header.id) {
header.id = header.textContent?.trim().toLowerCase().replace(/\s+/g, "-") + "-" + index;
}
const li = document.createElement("li");
const link = document.createElement("a");
link.href = `#${header.id}`;
link.textContent = header.textContent?.trim() || header.id;
// Apply styling based on heading level
const level = parseInt(header.tagName.charAt(1));
link.classList.add(
"block", "w-full", "text-left", "py-1.5", "px-3", "text-sm",
"transition-all", "duration-200", "border-l-2", "border-transparent",
"text-muted-foreground", "hover:text-foreground", "hover:bg-muted/30"
);
// Add indentation based on heading level
if (level === 1) {
link.classList.add("font-semibold");
} else if (level === 2) {
link.classList.add("ml-2");
} else if (level === 3) {
link.classList.add("ml-4");
} else if (level === 4) {
link.classList.add("ml-6");
}
li.appendChild(link);
tocList.appendChild(li);
/**
* Handle smooth scroll on link click
*/
link.addEventListener("click", function (e) {
e.preventDefault();
const targetElement = document.getElementById(header.id);
if (targetElement) {
targetElement.scrollIntoView({ behavior: "smooth", block: "start" });
}
});
});
/**
* Set up intersection observer for active section tracking
*/
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
const id = entry.target.getAttribute("id");
const link = document.querySelector(`#toc-list a[href="#${id}"]`);
if (entry.isIntersecting) {
// Remove active state from all links
document.querySelectorAll("#toc-list a").forEach((el) => {
el.classList.remove(
"bg-purple-500/10", "text-purple-500", "border-l-purple-500", "font-medium"
);
el.classList.add("text-muted-foreground", "border-transparent");
});
// Add active state to current link
if (link) {
link.classList.remove("text-muted-foreground", "border-transparent");
link.classList.add(
"bg-purple-500/10", "text-purple-500", "border-l-purple-500", "font-medium"
);
}
}
});
},
{
rootMargin: "-20% 0% -35% 0%",
threshold: 0
}
);
// Observe all headings for intersection
headers.forEach((header) => observer.observe(header));
});
</script>
<script is:inline>
const getThemePreference = () => {
if (typeof localStorage !== "undefined" && localStorage.getItem("theme")) {
return localStorage.getItem("theme");
}
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
};
const isDark = getThemePreference() === "dark";
document.documentElement.classList[isDark ? "add" : "remove"]("dark");
if (typeof localStorage !== "undefined") {
const observer = new MutationObserver(() => {
const isDark = document.documentElement.classList.contains("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
});
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class"],
});
}
</script>
<style>
/* Additional styling for smooth transitions */
#toc-list a:hover {
transform: translateX(2px);
background-color: rgba(168, 85, 247, 0.05);
}
#toc-list a:focus {
outline: 2px solid rgba(168, 85, 247, 0.3);
outline-offset: 1px;
}
#toc-list a {
transition: all 0.2s ease;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
scroll-behavior: smooth;
}
:root {
--transition-standard: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
body {
transition:
background-color var(--transition-standard),
color var(--transition-standard);
}
</style>