fix(editor): the view row reveals the active chip when shown again

This commit is contained in:
bchanot committed 2026-11-21 20:15:17 +01:00
1 parent b4ebc2d288
commit 85cf87695c
2 files changed
+46 -2

No files matched your search

@@ -110,10 +110,19 @@ export function useViewRow(
const row = rowRef.current
if (!row) return
const chip = chipOf(row, current)
setHints(hintState(row, chip && spanOf(row, chip)))
const next = hintState(row, chip && spanOf(row, chip))
setHints((prev) =>
prev.start === next.start &&
prev.end === next.end &&
prev.dot === next.dot
? prev
: next,
)
}, [rowRef, current])
const refreshRef = useRef(refresh)
refreshRef.current = refresh
const currentRef = useRef(current)
currentRef.current = current
const reveal = useCallback(
(id: string) => {
@@ -138,6 +147,9 @@ export function useViewRow(
[rowRef, reduced],
)
const revealRef = useRef(reveal)
revealRef.current = reveal
useLayoutEffect(() => {
openWindow()
return () => clearTimeout(openingTimer.current)
@@ -160,7 +172,11 @@ export function useViewRow(
return () => row.removeEventListener('scroll', onScroll)
let width = row.clientWidth
const observer = new ResizeObserver(() => {
if (width === 0 && row.clientWidth > 0) openWindow()
if (width === 0 && row.clientWidth > 0) {
openWindow()
// A view that changed while the row was hidden had nothing to reveal.
revealRef.current(currentRef.current)
}
width = row.clientWidth
refreshRef.current()
})
@@ -218,6 +218,34 @@ describe('the reveal of the active chip', () => {
expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' })
})
it('reveals the active chip when the row is shown again', () => {
const original = globalThis.ResizeObserver
const observers: Array<() => void> = []
vi.stubGlobal(
'ResizeObserver',
class {
constructor(callback: () => void) {
observers.push(callback)
}
observe() {}
disconnect() {}
},
)
vi.useFakeTimers()
const { again } = renderRow(vi.fn(), viewsOf(), 'v1')
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
// Hidden: width 0, the change reveals nothing.
stubGeometry(0, 0)
again({ current: 'v5' })
expect(scrollTo).not.toHaveBeenCalled()
// Shown again: the chip (400 to 500) is past the end hint of a 300 px row.
stubGeometry()
act(() => observers.forEach((callback) => callback()))
expect(scrollTo).toHaveBeenCalledTimes(1)
expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' })
globalThis.ResizeObserver = original
})
it('shows the end hint again when a name change widens the content', () => {
const { container } = render(
<ViewSwitcher views={viewsOf()} current="v1" onSelect={vi.fn()} />,