feat: add dark mode support with theme toggle

This commit is contained in:
张翔
2026-02-13 15:17:42 +08:00
parent 34c96c119d
commit 8175c2f035
8 changed files with 1761 additions and 241 deletions
+136 -111
View File
@@ -1,26 +1,23 @@
'use client';
import Link from 'next/link';
import { useState, useEffect, useCallback } from 'react';
import { Menu } from 'lucide-react';
import { Menu, X } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet';
import { ThemeToggle } from '@/components/ui/theme-toggle';
import { COMPANY_INFO, NAVIGATION } from '@/lib/constants';
import { cn } from '@/lib/utils';
export function Header() {
const [isOpen, setIsOpen] = useState(false);
const [activeSection, setActiveSection] = useState('home');
const [isScrolled, setIsScrolled] = useState(false);
// 处理平滑滚动
const handleNavClick = useCallback((e: React.MouseEvent<HTMLAnchorElement>, href: string) => {
e.preventDefault();
const targetId = href.replace('#', '');
const element = document.getElementById(targetId);
if (element) {
const headerOffset = 80; // header高度
const headerOffset = 64;
const elementPosition = element.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - headerOffset;
@@ -34,15 +31,12 @@ export function Header() {
}
}, []);
// 监听滚动,更新激活状态和header样式
useEffect(() => {
const handleScroll = () => {
// 更新header背景
setIsScrolled(window.scrollY > 10);
setIsScrolled(window.scrollY > 20);
// 计算当前激活的section
const sections = NAVIGATION.map(item => item.href.replace('#', ''));
const scrollPosition = window.scrollY + 100; // 添加偏移量
const scrollPosition = window.scrollY + 100;
for (let i = sections.length - 1; i >= 0; i--) {
const section = document.getElementById(sections[i]);
@@ -57,114 +51,145 @@ export function Header() {
};
window.addEventListener('scroll', handleScroll, { passive: true });
handleScroll(); // 初始化
handleScroll();
return () => window.removeEventListener('scroll', handleScroll);
}, []);
return (
<header
className={cn(
"fixed top-0 left-0 right-0 z-50 transition-all duration-300",
isScrolled
? "bg-white/95 backdrop-blur-md shadow-sm border-b border-[#E8E0E0]"
: "bg-transparent"
)}
>
<div className="container-custom">
<div className="flex items-center justify-between h-20">
{/* Logo */}
<Link
href="#home"
onClick={(e) => handleNavClick(e, '#home')}
className="flex items-center"
>
<img src="/logo.svg" alt={COMPANY_INFO.name} className="h-10 w-auto" />
</Link>
{/* Desktop Navigation */}
<nav className="hidden lg:flex items-center gap-1">
{NAVIGATION.map((item) => (
<a
key={item.id}
href={item.href}
onClick={(e) => handleNavClick(e, item.href)}
className={cn(
"relative px-4 py-2 text-sm font-medium transition-all duration-200",
activeSection === item.id.replace('#', '')
? "text-[#C41E3A]"
: "text-[#4A4A4A] hover:text-[#C41E3A]"
)}
>
{item.label}
{/* 活跃指示条 */}
<span
className={cn(
"absolute bottom-0 left-1/2 -translate-x-1/2 h-0.5 bg-[#C41E3A] transition-all duration-200",
activeSection === item.id.replace('#', '') ? "w-6" : "w-0"
)}
/>
</a>
))}
</nav>
{/* CTA Button - Desktop */}
<div className="hidden lg:block">
<Button
onClick={(e) => {
const element = document.getElementById('contact');
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
}}
<>
<header
className={`
fixed top-0 left-0 right-0 z-50
transition-all duration-300 ease-out
${isScrolled
? 'bg-white/95 dark:bg-[#171717]/95 backdrop-blur-sm border-b border-[#E5E5E5] dark:border-[#333333]'
: 'bg-transparent'
}
`}
>
<div className="container-wide">
<div className="flex items-center justify-between h-16">
<a
href="#home"
onClick={(e) => handleNavClick(e, '#home')}
className="flex items-center group"
>
</Button>
</div>
<img
src="/logo.svg"
alt={COMPANY_INFO.name}
className="h-8 w-auto transition-transform duration-200 group-hover:scale-105"
/>
</a>
{/* Mobile Menu */}
<Sheet open={isOpen} onOpenChange={setIsOpen}>
<SheetTrigger asChild className="lg:hidden">
<Button variant="ghost" size="icon">
<Menu className="h-6 w-6" />
<nav className="hidden md:flex items-center gap-1">
{NAVIGATION.map((item) => (
<a
key={item.id}
href={item.href}
onClick={(e) => handleNavClick(e, item.href)}
className={`
relative px-3 py-1.5 text-sm font-medium
transition-colors duration-200
${activeSection === item.id.replace('#', '')
? 'text-[#C41E3A]'
: 'text-[#525252] dark:text-[#D4D4D4] hover:text-[#C41E3A]'
}
`}
>
{item.label}
{activeSection === item.id.replace('#', '') && (
<span className="absolute bottom-0 left-1/2 -translate-x-1/2 w-1 h-1 bg-[#C41E3A] rounded-full" />
)}
</a>
))}
</nav>
<div className="hidden md:flex items-center gap-3">
<ThemeToggle />
<Button
variant="ghost"
size="sm"
onClick={(e) => {
const element = document.getElementById('contact');
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
}}
>
</Button>
</SheetTrigger>
<SheetContent side="right" className="w-[300px] bg-white">
<div className="flex flex-col gap-2 mt-8">
{NAVIGATION.map((item) => (
<a
key={item.id}
href={item.href}
onClick={(e) => handleNavClick(e, item.href)}
className={cn(
"px-4 py-3 text-lg font-medium rounded-lg transition-all duration-200",
activeSection === item.id.replace('#', '')
? "text-[#C41E3A] bg-[#FEF2F4]"
: "text-[#4A4A4A] hover:text-[#C41E3A] hover:bg-[#F5F0F0]"
)}
>
{item.label}
</a>
))}
<div className="mt-4 pt-4 border-t border-[#E8E0E0]">
<Button
className="w-full"
onClick={() => {
const element = document.getElementById('contact');
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
setIsOpen(false);
}}
>
</Button>
</div>
</div>
</SheetContent>
</Sheet>
<Button
size="sm"
onClick={(e) => {
const element = document.getElementById('about');
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
}}
>
</Button>
</div>
<button
className="md:hidden p-2 -mr-2 text-[#525252] dark:text-[#D4D4D4] hover:text-[#C41E3A] transition-colors"
onClick={() => setIsOpen(!isOpen)}
>
{isOpen ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
</button>
</div>
</div>
</div>
</header>
</header>
{isOpen && (
<div className="fixed inset-0 z-40 md:hidden">
<div
className="absolute inset-0 bg-black/10 backdrop-blur-sm"
onClick={() => setIsOpen(false)}
/>
<div className="absolute top-16 left-0 right-0 bg-white dark:bg-[#171717] border-b border-[#E5E5E5] dark:border-[#333333] animate-fade-in">
<nav className="container-wide py-4">
{NAVIGATION.map((item) => (
<a
key={item.id}
href={item.href}
onClick={(e) => handleNavClick(e, item.href)}
className={`
block px-4 py-3 text-base font-medium
transition-colors duration-200
border-l-2
${activeSection === item.id.replace('#', '')
? 'text-[#C41E3A] border-[#C41E3A] bg-[#FEF2F4] dark:bg-[#1A0F11]'
: 'text-[#525252] dark:text-[#D4D4D4] border-transparent hover:text-[#C41E3A] hover:bg-[#FAFAFA] dark:hover:bg-[#262626]'
}
`}
>
{item.label}
</a>
))}
<div className="mt-4 px-4 pt-4 border-t border-[#E5E5E5] space-y-3">
<div className="flex items-center justify-between">
<span className="text-sm text-[#737373]"></span>
<ThemeToggle />
</div>
<Button
className="w-full"
onClick={() => {
const element = document.getElementById('contact');
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
setIsOpen(false);
}}
>
</Button>
</div>
</nav>
</div>
</div>
)}
</>
);
}
+224
View File
@@ -0,0 +1,224 @@
'use client';
import * as React from 'react';
import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
import { cn } from '@/lib/utils';
const DropdownMenu = DropdownMenuPrimitive.Root;
const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger;
const DropdownMenuGroup = DropdownMenuPrimitive.Group;
const DropdownMenuPortal = DropdownMenuPrimitive.Portal;
const DropdownMenuSub = DropdownMenuPrimitive.Sub;
const DropdownMenuRadioGroup = DropdownMenuPrimitive.RadioGroup;
const DropdownMenuSubTrigger = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.SubTrigger>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubTrigger> & {
inset?: boolean;
}
>(({ className, inset, children, ...props }, ref) => (
<DropdownMenuPrimitive.SubTrigger
ref={ref}
className={cn(
'flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none focus:bg-[#F0F0F0] dark:focus:bg-[#333333] data-[state=open]:bg-[#F0F0F0] dark:data-[state=open]:bg-[#333333]',
inset && 'pl-8',
className
)}
{...props}
>
{children}
<svg
className="ml-auto h-4 w-4"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m9 18 6-6-6-6" />
</svg>
</DropdownMenuPrimitive.SubTrigger>
));
DropdownMenuSubTrigger.displayName = DropdownMenuPrimitive.SubTrigger.displayName;
const DropdownMenuSubContent = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.SubContent>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubContent>
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.SubContent
ref={ref}
className={cn(
'z-50 min-w-[8rem] overflow-hidden rounded-lg border border-[#E5E5E5] dark:border-[#333333] bg-white dark:bg-[#171717] p-1 text-[#171717] dark:text-[#FAFAFA] shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className
)}
{...props}
/>
));
DropdownMenuSubContent.displayName = DropdownMenuPrimitive.SubContent.displayName;
const DropdownMenuContent = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Content>
>(({ className, sideOffset = 4, ...props }, ref) => (
<DropdownMenuPrimitive.Portal>
<DropdownMenuPrimitive.Content
ref={ref}
sideOffset={sideOffset}
className={cn(
'z-50 min-w-[8rem] overflow-hidden rounded-lg border border-[#E5E5E5] dark:border-[#333333] bg-white dark:bg-[#171717] p-1 text-[#171717] dark:text-[#FAFAFA] shadow-lg',
'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className
)}
{...props}
/>
</DropdownMenuPrimitive.Portal>
));
DropdownMenuContent.displayName = DropdownMenuPrimitive.Content.displayName;
const DropdownMenuItem = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Item> & {
inset?: boolean;
}
>(({ className, inset, ...props }, ref) => (
<DropdownMenuPrimitive.Item
ref={ref}
className={cn(
'relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-[#F0F0F0] dark:focus:bg-[#333333] focus:text-[#171717] dark:focus:text-[#FAFAFA] data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
inset && 'pl-8',
className
)}
{...props}
/>
));
DropdownMenuItem.displayName = DropdownMenuPrimitive.Item.displayName;
const DropdownMenuCheckboxItem = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.CheckboxItem>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.CheckboxItem>
>(({ className, children, checked, ...props }, ref) => (
<DropdownMenuPrimitive.CheckboxItem
ref={ref}
className={cn(
'relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-[#F0F0F0] dark:focus:bg-[#333333] focus:text-[#171717] dark:focus:text-[#FAFAFA] data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
className
)}
checked={checked}
{...props}
>
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DropdownMenuPrimitive.ItemIndicator>
<svg
className="h-4 w-4"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M20 6 9 17l-5-5" />
</svg>
</DropdownMenuPrimitive.ItemIndicator>
</span>
{children}
</DropdownMenuPrimitive.CheckboxItem>
));
DropdownMenuCheckboxItem.displayName = DropdownMenuPrimitive.CheckboxItem.displayName;
const DropdownMenuRadioItem = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.RadioItem>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.RadioItem>
>(({ className, children, ...props }, ref) => (
<DropdownMenuPrimitive.RadioItem
ref={ref}
className={cn(
'relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-[#F0F0F0] dark:focus:bg-[#333333] focus:text-[#171717] dark:focus:text-[#FAFAFA] data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
className
)}
{...props}
>
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DropdownMenuPrimitive.ItemIndicator>
<svg
className="h-2 w-2 fill-current"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
>
<circle cx="12" cy="12" r="10" />
</svg>
</DropdownMenuPrimitive.ItemIndicator>
</span>
{children}
</DropdownMenuPrimitive.RadioItem>
));
DropdownMenuRadioItem.displayName = DropdownMenuPrimitive.RadioItem.displayName;
const DropdownMenuLabel = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Label> & {
inset?: boolean;
}
>(({ className, inset, ...props }, ref) => (
<DropdownMenuPrimitive.Label
ref={ref}
className={cn(
'px-2 py-1.5 text-sm font-semibold',
inset && 'pl-8',
className
)}
{...props}
/>
));
DropdownMenuLabel.displayName = DropdownMenuPrimitive.Label.displayName;
const DropdownMenuSeparator = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Separator>
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.Separator
ref={ref}
className={cn('-mx-1 my-1 h-px bg-[#E5E5E5] dark:bg-[#333333]', className)}
{...props}
/>
));
DropdownMenuSeparator.displayName = DropdownMenuPrimitive.Separator.displayName;
const DropdownMenuShortcut = ({
className,
...props
}: React.HTMLAttributes<HTMLSpanElement>) => {
return (
<span
className={cn('ml-auto text-xs tracking-widest opacity-60', className)}
{...props}
/>
);
};
DropdownMenuShortcut.displayName = 'DropdownMenuShortcut';
export {
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuCheckboxItem,
DropdownMenuRadioItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuGroup,
DropdownMenuPortal,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuRadioGroup,
};
+44
View File
@@ -0,0 +1,44 @@
'use client';
import { useTheme } from '@/contexts/theme-context';
import { Moon, Sun, Monitor } from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
export function ThemeToggle() {
const { theme, setTheme, resolvedTheme } = useTheme();
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="sm" className="relative w-9 h-9 p-0">
<Sun className="h-5 w-5 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
<Moon className="absolute h-5 w-5 rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
<span className="sr-only"></span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => setTheme('light')}>
<Sun className="mr-2 h-4 w-4" />
<span></span>
{theme === 'light' && <span className="ml-auto text-[#C41E3A]"></span>}
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme('dark')}>
<Moon className="mr-2 h-4 w-4" />
<span></span>
{theme === 'dark' && <span className="ml-auto text-[#C41E3A]"></span>}
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme('system')}>
<Monitor className="mr-2 h-4 w-4" />
<span></span>
{theme === 'system' && <span className="ml-auto text-[#C41E3A]"></span>}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}