From daf0329666e80acb59155fde02d761b966dfd00b Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Wed, 25 Nov 2026 01:05:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ui):=20=E5=A4=96=E5=A3=B3=E4=B8=8E?= =?UTF-8?q?=E6=AE=8B=E4=BD=99=E4=BB=B6=E6=8E=A5=E5=85=A5=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=B1=82=EF=BC=88B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Switch.tsx 删除,唯一消费者 native/guide-order.tsx 改用 ui 的 ToggleSwitch (native checkbox → button role="switch",受控写回契约不变:下一次值仍由 prop 推导)。 - tests/switch.spec.tsx 改名 tests/toggle-switch.spec.tsx,断言改 aria-checked; native-guide 两处开关查询同步改选择器。 - sidebar/dock-fallback.tsx 与 split-pane.tsx 判断不接入:前者只有定位/让位逻辑 与 BottomDockToggle 包壳,后者是纯布局与 pointer 拖拽手柄,统一层没有语义对应件。 - 收敛文档补 B4 实施记录,并把映射表中这两行改为「判断不接入」及理由。 --- docs/plans/2026-11-25-ui-kit-convergence.md | 14 ++++- src/client/Switch.module.css | 64 --------------------- src/client/Switch.tsx | 33 ----------- src/client/native/guide-order.tsx | 6 +- tests/native-guide.spec.tsx | 8 +-- tests/switch.spec.tsx | 45 --------------- tests/toggle-switch.spec.tsx | 43 ++++++++++++++ 7 files changed, 61 insertions(+), 152 deletions(-) delete mode 100644 src/client/Switch.module.css delete mode 100644 src/client/Switch.tsx delete mode 100644 tests/switch.spec.tsx create mode 100644 tests/toggle-switch.spec.tsx diff --git a/docs/plans/2026-11-25-ui-kit-convergence.md b/docs/plans/2026-11-25-ui-kit-convergence.md index f80e70a0..c026a772 100644 --- a/docs/plans/2026-11-25-ui-kit-convergence.md +++ b/docs/plans/2026-11-25-ui-kit-convergence.md @@ -62,9 +62,9 @@ comm -23 \ | `diff/DiffRows.tsx` | B3 | **已完成**:展开全部改 `ActionButton`;折叠行保持原生(测试按 `[data-expandable]` 定位) | | `TerminalView.tsx` | B3 | **已完成**:`StatusLine`(待连/已退出)+ `ActionButton`、`ErrorBlock`(不可用/错误)、`FallbackPane`(终端已失联) | | `OrphanedTab.tsx` | B3 | **本批已完成**:`FallbackPane`(`role="status"`)+ 既有 `EmptyState` | -| `Switch.tsx` | B4 | `controls.ToggleSwitch`(唯一消费者 `native/guide-order.tsx`) | -| `sidebar/dock-fallback.tsx` | B4 | `IconButton` / `ActionButton` | -| `split-pane.tsx` | B4 | `IconButton`(拖拽手柄保留原生 pointer 事件) | +| `Switch.tsx` | B4 | **已完成**:删除自绘开关,唯一消费者 `native/guide-order.tsx` 改用 `controls.ToggleSwitch`(native checkbox + `aria-label` → button `role="switch"` + `aria-checked`) | +| `sidebar/dock-fallback.tsx` | B4 | **判断不接入**:本文件只有定位/让位逻辑,渲染的是一个定位 `` 包壳与 `Sidebar.tsx` 的 `BottomDockToggle`,自身没有按钮或视觉件 | +| `split-pane.tsx` | B4 | **判断不接入**:纯布局与拖拽手柄(原生 pointer 事件 + 帧批处理),空面板卡片是整块可点的卡片形状,不是 `IconButton`/`ActionButton` 的语义 | ### C 类:缺共享件(2,本轮已造件) @@ -128,6 +128,14 @@ comm -23 \ 3. **B3 标签页与预览宿主**(`diff/*` → `TerminalView` → `media-preview` → `PdfView`/`LargeFileView` → `OrphanedTab` 收尾)。 4. **B4 文件树与侧栏外壳**(`Switch` → `dock-fallback` → `split-pane`):直接动交互与布局,放最后。 +## B4 实施记录(外壳与残余件) + +- **`Switch.tsx` → `ToggleSwitch`**:删掉自绘开关与 `Switch.module.css`,`native/guide-order.tsx` 改用组件层 `ToggleSwitch`。受控写回契约不变——下一次值仍由 prop 推导(`onChange(!checked)`),父级异步写回时开关不会被吞掉。`tests/switch.spec.tsx` 相应改名为 `tests/toggle-switch.spec.tsx`,断言从 `input[type=checkbox].checked` 改为 `[role="switch"]` 的 `aria-checked`;`tests/native-guide.spec.tsx` 两处开关查询(`Enable: Git`、`Show on the start page: A`)同步改选择器。 +- **`sidebar/dock-fallback.tsx` 不接入**:这个文件的价值全在相位判定、让位走查与 500ms 重对齐,渲染部分是定位包壳 + `BottomDockToggle`(后者在 `Sidebar.tsx`,已有自己的实现)。硬套 `IconButton` 只会把包壳变成按钮,语义反而错。 +- **`split-pane.tsx` 不接入**:`Divider` 是 pointer-capture 拖拽手柄(增量、帧批处理),`paneCard` 是整块空面板卡片,`TabBar` 有自己的组件。统一层没有对应形状,硬换会动交互。 + +结论:B4 实际只有一处组件替换,另两处按「没有语义对应的共享件就不换」保留,与 U1 盘点时把外壳件标为 B4 的预期不同——盘点看见的是「自绘视觉」,实际读下来这两处是布局与手势。 + ## 组件层补件记录(U2) 收敛过程中,接页面的批次报告了三处「两个以上页面需要、组件层却没有」的缺口。本轮只补组件层与契约测试,消费页面仍在各自批次分支上,切换留到合并后统一做。 diff --git a/src/client/Switch.module.css b/src/client/Switch.module.css deleted file mode 100644 index 829cbc50..00000000 --- a/src/client/Switch.module.css +++ /dev/null @@ -1,64 +0,0 @@ -/* Custom switch: a real checkbox (native semantics + focus, visually hidden) - driving a styled 36x20 track with a sliding thumb. */ -.switch { - position: relative; - display: inline-flex; - flex: none; - cursor: pointer; -} - -.switchInput { - position: absolute; - width: 1px; - height: 1px; - margin: 0; - opacity: 0; -} - -.switchTrack { - display: inline-flex; - align-items: center; - width: 36px; - height: 20px; - padding: 2px; - box-sizing: border-box; - border-radius: 10px; - border: 1px solid var(--dsw-alias-border-l2); - background: var(--dsw-alias-bg-layer-2); - transition: background 0.15s ease, border-color 0.15s ease; -} - -.switchThumb { - display: block; - width: 14px; - height: 14px; - border-radius: 50%; - background: var(--dsw-alias-label-tertiary); - transition: transform 0.15s ease, background 0.15s ease; -} - -.switch:hover .switchTrack { - border-color: var(--dsw-alias-label-dimmed); -} - -.switchInput:checked + .switchTrack { - border-color: var(--dsw-alias-button-primary-fill); - background: var(--dsw-alias-button-primary-fill); -} - -.switchInput:checked + .switchTrack .switchThumb { - transform: translateX(16px); - background: var(--dsw-alias-bg-layer-3); -} - -.switchInput:focus-visible + .switchTrack { - outline: 2px solid var(--dsw-alias-state-business-primary); - outline-offset: 2px; -} - -@media (prefers-reduced-motion: reduce) { - .switchTrack, - .switchThumb { - transition: none; - } -} diff --git a/src/client/Switch.tsx b/src/client/Switch.tsx deleted file mode 100644 index 138167ed..00000000 --- a/src/client/Switch.tsx +++ /dev/null @@ -1,33 +0,0 @@ -/** - * The shared on/off switch: a real checkbox (native semantics and focus) - * driving a styled track/thumb. Used by the settings rows and the start-page - * order list. - */ -import css from './Switch.module.css' - -/** One labelled switch. */ -export function Switch(props: { - checked: boolean - onChange: (next: boolean) => void - label: string -}) { - const { checked, onChange, label } = props - return ( - // The parent's write is asynchronous. Derive the next value from the PROP - // instead of the event's checkbox state: the controlled value can be reset - // before React's change handler runs, which previously made a switch - // impossible to turn back on. - - ) -} diff --git a/src/client/native/guide-order.tsx b/src/client/native/guide-order.tsx index 46ad81c0..6aa8379f 100644 --- a/src/client/native/guide-order.tsx +++ b/src/client/native/guide-order.tsx @@ -12,7 +12,7 @@ import { import clsx from 'clsx' import type { NativeGuideEntry } from './guide-catalog.ts' import { t } from '../locales.ts' -import { Switch } from '../Switch.tsx' +import { ToggleSwitch } from '../ui/index.ts' import { GuideRowsInput } from './guide-rows.tsx' import css from './guide.module.css' @@ -60,8 +60,8 @@ export function GuideOrderList({ entries, hidden, onMove, onToggle }: GuideOrder > - { onToggle(entry, !next) }} /> diff --git a/tests/native-guide.spec.tsx b/tests/native-guide.spec.tsx index 7ece22fe..0ecda98d 100644 --- a/tests/native-guide.spec.tsx +++ b/tests/native-guide.spec.tsx @@ -219,9 +219,9 @@ describe('GuideGrid', () => { ;(reorder as HTMLElement).click() await Promise.resolve() }) - const toggle = document.querySelector('input[aria-label="Enable: Git"]') as HTMLInputElement + const toggle = document.querySelector('[role="switch"][aria-label="Enable: Git"]') as HTMLElement expect(toggle).not.toBeNull() - expect(toggle.checked).toBe(false) + expect(toggle.getAttribute('aria-checked')).toBe('false') act(() => { toggle.click() }) expect(onToggleHidden).toHaveBeenCalledWith(expect.objectContaining({ key: 'git' }), false) mounted.unmount() @@ -340,9 +340,9 @@ describe('NativeGuide', () => { ;(reorder as HTMLElement).click() await Promise.resolve() }) - const toggle = document.querySelector('input[aria-label="Show on the start page: A"]') as HTMLInputElement + const toggle = document.querySelector('[role="switch"][aria-label="Show on the start page: A"]') as HTMLElement expect(toggle).not.toBeNull() - expect(toggle.checked).toBe(false) + expect(toggle.getAttribute('aria-checked')).toBe('false') await act(async () => { toggle.click() // The host deep-merges nested plain objects, so a patch with the key diff --git a/tests/switch.spec.tsx b/tests/switch.spec.tsx deleted file mode 100644 index 4dab4f4c..00000000 --- a/tests/switch.spec.tsx +++ /dev/null @@ -1,45 +0,0 @@ -// @vitest-environment jsdom -/** - * The shared switch: a controlled checkbox whose writes are asynchronous. - * A native toggle must not be swallowed by the controlled reset, or a switch - * turned off can never be turned back on. - */ -import { useState } from 'react' -import { describe, expect, it } from 'vitest' -import { act } from 'react-dom/test-utils' -import { Switch } from '../src/client/Switch.tsx' -import { renderRoot, setupReactAct } from './test-utils.ts' - -setupReactAct() - -function Controlled({ initial = true, onChange }: { initial?: boolean; onChange?: (value: boolean) => void }) { - const [on, setOn] = useState(initial) - return ( - { setOn(next); onChange?.(next) }} - /> - ) -} - -describe('Switch', () => { - it('turns off and back on even when the parent write is asynchronous', () => { - const changes: boolean[] = [] - const mounted = renderRoot( changes.push(value)} />) - const input = mounted.container.querySelector('input[type="checkbox"]') as HTMLInputElement - expect(input.checked).toBe(true) - const label = mounted.container.querySelector('label') as HTMLLabelElement - act(() => { label.click() }) - expect(input.checked).toBe(false) - act(() => { label.click() }) - expect(input.checked).toBe(true) - // Direct input activation (keyboard Space) takes the same path. - act(() => { input.click() }) - expect(input.checked).toBe(false) - act(() => { input.click() }) - expect(input.checked).toBe(true) - expect(changes).toEqual([false, true, false, true]) - mounted.unmount() - }) -}) diff --git a/tests/toggle-switch.spec.tsx b/tests/toggle-switch.spec.tsx new file mode 100644 index 00000000..a5c2d8b8 --- /dev/null +++ b/tests/toggle-switch.spec.tsx @@ -0,0 +1,43 @@ +// @vitest-environment jsdom +/** + * The shared toggle switch (ui/controls `ToggleSwitch`): a button with + * `role="switch"` whose next value derives from the controlled PROP. The + * parent's write is asynchronous, so a switch turned off must be able to turn + * back on even when the controlled value is reset before React's handler runs + * (the failure the old custom checkbox switch was built to avoid). + */ +import { useState } from 'react' +import { describe, expect, it } from 'vitest' +import { act } from 'react-dom/test-utils' +import { ToggleSwitch } from '../src/client/ui/index.ts' +import { renderRoot, setupReactAct } from './test-utils.ts' + +setupReactAct() + +function Controlled({ initial = true, onChange }: { initial?: boolean; onChange?: (value: boolean) => void }) { + const [on, setOn] = useState(initial) + return ( + { setOn(next); onChange?.(next) }} + /> + ) +} + +describe('ToggleSwitch', () => { + it('turns off and back on even when the parent write is asynchronous', () => { + const changes: boolean[] = [] + const mounted = renderRoot( changes.push(value)} />) + const toggle = mounted.container.querySelector('[role="switch"]') as HTMLElement + expect(toggle.getAttribute('aria-checked')).toBe('true') + act(() => { toggle.click() }) + expect(toggle.getAttribute('aria-checked')).toBe('false') + act(() => { toggle.click() }) + expect(toggle.getAttribute('aria-checked')).toBe('true') + act(() => { toggle.click() }) + expect(toggle.getAttribute('aria-checked')).toBe('false') + expect(changes).toEqual([false, true, false]) + mounted.unmount() + }) +})