feat(ui): 保时捷语法复刻收尾(ADR-0008/0009/0011/0012/0013) #65
No files matched your search
@@ -280,9 +280,20 @@ test('页头是内缩浮条:padding-top 48px / 高 64px(px 令牌,不是 r
|
||||
|
||||
const scrolled = await readBar();
|
||||
expect(scrolled.top, '滚动后浮条仍应在 48px').toBe(48);
|
||||
expect(scrolled.height, '滚动后浮条高度不变(02b「收成方汉堡」已判定不做)').toBe(64);
|
||||
expect(scrolled.height, '滚动后浮条高度不变(02b 的方块 = 浮条收窄,高度令牌不动)').toBe(64);
|
||||
await expect(page.locator('header'), '滚动后应切到 scrolled 状态').toHaveAttribute('data-header-state', 'scrolled');
|
||||
|
||||
// OPEN-02b(ADR-0015):桌面端滚动后收成左上白方汉堡。
|
||||
// 「见方」量在浮条本体上(border-box 64×64);汉堡按钮内容盒是 62px
|
||||
// (64 减上下各 1px 边框),只断言 ≥44px 触达下限(WCAG 2.5.8)。
|
||||
await expect(page.locator('header')).toHaveAttribute('data-header-collapsed', 'true');
|
||||
expect(scrolled.right - scrolled.left, '收起后浮条必须收窄成 64px 见方(--header-height 令牌,非 rem 漂移值)').toBe(64);
|
||||
const burger = page.getByTestId('desktop-menu-button');
|
||||
await expect(burger).toBeVisible();
|
||||
const burgerHeight = await burger.evaluate((el) => Math.round(el.getBoundingClientRect().height));
|
||||
expect(burgerHeight, '汉堡按钮触达区 ≥44px').toBeGreaterThanOrEqual(44);
|
||||
await expect(page.locator('nav[aria-label="主导航"]'), '收起后内联导航必须卸载').toHaveCount(0);
|
||||
|
||||
// 外壳仍贴顶 —— chrome-persistence 既有断言的形态不变
|
||||
await expect
|
||||
.poll(() => page.evaluate(() => Math.round(document.querySelector('header')!.getBoundingClientRect().top)), { timeout: 5000 })
|
||||
|
||||
@@ -81,3 +81,46 @@ test('洞察轴下拉可在 hover 时展开,并落到 /insights', { tag: '@smo
|
||||
const viewAll = page.locator('a:has-text("查看全部洞察")').first();
|
||||
await expect(viewAll).toHaveAttribute('href', '/insights');
|
||||
});
|
||||
|
||||
// ── OPEN-02b(ADR-0015):滚动收成白方汉堡 + 左侧抽屉 ──────────────────────
|
||||
// ADR-0012 §6 条件 1:桌面 hover mega 的原断言不删(上方 4 例照跑),
|
||||
// 抽屉展开后以「目的地可达」形式保留同源子断言;条件 2:可发现性走
|
||||
// visible → aria-expanded → 点击生效三步复算,不接受「目测汉堡在那儿」。
|
||||
test('滚动收起后:内联导航卸载,抽屉内保留完整双轴目的地(OPEN-02b 子断言)', async ({ page }) => {
|
||||
await page.goto('/', { waitUntil: 'domcontentloaded' });
|
||||
const nav = page.locator('nav[aria-label="主导航"]');
|
||||
await expect(nav).toBeVisible({ timeout: 10000 });
|
||||
await expectHydrated(nav);
|
||||
|
||||
await page.evaluate(() => {
|
||||
document.documentElement.style.scrollBehavior = 'auto';
|
||||
window.scrollTo(0, 600);
|
||||
});
|
||||
await expect.poll(() => page.evaluate(() => Math.round(window.scrollY)), { timeout: 5000 }).toBe(600);
|
||||
|
||||
await expect(page.locator('header')).toHaveAttribute('data-header-collapsed', 'true');
|
||||
await expect(nav, '收起后内联导航必须卸载(焦点序不留幽灵 Tab 站)').toHaveCount(0);
|
||||
|
||||
const burger = page.getByTestId('desktop-menu-button');
|
||||
await expect(burger).toBeVisible();
|
||||
await expect(burger).toHaveAttribute('aria-expanded', 'false');
|
||||
// dev 目标 hydration 会整体替换节点(本文件 :31-37 同款竞态):重发交互而非只重试断言
|
||||
await expect(async () => {
|
||||
await burger.click();
|
||||
await expect(burger).toHaveAttribute('aria-expanded', 'true', { timeout: 1000 });
|
||||
}).toPass({ timeout: 20000 });
|
||||
|
||||
const drawer = page.getByTestId('desktop-navigation-drawer');
|
||||
await expect(drawer).toBeVisible();
|
||||
|
||||
// 三轴直链目的地——与 nav-keyboard 的 VIEW_ALL_EXPECTED 同源值
|
||||
for (const [label, href] of [['行业', '/solutions'], ['能力', '/services'], ['洞察', '/insights']] as const) {
|
||||
await expect(drawer.locator(`a[href="${href}"]`).first(), `${label} 轴目的地 ${href} 必须在抽屉内可达`).toBeVisible();
|
||||
}
|
||||
// 二级目的地(产品矩阵)仍可达——「抽屉必须铺出二级」判据在移动抽屉上已守过一次
|
||||
await expect(drawer.locator('a[href="/products"]').first()).toBeVisible();
|
||||
|
||||
await page.keyboard.press('Escape');
|
||||
await expect(drawer).toHaveCount(0);
|
||||
await expect(burger).toHaveAttribute('aria-expanded', 'false');
|
||||
});
|
||||
@@ -106,6 +106,70 @@ test.describe('主导航下拉的键盘可操作性', { tag: '@a11y' }, () => {
|
||||
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
|
||||
await expect(viewAllLink(page)).toHaveCount(0);
|
||||
});
|
||||
|
||||
// ── OPEN-02b(ADR-0015):滚动收起态的键盘契约 ──────────────────────────
|
||||
// 与上方「8 次 Tab 到达触发器」同一条可达性不变式在收起态的重述:
|
||||
// 收起后 Tab 序里第一站之后就是汉堡(内联导航/CTA 已卸载、logo CSS 级隐藏不进 Tab 序)。
|
||||
// 展开后的抽屉承载完整双轴(目的地子断言在 nav-dropdown.spec.ts 的 02b 用例里),
|
||||
// 本用例守「到达 → 展开 → 焦点被困 → Esc 关闭并归还」这条链。
|
||||
test('滚动收起后:正文位置 8 次 Shift+Tab 内到达汉堡,Enter 展开抽屉,焦点被困,Esc 关闭并归还焦点', async ({ page, browserName }) => {
|
||||
test.skip(browserName === 'webkit', 'WebKit 本机不响应真实 Tab 键(N-27,见上方注释)');
|
||||
await page.goto('/');
|
||||
const header = page.locator('header');
|
||||
await expect(header).toBeVisible();
|
||||
await expectHydrated(header);
|
||||
|
||||
// 滚动收起必须「重发交互而不只是重读断言」:dev 目标下 hydration 会整体替换
|
||||
// 节点、组件态随之丢失(本文件 :21-29 记录的同款竞态,本轮实测快照里
|
||||
// 页头回到顶部态、焦点落在「行业」上)⇒ scrollTo + 收起断言整体可重发。
|
||||
await expect(async () => {
|
||||
await page.evaluate(() => {
|
||||
document.documentElement.style.scrollBehavior = 'auto';
|
||||
window.scrollTo(0, 600);
|
||||
});
|
||||
await expect.poll(() => page.evaluate(() => Math.round(window.scrollY)), { timeout: 2000 }).toBe(600);
|
||||
await expect(header).toHaveAttribute('data-header-collapsed', 'true', { timeout: 1000 });
|
||||
}).toPass({ timeout: 20000 });
|
||||
|
||||
const burger = page.getByTestId('desktop-menu-button');
|
||||
await expect(burger).toBeVisible();
|
||||
|
||||
// ⚠ 键盘契约的方向:收起态的「到达汉堡」只能从**正文位置向上走(Shift+Tab)**。
|
||||
// 从 body 正向 Tab 的第一站是顶部跳转链接,浏览器为显示焦点会把页面滚回页首,
|
||||
// 收起态随之正确撤销(滚动位 ≤20 ⇒ 顶部态)——这不是缺陷,是焦点滚动语义,
|
||||
// 本用例第一版就是这么红的(快照:焦点在跳转链接、页头回到顶部态)。
|
||||
// 页首态的「正向 Tab 契约」仍由上方既有用例守着。
|
||||
// 锚点必须是**可见且可聚焦**的正文链接:`main a` 的第一个命中可能是不参与
|
||||
// Tab 序的隐藏链接(探针实测 focus() 落空、activeElement 仍是 BODY)。
|
||||
const anchor = page.locator('main a:visible').first();
|
||||
await anchor.evaluate((el) => (el as HTMLElement).focus({ preventScroll: true }));
|
||||
await expect(anchor, '锚点必须真的接管焦点,否则 Shift+Tab 从 BODY 出发会掉进 cookie 横幅的焦点环').toBeFocused();
|
||||
await expect.poll(() => page.evaluate(() => Math.round(window.scrollY)), { timeout: 5000 }).toBe(600);
|
||||
|
||||
// 有界的键盘行走(同上方既有用例的配方:不押死次数,上限 8)
|
||||
let reached = false;
|
||||
for (let i = 0; i < 8 && !reached; i += 1) {
|
||||
await page.keyboard.press('Shift+Tab');
|
||||
reached = await burger.evaluate((el) => document.activeElement === el).catch(() => false);
|
||||
}
|
||||
await expect(burger, '正文位置 8 次 Shift+Tab 内必须能到达收起态汉堡').toBeFocused();
|
||||
|
||||
await page.keyboard.press('Enter');
|
||||
await expect(burger).toHaveAttribute('aria-expanded', 'true');
|
||||
const drawer = page.getByTestId('desktop-navigation-drawer');
|
||||
await expect(drawer).toBeVisible();
|
||||
|
||||
// focus trap:Tab 多次后焦点必须仍在抽屉容器内
|
||||
for (let i = 0; i < 5; i += 1) {
|
||||
await page.keyboard.press('Tab');
|
||||
}
|
||||
const trapped = await drawer.evaluate((el) => el.contains(document.activeElement));
|
||||
expect(trapped, '焦点必须困在抽屉内(focus trap)').toBe(true);
|
||||
|
||||
await page.keyboard.press('Escape');
|
||||
await expect(drawer).toHaveCount(0);
|
||||
await expect(burger, 'Esc 关闭后焦点必须归还唤起元素(汉堡)').toBeFocused();
|
||||
});
|
||||
});
|
||||
|
||||
// ── 「查看全部」目的地不变量 ──────────────────────────────────────────────
|
||||
|
||||
@@ -284,7 +284,14 @@ test.describe('L2: 组件视觉状态测试', () => {
|
||||
window.scrollTo(0, Math.max(0, Math.round(top)));
|
||||
});
|
||||
await page.waitForTimeout(250);
|
||||
await expect(card).toHaveScreenshot('product-card.png');
|
||||
// OPEN-02b(ADR-0015):页头随滚动在「浮条 ⇄ 方汉堡」两态间切换,而卡片 791px 高
|
||||
// 装不进 800 视口 ⇒ toHaveScreenshot 自带的 scroll-into-view 会再滚一次,
|
||||
// 半透明浮条/汉堡的像素就可能落进卡片盒(2026-12-02 实测 3926px 差,diff 图上
|
||||
// 叠着行业/能力触发器与 logo 字样)。与 footer-section 用例同一先例:mask 掉
|
||||
// fixed 页头,截图与页头状态彻底解耦。卡片本体像素不受 mask 影响。
|
||||
await expect(card).toHaveScreenshot('product-card.png', {
|
||||
mask: [page.locator('header')],
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 31 KiB |
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 43 KiB |
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 99 KiB |
@@ -145,6 +145,11 @@ jest.mock('@/components/layout/mega-dropdown', () => ({
|
||||
|
||||
import { Header } from './header';
|
||||
|
||||
/** jsdom 的 scrollY 只读;测试里用可写定义桩替(afterEach 归零,防跨用例污染)。 */
|
||||
function stubScrollY(value: number) {
|
||||
Object.defineProperty(window, 'scrollY', { value, configurable: true, writable: true });
|
||||
}
|
||||
|
||||
describe('Header', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
@@ -338,9 +343,138 @@ describe('Header', () => {
|
||||
render(<Header />);
|
||||
const contactLink = screen.getByText('联系我们').closest('a');
|
||||
const serviceLink = screen.getByText('服务').closest('a');
|
||||
|
||||
|
||||
expect(contactLink).toHaveAttribute('href', '/contact');
|
||||
expect(serviceLink).toHaveAttribute('href', '/services');
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 桌面端滚动收起 + 左侧抽屉(OPEN-02b / ADR-0015)。
|
||||
* 判据全部来自 ADR-0012 §6 翻转条件与 ADR-0015 §3 规格。
|
||||
*/
|
||||
describe('Desktop collapse (OPEN-02b / ADR-0015)', () => {
|
||||
afterEach(() => {
|
||||
stubScrollY(0);
|
||||
});
|
||||
|
||||
it('should collapse desktop navigation after scrolling past the threshold', () => {
|
||||
render(<Header />);
|
||||
stubScrollY(100);
|
||||
fireEvent.scroll(window);
|
||||
|
||||
// 桌面三轴导航与 CTA 整体卸载(焦点序里不留幽灵 Tab 站)
|
||||
expect(screen.queryByTestId('desktop-navigation')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('consult-button')).not.toBeInTheDocument();
|
||||
// 汉堡方块挂载,header 标注收起态
|
||||
expect(screen.getByTestId('desktop-menu-button')).toBeInTheDocument();
|
||||
expect(screen.getByRole('banner')).toHaveAttribute('data-header-collapsed', 'true');
|
||||
expect(screen.getByRole('banner')).toHaveAttribute('data-header-state', 'scrolled');
|
||||
// 方块宽度走 px 令牌(--header-height = 64px),不是 w-16 的 rem 漂移值(ADR-0012 §2.4 同源坑)
|
||||
expect(screen.getByTestId('header-bar').className).toContain('md:w-[var(--header-height)]');
|
||||
// logo 是 CSS 级隐藏(md:hidden),明暗双版恒挂载的契约不破
|
||||
expect(screen.getAllByAltText('四川睿新致远科技有限公司')).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should restore desktop navigation when scrolled back to top', () => {
|
||||
render(<Header />);
|
||||
stubScrollY(100);
|
||||
fireEvent.scroll(window);
|
||||
stubScrollY(0);
|
||||
fireEvent.scroll(window);
|
||||
|
||||
expect(screen.getByTestId('desktop-navigation')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('desktop-menu-button')).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('banner')).toHaveAttribute('data-header-collapsed', 'false');
|
||||
});
|
||||
|
||||
it('should sync collapse state on mount when the page loads pre-scrolled (scroll restoration)', () => {
|
||||
// 带恢复位的整页刷新不触发 scroll 事件——挂载时必须补一拍同步(e2e 实测缺陷)
|
||||
stubScrollY(600);
|
||||
render(<Header />);
|
||||
|
||||
expect(screen.getByTestId('desktop-menu-button')).toBeInTheDocument();
|
||||
expect(screen.getByRole('banner')).toHaveAttribute('data-header-collapsed', 'true');
|
||||
expect(screen.queryByTestId('desktop-navigation')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should open the desktop drawer from the collapsed hamburger with live aria-controls', () => {
|
||||
render(<Header />);
|
||||
stubScrollY(100);
|
||||
fireEvent.scroll(window);
|
||||
|
||||
const burger = screen.getByTestId('desktop-menu-button');
|
||||
// 关闭态不留悬空 aria-controls(面板条件挂载,同移动抽屉既有契约)
|
||||
expect(burger).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(burger).not.toHaveAttribute('aria-controls');
|
||||
|
||||
fireEvent.click(burger);
|
||||
|
||||
expect(screen.getByTestId('desktop-navigation-drawer')).toBeInTheDocument();
|
||||
expect(burger).toHaveAttribute('aria-expanded', 'true');
|
||||
expect(burger).toHaveAttribute('aria-controls', 'desktop-menu');
|
||||
expect(document.getElementById('desktop-menu')).toBe(
|
||||
screen.getByTestId('desktop-navigation-drawer'),
|
||||
);
|
||||
});
|
||||
|
||||
it('should close the desktop drawer through useFocusTrap onEscape', async () => {
|
||||
render(<Header />);
|
||||
stubScrollY(100);
|
||||
fireEvent.scroll(window);
|
||||
fireEvent.click(screen.getByTestId('desktop-menu-button'));
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('desktop-navigation-drawer')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// 两个焦点陷阱(移动 / 桌面)都注册了 onEscape;桌面抽屉是第二个 hook 调用,
|
||||
// 取最后一个 onEscape 才是桌面通道的。
|
||||
const options = mockUseFocusTrap.mock.calls
|
||||
.map((call) => call[1] as { onEscape?: () => void } | undefined)
|
||||
.filter((call) => typeof call?.onEscape === 'function')
|
||||
.pop();
|
||||
expect(options?.onEscape).toBeInstanceOf(Function);
|
||||
|
||||
act(() => {
|
||||
options?.onEscape?.();
|
||||
});
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByTestId('desktop-navigation-drawer')).not.toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByTestId('desktop-menu-button')).toHaveAttribute('aria-expanded', 'false');
|
||||
});
|
||||
|
||||
it('should force-close the desktop drawer when scrolled back to top', () => {
|
||||
render(<Header />);
|
||||
stubScrollY(100);
|
||||
fireEvent.scroll(window);
|
||||
fireEvent.click(screen.getByTestId('desktop-menu-button'));
|
||||
expect(screen.getByTestId('desktop-navigation-drawer')).toBeInTheDocument();
|
||||
|
||||
// 回顶时汉堡卸载,抽屉若不强制关闭会失去唤起锚点(ADR-0015 §3 规格)
|
||||
stubScrollY(0);
|
||||
fireEvent.scroll(window);
|
||||
|
||||
expect(screen.queryByTestId('desktop-navigation-drawer')).not.toBeInTheDocument();
|
||||
expect(screen.getByTestId('desktop-navigation')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should expose the full double-axis destinations inside the desktop drawer', () => {
|
||||
render(<Header />);
|
||||
stubScrollY(100);
|
||||
fireEvent.scroll(window);
|
||||
fireEvent.click(screen.getByTestId('desktop-menu-button'));
|
||||
|
||||
const drawer = screen.getByTestId('desktop-navigation-drawer');
|
||||
const hrefs = within(drawer)
|
||||
.getAllByRole('link')
|
||||
.map((link) => link.getAttribute('href'));
|
||||
|
||||
// 完整双轴(行业 / 能力 / 洞察)+ 产品矩阵 + CTA——二级目的地不在抽屉里
|
||||
// 就是移动端那次「孤儿路径」事故的同款复发(header.tsx DrawerNavList 注释)
|
||||
for (const href of ['/products', '/solutions', '/services', '/about', '/contact']) {
|
||||
expect(hrefs).toContain(href);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
+266
-153
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { StaticLink } from '@/components/ui/static-link';
|
||||
import Image from 'next/image';
|
||||
import { usePathname } from 'next/navigation';
|
||||
@@ -14,23 +14,136 @@ import { useFocusTrap } from '@/hooks/use-focus-trap';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { ThemeToggle } from '@/components/theme/theme-toggle';
|
||||
|
||||
/**
|
||||
* 抽屉导航列表:移动端抽屉(OPEN-02a 之前就有)与桌面收起态抽屉(ADR-0015)共用。
|
||||
*
|
||||
* 「完整双轴」契约的落点:`config.mainNav` 全量直链 + 各轴 `megaDropdown` 分组**内联展开**
|
||||
* (桌面 hover mega 面板收起后不可达,抽屉必须把二级目的地也铺出来——同一判据在
|
||||
* 移动抽屉上已由 `mobile-user-journeys.spec.ts` 守过一次)。不手风琴化:面板
|
||||
* `overflow-y-auto` 可滚,常驻展开少一个交互态、也少一处 a11y 契约要维护。
|
||||
*/
|
||||
function DrawerNavList({ onClose }: { onClose: () => void }) {
|
||||
const config = useSiteConfig();
|
||||
const pathname = usePathname();
|
||||
const isActive = useCallback(
|
||||
(item: NavigationItem) => isNavItemActive(pathname, item.href),
|
||||
[pathname],
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* 桌面浮条收起后顶栏 ThemeToggle 随之卸载,抽屉是唯一主题入口;
|
||||
w-11 h-11 = 44px,满足触摸目标下限。放在 nav 之外,避免混入导航链接列表。 */}
|
||||
<div className="max-w-container mx-auto px-4 sm:px-6 pt-6 flex items-center justify-between">
|
||||
<span className="text-sm text-text-secondary">外观主题</span>
|
||||
<ThemeToggle className="w-11 h-11" />
|
||||
</div>
|
||||
<nav className="max-w-container mx-auto px-4 sm:px-6 py-6">
|
||||
{config.mainNav.map((item, index) => {
|
||||
const groups = item.dropdownKey ? config.megaDropdown?.[item.dropdownKey] : undefined;
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
key={item.id}
|
||||
initial={{ x: 20, opacity: 0 }}
|
||||
animate={{ x: 0, opacity: 1 }}
|
||||
transition={{ delay: index * 0.05 }}
|
||||
>
|
||||
<StaticLink
|
||||
href={item.href}
|
||||
onClick={onClose}
|
||||
className={`
|
||||
block px-4 py-4 text-base font-medium
|
||||
transition-all duration-normal ease-out
|
||||
${isActive(item)
|
||||
? 'text-text-primary bg-bg-secondary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
}
|
||||
`}
|
||||
style={{ minHeight: '48px', display: 'flex', alignItems: 'center' }}
|
||||
>
|
||||
{item.label}
|
||||
</StaticLink>
|
||||
|
||||
{groups?.length ? (
|
||||
<div className="px-4 pb-4">
|
||||
{groups.map((group) => (
|
||||
<div key={group.id} className="mt-3 first:mt-0">
|
||||
<div className="text-[11px] tracking-[0.15em] uppercase font-bold text-text-muted mb-1.5">
|
||||
{group.title}
|
||||
</div>
|
||||
<ul className="list-none m-0 p-0 space-y-1">
|
||||
{(group.items ?? []).map((sub) => (
|
||||
<li key={sub.id}>
|
||||
<StaticLink
|
||||
href={sub.href}
|
||||
onClick={onClose}
|
||||
className="block px-2 py-2 text-sm text-text-secondary hover:text-brand-ink transition-colors duration-hover"
|
||||
style={{ minHeight: '40px', display: 'flex', alignItems: 'center' }}
|
||||
>
|
||||
{sub.title}
|
||||
</StaticLink>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</motion.div>
|
||||
);
|
||||
})}
|
||||
<div className="mt-6 px-4 pt-6 border-t border-border-primary">
|
||||
<Button
|
||||
className="w-full"
|
||||
asChild
|
||||
size="lg"
|
||||
>
|
||||
<StaticLink href="/contact" onClick={onClose}>
|
||||
<MessageCircle className="w-4 h-4" />
|
||||
预约咨询
|
||||
</StaticLink>
|
||||
</Button>
|
||||
</div>
|
||||
</nav>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function HeaderContent() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [isScrolled, setIsScrolled] = useState(false);
|
||||
/**
|
||||
* 桌面端收起态抽屉(OPEN-02b,ADR-0015)。`isScrolled` 只表达「滚过了阈值」,
|
||||
* 抽屉开关独立成态:收起 → 点汉堡开抽屉 → 抽屉内导航时浮条形态不变。
|
||||
* 回滚到顶(scrollY ≤ 20)时强制关闭——那时汉堡按钮卸载,抽屉会失去唤起锚点。
|
||||
*/
|
||||
const [isDesktopDrawerOpen, setIsDesktopDrawerOpen] = useState(false);
|
||||
const [openDropdown, setOpenDropdown] = useState<string | null>(null);
|
||||
const pathname = usePathname();
|
||||
const focusTrapRef = useFocusTrap<HTMLDivElement>(isOpen, {
|
||||
onEscape: () => setIsOpen(false),
|
||||
});
|
||||
const desktopFocusTrapRef = useFocusTrap<HTMLDivElement>(isDesktopDrawerOpen, {
|
||||
onEscape: () => setIsDesktopDrawerOpen(false),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handleScroll = () => {
|
||||
setIsScrolled(window.scrollY > 20);
|
||||
if (window.scrollY <= 20) {
|
||||
setIsDesktopDrawerOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleGlobalKeyDown = (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape' && isOpen) {
|
||||
setIsOpen(false);
|
||||
if (e.key === 'Escape') {
|
||||
if (isOpen) {
|
||||
setIsOpen(false);
|
||||
}
|
||||
if (isDesktopDrawerOpen) {
|
||||
setIsDesktopDrawerOpen(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -41,23 +154,39 @@ function HeaderContent() {
|
||||
window.removeEventListener('scroll', handleScroll);
|
||||
window.removeEventListener('keydown', handleGlobalKeyDown);
|
||||
};
|
||||
}, [isOpen]);
|
||||
}, [isOpen, isDesktopDrawerOpen]);
|
||||
|
||||
// 挂载时同步一次滚动位(只跑一拍,不随抽屉开关重订阅):带 scroll restoration 的
|
||||
// 整页刷新(在滚动位按 F5 / bfcache 恢复)**不触发 scroll 事件**,若不补这一拍,
|
||||
// 页头会在 scrollY=600 时仍显示顶部态——本轮 nav-keyboard e2e 实测抓到(快照:
|
||||
// 焦点在 logo、主导航完整在列,而页面在滚动位)。hydration 后执行,无 SSR 失配。
|
||||
const didMountRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (didMountRef.current) return;
|
||||
didMountRef.current = true;
|
||||
setIsScrolled(window.scrollY > 20);
|
||||
if (window.scrollY <= 20) {
|
||||
setIsDesktopDrawerOpen(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
||||
// Escape 关闭菜单;Enter/Space 已由原生 button 的 onClick 处理,
|
||||
// 此处不再拦截,避免键盘操作时重复触发 toggle。
|
||||
if (e.key === 'Escape' && isOpen) {
|
||||
if (e.key === 'Escape' && (isOpen || isDesktopDrawerOpen)) {
|
||||
e.preventDefault();
|
||||
setIsOpen(false);
|
||||
setIsDesktopDrawerOpen(false);
|
||||
}
|
||||
}, [isOpen]);
|
||||
}, [isOpen, isDesktopDrawerOpen]);
|
||||
|
||||
const config = useSiteConfig();
|
||||
|
||||
const handleNavClick = useCallback(() => {
|
||||
// StaticLink 新契约:onClick 先于导航执行,不调 preventDefault 即由
|
||||
// next/link 完成客户端跳转,此处只负责关闭抽屉。
|
||||
// next/link 完成客户端跳转,此处只负责关闭抽屉(移动 + 桌面收起态共用一份导航列表)。
|
||||
setIsOpen(false);
|
||||
setIsDesktopDrawerOpen(false);
|
||||
}, []);
|
||||
|
||||
// 激活判定唯一实现见 lib/nav-active.ts —— 原来是 8 个分支的 per-id `if` 阶梯,
|
||||
@@ -76,6 +205,7 @@ function HeaderContent() {
|
||||
(它测的是吸顶没被 transform 劫持,与浮条无关)。 */}
|
||||
<header
|
||||
data-header-state={isScrolled ? 'scrolled' : 'top'}
|
||||
data-header-collapsed={isScrolled ? 'true' : 'false'}
|
||||
className="fixed inset-x-0 top-0 z-50"
|
||||
>
|
||||
{/* 内缩层:与 `container-x` 的 content 边对齐(1600 档实测 165…1435,
|
||||
@@ -83,12 +213,23 @@ function HeaderContent() {
|
||||
<div className="mx-auto w-full max-w-container px-4 pt-[var(--header-inset-top)] sm:px-6 lg:px-10">
|
||||
<div
|
||||
data-testid="header-bar"
|
||||
className="relative flex items-center justify-between border border-border-primary bg-bg-primary transition-colors duration-normal"
|
||||
className={cn(
|
||||
'relative flex items-center justify-between border border-border-primary bg-bg-primary transition-colors duration-normal',
|
||||
// OPEN-02b(ADR-0015):桌面端滚动后浮条收成白方汉堡。宽度走 --header-height
|
||||
// 令牌而不是 w-16 —— 本仓 ≥1024 根字号 18px,w-16(4rem) 会漂到 72px
|
||||
// (同 ADR-0012 §2.4 的 rem 坑)。移动端 (<md) 仍是全宽条,行为不变。
|
||||
isScrolled ? 'w-full md:w-[var(--header-height)]' : 'w-full'
|
||||
)}
|
||||
style={{ height: 'var(--header-height)' }}
|
||||
>
|
||||
<StaticLink
|
||||
href="/"
|
||||
className="relative flex items-center group/logo"
|
||||
className={cn(
|
||||
'relative flex items-center group/logo',
|
||||
// 收起态在桌面档隐藏 logo(CSS 级隐藏而非卸载:Logo 明暗双版恒挂载的
|
||||
// 单测契约不动,也避免双 <Image> 反复重挂的 hydration 抖动)
|
||||
isScrolled ? 'md:hidden' : '',
|
||||
)}
|
||||
aria-label="返回首页"
|
||||
>
|
||||
{/* Logo 明暗双版:浅色背景用 logo.svg(黑字),深色背景用 logo-white.svg(白字)。
|
||||
@@ -117,67 +258,91 @@ function HeaderContent() {
|
||||
/>
|
||||
</StaticLink>
|
||||
|
||||
<nav className="hidden md:flex items-center gap-0.5" role="navigation" aria-label="主导航" data-testid="desktop-navigation">
|
||||
{config.mainNav.map((item) => (
|
||||
item.hasDropdown ? (
|
||||
<MegaDropdown
|
||||
key={item.id}
|
||||
label={item.label}
|
||||
groups={config.megaDropdown[item.dropdownKey!] ?? []}
|
||||
/* 「查看全部」落点 = 导航项自身 href,不靠标签反推(原缺陷见 mega-dropdown.tsx props 注释) */
|
||||
viewAllHref={item.href}
|
||||
isOpen={openDropdown === item.id}
|
||||
isActive={isActive(item)}
|
||||
onToggle={() => {
|
||||
setOpenDropdown((prev) => prev === item.id ? null : item.id);
|
||||
}}
|
||||
onOpen={() => setOpenDropdown(item.id)}
|
||||
onClose={() => setOpenDropdown((prev) => prev === item.id ? null : prev)}
|
||||
/>
|
||||
) : (
|
||||
<div key={item.id} className="relative -mx-2 px-2 py-2">
|
||||
<StaticLink
|
||||
href={item.href}
|
||||
onClick={handleNavClick}
|
||||
className={cn(
|
||||
'relative inline-flex items-center px-3 py-2 text-sm font-medium',
|
||||
'transition-all duration-normal ease-out',
|
||||
isActive(item)
|
||||
? 'text-text-primary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
)}
|
||||
aria-current={isActive(item) ? 'page' : undefined}
|
||||
>
|
||||
{item.label}
|
||||
<span
|
||||
{/* 桌面端三轴导航:收起态**整体卸载**(不是 display:none——焦点序里不能留幽灵 Tab 站),
|
||||
「打开状态」由抽屉承载完整双轴(DrawerNavList)。 */}
|
||||
{!isScrolled && (
|
||||
<nav className="hidden md:flex items-center gap-0.5" role="navigation" aria-label="主导航" data-testid="desktop-navigation">
|
||||
{config.mainNav.map((item) => (
|
||||
item.hasDropdown ? (
|
||||
<MegaDropdown
|
||||
key={item.id}
|
||||
label={item.label}
|
||||
groups={config.megaDropdown[item.dropdownKey!] ?? []}
|
||||
/* 「查看全部」落点 = 导航项自身 href,不靠标签反推(原缺陷见 mega-dropdown.tsx props 注释) */
|
||||
viewAllHref={item.href}
|
||||
isOpen={openDropdown === item.id}
|
||||
isActive={isActive(item)}
|
||||
onToggle={() => {
|
||||
setOpenDropdown((prev) => prev === item.id ? null : item.id);
|
||||
}}
|
||||
onOpen={() => setOpenDropdown(item.id)}
|
||||
onClose={() => setOpenDropdown((prev) => prev === item.id ? null : prev)}
|
||||
/>
|
||||
) : (
|
||||
<div key={item.id} className="relative -mx-2 px-2 py-2">
|
||||
<StaticLink
|
||||
href={item.href}
|
||||
onClick={handleNavClick}
|
||||
className={cn(
|
||||
'absolute -bottom-0.5 left-3 right-3 h-0.5 bg-brand',
|
||||
'relative inline-flex items-center px-3 py-2 text-sm font-medium',
|
||||
'transition-all duration-normal ease-out',
|
||||
isActive(item)
|
||||
? 'opacity-100 scale-x-100'
|
||||
: 'opacity-0 scale-x-0'
|
||||
? 'text-text-primary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
)}
|
||||
/>
|
||||
</StaticLink>
|
||||
</div>
|
||||
)
|
||||
))}
|
||||
</nav>
|
||||
aria-current={isActive(item) ? 'page' : undefined}
|
||||
>
|
||||
{item.label}
|
||||
<span
|
||||
className={cn(
|
||||
'absolute -bottom-0.5 left-3 right-3 h-0.5 bg-brand',
|
||||
'transition-all duration-normal ease-out',
|
||||
isActive(item)
|
||||
? 'opacity-100 scale-x-100'
|
||||
: 'opacity-0 scale-x-0'
|
||||
)}
|
||||
/>
|
||||
</StaticLink>
|
||||
</div>
|
||||
)
|
||||
))}
|
||||
</nav>
|
||||
)}
|
||||
|
||||
<div className="hidden md:flex items-center gap-3">
|
||||
<ThemeToggle />
|
||||
<Button
|
||||
size="sm"
|
||||
asChild
|
||||
className=""
|
||||
{!isScrolled && (
|
||||
<div className="hidden md:flex items-center gap-3">
|
||||
<ThemeToggle />
|
||||
<Button
|
||||
size="sm"
|
||||
asChild
|
||||
className=""
|
||||
>
|
||||
<StaticLink href="/contact" data-testid="consult-button" aria-label="预约咨询">
|
||||
<MessageCircle className="w-4 h-4" />
|
||||
<span className="hidden lg:inline">预约咨询</span>
|
||||
<span className="lg:hidden" aria-hidden="true">咨询</span>
|
||||
</StaticLink>
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 桌面收起态汉堡(OPEN-02b / ADR-0015):只负责开合抽屉。
|
||||
隐藏档用 hidden md:flex —— jsdom 单测不受 CSS 影响,真实移动端不渲染交互面。
|
||||
aria-controls 仅展开时指向抽屉(面板条件挂载,关闭时不留悬空引用)。 */}
|
||||
{isScrolled && (
|
||||
<button
|
||||
data-testid="desktop-menu-button"
|
||||
onClick={() => setIsDesktopDrawerOpen((prev) => !prev)}
|
||||
onKeyDown={handleKeyDown}
|
||||
aria-expanded={isDesktopDrawerOpen}
|
||||
aria-controls={isDesktopDrawerOpen ? 'desktop-menu' : undefined}
|
||||
aria-label={isDesktopDrawerOpen ? '关闭菜单' : '打开菜单'}
|
||||
className="hidden md:flex w-full h-full items-center justify-center text-text-secondary hover:text-text-primary transition-all duration-normal ease-out active:scale-95"
|
||||
style={{ minWidth: '44px', minHeight: '44px' }}
|
||||
>
|
||||
<StaticLink href="/contact" data-testid="consult-button" aria-label="预约咨询">
|
||||
<MessageCircle className="w-4 h-4" />
|
||||
<span className="hidden lg:inline">预约咨询</span>
|
||||
<span className="lg:hidden" aria-hidden="true">咨询</span>
|
||||
</StaticLink>
|
||||
</Button>
|
||||
</div>
|
||||
{isDesktopDrawerOpen ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
|
||||
</button>
|
||||
)}
|
||||
|
||||
<button
|
||||
className="md:hidden p-2 -mr-2 text-text-secondary hover:text-text-primary hover:bg-bg-secondary rounded-sm transition-all duration-normal ease-out active:scale-95"
|
||||
@@ -228,96 +393,44 @@ function HeaderContent() {
|
||||
aria-label="移动端导航"
|
||||
data-testid="mobile-navigation"
|
||||
>
|
||||
{/* 桌面端 ThemeToggle 在 `hidden md:flex` 容器内,移动端只有抽屉可达;
|
||||
w-11 h-11 = 44px,满足触摸目标下限。放在 nav 之外,避免混入导航链接列表。 */}
|
||||
<div className="max-w-container mx-auto px-4 sm:px-6 pt-6 flex items-center justify-between">
|
||||
<span className="text-sm text-text-secondary">外观主题</span>
|
||||
<ThemeToggle className="w-11 h-11" />
|
||||
</div>
|
||||
<nav className="max-w-container mx-auto px-4 sm:px-6 py-6">
|
||||
{config.mainNav.map((item, index) => {
|
||||
/**
|
||||
* 抽屉必须把该轴的**二级目的地**也铺出来(2026-10-17 补,缺陷由 e2e 守卫发现)。
|
||||
*
|
||||
* 一级导航改双轴后「产品」退出一级、降为能力面板里的组
|
||||
* (ADR-0008 决策二)。桌面端靠 hover mega 面板仍可达,
|
||||
* 但移动端抽屉原本**只渲染 `config.mainNav` 这一层** ⇒
|
||||
* `/products` 在手机上彻底不可达(实测底部 TabBar 的 hrefs 只有
|
||||
* `['/', '/solutions', '/services', '/insights', '/contact']`),
|
||||
* 变成只有页脚能到的**移动端孤儿路径**。
|
||||
* `mobile-user-journeys.spec.ts` 里那条
|
||||
* 「两条导航机制都到不了 ⇒ 可能已成孤儿」的断言就是这么红的 ——
|
||||
* 判据命中了真事故,不是测试写坏。
|
||||
*
|
||||
* 这里不手风琴化:抽屉本身 `overflow-y-auto` 可滚,
|
||||
* 而常驻展开少一个交互态、也少一处 a11y 契约要维护。
|
||||
*/
|
||||
const groups = item.dropdownKey ? config.megaDropdown?.[item.dropdownKey] : undefined;
|
||||
{/* 抽屉内容与桌面收起态抽屉同源(DrawerNavList):完整双轴 + 主题切换 + CTA */}
|
||||
<DrawerNavList onClose={handleNavClick} />
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
key={item.id}
|
||||
initial={{ x: 20, opacity: 0 }}
|
||||
animate={{ x: 0, opacity: 1 }}
|
||||
transition={{ delay: index * 0.05 }}
|
||||
>
|
||||
<StaticLink
|
||||
href={item.href}
|
||||
onClick={handleNavClick}
|
||||
className={`
|
||||
block px-4 py-4 text-base font-medium
|
||||
transition-all duration-normal ease-out
|
||||
${isActive(item)
|
||||
? 'text-text-primary bg-bg-secondary'
|
||||
: 'text-text-secondary hover:text-text-primary hover:bg-bg-secondary'
|
||||
}
|
||||
`}
|
||||
style={{ minHeight: '48px', display: 'flex', alignItems: 'center' }}
|
||||
>
|
||||
{item.label}
|
||||
</StaticLink>
|
||||
|
||||
{groups?.length ? (
|
||||
<div className="px-4 pb-4">
|
||||
{groups.map((group) => (
|
||||
<div key={group.id} className="mt-3 first:mt-0">
|
||||
<div className="text-[11px] tracking-[0.15em] uppercase font-bold text-text-muted mb-1.5">
|
||||
{group.title}
|
||||
</div>
|
||||
<ul className="list-none m-0 p-0 space-y-1">
|
||||
{(group.items ?? []).map((sub) => (
|
||||
<li key={sub.id}>
|
||||
<StaticLink
|
||||
href={sub.href}
|
||||
onClick={handleNavClick}
|
||||
className="block px-2 py-2 text-sm text-text-secondary hover:text-brand-ink transition-colors duration-hover"
|
||||
style={{ minHeight: '40px', display: 'flex', alignItems: 'center' }}
|
||||
>
|
||||
{sub.title}
|
||||
</StaticLink>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</motion.div>
|
||||
);
|
||||
})}
|
||||
<div className="mt-6 px-4 pt-6 border-t border-border-primary">
|
||||
<Button
|
||||
className="w-full"
|
||||
asChild
|
||||
size="lg"
|
||||
>
|
||||
<StaticLink href="/contact" onClick={() => setIsOpen(false)}>
|
||||
<MessageCircle className="w-4 h-4" />
|
||||
预约咨询
|
||||
</StaticLink>
|
||||
</Button>
|
||||
</div>
|
||||
</nav>
|
||||
{/* ── 桌面收起态抽屉(OPEN-02b / ADR-0015)──────────────────────────────
|
||||
左侧滑入(对标形态);`hidden md:block` 与移动抽屉的 `md:hidden` 互斥,
|
||||
同一份 DrawerNavList 渲染完整双轴。焦点陷阱 + Esc + 遮罩点击三路关闭。
|
||||
抽屉 z-40 低于页头 z-50:收起方块悬浮在遮罩之上,保持可点击关合。 */}
|
||||
<AnimatePresence mode="wait">
|
||||
{isDesktopDrawerOpen && (
|
||||
<motion.div
|
||||
ref={desktopFocusTrapRef}
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
transition={{ duration: 0.2 }}
|
||||
className="fixed inset-0 z-40 hidden md:block"
|
||||
>
|
||||
<div
|
||||
className="absolute inset-0 bg-black/50 backdrop-blur-sm"
|
||||
onClick={() => setIsDesktopDrawerOpen(false)}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<motion.div
|
||||
initial={{ x: '-100%' }}
|
||||
animate={{ x: 0 }}
|
||||
exit={{ x: '-100%' }}
|
||||
transition={{ duration: 0.25, ease: [0.22, 1, 0.36, 1] }}
|
||||
className="absolute inset-y-0 left-0 w-full max-w-md bg-bg-primary/95 backdrop-blur-xl overflow-y-auto"
|
||||
id="desktop-menu"
|
||||
role="navigation"
|
||||
aria-label="桌面导航抽屉"
|
||||
data-testid="desktop-navigation-drawer"
|
||||
>
|
||||
<DrawerNavList onClose={handleNavClick} />
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user