Skip to content

Commit 56bfd08

Browse files
authored
Merge pull request #4756 from mumberrymountain/fix-issue-4751
Fix: use latest IntersectionObserver entry to determine visibility (#4751)
2 parents 7417849 + 3f7745a commit 56bfd08

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/js/modules/ResizeTable/ResizeTable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default class ResizeTable extends Module{
111111

112112
initializeVisibilityObserver(){
113113
this.visibilityObserver = new IntersectionObserver((entries) => {
114-
this.visible = entries[0].isIntersecting;
114+
this.visible = entries[entries.length - 1].isIntersecting;
115115

116116
if(!this.initialized){
117117
this.initialized = true;

test/unit/modules/ResizeTable.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,39 @@ describe("ResizeTable module", () => {
100100
// Clean up
101101
redrawSpy.mockRestore();
102102
});
103+
104+
it('should determine whether to redraw table or not using latest visibility state from batched IntersectionObserver entries', () => {
105+
let observedElements = [];
106+
let observerCallback;
107+
let redrawTableCalled = false;
108+
109+
// mock intersectionObserver
110+
global.IntersectionObserver = jest.fn((callback) => {
111+
observerCallback = callback;
112+
return {
113+
observe: jest.fn((el) => observedElements.push(el)),
114+
unobserve: jest.fn(),
115+
disconnect: jest.fn()
116+
};
117+
});
118+
119+
// mock redrawTable
120+
resizeTableMod.redrawTable = function() {
121+
redrawTableCalled = true;
122+
};
123+
124+
resizeTableMod.initializeVisibilityObserver();
125+
126+
// reproduce IntersectionObserver being called when table rendered for the first time
127+
observerCallback([
128+
{ target: tabulator.element, isIntersecting: true },
129+
]);
130+
131+
observerCallback([
132+
{ target: tabulator.element, isIntersecting: false },
133+
{ target: tabulator.element, isIntersecting: true },
134+
]);
135+
136+
expect(redrawTableCalled).toBe(true);
137+
});
103138
});

0 commit comments

Comments
 (0)