fix(editor): the view row reveals the active chip when shown again
This commit is contained in:
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
|
const row = rowRef.current
|
||||||
if (!row) return
|
if (!row) return
|
||||||
const chip = chipOf(row, current)
|
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])
|
}, [rowRef, current])
|
||||||
const refreshRef = useRef(refresh)
|
const refreshRef = useRef(refresh)
|
||||||
refreshRef.current = refresh
|
refreshRef.current = refresh
|
||||||
|
const currentRef = useRef(current)
|
||||||
|
currentRef.current = current
|
||||||
|
|
||||||
const reveal = useCallback(
|
const reveal = useCallback(
|
||||||
(id: string) => {
|
(id: string) => {
|
||||||
@@ -138,6 +147,9 @@ export function useViewRow(
|
|||||||
[rowRef, reduced],
|
[rowRef, reduced],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const revealRef = useRef(reveal)
|
||||||
|
revealRef.current = reveal
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
openWindow()
|
openWindow()
|
||||||
return () => clearTimeout(openingTimer.current)
|
return () => clearTimeout(openingTimer.current)
|
||||||
@@ -160,7 +172,11 @@ export function useViewRow(
|
|||||||
return () => row.removeEventListener('scroll', onScroll)
|
return () => row.removeEventListener('scroll', onScroll)
|
||||||
let width = row.clientWidth
|
let width = row.clientWidth
|
||||||
const observer = new ResizeObserver(() => {
|
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
|
width = row.clientWidth
|
||||||
refreshRef.current()
|
refreshRef.current()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -218,6 +218,34 @@ describe('the reveal of the active chip', () => {
|
|||||||
expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' })
|
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', () => {
|
it('shows the end hint again when a name change widens the content', () => {
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
<ViewSwitcher views={viewsOf()} current="v1" onSelect={vi.fn()} />,
|
<ViewSwitcher views={viewsOf()} current="v1" onSelect={vi.fn()} />,
|
||||||
|
|||||||
Reference in new issue
Block a user