Skip to content

Commit 3286e82

Browse files
committed
重构:将Header组件模块化,拆分样式和功能代码
1 parent 1e714d8 commit 3286e82

File tree

6 files changed

+226
-190
lines changed

6 files changed

+226
-190
lines changed

office-website/src/components/Header.tsx

Lines changed: 0 additions & 190 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import { Logo } from '../../styles/components/Header.styles';
3+
4+
const LogoComponent: React.FC = () => {
5+
return (
6+
<Logo href="/">
7+
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
8+
<path d="M12 2L2 7L12 12L22 7L12 2Z" fill="#3B82F6" />
9+
<path d="M2 17L12 22L22 17" stroke="#3B82F6" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
10+
<path d="M2 12L12 17L22 12" stroke="#3B82F6" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
11+
</svg>
12+
<span>Typescript Userscript Template</span>
13+
</Logo>
14+
);
15+
};
16+
17+
export default LogoComponent;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { MobileMenuButton } from '../../styles/components/Header.styles';
3+
4+
const MobileMenuComponent: React.FC = () => {
5+
return (
6+
<MobileMenuButton>
7+
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
8+
<path d="M4 6H20M4 12H20M4 18H20" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
9+
</svg>
10+
</MobileMenuButton>
11+
);
12+
};
13+
14+
export default MobileMenuComponent;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { NavLinks, NavLink, GitHubButton } from '../../styles/components/Header.styles';
3+
4+
interface NavBarProps {
5+
activeSection: string;
6+
}
7+
8+
const NavBar: React.FC<NavBarProps> = ({ activeSection }) => {
9+
return (
10+
<NavLinks>
11+
<NavLink href="#features" active={activeSection === 'features'}>特性</NavLink>
12+
<NavLink href="#workflow" active={activeSection === 'workflow'}>工作流程</NavLink>
13+
<NavLink href="#installation" active={activeSection === 'installation'}>安装指南</NavLink>
14+
<NavLink href="#typescript" active={activeSection === 'typescript'}>Typescript支持</NavLink>
15+
<NavLink href="#community" active={activeSection === 'community'}>加入社区</NavLink>
16+
<GitHubButton href="https://github.com/JSREI/typescript-userscript-template" target="_blank">
17+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
18+
<path d="M12 2C6.477 2 2 6.477 2 12C2 16.419 4.865 20.166 8.839 21.489C9.339 21.581 9.5 21.27 9.5 21.006V19.505C6.735 20.126 6.151 18.141 6.151 18.141C5.681 17.021 5.031 16.711 5.031 16.711C4.151 16.105 5.101 16.117 5.101 16.117C6.101 16.19 6.651 17.141 6.651 17.141C7.551 18.542 9.101 18.19 9.541 17.939C9.631 17.293 9.901 16.839 10.201 16.591C7.975 16.339 5.651 15.486 5.651 11.677C5.651 10.55 6.051 9.631 6.676 8.901C6.576 8.651 6.226 7.626 6.776 6.227C6.776 6.227 7.626 5.961 9.501 7.281C10.301 7.061 11.151 6.951 12.001 6.949C12.851 6.949 13.701 7.061 14.501 7.281C16.376 5.959 17.226 6.227 17.226 6.227C17.776 7.626 17.426 8.651 17.326 8.901C17.952 9.631 18.347 10.55 18.347 11.677C18.347 15.496 16.018 16.335 13.786 16.581C14.151 16.886 14.486 17.491 14.486 18.421V21.006C14.486 21.271 14.646 21.586 15.156 21.486C19.135 20.162 21.996 16.417 21.996 12C21.996 6.477 17.52 2 12 2Z" fill="currentColor" />
19+
</svg>
20+
GitHub
21+
</GitHubButton>
22+
</NavLinks>
23+
);
24+
};
25+
26+
export default NavBar;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { useState, useEffect } from 'react';
2+
import { HeaderContainer, NavContainer } from '../../styles/components/Header.styles';
3+
import LogoComponent from './LogoComponent';
4+
import NavBar from './NavBar';
5+
import MobileMenuComponent from './MobileMenuComponent';
6+
7+
const Header: React.FC = () => {
8+
const [isScrolled, setIsScrolled] = useState(false);
9+
const [activeSection, setActiveSection] = useState('');
10+
11+
// 监听滚动事件,更新导航栏样式
12+
useEffect(() => {
13+
const handleScroll = () => {
14+
setIsScrolled(window.scrollY > 50);
15+
16+
// 获取所有锚点对应的部分
17+
const sections = ['features', 'workflow', 'installation', 'community', 'typescript'];
18+
let currentActive = '';
19+
20+
// 找出当前可见的最顶部部分
21+
for (const section of sections) {
22+
const element = document.getElementById(section);
23+
if (element) {
24+
const rect = element.getBoundingClientRect();
25+
if (rect.top <= 150 && rect.bottom >= 150) {
26+
currentActive = section;
27+
break;
28+
}
29+
}
30+
}
31+
32+
// 如果没有找到可见部分,但页面已经滚动,则激活第一个部分
33+
if (!currentActive && window.scrollY > 100) {
34+
currentActive = sections[0];
35+
}
36+
37+
setActiveSection(currentActive);
38+
};
39+
40+
window.addEventListener('scroll', handleScroll);
41+
handleScroll(); // 初始化
42+
43+
return () => {
44+
window.removeEventListener('scroll', handleScroll);
45+
};
46+
}, []);
47+
48+
return (
49+
<HeaderContainer isScrolled={isScrolled}>
50+
<NavContainer>
51+
<LogoComponent />
52+
<NavBar activeSection={activeSection} />
53+
<MobileMenuComponent />
54+
</NavContainer>
55+
</HeaderContainer>
56+
);
57+
};
58+
59+
export default Header;

0 commit comments

Comments
 (0)