◉ClassyGitshadowzzh

TopBar.astro

TopBar.astro
---
// src/components/TopBar.astro
import ThemeToggle from './ThemeToggle.astro';
import type { ModuleEntry } from '../utils/collection';

interface Props { entry?: ModuleEntry; }
const { entry } = Astro.props;
const title = entry
  ? `${entry.data.module} / ${entry.data.title}${entry.data.variant === 'python' ? ' · Python' : ''}`
  : 'Pi Agent Book';
---

<header class="topbar">
  <div class="left-cluster">
    <button class="hamburger" aria-label="打开目录" aria-expanded="false">☰</button>
    <a href="/" class="brand">Pi Agent <span class="brand-accent">·</span> Book</a>
  </div>
  <nav class="crumb" aria-label="breadcrumb">{title}</nav>
  <div class="actions">
    <button
      class="immersive-toggle"
      type="button"
      data-immersive-toggle
      aria-pressed="false"
      aria-label="切换沉浸式阅读"
      title="沉浸式阅读 (F)"
    >
      <svg width="16" height="16" viewBox="0 0 20 20" fill="none" aria-hidden="true">
        <path d="M3 4.5h5v11H3zM12 4.5h5v11h-5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
        <path d="M7.5 10h5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-dasharray="1.5 2"/>
      </svg>
      <span class="immersive-toggle-label">沉浸式阅读</span>
    </button>
    <ThemeToggle />
  </div>
</header>

<script>
  // Hamburger toggle: open/close the left TOC drawer on mobile.
  const ham = document.querySelector('.hamburger');
  ham?.addEventListener('click', () => {
    document.body.classList.toggle('toc-open');
    ham.setAttribute('aria-expanded', String(document.body.classList.contains('toc-open')));
  });
  // Close drawer when clicking outside (on the main content area).
  document.addEventListener('click', (e) => {
    if (!document.body.classList.contains('toc-open')) return;
    const target = e.target as HTMLElement;
    if (target.closest('.hamburger') || target.closest('.toc-left')) return;
    document.body.classList.remove('toc-open');
    ham?.setAttribute('aria-expanded', 'false');
  });
</script>

<style>
  .topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--topbar-h);
    padding: 0 var(--space-6);
    background: var(--topbar-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: var(--border-w) solid var(--rule);
  }
  :root[data-theme="dark"] .topbar {
    background: var(--topbar-bg);
  }

  .left-cluster {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
  }

  .brand {
    font-family: var(--font-display);
    font-size: 22px;
    color: var(--ink-strong);
    text-decoration: none;
    white-space: nowrap;
  }
  .brand:hover { text-decoration: none; }
  .brand-accent { color: var(--accent); }

  .crumb {
    font-family: var(--font-mono);
    font-size: var(--fs-small);
    color: var(--soft);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1 1 auto;
    padding: 0 var(--space-4);
  }

  .actions {
    display: flex;
    gap: var(--space-3);
    align-items: center;
  }

  /* 沉浸式阅读按钮:图标 + 中文文字 */
  .immersive-toggle {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    height: 32px;
    padding: 0 var(--space-3);
    background: transparent;
    border: 1px solid var(--rule);
    border-radius: 9999px;
    color: var(--soft);
    font-family: var(--font-body);
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease),
                color var(--dur-fast) var(--ease),
                border-color var(--dur-fast) var(--ease);
  }
  .immersive-toggle:hover {
    background: var(--paper-sunken);
    color: var(--ink);
    border-color: var(--soft);
  }
  .immersive-toggle:focus-visible {
    outline: var(--outline-w) solid var(--accent);
    outline-offset: var(--outline-offset);
  }
  .immersive-toggle-label { letter-spacing: 0.02em; }

  /* 沉浸按钮激活态 */
  .immersive-toggle[aria-pressed="true"] {
    background: var(--accent-soft);
    color: var(--accent);
    border-color: var(--accent);
  }

  .hamburger { display: none; }

  @media (max-width: 767px) {
    .hamburger {
      display: block;
      background: none;
      border: none;
      font-size: var(--fs-h2);
      color: var(--ink);
      cursor: pointer;
      padding: 0;
      margin-right: var(--space-3);
      line-height: 1;
    }
    /* 小屏隐藏沉浸按钮文字,只保留图标,避免挤占 */
    .immersive-toggle-label { display: none; }
    .immersive-toggle { padding: 0; width: 32px; justify-content: center; }
  }
</style>