Files
Reims-landing/src/components/layout/HeaderControls.tsx
T

156 lines
7.0 KiB
TypeScript

import { Menu, Moon, Search, Sun, X } from 'lucide-react';
import React, { useEffect, useState } from 'react';
import { t } from '../../i18n/translations';
import { localizedPath, switchLocalePath, type Locale } from '../../i18n/types';
import { formatStars, useLatestRelease } from '../../lib/releases';
import { SearchModal } from './SearchModal';
interface Props {
locale: Locale;
pathname: string;
activeSection: string;
}
export const HeaderControls: React.FC<Props> = ({ locale, pathname, activeSection }) => {
const strings = t(locale);
const { data: release } = useLatestRelease();
const [dark, setDark] = useState(false);
const [searchOpen, setSearchOpen] = useState(false);
const [mobileOpen, setMobileOpen] = useState(false);
useEffect(() => {
const syncTheme = () => setDark(document.documentElement.getAttribute('data-theme') === 'dark');
syncTheme();
const onKey = (e: KeyboardEvent) => {
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') {
e.preventDefault();
setSearchOpen((v) => !v);
}
};
document.addEventListener('astro:after-swap', syncTheme);
window.addEventListener('keydown', onKey);
return () => {
document.removeEventListener('astro:after-swap', syncTheme);
window.removeEventListener('keydown', onKey);
};
}, []);
const toggleTheme = () => {
const next = !dark;
setDark(next);
document.documentElement.setAttribute('data-theme', next ? 'dark' : 'light');
try {
localStorage.setItem('reims-landing-theme', next ? 'dark' : 'light');
} catch {
// Ignore storage failures.
}
};
const targetLocale: Locale = locale === 'zh-CN' ? 'en-US' : 'zh-CN';
const langHref = switchLocalePath(pathname, targetLocale);
const navItems = [
{ key: 'features', label: strings.nav.features, href: localizedPath(locale, '/') },
{ key: 'docs', label: strings.nav.docs, href: localizedPath(locale, '/docs/quick-start') },
{ key: 'plugins', label: strings.nav.plugins, href: localizedPath(locale, '/plugins') },
{ key: 'compatibility', label: strings.nav.compatibility, href: localizedPath(locale, '/compatibility') },
{ key: 'changelog', label: strings.nav.changelog, href: localizedPath(locale, '/changelog') },
];
return (
<>
<div className="flex shrink-0 items-center gap-1 sm:gap-1.5 lg:col-start-3 lg:justify-self-end">
<a
href={langHref}
data-astro-prefetch="load"
aria-label={strings.nav.langSwitchAria}
className="inline-flex h-8 min-w-8 shrink-0 items-center justify-center whitespace-nowrap rounded-full border border-border-strong px-2 text-[12px] font-medium text-slate transition-[color,background-color,transform] duration-150 hover:bg-surface hover:text-ink active:scale-95"
>
{strings.nav.langSwitchLabel}
</a>
<button
type="button"
onClick={() => setSearchOpen(true)}
aria-label={strings.nav.searchAria}
title="Ctrl+K"
className="grid size-8 shrink-0 place-items-center rounded-full border border-border-strong text-slate transition-[color,background-color,transform] duration-150 hover:bg-surface hover:text-ink active:scale-95 sm:border-transparent"
>
<Search className="size-[16px]" aria-hidden="true" />
</button>
<button
type="button"
onClick={toggleTheme}
aria-label={strings.nav.themeAria}
aria-pressed={dark}
className="grid size-8 shrink-0 place-items-center rounded-full border border-border-strong text-slate transition-[color,background-color,transform] duration-150 hover:bg-surface hover:text-ink active:scale-95 sm:border-transparent"
>
<Sun
className={`col-start-1 row-start-1 size-[17px] transition-[opacity,filter] duration-300 ${
dark ? 'opacity-100 blur-none' : 'opacity-0 blur-[3px]'
}`}
aria-hidden="true"
/>
<Moon
className={`col-start-1 row-start-1 size-[16px] transition-[opacity,filter] duration-300 ${
dark ? 'opacity-0 blur-[3px]' : 'opacity-100 blur-none'
}`}
aria-hidden="true"
/>
</button>
<a
href="https://github.com/Nwflower/Reims-Go"
target="_blank"
rel="noopener noreferrer"
aria-label={`GitHub Reims-Go · ${strings.nav.starPrefix} ${formatStars(release.stars)}`}
className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded-full border border-border-strong px-2.5 text-[13px] font-medium text-ink transition-colors duration-150 hover:bg-surface sm:px-3"
>
<svg viewBox="0 0 24 24" className="size-[15px] fill-current" aria-hidden="true">
<path d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.53 1.032 1.53 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0 1 12 6.844a9.59 9.59 0 0 1 2.504.337c1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.02 10.02 0 0 0 22 12.017C22 6.484 17.522 2 12 2Z" />
</svg>
<span className="tnum hidden items-center gap-1 sm:inline-flex">
<span className="text-slate">{strings.nav.starPrefix}</span>
<span>{formatStars(release.stars)}</span>
</span>
</a>
<button
type="button"
onClick={() => setMobileOpen((v) => !v)}
aria-label="Menu"
aria-expanded={mobileOpen}
className="grid size-8 shrink-0 place-items-center rounded-full border border-border-strong text-slate transition-colors hover:text-ink lg:hidden"
>
{mobileOpen ? <X className="size-4" /> : <Menu className="size-4" />}
</button>
</div>
{mobileOpen && (
<div className="fixed inset-x-0 top-14 z-40 border-b border-border bg-canvas/95 px-5 py-3 shadow-lg backdrop-blur-xl lg:hidden">
<ul className="flex flex-col gap-1">
{navItems.map((item) => (
<li key={item.key}>
<a
href={item.href}
onClick={() => setMobileOpen(false)}
className={`block rounded-xl px-3 py-2.5 text-sm transition-colors ${
activeSection === item.key
? 'bg-surface font-medium text-ink'
: 'text-slate hover:bg-surface hover:text-ink'
}`}
>
{item.label}
</a>
</li>
))}
</ul>
</div>
)}
<SearchModal open={searchOpen} onOpenChange={setSearchOpen} locale={locale} />
</>
);
};