Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/overflow-reset-on-disable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Discard stale overflow measurements when an item stops being observed.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ describe('useIsClipped', () => {
expect(MockIntersectionObserver.instances.every(o => !o.observed.has(item))).toBe(true)
})

it('waits for a fresh measurement after re-enabling a clipped item', () => {
vi.stubGlobal('IntersectionObserver', MockIntersectionObserver)
const {rerender} = render(<Harness />)
const item = page.getByTestId('item').element()
const observer = MockIntersectionObserver.instances.at(-1)!
observer.trigger([{target: item, isIntersecting: false, intersectionRatio: 0}])
expect(item.getAttribute('data-overflowing')).toBe('true')

rerender(<Harness disabled />)
expect(item.getAttribute('data-overflowing')).toBe('false')
rerender(<Harness />)
expect(item.getAttribute('data-overflowing')).toBe('false')

observer.trigger([{target: item, isIntersecting: false, intersectionRatio: 0}])
expect(item.getAttribute('data-overflowing')).toBe('true')
})

it('reflects overflow state from the shared root-scoped observer', () => {
vi.stubGlobal('IntersectionObserver', MockIntersectionObserver)
render(<Harness />)
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/internal/hooks/useOverflowObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export function useIsClipped(ref: RefObject<HTMLElement | null>, options?: {disa
}
}

return observe(element, updateOverflowState)
const unobserve = observe(element, updateOverflowState)
return () => {
unobserve()
isOverflowingRef.current = false
}
},
[ref, observe, disabled],
)
Expand Down
Loading