Files
deepseek-harness/packages/client/ui-primitives/src/AnimatedRows.tsx
T
63026c7a5c feat(client): 左侧栏纵向空间优化,工作区滚动吸顶优化 (#7337)
* feat(client): share sidebar scrolling and dock workspace header

* fix(client): keep shared sidebar scrolling vertical

* fix(client): match sticky workspace header to sidebar fill

* fix(client): stabilize transparent sticky sidebar and compact row layout

* refactor(ui-primitives): keep sticky section observer internal

* fix(ui-primitives): limit row animations to visible scrollports

* feat(ui-workspace): drive sticky clipping with scroll timelines

* refactor(ui-primitives): bound row animations by the window

* feat(client): hand off sidebar wheel scrolling to the workspace list

The workspace list scrolls inside its own one-viewport section below the toolbar instead of sliding under a clipped sticky toolbar. The sidebar viewport scrolls the navigation away first; ui-sidebar's scroll hand-off splits each wheel event between the viewport and the section scroller, which the occupant binds through the slot-level useSectionScroller hook supplied by hookContext.

* test(client): cover the sidebar scroll hand-off and align the list scrollbar

Test-owned sidebar roles declare the slot-level scroller inject and pass its hookContext. Unit tests cover the hand-off split, wheel filtering, and listener rebinding; the e2e scenario checks one wheel event crossing from the navigation into the list and back. The list area reaches 2px from the sidebar edge for its scrollbar while rows keep the 8px inset. The unused clip module, its tests, and the sticky clearance variable are removed; READMEs and the slot catalog describe the nested scrolling.

* refactor(ui-primitives): restore MenuGroup sticky observation

The section observer was extracted for a sticky workspace toolbar that nested scrolling no longer has, so MenuGroup is its only consumer again. MenuGroup and its tests return to the master implementation.

* fix(client): keep native sidebar scrolling away from the hand-off boundary

The hand-off splits a wheel gesture in JavaScript only when its first event must move the sidebar viewport or starts outside the section scroller, and then keeps that gesture, including momentum, until wheel events pause for 100ms. Gestures the section scroller can take alone stay native, an uncancelable native gesture that reaches the section top passes the rest to the viewport, and events another listener canceled are left alone. The section is at most one viewport tall, and exactly that tall while search is expanded, so a short list leaves the navigation in place. Stale absence assertions and unused viewport declarations are removed; the READMEs document portals and the data-sidebar markers.

---------

Co-authored-by: Yif <[email protected]>
Co-authored-by: imccyu <[email protected]>
2026-11-02 22:27:26 +08:00

196 lines
7.1 KiB
TypeScript

/** React-commit-driven movement and entry/exit fades for keyed rows. */
import { Component, createRef, type AriaRole, type ReactNode } from 'react'
import css from './AnimatedRows.module.css'
const ROW_FADE_MS = 100
const ROW_GLIDE_MS = 200
/** Keyed row content, list semantics, and animation readiness. */
export interface AnimatedRowsProps {
children: ReactNode
className: string
role?: AriaRole
label?: string
/** Unique DOM-order keys matching the rendered data-row-key attributes. */
rowKeys: readonly string[]
ready: boolean
/** Changes that replace the view or reveal hidden rows settle immediately. */
resetKey: string
}
interface RowPosition {
element: HTMLElement
rect: DOMRect
opacity: number
}
type Viewport = Pick<DOMRect, 'top' | 'bottom' | 'left' | 'right'>
interface RowSnapshot {
viewport: Viewport
positions: Map<string, RowPosition>
removed: Map<string, RowPosition>
}
function sameRows(previous: AnimatedRowsProps, next: AnimatedRowsProps): boolean {
return previous.rowKeys.length === next.rowKeys.length
&& previous.rowKeys.every((key, index) => key === next.rowKeys[index])
}
function visibleViewport(list: HTMLElement): Viewport {
const rect = list.getBoundingClientRect()
return {
top: Math.max(0, rect.top), bottom: Math.min(window.innerHeight, rect.bottom),
left: Math.max(0, rect.left), right: Math.min(window.innerWidth, rect.right),
}
}
function intersects(row: DOMRect, viewport: Viewport): boolean {
return row.bottom > viewport.top && row.top < viewport.bottom
&& row.right > viewport.left && row.left < viewport.right
}
/**
* Animates keyed rows only when their rendered membership or order changes.
* Rows must intersect both the list and the browser window before or after the commit.
* Motion starts after the first pointer or keyboard input inside the mounted list.
* The parent supplies a positioned container for the inert exit overlay.
*/
export class AnimatedRows extends Component<AnimatedRowsProps> {
private armed = false
private readonly list = createRef<HTMLDivElement>()
private readonly overlay = createRef<HTMLDivElement>()
private readonly movements = new Map<HTMLElement, Animation>()
private readonly exits = new Map<string, { element: HTMLElement; animation: Animation }>()
override getSnapshotBeforeUpdate(previous: AnimatedRowsProps): RowSnapshot | null {
const list = this.list.current
if (!this.armed || sameRows(previous, this.props) || previous.resetKey !== this.props.resetKey
|| !previous.ready || !this.props.ready || list === null
|| typeof list.animate !== 'function'
|| window.matchMedia('(prefers-reduced-motion: reduce)').matches) return null
const viewport = visibleViewport(list)
const positions = this.readPositions()
const nextKeys = new Set(this.props.rowKeys)
const removed = new Map<string, RowPosition>()
for (const [key, row] of positions) {
if (nextKeys.has(key) || !intersects(row.rect, viewport)) continue
const clone = row.element.cloneNode(true) as HTMLElement
clone.removeAttribute('data-row-key')
clone.inert = true
clone.style.paddingInlineStart = getComputedStyle(row.element).paddingInlineStart
removed.set(key, { ...row, element: clone })
}
return { viewport, positions, removed }
}
override componentDidUpdate(previous: AnimatedRowsProps, _state: unknown, snapshot: RowSnapshot | null): void {
if (snapshot === null) {
if (!sameRows(previous, this.props) || previous.resetKey !== this.props.resetKey
|| previous.ready !== this.props.ready) this.clear()
return
}
this.cancelMovements()
const list = this.list.current as HTMLDivElement
const overlay = this.overlay.current as HTMLDivElement
const viewport = visibleViewport(list)
const origin = overlay.getBoundingClientRect()
const positions = this.readPositions()
for (const [key, row] of positions) {
this.removeExit(key)
const previousRow = snapshot.positions.get(key)
if (!intersects(row.rect, viewport)
&& (previousRow === undefined || !intersects(previousRow.rect, snapshot.viewport))) continue
if (previousRow === undefined) {
this.move(row.element, [{ opacity: 0 }, { opacity: 1 }], ROW_FADE_MS)
continue
}
const dx = previousRow.rect.left - row.rect.left
const dy = previousRow.rect.top - row.rect.top
if (dx === 0 && dy === 0 && previousRow.opacity === 1) continue
this.move(row.element, [
{ transform: `translate(${String(dx)}px, ${String(dy)}px)`, opacity: previousRow.opacity },
{ transform: 'translate(0, 0)', opacity: 1 },
], ROW_GLIDE_MS)
}
for (const [key, row] of snapshot.removed) {
const { element } = row
this.removeExit(key)
Object.assign(element.style, {
position: 'absolute', margin: '0', transform: 'none', boxSizing: 'border-box',
left: `${String(row.rect.left - origin.left)}px`,
top: `${String(row.rect.top - origin.top)}px`,
width: `${String(row.rect.width)}px`, height: `${String(row.rect.height)}px`,
})
overlay.append(element)
const animation = element.animate([{ opacity: row.opacity }, { opacity: 0 }], {
duration: ROW_FADE_MS, easing: 'ease-out', fill: 'forwards',
})
this.exits.set(key, { element, animation })
animation.onfinish = () => { this.removeExit(key) }
}
}
override componentWillUnmount(): void {
this.clear()
}
private readPositions(): Map<string, RowPosition> {
const list = this.list.current as HTMLDivElement
const rows = list.querySelectorAll<HTMLElement>('[data-row-key]')
return new Map(Array.from(rows, element => [element.dataset.rowKey as string, {
element,
rect: element.getBoundingClientRect(),
opacity: this.movements.has(element) ? Number(getComputedStyle(element).opacity) : 1,
}]))
}
private move(element: HTMLElement, keyframes: Keyframe[], duration: number): void {
const animation = element.animate(keyframes, { duration, easing: 'ease-out' })
this.movements.set(element, animation)
animation.onfinish = () => { this.movements.delete(element); animation.cancel() }
}
private cancelMovements(): void {
for (const animation of this.movements.values()) {
animation.onfinish = null
animation.cancel()
}
this.movements.clear()
}
private removeExit(key: string): void {
const exit = this.exits.get(key)
if (exit === undefined) return
exit.animation.onfinish = null
exit.animation.cancel()
exit.element.remove()
this.exits.delete(key)
}
private clear(): void {
this.cancelMovements()
for (const key of this.exits.keys()) this.removeExit(key)
}
override render(): ReactNode {
return <>
<div
ref={this.list}
className={this.props.className}
role={this.props.role}
aria-label={this.props.label}
onPointerDownCapture={() => { this.armed = true }}
onKeyDownCapture={() => { this.armed = true }}
>
{this.props.children}
</div>
<div ref={this.overlay} className={css.exits} aria-hidden="true" />
</>
}
}