refactor(ui): 合入 B4 外壳与残余件(Switch→ToggleSwitch)
This commit is contained in:
commit
ac547a3f3d
7 files changed
+61
-152
No files matched your search
@@ -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 | **判断不接入**:本文件只有定位/让位逻辑,渲染的是一个定位 `<span>` 包壳与 `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)
|
||||
|
||||
收敛过程中,接页面的批次报告了三处「两个以上页面需要、组件层却没有」的缺口。本轮只补组件层与契约测试,消费页面仍在各自批次分支上,切换留到合并后统一做。
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
<label className={css.switch}>
|
||||
<input
|
||||
type="checkbox"
|
||||
className={css.switchInput}
|
||||
checked={checked}
|
||||
aria-label={label}
|
||||
onChange={() => { onChange(!checked) }}
|
||||
/>
|
||||
<span className={css.switchTrack} aria-hidden="true">
|
||||
<span className={css.switchThumb} />
|
||||
</span>
|
||||
</label>
|
||||
)
|
||||
}
|
||||
@@ -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
|
||||
>
|
||||
<IconChevronDownOutlineRegular size={12} />
|
||||
</button>
|
||||
<Switch
|
||||
label={`${entry.descriptorId === undefined ? t('settingsGuideShow') : t('settingsGuideEnable')}: ${title}`}
|
||||
<ToggleSwitch
|
||||
aria-label={`${entry.descriptorId === undefined ? t('settingsGuideShow') : t('settingsGuideEnable')}: ${title}`}
|
||||
checked={shown}
|
||||
onChange={(next) => { onToggle(entry, !next) }}
|
||||
/>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
<Switch
|
||||
label="test switch"
|
||||
checked={on}
|
||||
onChange={(next) => { 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(<Controlled onChange={value => 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()
|
||||
})
|
||||
})
|
||||
@@ -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 (
|
||||
<ToggleSwitch
|
||||
aria-label="test switch"
|
||||
checked={on}
|
||||
onChange={(next) => { 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(<Controlled onChange={value => 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()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user